public void Test_ObjectRead() { SyncTestHelpers.RequiresRos(); AsyncContext.Run(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.RequiresRos(); AsyncContext.Run(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 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)); } }); }
public void Test_ObjectSetPermissions() { SyncTestHelpers.RequiresRos(); AsyncContext.Run(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 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 User_OfferPermissions_GrantsPermissions() { SyncTestHelpers.RequiresRos(); AsyncContext.Run(async () => { var alice = await SyncTestHelpers.GetUserAsync(); var bob = await SyncTestHelpers.GetUserAsync(); var realmPath = $"/{alice.Identity}/testPermission"; var realmUrl = SyncTestHelpers.RealmUri(realmPath).AbsoluteUri; EnsureRealmExists(alice, realmUrl); var token = await alice.OfferPermissionsAsync(realmUrl, AccessLevel.Write).Timeout(2000); var alicesUrl = await bob.AcceptPermissionOfferAsync(token).Timeout(2000); Assert.That(alicesUrl, Is.EqualTo(realmPath)); await AssertPermissions(alice, bob, realmPath, AccessLevel.Write).Timeout(10000); }); }
public void GetInstanceAsync_CreatesNonExistentRealm() { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUserAsync(); var realmUri = SyncTestHelpers.RealmUri("~/GetInstanceAsync_CreatesNonExistentRealm"); var config = new SyncConfiguration(user, realmUri, 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 User_AcceptPermissionOffer_WhenOfferExpired_ShouldGetError() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUserAsync(); var bob = await SyncTestHelpers.GetUserAsync(); var realmUrl = SyncTestHelpers.RealmUri($"{alice.Identity}/testPermission").AbsoluteUri; 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 AssertThrows <PermissionException>(() => bob.AcceptPermissionOfferAsync(token), ex => { Assert.That(ex.ErrorCode, Is.EqualTo(ErrorCode.ExpiredPermissionOffer)); }); }); }
private void TestSSLCore(Action <SyncConfiguration> setupSecureConfig, bool openAsync) { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUserAsync(); const string path = "~/TestSSLCore"; var realmUri = SyncTestHelpers.RealmUri(path); var config = new SyncConfiguration(user, realmUri); var secureRealmUri = SyncTestHelpers.SecureRealmUri(path); var secureConfig = new SyncConfiguration(user, secureRealmUri, config.DatabasePath + "2"); setupSecureConfig(secureConfig); using (var realm = GetRealm(config)) { realm.Write(() => { realm.Add(new IntPrimaryKeyWithValueObject { Id = 1, StringValue = "some value" }); }); await GetSession(realm).WaitForUploadAsync(); } using (var newRealm = await SyncTestHelpers.GetInstanceAsync(secureConfig, openAsync)) { CleanupOnTearDown(newRealm); var items = newRealm.All <IntPrimaryKeyWithValueObject>(); Assert.That(items.Count(), Is.EqualTo(1)); Assert.That(items.Single().StringValue, Is.EqualTo("some value")); } }); }
public void UserLogin_WhenAnonymousAndOtherUsersLoggedIn_ShouldLoginDifferentUser() { SyncTestHelpers.RequiresRos(); AsyncContext.Run(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_ShouldDownloadRealm(bool singleTransaction) { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUserAsync(); var realmUri = SyncTestHelpers.RealmUri("~/GetInstanceAsync_ShouldDownloadRealm"); var config = new SyncConfiguration(user, realmUri, Guid.NewGuid().ToString()); var asyncConfig = new SyncConfiguration(user, realmUri, config.DatabasePath + "_async"); using (var realm = GetRealm(config)) { AddDummyData(realm, singleTransaction); await GetSession(realm).WaitForUploadAsync(); } using (var asyncRealm = await GetRealmAsync(asyncConfig)) { Assert.That(asyncRealm.All <IntPrimaryKeyWithValueObject>().Count(), Is.EqualTo(500)); } }); }
public void Test_ClassSetPermissions() { SyncTestHelpers.RequiresRos(); AsyncContext.Run(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 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 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 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 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 WaitForSyncAsync(realm); Assert.That(objects.Count(), Is.EqualTo(6)); } }); }
private async Task <Realm> GetQueryBasedRealm(bool openAsync, [CallerMemberName] string realmPath = null) { SyncTestHelpers.RequiresRos(); var user = await SyncTestHelpers.GetUserAsync(); var config = new QueryBasedSyncConfiguration(SyncTestHelpers.RealmUri($"~/{realmPath}_{openAsync}"), user, Guid.NewGuid().ToString()) { ObjectClasses = new[] { typeof(ObjectA), typeof(ObjectB) } }; using (var original = GetRealm(config)) { original.Write(() => { for (var i = 0; i < 10; i++) { original.Add(new ObjectA { StringValue = "A #" + i, IntValue = i, B = new ObjectB { StringValue = "B #" + i, BoolValue = i % 2 == 0, } }); } }); await GetSession(original).WaitForUploadAsync(); } try { Realm.DeleteRealm(config); } catch { } config = new QueryBasedSyncConfiguration(config.ServerUri, config.User, config.DatabasePath + "_partial") { ObjectClasses = config.ObjectClasses }; Realm result; // We test both `GetInstance` and `GetInstanceAsync` to guard against regressions: // https://github.com/realm/realm-dotnet/issues/1814 if (openAsync) { result = await GetRealmAsync(config).Timeout(5000); } else { result = GetRealm(config); await GetSession(result).WaitForDownloadAsync(); } Assert.That(result.All <ObjectB>().Count(), Is.EqualTo(0)); Assert.That(result.All <ObjectA>().Count(), Is.EqualTo(0)); return(result); }