public void GetInstanceAsync_ReportsProgress()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var realmPath             = Guid.NewGuid().ToString();
                var user                  = await SyncTestHelpers.GetUserAsync();
                var config                = new FullSyncConfiguration(new Uri($"/~/{realmPath}", UriKind.Relative), user, Guid.NewGuid().ToString());
                const int ObjectSize      = 1000000;
                const int ObjectsToRecord = 20;
                using (var realm = GetRealm(config))
                {
                    for (var i = 0; i < ObjectsToRecord; i++)
                    {
                        realm.Write(() =>
                        {
                            realm.Add(new HugeSyncObject(ObjectSize));
                        });
                    }

                    await SyncTestHelpers.WaitForSyncAsync(realm);
                }

                var callbacksInvoked = 0;

                var lastProgress = default(SyncProgress);
                config           = new FullSyncConfiguration(new Uri($"/~/{realmPath}", UriKind.Relative), user, Guid.NewGuid().ToString())
                {
                    OnProgress = (progress) =>
                    {
                        callbacksInvoked++;
                        lastProgress = progress;
                    }
                };

                using (var realm = await GetRealmAsync(config))
                {
                    Assert.That(realm.All <HugeSyncObject>().Count(), Is.EqualTo(ObjectsToRecord));
                    Assert.That(callbacksInvoked, Is.GreaterThan(0));
                    Assert.That(lastProgress.TransferableBytes, Is.EqualTo(lastProgress.TransferredBytes));
                }
            });
        }
        public void UserLogin_WhenAnonymousAndOtherUsersLoggedIn_ShouldLoginDifferentUser()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                // Login a regular user
                await SyncTestHelpers.GetUserAsync();

                // Login an anonymous user
                var first = await User.LoginAsync(Credentials.Anonymous(), SyncTestHelpers.AuthServerUri);

                // Login another regular user
                await SyncTestHelpers.GetUserAsync();

                // Login a second anonymous user
                var second = await User.LoginAsync(Credentials.Anonymous(), SyncTestHelpers.AuthServerUri);

                // Expect that the anonymous users to be different
                Assert.That(first.Identity, Is.Not.EqualTo(second.Identity));
                Assert.That(User.AllLoggedIn.Length, Is.EqualTo(4));
            });
        }
        public void GetInstanceAsync_CreatesNonExistentRealm()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var user     = await SyncTestHelpers.GetUserAsync();
                var realmUri = SyncTestHelpers.RealmUri("~/GetInstanceAsync_CreatesNonExistentRealm");
                var config   = new FullSyncConfiguration(realmUri, user, Guid.NewGuid().ToString());

                try
                {
                    await GetRealmAsync(config);
                }
                catch (Exception ex)
                {
                    Assert.That(ex, Is.TypeOf <RealmException>().And.InnerException.TypeOf <SessionException>());
                    var sessionException = (SessionException)ex.InnerException;
                    Assert.That(sessionException.ErrorCode, Is.EqualTo((ErrorCode)89));
                    Assert.That(sessionException.Message, Contains.Substring("Operation canceled"));
                }
            });
        }
        public void Test_ObjectRead()
        {
            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);

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

                    r.Add(new ObjectWithPermissions
                    {
                        Id          = 2,
                        StringValue = "Value 2"
                    });
                }, addObjects: false);

                using (var realm = GetRealm(userA, realmUri))
                {
                    var subscription = await SubscribeToObjectsAsync(realm);
                    Assert.That(subscription.Results.Count(), Is.EqualTo(1));
                }

                using (var realm = GetRealm(userB, realmUri))
                {
                    var subscription = await SubscribeToObjectsAsync(realm);
                    Assert.That(subscription.Results.Count(), Is.EqualTo(0));
                }
            });
        }
        public void Test_ClassRead()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var userA = await SyncTestHelpers.GetUserAsync();
                var userB = await SyncTestHelpers.GetUserAsync();

                var realmUri = await CreateRealm(r =>
                {
                    CreatePermissions(ClassPermission.Get <ObjectWithPermissions>(r).Permissions);
                    var reader = PermissionRole.Get(r, "reader");
                    reader.Users.Add(userA);
                });

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

                    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(3));
                }

                using (var realm = GetRealm(userB, realmUri))
                {
                    var subscription = await SubscribeToObjectsAsync(realm);
                    AssertRealmPrivileges(realm, RealmPrivileges.Read | RealmPrivileges.Update | RealmPrivileges.ModifySchema | RealmPrivileges.SetPermissions);
                    AssertClassPrivileges(realm, 0);

                    Assert.That(subscription.Results.Count(), Is.EqualTo(0));
                }
            });
        }
Esempio n. 6
0
        public void User_GetPermissionOffers_Test()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var alice = await SyncTestHelpers.GetUserAsync();

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

                var readToken  = await alice.OfferPermissionsAsync(realmUrl, AccessLevel.Read);
                var writeToken = await alice.OfferPermissionsAsync(realmUrl, AccessLevel.Write);

                var offers = await alice.GetPermissionOffersAsync();
                Assert.That(offers.Count(), Is.EqualTo(2));

                await alice.InvalidateOfferAsync(readToken);
                var offersAfterDeletion = await alice.GetPermissionOffersAsync();

                Assert.That(offersAfterDeletion.Count(), Is.EqualTo(1));
                Assert.That(offersAfterDeletion.Single().Token, Is.EqualTo(writeToken));
            });
        }
Esempio n. 7
0
        public void User_AcceptPermissionOffer_WhenOfferExpired_ShouldGetError()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var alice = await SyncTestHelpers.GetUserAsync();
                var bob   = await SyncTestHelpers.GetUserAsync();

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

                var token = await alice.OfferPermissionsAsync(realmUrl, AccessLevel.Write, expiresAt: DateTimeOffset.UtcNow.AddSeconds(1));

                Assert.That(token, Is.Not.Null);

                await Task.Delay(2000);

                await TestHelpers.AssertThrows <HttpException>(() => bob.AcceptPermissionOfferAsync(token), ex =>
                {
                    Assert.That(ex.ErrorCode, Is.EqualTo(ErrorCode.ExpiredPermissionOffer));
                });
            });
        }
        public void Test_ObjectSetPermissions()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var userA = await SyncTestHelpers.GetUserAsync();
                var userB = await SyncTestHelpers.GetUserAsync();

                var realmUri = await CreateRealm(assignRoles: null, addObjects: false);

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

                    realm.Write(() =>
                    {
                        var obj1 = realm.Add(new ObjectWithPermissions
                        {
                            Id          = 1,
                            StringValue = "1"
                        });

                        var foo            = PermissionRole.Get(realm, "foo");
                        var permission     = Permission.Get(foo, obj1);
                        permission.CanRead = true;
                        foo.Users.Add(userB);
                    });

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

                using (var realm = GetRealm(userB, realmUri))
                {
                    var subscription = await SubscribeToObjectsAsync(realm);
                    Assert.That(subscription.Results.Count(), Is.EqualTo(1));
                }
            });
        }
        public void GetInstanceAsync_ShouldDownloadRealm(bool singleTransaction)
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var user = await SyncTestHelpers.GetUserAsync();

                var realmUri = SyncTestHelpers.RealmUri("~/GetInstanceAsync_ShouldDownloadRealm");

                var config      = new FullSyncConfiguration(realmUri, user, Guid.NewGuid().ToString());
                var asyncConfig = new FullSyncConfiguration(realmUri, user, config.DatabasePath + "_async");

                using (var realm = GetRealm(config))
                {
                    AddDummyData(realm, singleTransaction);

                    await SyncTestHelpers.WaitForUploadAsync(realm);
                }

                using (var asyncRealm = await GetRealmAsync(asyncConfig))
                {
                    Assert.That(asyncRealm.All <IntPrimaryKeyWithValueObject>().Count(), Is.EqualTo(500));
                }
            });
        }
Esempio n. 10
0
        private void TestSSLCore(Action <FullSyncConfiguration> setupSecureConfig, bool openAsync)
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var user          = await SyncTestHelpers.GetUserAsync();
                const string path = "~/TestSSLCore";
                var realmUri      = SyncTestHelpers.RealmUri(path);
                var config        = new FullSyncConfiguration(realmUri, user);

                var secureRealmUri = SyncTestHelpers.SecureRealmUri(path);
                var secureConfig   = new FullSyncConfiguration(secureRealmUri, user, config.DatabasePath + "2");
                setupSecureConfig(secureConfig);

                using (var realm = GetRealm(config))
                {
                    realm.Write(() =>
                    {
                        realm.Add(new IntPrimaryKeyWithValueObject
                        {
                            Id          = 1,
                            StringValue = "some value"
                        });
                    });

                    await SyncTestHelpers.WaitForUploadAsync(realm);
                }

                using (var newRealm = await GetRealmAsync(secureConfig, openAsync))
                {
                    var items = newRealm.All <IntPrimaryKeyWithValueObject>();

                    Assert.That(items.Count(), Is.EqualTo(1));
                    Assert.That(items.Single().StringValue, Is.EqualTo("some value"));
                }
            });
        }
        public void Test_ClassSetPermissions()
        {
            SyncTestHelpers.RunRosTestAsync(async() =>
            {
                var userA = await SyncTestHelpers.GetUserAsync();
                var userB = await SyncTestHelpers.GetUserAsync();
                var userC = await SyncTestHelpers.GetUserAsync();

                var realmUri = await CreateRealm(r =>
                {
                    CreatePermissions(ClassPermission.Get <ObjectWithPermissions>(r).Permissions);

                    var reader = PermissionRole.Get(r, "reader");
                    reader.Users.Add(userA);
                    reader.Users.Add(userB);
                    reader.Users.Add(userC);

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

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

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

                    // B is 'writer' - shouldn't be able to update the role access level
                    realm.Write(() =>
                    {
                        var readerPermission       = Permission.Get <ObjectWithPermissions>("reader", realm);
                        readerPermission.CanUpdate = true;
                        readerPermission.CanCreate = true;
                    });

                    await SyncTestHelpers.WaitForSyncAsync(realm);
                }

                using (var realm = GetRealm(userC, realmUri))
                {
                    // C shouldn't be able to create objects
                    var subscription = await SubscribeToObjectsAsync(realm);
                    AssertRealmPrivileges(realm, RealmPrivileges.Read | RealmPrivileges.Update | RealmPrivileges.ModifySchema | RealmPrivileges.SetPermissions);
                    AssertClassPrivileges(realm, ClassPrivileges.Read | ClassPrivileges.Subscribe);
                    AssertObjectPrivileges(realm, ObjectPrivileges.Read);

                    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(3));
                }

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

                    // A should be able to update role access level
                    realm.Write(() =>
                    {
                        var readerPermission       = Permission.Get <ObjectWithPermissions>("reader", realm);
                        readerPermission.CanUpdate = true;
                        readerPermission.CanCreate = true;
                    });

                    await SyncTestHelpers.WaitForSyncAsync(realm);
                }

                using (var realm = GetRealm(userC, realmUri))
                {
                    // C should now be able to create objects
                    // Why does my subscription timeout?
                    // var subscription = await SubscribeToObjectsAsync(realm);

                    await SyncTestHelpers.WaitForSyncAsync(realm);
                    AssertRealmPrivileges(realm, RealmPrivileges.Read | RealmPrivileges.Update | RealmPrivileges.ModifySchema | RealmPrivileges.SetPermissions);
                    AssertClassPrivileges(realm, ClassPrivileges.Read | ClassPrivileges.Subscribe | ClassPrivileges.Update | ClassPrivileges.Create);
                    AssertObjectPrivileges(realm, ObjectPrivileges.Read | ObjectPrivileges.Update | ObjectPrivileges.Delete | ObjectPrivileges.SetPermissions);

                    var objects = realm.All <ObjectWithPermissions>();
                    Assert.That(objects.Count(), Is.EqualTo(3));

                    AddObjectsToRealm(realm, new[] { 4, 5, 6 });

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