public void PermissionResponse_WhenOfferIsValid_ShouldSetRealmUrl() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bob = await SyncTestHelpers.GetUser(); // Opening a synced realm with just read permission fails. // OS issue: https://github.com/realm/realm-object-store/issues/312 var realmUrl = await GrantPermissions(alice, bob); var syncConfig = new SyncConfiguration(bob, new Uri(realmUrl)); Realm realm = null; Assert.That(() => realm = Realm.GetInstance(syncConfig), Throws.Nothing); var handler = new EventHandler <ErrorEventArgs>((sender, e) => { Assert.Fail("Opening the realm should not cause an error.", e.Exception); }); Session.Error += handler; await Task.Delay(2000); realm.Dispose(); Session.Error -= handler; }); }
public void GetInstanceAsync_ShouldDownloadRealm(bool singleTransaction) { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); var realmUri = new Uri(SyncTestHelpers.GetRealmUrl()); var config = new SyncConfiguration(user, realmUri); var asyncConfig = new SyncConfiguration(user, realmUri, config.DatabasePath + "_async"); try { using (var realm = Realm.GetInstance(config)) { AddDummyData(realm, singleTransaction); await realm.GetSession().WaitForUploadAsync(); } using (var asyncRealm = await Realm.GetInstanceAsync(asyncConfig)) { Assert.That(asyncRealm.All <IntPrimaryKeyWithValueObject>().Count(), Is.EqualTo(500)); } } finally { Realm.DeleteRealm(config); Realm.DeleteRealm(asyncConfig); } }); }
public void PermissionChange_IsProcessedByServer() { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); var permissionChange = await CreateChange(user, "*"); Assert.That(permissionChange.Status, Is.EqualTo(ManagementObjectStatus.Success)); }); }
public void User_ApplyPermissions_WithUserId_GrantsAndRevokesPermissions() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bob = await SyncTestHelpers.GetUser(); await TestApplyPermissions(alice, bob, PermissionCondition.UserId(bob.Identity)); }); }
public void PermissionOffer_WhenValid_TokenIsSet() { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); var permissionOffer = await CreateOffer(user); Assert.That(permissionOffer.Status, Is.EqualTo(ManagementObjectStatus.Success)); Assert.That(permissionOffer.Token, Is.Not.Null); }); }
public void PermissionResponse_WhenTokenIsInvalid_ShouldGetError() { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); var permissionResponse = await CreateResponse(user, "Some string"); Assert.That(permissionResponse.Status, Is.EqualTo(ManagementObjectStatus.Error)); Assert.That(permissionResponse.ErrorCode, Is.EqualTo(ErrorCode.InvalidParameters)); Assert.That(permissionResponse.StatusMessage, Is.Not.Null); }); }
public void TrustedCA_WhenFileDoesntExist_Throws() { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); var config = new SyncConfiguration(user, new Uri(SyncTestHelpers.GetRealmUrl())) { TrustedCAPath = "something.pem" }; Assert.That(() => Realm.GetInstance(config), Throws.TypeOf <FileNotFoundException>()); }); }
public void User_ApplyPermissions_WithEmail_GrantsAndRevokesPermissions() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bobEmail = $"{Guid.NewGuid()}@foo.bar"; var bobCredentials = Credentials.UsernamePassword(bobEmail, "a", createUser: true); var bob = await User.LoginAsync(bobCredentials, new Uri($"http://{Constants.ServerUrl}")); await TestApplyPermissions(alice, bob, PermissionCondition.Email(bobEmail)); }); }
public void Permission_ValidateWrite() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bob = await SyncTestHelpers.GetUser(); var realmUrl = await GrantPermissions(alice, bob); await ValidateWriteAndSync(realmUrl, alice, bob, 1, 2); }); }
public void PermissionOffer_WhenExpired_ShouldGetError() { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); var permissionOffer = await CreateOffer(user, expiresAt: DateTimeOffset.UtcNow.AddDays(-1)); Assert.That(permissionOffer.Status, Is.EqualTo(ManagementObjectStatus.Error)); Assert.That(permissionOffer.Token, Is.Null); Assert.That(permissionOffer.ErrorCode, Is.EqualTo(ErrorCode.ExpiredPermissionOffer)); Assert.That(permissionOffer.StatusMessage, Is.Not.Null); }); }
public void User_AcceptPermissionOffer_WhenTokenIsInvalid_ShouldGetError() { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); await AssertThrows <PermissionException>(() => user.AcceptPermissionOfferAsync("some string"), ex => { Assert.That(ex.ErrorCode, Is.EqualTo(ErrorCode.InvalidParameters)); }); }); }
public void User_OfferPermissions_WhenNoAccess_ShouldThrow() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var realmUrl = $"realm://{Constants.ServerUrl}/{alice.Identity}/testPermission"; EnsureRealmExists(alice, realmUrl); await AssertThrows <ArgumentException>(() => alice.OfferPermissionsAsync(realmUrl, AccessLevel.None)); }); }
public void GetInstanceAsync_OpensReadonlyRealm(bool singleTransaction) { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bob = await SyncTestHelpers.GetUser(); var realmUrl = SyncTestHelpers.GetRealmUrl(userId: alice.Identity); var aliceConfig = new SyncConfiguration(alice, new Uri(realmUrl)); var aliceRealm = Realm.GetInstance(aliceConfig); // TODO: replace with wrapper API await GrantPermissions(alice, bob, realmUrl); AddDummyData(aliceRealm, singleTransaction); var bobConfig = new SyncConfiguration(bob, new Uri(realmUrl)); var bobRealm = await Realm.GetInstanceAsync(bobConfig); var bobsObjects = bobRealm.All <IntPrimaryKeyWithValueObject>(); var alicesObjects = aliceRealm.All <IntPrimaryKeyWithValueObject>(); Assert.That(bobsObjects.Count(), Is.EqualTo(alicesObjects.Count())); var bobTcs = new TaskCompletionSource <object>(); bobsObjects.AsRealmCollection().CollectionChanged += (sender, e) => { bobTcs.TrySetResult(null); }; aliceRealm.Write(() => { aliceRealm.Add(new IntPrimaryKeyWithValueObject { Id = 9999, StringValue = "Some value" }); }); await bobTcs.Task.Timeout(1000); Assert.That(bobsObjects.Count(), Is.EqualTo(alicesObjects.Count())); var bobObject = bobRealm.Find <IntPrimaryKeyWithValueObject>(9999); Assert.That(bobObject, Is.Not.Null); Assert.That(bobObject.StringValue, Is.EqualTo("Some value")); }); }
public void Permission_ValidateManage() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bob = await SyncTestHelpers.GetUser(); var charlie = await SyncTestHelpers.GetUser(); var alicesUrl = await GrantPermissions(alice, bob, mayManage: true); await GrantPermissions(bob, charlie, realmUrl: alicesUrl); await ValidateWriteAndSync(alicesUrl, alice, charlie, 1, 2); await ValidateWriteAndSync(alicesUrl, bob, charlie, 3, 4); }); }
public void PermissionResponse_WhenOfferExpired_ShouldGetError() { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); var permissionOffer = await CreateOffer(user, expiresAt: DateTimeOffset.UtcNow.AddSeconds(2)); Assert.That(permissionOffer.Status, Is.EqualTo(ManagementObjectStatus.Success)); Assert.That(permissionOffer.Token, Is.Not.Null); await Task.Delay(2500); var permissionResponse = await CreateResponse(user, permissionOffer.Token); Assert.That(permissionResponse.Status, Is.EqualTo(ManagementObjectStatus.Error)); Assert.That(permissionResponse.ErrorCode, Is.EqualTo(ErrorCode.ExpiredPermissionOffer)); Assert.That(permissionResponse.StatusMessage, Is.Not.Null); }); }
public void PermissionChange_UpdatesPermissionRealm() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bob = await SyncTestHelpers.GetUser(); var permissionRealm = alice.GetPermissionRealm(); var tcs = new TaskCompletionSource <Permission>(); var aliceId = alice.Identity; // LINQ :/ var token = permissionRealm.All <Permission>() .Where(p => p.UserId != aliceId) .SubscribeForNotifications((sender, changes, error) => { if (sender.Count > 0) { try { tcs.TrySetResult(sender.Single()); } catch (Exception ex) { tcs.TrySetException(ex); } } }); var realmPath = $"/{alice.Identity}/testPermission"; var realmUrl = $"realm://{Constants.ServerUrl}{realmPath}"; EnsureRealmExists(alice, realmUrl); await CreateChange(alice, bob.Identity, realmUrl); var permission = await tcs.Task; Assert.That(permission.UserId, Is.EqualTo(bob.Identity)); Assert.That(permission.Path, Is.EqualTo(realmPath)); Assert.That(permission.MayRead, Is.True); Assert.That(permission.MayWrite, Is.False); Assert.That(permission.MayManage, Is.False); token.Dispose(); permissionRealm.Dispose(); }); }
public void User_OfferPermissions_GrantsPermissions() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bob = await SyncTestHelpers.GetUser(); var realmPath = $"/{alice.Identity}/testPermission"; var realmUrl = $"realm://{Constants.ServerUrl}{realmPath}"; EnsureRealmExists(alice, realmUrl); var token = await alice.OfferPermissionsAsync(realmUrl, AccessLevel.Write); var alicesUrl = await bob.AcceptPermissionOfferAsync(token); Assert.That(alicesUrl, Is.EqualTo(realmUrl)); await AssertPermissions(alice, bob, realmPath, AccessLevel.Write); }); }
private static void TestSSLCore(Action <SyncConfiguration> setupSecureConfig, bool openAsync) { AsyncContext.Run(async() => { var user = await SyncTestHelpers.GetUser(); var realmUrl = SyncTestHelpers.GetRealmUrl(); var config = new SyncConfiguration(user, new Uri(realmUrl)); var secureRealmUrl = realmUrl.Replace("realm://", "realms://").Replace("9080", "9443"); var secureConfig = new SyncConfiguration(user, new Uri(secureRealmUrl), config.DatabasePath + "2"); setupSecureConfig(secureConfig); try { using (var realm = Realm.GetInstance(config)) { realm.Write(() => { realm.Add(new IntPrimaryKeyWithValueObject { Id = 1, StringValue = "some value" }); }); await realm.GetSession().WaitForUploadAsync(); } using (var newRealm = await SyncTestHelpers.GetInstanceAsync(secureConfig, openAsync)) { var items = newRealm.All <IntPrimaryKeyWithValueObject>(); Assert.That(items.Count(), Is.EqualTo(1)); Assert.That(items.Single().StringValue, Is.EqualTo("some value")); } } finally { Realm.DeleteRealm(config); Realm.DeleteRealm(secureConfig); } }); }
public void User_AcceptPermissionOffer_WhenOfferExpired_ShouldGetError() { AsyncContext.Run(async() => { var alice = await SyncTestHelpers.GetUser(); var bob = await SyncTestHelpers.GetUser(); var realmUrl = $"realm://{Constants.ServerUrl}/{alice.Identity}/testPermission"; 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)); }); }); }