public void User_InvalidateOffer_InvalidatesTheOffer() { SyncTestHelpers.RunRosTestAsync(async() => { var alice = await SyncTestHelpers.GetUserAsync(); var bob = await SyncTestHelpers.GetUserAsync(); var charlie = await SyncTestHelpers.GetUserAsync(); var realmUrl = SyncTestHelpers.RealmUri($"{alice.Identity}/testPermission").AbsoluteUri; await EnsureRealmExists(alice, realmUrl); var token = await alice.OfferPermissionsAsync(realmUrl, AccessLevel.Write); var bobUrl = await bob.AcceptPermissionOfferAsync(token); Assert.That(bobUrl, Is.EqualTo($"/{alice.Identity}/testPermission")); await alice.InvalidateOfferAsync(token); await TestHelpers.AssertThrows <HttpException>(() => charlie.AcceptPermissionOfferAsync(token), ex => { Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.Forbidden)); Assert.That(ex.ErrorCode, Is.EqualTo(ErrorCode.ExpiredPermissionOffer)); Assert.That(ex.Message, Is.EqualTo("The permission offer is expired.")); }); }); }
private async Task <Realm> GetQueryBasedRealm(bool openAsync, [CallerMemberName] string realmPath = null) { var user = await SyncTestHelpers.GetUserAsync(); var config = new QueryBasedSyncConfiguration(SyncTestHelpers.RealmUri($"~/{realmPath}_{openAsync}"), user, Guid.NewGuid().ToString()) { ObjectClasses = new[] { typeof(ObjectA), typeof(ObjectB), typeof(ObjectC) } }; using (var original = GetRealm(config)) { original.Write(() => { for (var i = 0; i < 10; i++) { var a = original.Add(new ObjectA { StringValue = "A #" + i, IntValue = i, B = new ObjectB { StringValue = "B #" + i, BoolValue = i % 2 == 0, } }); original.Add(new ObjectC { IntValue = i, B = a.B }); } }); await SyncTestHelpers.WaitForUploadAsync(original); } try { Realm.DeleteRealm(config); } catch { } config = new QueryBasedSyncConfiguration(config.ServerUri, config.User, config.DatabasePath + "_partial") { ObjectClasses = config.ObjectClasses }; // We test both `GetInstance` and `GetInstanceAsync` to guard against regressions: // https://github.com/realm/realm-dotnet/issues/1814 var result = await GetRealmAsync(config, openAsync).Timeout(5000); Assert.That(result.All <ObjectB>().Count(), Is.EqualTo(0)); Assert.That(result.All <ObjectA>().Count(), Is.EqualTo(0)); return(result); }
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)); }); }
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 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))); }); }
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); }
private async Task <FullSyncConfiguration> GetClientResyncConfig(ClientResyncMode?_mode = null) { if (!_mode.HasValue) { _mode = _clientResyncMode; } var user = await User.LoginAsync(Credentials.UsernamePassword("foo", "bar"), SyncTestHelpers.AuthServerUri); return(new FullSyncConfiguration(SyncTestHelpers.RealmUri($"~/{_mode.Value}"), user, $"{_mode}.realm") { ClientResyncMode = _mode.Value, ObjectClasses = new[] { typeof(IntPrimaryKeyWithValueObject) }, }); }
public void WriteToReadOnlyRealm_ThrowsPermissionDenied() { SyncTestHelpers.RunRosTestAsync(async() => { var alice = await SyncTestHelpers.GetUserAsync(); var bob = await SyncTestHelpers.GetUserAsync(); var realmPath = $"/{alice.Identity}/willBeReadonly"; var realmUrl = SyncTestHelpers.RealmUri(realmPath).AbsoluteUri; await EnsureRealmExists(alice, realmUrl); // Give Bob just read permissions await alice.ApplyPermissionsAsync(PermissionCondition.UserId(bob.Identity), realmUrl, AccessLevel.Read); var config = new FullSyncConfiguration(new Uri(realmUrl), bob, Guid.NewGuid().ToString()); var sessionErrorTask = TestHelpers.EventToTask <ErrorEventArgs>(h => Session.Error += h, h => Session.Error -= h); using (var realm = GetRealm(config)) { realm.Write(() => realm.Add(new Person())); try { // Sometimes PermissionDenied will be thrown too fast moving the session to an error state await SyncTestHelpers.WaitForUploadAsync(realm).Timeout(1000); } catch { } } var sessionError = await sessionErrorTask.Timeout(1000); Assert.That(sessionError.Exception, Is.TypeOf <PermissionDeniedException>()); var pde = (PermissionDeniedException)sessionError.Exception; Assert.That(pde.ErrorCode, Is.EqualTo(ErrorCode.PermissionDenied)); Assert.That(File.Exists(config.DatabasePath), Is.True); var result = pde.DeleteRealmUserInfo(); Assert.That(result, Is.True); Assert.That(File.Exists(config.DatabasePath), Is.False); }); }
public void GetInstanceAsync_OpensReadonlyRealm(bool singleTransaction) { SyncTestHelpers.RunRosTestAsync(async() => { var alice = await SyncTestHelpers.GetUserAsync(); var bob = await SyncTestHelpers.GetUserAsync(); var realmUri = SyncTestHelpers.RealmUri($"{alice.Identity}/GetInstanceAsync_OpensReadonlyRealm"); var aliceConfig = new FullSyncConfiguration(realmUri, alice, Guid.NewGuid().ToString()); var aliceRealm = GetRealm(aliceConfig); await alice.ApplyPermissionsAsync(PermissionCondition.UserId(bob.Identity), realmUri.AbsoluteUri, AccessLevel.Read).Timeout(1000); AddDummyData(aliceRealm, singleTransaction); var bobConfig = new FullSyncConfiguration(realmUri, bob, Guid.NewGuid().ToString()); var bobRealm = await GetRealmAsync(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")); }); }
private async Task TestApplyPermissions(User alice, User bob, PermissionCondition condition) { var realmPath = $"/{alice.Identity}/testPermission"; var realmUrl = SyncTestHelpers.RealmUri(realmPath).AbsoluteUri; await EnsureRealmExists(alice, realmUrl); // Grant write permissions await alice.ApplyPermissionsAsync(condition, realmUrl, AccessLevel.Write); await ValidateWriteAndSync(realmUrl, alice, bob, 1, 2); await AssertPermissions(alice, bob, realmPath, AccessLevel.Write); // Revoke permissions await alice.ApplyPermissionsAsync(condition, realmUrl, AccessLevel.None); await AssertPermissions(alice, bob, realmPath, AccessLevel.None); }
public void User_OfferPermissions_GrantsPermissions() { SyncTestHelpers.RunRosTestAsync(async() => { var alice = await SyncTestHelpers.GetUserAsync(); var bob = await SyncTestHelpers.GetUserAsync(); var realmPath = $"/{alice.Identity}/testPermission"; var realmUrl = SyncTestHelpers.RealmUri(realmPath).AbsoluteUri; await 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() { 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 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)); }); }
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)); }); }); }
private async Task <Uri> CreateRealm(Action <Realm> assignRoles = null, bool addObjects = true) { var uri = SyncTestHelpers.RealmUri(Guid.NewGuid().ToString()); var admin = await SyncTestHelpers.GetAdminUserAsync(); var config = new QueryBasedSyncConfiguration(uri, admin) { ObjectClasses = new[] { typeof(ObjectWithPermissions) } }; using (var realm = GetRealm(config)) { var objects = realm.All <ObjectWithPermissions>(); var subscription = await SubscribeToObjectsAsync(realm); if (addObjects) { Assert.That(subscription.Results.Count(), Is.EqualTo(0)); AddObjectsToRealm(realm, new[] { 1, 2, 3 }); await SyncTestHelpers.WaitForSyncAsync(realm); Assert.That(subscription.Results.Count(), Is.EqualTo(3)); } if (assignRoles != null) { realm.Write(() => { assignRoles(realm); }); } await SyncTestHelpers.WaitForSyncAsync(realm); } return(uri); }
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)); } }); }
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")); } }); }