예제 #1
0
 public void App_Login_Anonymous()
 {
     SyncTestHelpers.RunBaasTestAsync(async() =>
     {
         var user = await DefaultApp.LogInAsync(Credentials.Anonymous());
     });
 }
        public void User_LinkCredentials_MultipleTimes_AllowsLoginWithAllCredentials()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await DefaultApp.LogInAsync(Credentials.Anonymous());

                var email = SyncTestHelpers.GetVerifiedUsername();
                await DefaultApp.EmailPasswordAuth.RegisterUserAsync(email, SyncTestHelpers.DefaultPassword);
                var linkedUser1 = await user.LinkCredentialsAsync(Credentials.EmailPassword(email, SyncTestHelpers.DefaultPassword));
                Assert.That(linkedUser1.Id, Is.EqualTo(user.Id));

                var functionId  = Guid.NewGuid().ToString();
                var linkedUser2 = await user.LinkCredentialsAsync(Credentials.Function(new { realmCustomAuthFuncUserId = functionId }));
                Assert.That(linkedUser2.Id, Is.EqualTo(user.Id));

                var emailPasswordUser = await DefaultApp.LogInAsync(Credentials.EmailPassword(email, SyncTestHelpers.DefaultPassword));
                Assert.That(emailPasswordUser.Id, Is.EqualTo(user.Id));

                var functionUser = await DefaultApp.LogInAsync(Credentials.Function(new { realmCustomAuthFuncUserId = functionId }));
                Assert.That(functionUser.Id, Is.EqualTo(user.Id));

                Assert.That(user.Identities, Has.Length.EqualTo(3));
                Assert.That(user.Identities[0].Provider, Is.EqualTo(Credentials.AuthProvider.Anonymous));
                Assert.That(user.Identities[0].Id, Is.Not.Null);

                Assert.That(user.Identities[1].Provider, Is.EqualTo(Credentials.AuthProvider.EmailPassword));
                Assert.That(user.Identities[1].Id, Is.Not.Null);

                Assert.That(user.Identities[2].Provider, Is.EqualTo(Credentials.AuthProvider.Function));
                Assert.That(user.Identities[2].Id, Is.EqualTo(functionId));
            });
        }
        public void TestManualClientResync()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var config = await GetIntegrationConfigAsync();

                Realm.DeleteRealm(config);
                using (var realm = await Realm.GetInstanceAsync(config))
                {
                    realm.Write(() =>
                    {
                        realm.Add(new IntPrimaryKeyWithValueObject());
                    });

                    await WaitForUploadAsync(realm);
                }

                // Delete Realm in ROS
                Debugger.Break();

                Exception ex   = null;
                Session.Error += (s, e) =>
                {
                    ex = e.Exception;
                };

                using (var realm = Realm.GetInstance(config))
                {
                    await Task.Delay(100);
                }

                Assert.That(ex, Is.InstanceOf <ClientResetException>());
            });
        }
        public void UserApiKeys_Enable_ReenablesKey()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var first  = await user.ApiKeys.CreateAsync("first");
                var second = await user.ApiKeys.CreateAsync("second");

                Assert.That(first.IsEnabled);
                Assert.That(second.IsEnabled);

                await user.ApiKeys.DisableAsync(first.Id);

                var keys = await user.ApiKeys.FetchAllAsync();

                Assert.That(keys.ElementAt(0).Id, Is.EqualTo(first.Id));
                Assert.IsFalse(keys.ElementAt(0).IsEnabled);

                Assert.That(keys.ElementAt(1).Id, Is.EqualTo(second.Id));
                Assert.IsTrue(keys.ElementAt(1).IsEnabled);

                await user.ApiKeys.EnableAsync(first.Id);

                keys = await user.ApiKeys.FetchAllAsync();

                Assert.That(keys.ElementAt(0).Id, Is.EqualTo(first.Id));
                Assert.IsTrue(keys.ElementAt(0).IsEnabled);

                Assert.That(keys.ElementAt(1).Id, Is.EqualTo(second.Id));
                Assert.IsTrue(keys.ElementAt(1).IsEnabled);
            });
        }
        public void UserCustomData()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();
                Assert.That(user.GetCustomData(), Is.Null);

                var updatedData = await user.RefreshCustomDataAsync();
                Assert.That(updatedData, Is.Null);

                var collection = user.GetMongoClient("BackingDB").GetDatabase("my-db").GetCollection("users");

                var customDataDoc = BsonDocument.Parse(@"{
                    _id: ObjectId(""" + ObjectId.GenerateNewId() + @"""),
                    user_id: """ + user.Id + @""",
                    age: 153,
                    interests: [ ""painting"", ""sci-fi"" ]
                }");

                await collection.InsertOneAsync(customDataDoc);

                updatedData = await user.RefreshCustomDataAsync();

                Assert.That(updatedData, Is.Not.Null);
                Assert.That(updatedData["age"].AsInt32, Is.EqualTo(153));
                Assert.That(updatedData["interests"].AsBsonArray.Select(i => i.AsString), Is.EquivalentTo(new[] { "painting", "sci-fi" }));

                Assert.That(user.GetCustomData(), Is.Not.Null);
                Assert.That(user.GetCustomData()["age"].AsInt32, Is.EqualTo(153));
            });
        }
        public void UserApiKeys_CanLoginWithReenabledKey()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user   = await GetUserAsync();
                var apiKey = await user.ApiKeys.CreateAsync("my-api-key");

                await user.ApiKeys.DisableAsync(apiKey.Id);

                var credentials = Credentials.ApiKey(apiKey.Value);

                var ex = await TestHelpers.AssertThrows <AppException>(() => DefaultApp.LogInAsync(credentials));
                Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
                Assert.That(ex.HelpLink, Does.Contain("logs?co_id="));
                Assert.That(ex.Message, Is.EqualTo("AuthError: invalid API key"));

                await user.ApiKeys.EnableAsync(apiKey.Id);

                var apiKeyUser = await DefaultApp.LogInAsync(credentials);

                Assert.That(apiKeyUser.Id, Is.EqualTo(user.Id));

                Assert.That(apiKeyUser.Provider, Is.EqualTo(Credentials.AuthProvider.ApiKey));
                Assert.That(apiKeyUser.RefreshToken, Is.Not.EqualTo(user.RefreshToken));
            });
        }
        public void User_LinkCredentials_AllowsLoginWithNewCredentials()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await DefaultApp.LogInAsync(Credentials.Anonymous());

                Assert.That(user.Identities, Has.Length.EqualTo(1));
                Assert.That(user.Identities[0].Provider, Is.EqualTo(Credentials.AuthProvider.Anonymous));
                Assert.That(user.Identities[0].Id, Is.Not.Null);

                var email = SyncTestHelpers.GetVerifiedUsername();
                await DefaultApp.EmailPasswordAuth.RegisterUserAsync(email, SyncTestHelpers.DefaultPassword);
                var linkedUser = await user.LinkCredentialsAsync(Credentials.EmailPassword(email, SyncTestHelpers.DefaultPassword));

                Assert.That(user.Identities, Has.Length.EqualTo(2));
                Assert.That(user.Identities[1].Provider, Is.EqualTo(Credentials.AuthProvider.EmailPassword));
                Assert.That(user.Identities[1].Id, Is.Not.Null);

                Assert.That(linkedUser.Identities, Has.Length.EqualTo(2));
                Assert.That(linkedUser.Id, Is.EqualTo(user.Id));
                Assert.That(linkedUser.Identities, Is.EquivalentTo(user.Identities));

                var emailPasswordUser = await DefaultApp.LogInAsync(Credentials.EmailPassword(email, SyncTestHelpers.DefaultPassword));

                Assert.That(emailPasswordUser.Id, Is.EqualTo(user.Id));
                Assert.That(emailPasswordUser.Identities, Is.EquivalentTo(user.Identities));
            });
        }
        public void UserCustomData_Generic()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();
                Assert.That(user.GetCustomData <CustomDataDocument>(), Is.Null);

                var updatedData = await user.RefreshCustomDataAsync <CustomDataDocument>();
                Assert.That(updatedData, Is.Null);

                var collection = user.GetMongoClient("BackingDB").GetDatabase("my-db").GetCollection <CustomDataDocument>("users");

                var customDataDoc = new CustomDataDocument
                {
                    UserId    = user.Id,
                    Age       = 45,
                    Interests = new[] { "swimming", "biking" }
                };

                await collection.InsertOneAsync(customDataDoc);

                updatedData = await user.RefreshCustomDataAsync <CustomDataDocument>();

                Assert.That(updatedData, Is.Not.Null);
                Assert.That(updatedData.Age, Is.EqualTo(45));
                Assert.That(updatedData.Interests, Is.EquivalentTo(new[] { "swimming", "biking" }));

                var customData = user.GetCustomData <CustomDataDocument>();

                Assert.That(customData, Is.Not.Null);
                Assert.That(customData.Age, Is.EqualTo(45));
                Assert.That(customData.Interests, Is.EquivalentTo(new[] { "swimming", "biking" }));
            });
        }
예제 #9
0
 public void GetInstanceAsync_CreatesNonExistentRealm()
 {
     SyncTestHelpers.RunBaasTestAsync(async() =>
     {
         var config = await GetIntegrationConfigAsync();
         await GetRealmAsync(config);
     });
 }
 public void User_Push_DeregisterDevice()
 {
     SyncTestHelpers.RunBaasTestAsync(async() =>
     {
         var user = await GetUserAsync();
         await user.GetPushClient("gcm").DeregisterDeviceAsync();
     });
 }
 public void User_Push_RegisterDevice_WrongService()
 {
     SyncTestHelpers.RunBaasTestAsync(async() =>
     {
         var user = await GetUserAsync();
         var ex   = await TestHelpers.AssertThrows <AppException>(() => user.GetPushClient("non-existent").RegisterDeviceAsync("hello"));
         Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
         Assert.That(ex.Message, Does.Contain("service not found: 'non-existent'"));
     });
 }
        public void UserApiKeys_FetchAll_WithNoKeys()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var keys = await user.ApiKeys.FetchAllAsync();
                Assert.That(keys, Is.Empty);
            });
        }
예제 #13
0
        public void CallFunction_WrongGeneric()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();
                var ex   = await TestHelpers.AssertThrows <Exception>(() => user.Functions.CallAsync <string>("sumFunc", 1, 2));

                Assert.That(ex.Message, Does.Contain("Cannot deserialize"));
            });
        }
예제 #14
0
        public void CallFunction_NoArguments()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user   = await GetUserAsync();
                var result = await user.Functions.CallAsync <int>("sumFunc");

                Assert.That(result, Is.EqualTo(0));
            });
        }
예제 #15
0
        public void CallFunctionGeneric_ReturnsResult()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user   = await GetUserAsync();
                var result = await user.Functions.CallAsync <int>("sumFunc", 1, 2, 3);

                Assert.That(result, Is.EqualTo(6));
            });
        }
예제 #16
0
        public void CallFunction_ReturnsResult()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user   = await GetUserAsync();
                var result = await user.Functions.CallAsync("sumFunc", 1L, 2L, 3L);

                Assert.That(result.AsInt64, Is.EqualTo(6));
            });
        }
        public void UserApiKeys_Fetch_WhenNoneExist_ReturnsNull()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var apiKey = await user.ApiKeys.FetchAsync(ObjectId.GenerateNewId());

                Assert.That(apiKey, Is.Null);
            });
        }
예제 #18
0
        public void OpenRealm_GuidPK_Works()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var partitionValue = Guid.NewGuid();
                var config1        = await GetIntegrationConfigAsync(partitionValue);
                var config2        = await GetIntegrationConfigAsync(partitionValue);

                await RunPartitionKeyTestsCore(config1, config2);
            });
        }
예제 #19
0
        public void OpenRealm_Int64PK_Works()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var partitionValue = TestHelpers.Random.Next(0, int.MaxValue);
                var config1        = await GetIntegrationConfigAsync(partitionValue);
                var config2        = await GetIntegrationConfigAsync(partitionValue);

                await RunPartitionKeyTestsCore(config1, config2);
            });
        }
예제 #20
0
        public void CallFunction_AndTestDeserialization_Null()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                string str = null;
                var result = await user.Functions.CallAsync <string>("mirror", str);

                Assert.That(result, Is.Null);
            });
        }
        public void UserApiKeys_Create_CreatesApiKeyAndRevealsValue()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user   = await GetUserAsync();
                var apiKey = await user.ApiKeys.CreateAsync("my-api-key");

                Assert.That(apiKey.IsEnabled);
                Assert.That(apiKey.Name, Is.EqualTo("my-api-key"));
                Assert.That(apiKey.Value, Is.Not.Null);
                Assert.That(apiKey.Id, Is.Not.EqualTo(default(ObjectId)));
            });
        }
        public void User_LinkCredentials_WhenAnonymous_Throws()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var ex = await TestHelpers.AssertThrows <AppException>(() => user.LinkCredentialsAsync(Credentials.Anonymous()));

                // TODO: this should be bad request when https://jira.mongodb.org/browse/REALMC-7028 is fixed
                Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
                Assert.That(ex.Message, Does.Contain("linking an anonymous identity is not allowed"));
            });
        }
        public void UserApiKeys_Create_WithInvalidName_Throws()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var ex = await TestHelpers.AssertThrows <AppException>(() => user.ApiKeys.CreateAsync("My very cool key"));

                Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
                Assert.That(ex.Message, Does.Contain("InvalidParameter"));
                Assert.That(ex.Message, Does.Contain("can only contain ASCII letters, numbers, underscores, and hyphens"));
                Assert.That(ex.HelpLink, Does.Contain("logs?co_id="));
            });
        }
        public void User_Facebook_LogsInAndReadsDataFromFacebook()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                const string fbToken = "EAAFYw2aZAL1EBAHBBH22XBDZAutJFQ65KxH0bZAexYul5KtsHcjhI722XYEr4jKlaNvlosFsdZCT8dGUQNy2euZB684mpvtIIJEWWYMoH66bbEbKIrHRWqZBC8KMpSscoyzhFTJMpDYsrIilZBRN1A6bicXGaUNXVz5A0ucyZB7WkmQ8uUmdRWel9q6S8BJH3ZBCZAzWtcZCYmgEwZDZD";
                var credentials      = Credentials.Facebook(fbToken);
                var user             = await DefaultApp.LogInAsync(credentials);

                Assert.That(user.Id, Is.Not.Null);

                Assert.That(user.Profile.FirstName, Is.Not.Null);
                Assert.That(user.Profile.LastName, Is.Not.Null);
            });
        }
        public void UserApiKeys_FetchAll_WithOneKey()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var key1 = await user.ApiKeys.CreateAsync("foo");

                var keys = await user.ApiKeys.FetchAllAsync();
                Assert.That(keys.Count(), Is.EqualTo(1));

                AssertKeysAreSame(key1, keys.Single());
            });
        }
        public void UserApiKeys_Fetch_WhenIdMatches_ReturnsKey()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var key = await user.ApiKeys.CreateAsync("foo");

                var fetched = await user.ApiKeys.FetchAsync(key.Id);

                Assert.That(fetched, Is.Not.Null);
                AssertKeysAreSame(key, fetched);
            });
        }
        public void UserApiKeys_EnableApiKey_WhenEnabled_IsNoOp()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var key = await user.ApiKeys.CreateAsync("foo");
                Assert.That(key.IsEnabled);

                await user.ApiKeys.EnableAsync(key.Id);

                var fetched = await user.ApiKeys.FetchAsync(key.Id);
                Assert.That(fetched.IsEnabled);
            });
        }
        public void UserApiKeys_EnableApiKey_WhenNonExistent_Throws()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await GetUserAsync();

                var id = ObjectId.GenerateNewId();
                var ex = await TestHelpers.AssertThrows <AppException>(() => user.ApiKeys.EnableAsync(id));

                Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
                Assert.That(ex.Message, Does.Contain("doesn't exist"));
                Assert.That(ex.Message, Does.Contain(id.ToString()));
                Assert.That(ex.HelpLink, Does.Contain("logs?co_id="));
            });
        }
        public void User_LinkCredentials_WhenInUse_Throws()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var existingEmail = SyncTestHelpers.GetVerifiedUsername();
                await DefaultApp.EmailPasswordAuth.RegisterUserAsync(existingEmail, SyncTestHelpers.DefaultPassword);
                var emailUser = await DefaultApp.LogInAsync(Credentials.EmailPassword(existingEmail, SyncTestHelpers.DefaultPassword));

                var anonUser = await DefaultApp.LogInAsync(Credentials.Anonymous());

                var ex = await TestHelpers.AssertThrows <AppException>(() => anonUser.LinkCredentialsAsync(Credentials.EmailPassword(existingEmail, SyncTestHelpers.DefaultPassword)));

                Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
                Assert.That(ex.Message, Does.Contain("a user already exists with the specified provider"));
            });
        }
        public void UserApiKeys_CanLoginWithGeneratedKey()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user   = await GetUserAsync();
                var apiKey = await user.ApiKeys.CreateAsync("my-api-key");

                var credentials = Credentials.ApiKey(apiKey.Value);
                var apiKeyUser  = await DefaultApp.LogInAsync(credentials);

                Assert.That(apiKeyUser.Id, Is.EqualTo(user.Id));

                Assert.That(apiKeyUser.Provider, Is.EqualTo(Credentials.AuthProvider.ApiKey));
                Assert.That(apiKeyUser.RefreshToken, Is.Not.EqualTo(user.RefreshToken));
            });
        }