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"));
            });
        }
        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));
            });
        }
        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));
            });
        }
        public void UserLogin_WhenAnonymous_LogsUserIn()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var credentials = Credentials.Anonymous();
                var user        = await User.LoginAsync(credentials, SyncTestHelpers.AuthServerUri);

                Assert.That(user, Is.Not.Null);
                Assert.That(user.Identity, Is.Not.Null);
            });
        }
        public void UserLogin_WhenAnonymousAndDifferentCredentials_ShouldLoginDifferentUser()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var first  = await User.LoginAsync(Credentials.Anonymous(), SyncTestHelpers.AuthServerUri);
                var second = await User.LoginAsync(Credentials.Anonymous(), SyncTestHelpers.AuthServerUri);

                Assert.That(first.Identity, Is.Not.EqualTo(second.Identity));
                Assert.That(User.AllLoggedIn.Length, Is.EqualTo(2));
            });
        }
        public void UserLogin_WhenNickname_LogsUserIn()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var credentials = Credentials.Nickname(Guid.NewGuid().ToString());
                var user        = await User.LoginAsync(credentials, SyncTestHelpers.AuthServerUri);

                Assert.That(user, Is.Not.Null);
                Assert.That(user.Identity, Is.Not.Null);
            });
        }
Exemple #7
0
        protected async Task <User> GetUserAsync(App app = null)
        {
            app ??= DefaultApp;

            var username = SyncTestHelpers.GetVerifiedUsername();
            await app.EmailPasswordAuth.RegisterUserAsync(username, SyncTestHelpers.DefaultPassword);

            var credentials = Credentials.EmailPassword(username, SyncTestHelpers.DefaultPassword);

            return(await app.LogInAsync(credentials));
        }
Exemple #8
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);
            });
        }
        public void Test_ObjectUpdate()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var userA = await SyncTestHelpers.GetUserAsync();
                var userB = await SyncTestHelpers.GetUserAsync();

                var realmUri = await CreateRealm(r =>
                {
                    var reader = PermissionRole.Get(r, "reader");
                    reader.Users.Add(userA);
                    reader.Users.Add(userB);

                    var writer = PermissionRole.Get(r, "writer");
                    writer.Users.Add(userA);

                    var obj1 = r.Add(new ObjectWithPermissions
                    {
                        Id          = 1,
                        StringValue = "Value 1"
                    });
                    CreatePermissions(obj1.Permissions);
                }, addObjects: false);

                using (var realm = GetRealm(userA, realmUri))
                {
                    var subscription = await SubscribeToObjectsAsync(realm);
                    var obj1         = subscription.Results.Single();
                    realm.Write(() =>
                    {
                        obj1.StringValue = "New value";
                    });

                    await SyncTestHelpers.WaitForSyncAsync(realm);

                    Assert.That(obj1.StringValue, Is.EqualTo("New value"));
                }

                using (var realm = GetRealm(userB, realmUri))
                {
                    var subscription = await SubscribeToObjectsAsync(realm);
                    var obj1         = subscription.Results.Single();
                    realm.Write(() =>
                    {
                        obj1.StringValue = "New value #2";
                    });

                    Assert.That(obj1.StringValue, Is.EqualTo("New value #2"));
                    await SyncTestHelpers.WaitForSyncAsync(realm);

                    Assert.That(obj1.StringValue, Is.EqualTo("New value"));
                }
            });
        }
Exemple #10
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);
            });
        }
        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);
            });
        }
Exemple #12
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 TrustedCA_WhenFileDoesntExist_Throws()
 {
     SyncTestHelpers.RunRosTestAsync(async() =>
     {
         var user   = await SyncTestHelpers.GetUserAsync();
         var config = new FullSyncConfiguration(SyncTestHelpers.RealmUri("~/TrustedCA_WhenFileDoesntExist_Throws"), user)
         {
             TrustedCAPath = "something.pem"
         };
         Assert.That(() => GetRealm(config), Throws.TypeOf <FileNotFoundException>());
     });
 }
        public void DefaultConfiguration_WhenOneUserLoggedIn_ShouldWork(string userScheme, string realmScheme)
        {
            TestHelpers.RunAsyncTest(async() =>
            {
                await SyncTestHelpers.GetFakeUserAsync(scheme: userScheme);

                var config = new QueryBasedSyncConfiguration();
                Assert.That(!config.IsFullSync);
                Assert.That(config.ServerUri.Scheme, Is.EqualTo(realmScheme));
                Assert.That(config.ServerUri.Segments, Is.EqualTo(new[] { "/", "default" }));
            });
        }
        public void UserAllLoggedIn_WhenThereIsOneUser_ShouldReturnThatUser()
        {
            TestHelpers.RunAsyncTest(async() =>
            {
                var user = await SyncTestHelpers.GetFakeUserAsync();

                var users = User.AllLoggedIn;

                Assert.That(users.Length, Is.EqualTo(1));
                Assert.That(users[0], Is.EqualTo(user));
            });
        }
Exemple #16
0
        public void User_OfferPermissions_WhenExpired_ShouldThrow()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var alice = await SyncTestHelpers.GetUserAsync();

                var realmUrl = SyncTestHelpers.RealmUri($"{alice.Identity}/testPermission").AbsoluteUri;
                await EnsureRealmExists(alice, realmUrl);

                await TestHelpers.AssertThrows <ArgumentException>(() => alice.OfferPermissionsAsync(realmUrl, AccessLevel.Write, DateTimeOffset.UtcNow.AddDays(-1)));
            });
        }
Exemple #17
0
        public void User_OfferPermissions_WhenNoAccess_ShouldThrow()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var alice = await SyncTestHelpers.GetUserAsync();

                var realmUrl = SyncTestHelpers.RealmUri($"{alice.Identity}/testPermission").AbsoluteUri;
                await EnsureRealmExists(alice, realmUrl);

                await TestHelpers.AssertThrows <ArgumentException>(() => alice.OfferPermissionsAsync(realmUrl, AccessLevel.None));
            });
        }
Exemple #18
0
        public void User_ApplyPermissions_WithEmail_GrantsAndRevokesPermissions()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var alice          = await SyncTestHelpers.GetUserAsync();
                var bobEmail       = $"{Guid.NewGuid()}@foo.bar";
                var bobCredentials = Credentials.UsernamePassword(bobEmail, "a", createUser: true);
                var bob            = await User.LoginAsync(bobCredentials, SyncTestHelpers.AuthServerUri);

                await TestApplyPermissions(alice, bob, PermissionCondition.Email(bobEmail)).Timeout(20000);
            });
        }
Exemple #19
0
        public void User_AcceptPermissionOffer_WhenTokenIsInvalid_ShouldGetError()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var user = await SyncTestHelpers.GetUserAsync();

                await TestHelpers.AssertThrows <HttpException>(() => user.AcceptPermissionOfferAsync("some string"), ex =>
                {
                    Assert.That(ex.ErrorCode, Is.EqualTo(ErrorCode.InvalidParameters));
                });
            });
        }
        public void Test_RealmUpdate()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var userA = await SyncTestHelpers.GetUserAsync();
                var userB = await SyncTestHelpers.GetUserAsync();

                var realmUri = await CreateRealm(r =>
                {
                    CreatePermissions(RealmPermission.Get(r).Permissions);
                    var reader = PermissionRole.Get(r, "reader");
                    reader.Users.Add(userA);
                    reader.Users.Add(userB);

                    var writer = PermissionRole.Get(r, "writer");
                    writer.Users.Add(userA);
                });

                using (var realm = GetRealm(userA, realmUri))
                {
                    var subscription = await SubscribeToObjectsAsync(realm);
                    AssertRealmPrivileges(realm, RealmPrivileges.Read | RealmPrivileges.Update);
                    AssertClassPrivileges(realm, ClassPrivileges.Read | ClassPrivileges.Subscribe | ClassPrivileges.Create | ClassPrivileges.SetPermissions | ClassPrivileges.Update);
                    AssertObjectPrivileges(realm, ObjectPrivileges.Read | ObjectPrivileges.Delete | ObjectPrivileges.SetPermissions | ObjectPrivileges.Update);

                    Assert.That(subscription.Results.Count(), Is.EqualTo(3));
                    AddObjectsToRealm(realm, new[] { 4, 5, 6 });

                    Assert.That(subscription.Results.Count(), Is.EqualTo(6));

                    await SyncTestHelpers.WaitForSyncAsync(realm);
                    Assert.That(subscription.Results.Count(), Is.EqualTo(6));
                }

                using (var realm = GetRealm(userB, realmUri))
                {
                    var subscription = await SubscribeToObjectsAsync(realm);

                    AssertRealmPrivileges(realm, RealmPrivileges.Read);
                    AssertClassPrivileges(realm, ClassPrivileges.Read | ClassPrivileges.Subscribe);
                    AssertObjectPrivileges(realm, ObjectPrivileges.Read);

                    Assert.That(subscription.Results.Count(), Is.EqualTo(6));
                    AddObjectsToRealm(realm, new[] { 7, 8, 9 });

                    Assert.That(subscription.Results.Count(), Is.EqualTo(9));

                    await SyncTestHelpers.WaitForSyncAsync(realm);
                    Assert.That(subscription.Results.Count(), Is.EqualTo(6));
                }
            });
        }
Exemple #21
0
        private async Task<Realm> GetSyncedRealm(Type objectType)
        {
            var credentials = Credentials.UsernamePassword(Constants.AdminUsername, Constants.AdminPassword, false);
            var user = await User.LoginAsync(credentials, SyncTestHelpers.AuthServerUri);
            var configuration = new FullSyncConfiguration(SyncTestHelpers.RealmUri($"~/merge_by_pk_{objectType.Name}"), user, Guid.NewGuid().ToString())
            {
                ObjectClasses = new[] { objectType }
            };

            Realm.DeleteRealm(configuration);

            return GetRealm(configuration);
        }
        public void UserLogin_WhenNicknameAfterLogout_LogsSameUserIn()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var nickname = Guid.NewGuid().ToString();
                var first    = await User.LoginAsync(Credentials.Nickname(nickname), SyncTestHelpers.AuthServerUri);
                await first.LogOutAsync();

                var second = await User.LoginAsync(Credentials.Nickname(nickname), SyncTestHelpers.AuthServerUri);

                Assert.That(first.Identity, Is.EqualTo(second.Identity));
            });
        }
        public void GetInstance_WhenDynamicAndDoesntExist_ReturnsEmptySchema()
        {
            TestHelpers.RunAsyncTest(async() =>
            {
                var config       = await SyncTestHelpers.GetFakeConfigAsync();
                config.IsDynamic = true;

                using (var realm = GetRealm(config))
                {
                    Assert.That(realm.Schema, Is.Empty);
                }
            });
        }
        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_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 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);
            });
        }
Exemple #27
0
        /// <summary>
        /// Opens a random realm and calls session.Stop(). It will assert state changes
        /// to Inactive.
        /// </summary>
        /// <returns>The stopped session.</returns>
        private async Task <Session> OpenRealmAndStopSession()
        {
            var config = await SyncTestHelpers.GetFakeConfigAsync();

            var realm   = GetRealm(config);
            var session = GetSession(realm);

            Assert.That(session.State, Is.EqualTo(SessionState.Active));

            session.Stop();
            Assert.That(session.State, Is.EqualTo(SessionState.Inactive));

            return(session);
        }
        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_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="));
            });
        }