Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(DOCSP-33121): C++: Update Sync error handling docs for v0.4.0-preview #3081

Merged
merged 9 commits into from
Nov 28, 2023
60 changes: 36 additions & 24 deletions examples/cpp/beta/sync/authentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ static const std::string APP_ID = "cpp-tester-uliix";

TEST_CASE("create and log in an email/password user", "[realm][sync]") {
// :snippet-start: beta-register-user
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

auto userEmail = "testUser" + random_string() + "@example.com";
auto userPassword = "password1234";
Expand All @@ -43,8 +44,9 @@ TEST_CASE("create and log in an email/password user", "[realm][sync]") {

TEST_CASE("create and log in an anonymous user", "[realm][sync]") {
// :snippet-start: beta-anonymous-login
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

auto user = app.login(realm::App::credentials::anonymous()).get();
// :snippet-end:
Expand All @@ -59,8 +61,9 @@ TEST_CASE("test custom function authentication", "[realm][sync]") {
// The parameter details vary depending on how you define your custom authentication function.
realm::bson::BsonDocument params = {{ "username", "bob" }};

auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

auto user = app.login(realm::App::credentials::function(params)).get();
// :snippet-end:
Expand All @@ -70,8 +73,9 @@ TEST_CASE("test custom function authentication", "[realm][sync]") {
}

TEST_CASE("test get user access token", "[realm][sync]") {
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

auto user = app.login(realm::App::credentials::anonymous()).get();
// :snippet-start: get-user-access-token
Expand All @@ -93,8 +97,9 @@ void testAPIKeyAuthSyntax() {
auto API_KEY = "this was tested with a valid API key when written";

// :snippet-start: beta-api-key
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

auto user = app.login(realm::App::credentials::api_key(API_KEY)).get();
// :snippet-end:
Expand All @@ -104,17 +109,19 @@ void testCustomJWTAuthSyntax() {
// :snippet-start: beta-custom-jwt
auto token = "<jwt>";

auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

auto user = app.login(realm::App::credentials::custom(token)).get();
// :snippet-end:
}

void testSignInWithFacebookAuthSyntax() {
// :snippet-start: beta-facebook
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

auto accessToken = "<token>";

Expand All @@ -124,8 +131,9 @@ void testSignInWithFacebookAuthSyntax() {

void testSignInWithAppleAuthSyntax() {
// :snippet-start: beta-apple
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

auto idToken = "<token>";

Expand All @@ -138,8 +146,9 @@ void testSignInWithGoogleAuthCodeSyntax() {
auto authCode = realm::App::credentials::auth_code{ myAuthCode };

// :snippet-start: beta-google-auth-code
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

// The auth_code below is the user's server auth code you got from Google
auto user = app.login(realm::App::credentials::google(authCode)).get();
Expand All @@ -151,17 +160,19 @@ void testSignInWithGoogleIdTokenSyntax() {
auto idToken = realm::App::credentials::id_token{ myIdToken };

// :snippet-start: beta-google-id-token
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

// The id_token below is the user's OpenID Connect id_token you got from the Google OAuth response
auto user = app.login(realm::App::credentials::google(idToken)).get();
// :snippet-end:
}

TEST_CASE("get the current user", "[realm][sync]") {
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();

// :snippet-start: beta-get-current-user
Expand All @@ -174,8 +185,9 @@ TEST_CASE("get the current user", "[realm][sync]") {
}

TEST_CASE("confirm the user is logged in", "[realm][sync]") {
auto app = realm::App(APP_ID);
app.get_sync_manager().set_log_level(realm::logger::level::warn);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
// :snippet-start: check-user-is-logged-in
auto user = app.login(realm::App::credentials::anonymous()).get();
CHECK(user.is_logged_in());
Expand Down
5 changes: 3 additions & 2 deletions examples/cpp/beta/sync/call-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ TEST_CASE("call a function", "[realm][sync]")
// :snippet-start: beta-call-a-function
// Connect to an App Services App and authenticate a user
// :snippet-start: connect-app-services
auto app = realm::App(APP_ID);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
// :snippet-end:
app.get_sync_manager().set_log_level(realm::logger::level::warn); // :remove:
auto user = app.login(realm::App::credentials::anonymous()).get();
auto sync_config = user.flexible_sync_configuration();

Expand Down
4 changes: 3 additions & 1 deletion examples/cpp/beta/sync/custom-user-data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ static const std::string APP_ID = "cpp-tester-uliix";

TEST_CASE("custom user data", "[realm][sync]")
{
auto app = realm::App(APP_ID);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

// :snippet-start: beta-create
auto user = app.login(realm::App::credentials::anonymous()).get();
Expand Down
20 changes: 15 additions & 5 deletions examples/cpp/beta/sync/flexible-sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ struct Alpha_Sync_Dog : realm::object<Alpha_Sync_Dog> {
};

TEST_CASE("subscribe to a all objects of a type", "[sync]") {
auto app = realm::App(APP_ID);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto syncConfig = user.flexible_sync_configuration();
auto syncedRealmRef = realm::async_open<FlexibleSync_Dog>(syncConfig).get_future().get();
Expand Down Expand Up @@ -73,7 +75,9 @@ TEST_CASE("subscribe to a all objects of a type", "[sync]") {
TEST_CASE("subscribe to a subset of objects", "[sync]") {
// :snippet-start: flexible-sync-prerequisites
// Initialize the App, authenticate a user, and open the realm
auto app = realm::App(APP_ID);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto syncConfig = user.flexible_sync_configuration();
auto syncedRealmRef = realm::async_open<FlexibleSync_Dog>(syncConfig).get_future().get();
Expand Down Expand Up @@ -133,7 +137,9 @@ REALM_SCHEMA(Beta_FlexibleSync_Dog, _id, name, age)
// so I'm removing the Bluehawk markup but keeping the test to
// verify it all works as expected in the experimental namespace
TEST_CASE("beta subscribe to a all objects of a type", "[sync]") {
auto app = realm::App(APP_ID);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto syncConfig = user.flexible_sync_configuration();
auto syncedRealm = realm::experimental::db(syncConfig);
Expand All @@ -158,7 +164,9 @@ TEST_CASE("beta subscribe to a all objects of a type", "[sync]") {
TEST_CASE("beta subscribe to a subset of objects", "[sync]") {
// :snippet-start: beta-flexible-sync-prerequisites
// Initialize the App, authenticate a user, and open the realm
auto app = realm::App(APP_ID);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto syncConfig = user.flexible_sync_configuration();
auto syncedRealm = realm::experimental::db(syncConfig);
Expand Down Expand Up @@ -196,7 +204,9 @@ TEST_CASE("beta subscribe to a subset of objects", "[sync]") {

TEST_CASE("open a synced realm with old syntax", "[realm][sync]") {
// :snippet-start: open-a-synced-realm
auto app = realm::App(APP_ID);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
// Ensure anonymous authentication is enabled in the App Services App
auto user = app.login(realm::App::credentials::anonymous()).get();
auto sync_config = user.flexible_sync_configuration();
Expand Down
4 changes: 3 additions & 1 deletion examples/cpp/beta/sync/quick-start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ TEST_CASE("local quick start", "[realm][write]") {
// After skipping and then un-skipping this test with #if 0/#endif, I can't
// seem to replicate the issue. May require debugging in the future.
TEST_CASE("sync quick start", "[realm][write][sync][sync-logger]") {
auto app = realm::App(APP_ID);
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);
auto logLevel = realm::logger::level::info;
app.get_sync_manager().set_log_level(logLevel);
// :snippet-start: beta-authenticate-user
Expand Down
Loading
Loading