public void CanReadDocumentWhichWasNotSecured() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); // by not specifying that, we say that anyone can read this //s.SetAuthorizationFor(company, new DocumentAuthorization()); s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Bid"); Assert.NotNull(s.Load<Company>(company.Id)); } }
public void Will_limit_replication_history_size_on_items_marked_with_not_for_replication() { var store1 = CreateStore(); using (var session = store1.OpenSession()) { var entity = new Company {Name = "Hibernating Rhinos"}; session.Store(entity); session.Advanced.GetMetadataFor(entity)["Raven-Not-For-Replication"] = "true"; session.SaveChanges(); } for (int i = 0; i < 100; i++) { using (var session = store1.OpenSession()) { var company = session.Load<Company>(1); company.Name = i%2 == 0 ? "a" : "b"; session.SaveChanges(); } } using (var session = store1.OpenSession()) { var company = session.Load<Company>(1); var ravenJArray = session.Advanced.GetMetadataFor(company).Value<RavenJArray>(Constants.RavenReplicationHistory); Assert.Equal(50, ravenJArray.Length); } }
public void WillAbortDeleteIfUserDoesNotHavePermissions() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization());// deny everyone s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Rename"); Assert.Throws<InvalidOperationException>(() => s.Advanced.DatabaseCommands.Delete(company.Id, null)); } }
public void CannotReadDocumentWithoutPermissionToIt() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization());// deny everyone s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Bid"); var readVetoException = Assert.Throws<ReadVetoException>(() => s.Load<Company>(company.Id)); Assert.Equal(@"Document could not be read because of a read veto. The read was vetoed by: Raven.Bundles.Authorization.Triggers.AuthorizationReadTrigger Veto reason: Could not find any permissions for operation: Company/Bid on companies/1 for user Authorization/Users/Ayende. No one may perform operation Company/Bid on companies/1 ", readVetoException.Message); } }
public void BugWhenSavingDocumentWithPreviousAuthorization_WithQuery() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { User = UserId, Allow = true, Operation = "Company/Bid" } } }); s.SaveChanges(); } for (int i = 0; i < 15; i++) { using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Bid"); var c = s.Query<Company>().Customize(x => x.WaitForNonStaleResults()).First(); c.Name = "other " + i; s.SaveChanges(); } } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Bid"); var load = s.Load<Company>(company.Id); Assert.NotNull(load); Assert.Equal("other 14", load.Name); } }
public void BugWhenUpdatingUserRolesQuery() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { Role = "Admins", Allow = true, Operation = "Company/Bid" } } }); s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Bid"); Assert.Empty(s.Query<Company>().ToArray()); } using (var s = store.OpenSession()) { var user = s.Load<AuthorizationUser>(UserId); user.Roles = new List<string> { "Admins" }; s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Bid"); Assert.NotEmpty(s.Query<Company>().ToArray()); } }
public void CanAskWhateverUserHavePermissions() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { Role = "Admins", Allow = true, Operation = "Company/Bid" } } }); s.SaveChanges(); } using (var s = store.OpenSession()) { var isOperationAllowedOnDocument = s.Advanced.IsOperationAllowedOnDocument(UserId, "Company/Bid", "companies/1"); Assert.False(isOperationAllowedOnDocument.IsAllowed); Assert.Equal("Could not find any permissions for operation: Company/Bid on companies/1 for user Authorization/Users/Ayende.\r\nOnly the following may perform operation Company/Bid on companies/1:\r\n\tOperation: Company/Bid, User: , Role: Admins, Allow: True, Priority: 0\r\n", isOperationAllowedOnDocument.Reasons[0]); } using (var s = store.OpenSession()) { var user = s.Load<AuthorizationUser>(UserId); user.Roles = new List<string> { "Admins" }; s.SaveChanges(); } using (var s = store.OpenSession()) { var isOperationAllowedOnDocument = s.Advanced.IsOperationAllowedOnDocument(UserId, "Company/Bid", "companies/1"); Assert.True(isOperationAllowedOnDocument.IsAllowed); Assert.Equal(new[] { "Operation: Company/Bid, User: , Role: Admins, Allow: True, Priority: 0" }, isOperationAllowedOnDocument.Reasons.ToArray()); } }
public void Will_automatically_set_metadata() { var company = new Company {Name = "Company Name"}; using (var session = documentStore.OpenSession()) { session.Store(company); session.SaveChanges(); } using (var session = documentStore.OpenSession()) { var company2 = session.Load<Company>(company.Id); var metadata = session.Advanced.GetMetadataFor(company2); Assert.Equal("Current", metadata.Value<string>("Raven-Document-Revision-Status")); Assert.Equal(1, metadata.Value<int>("Raven-Document-Revision")); } }
public void Will_automatically_craete_duplicate_on_first_insert() { var company = new Company {Name = "Company Name"}; using (var session = documentStore.OpenSession()) { session.Store(company); session.SaveChanges(); } using (var session = documentStore.OpenSession()) { var company2 = session.Load<Company>(company.Id + "/revisions/1"); var metadata = session.Advanced.GetMetadataFor(company2); Assert.Equal(company.Name, company2.Name); Assert.Equal("Historical", metadata.Value<string>("Raven-Document-Revision-Status")); } }
public void StoreAndLoad() { const string CompanyName = "Company Name"; var company = new Company { Name = CompanyName }; using (var session = documentStore.OpenSession()) { session.Store(company); session.SaveChanges(); } using (var session = documentStore.OpenSession()) { Assert.Equal(CompanyName, session.Load<Company>(1).Name); } AssertPlainTextIsNotSavedInDatabase_ExceptIndexes(CompanyName); }
public void Can_add_entity_with_expiry_then_read_it_before_it_expires() { var company = new Company {Name = "Company Name"}; var expiry = DateTime.UtcNow.AddMinutes(5); using (var session = documentStore.OpenSession()) { session.Store(company); session.Advanced.GetMetadataFor(company)["Raven-Expiration-Date"] = new RavenJValue(expiry); session.SaveChanges(); } using (var session = documentStore.OpenSession()) { var company2 = session.Load<Company>(company.Id); Assert.NotNull(company2); var metadata = session.Advanced.GetMetadataFor(company2); Assert.Equal(expiry.ToString(), metadata.Value<DateTime>("Raven-Expiration-Date").ToString()); } }
public void GivingPermissionToRoleOnTagAssociatedWithRoleWillAllow() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = userId, Name = "Ayende Rahien", Roles = { "Authorization/Roles/Managers" } }); s.Store(new AuthorizationRole { Id = "Authorization/Roles/Managers", Permissions = { new OperationPermission { Allow = true, Operation = operation, Tag = "Fortune 500" } } }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Tags = { "Fortune 500" } }); s.SaveChanges(); } var jsonDocument = server.Database.Get(company.Id, null); var isAllowed = authorizationDecisions.IsAllowed(userId, operation, company.Id, jsonDocument.Metadata, null); Assert.True(isAllowed); }
public void After_expiry_passed_document_will_be_physically_deleted() { var company = new Company { Id = "companies/1", Name = "Company Name" }; var expiry = DateTime.UtcNow.AddMinutes(5); using (var session = documentStore.OpenSession()) { session.Store(company); session.Advanced.GetMetadataFor(company)["Raven-Expiration-Date"] = new JValue(expiry); session.SaveChanges(); session.Advanced.LuceneQuery<Company>("Raven/DocumentsByExpirationDate") .WaitForNonStaleResults() .ToList(); } ExpirationReadTrigger.GetCurrentUtcDate = () => DateTime.UtcNow.AddMinutes(10); using (var session = documentStore.OpenSession()) { session.Store(new Company { Id = "companies/2", Name = "Company Name" }); session.SaveChanges(); // this forces the background task to run } JsonDocument documentByKey = null; for (int i = 0; i < 15; i++) { ravenDbServer.Database.TransactionalStorage.Batch(accessor => { documentByKey = accessor.Documents.DocumentByKey("companies/1", null); }); if (documentByKey == null) return; Thread.Sleep(100); } Assert.False(true, "Document was not deleted"); }
public void BugWhenSavingDocumentOnDatabase() { string database = "test_auth"; store.DatabaseCommands.EnsureDatabaseExists(database); var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession(database)) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { User = UserId, Allow = true, Operation = "Company/Bid" } } }); s.SaveChanges(); } using (var s = store.OpenSession(database)) { s.SecureFor(UserId, "Company/Bid"); Assert.NotNull(s.Load<Company>(company.Id)); } }
public void Can_add_entity_with_expiry_then_read_it_before_it_expires() { var company = new Company {Name = "Company Name"}; var expiry = DateTime.UtcNow.AddMinutes(5); using (var session = documentStore.OpenSession()) { session.Store(company); session.Advanced.GetMetadataFor(company)["Raven-Expiration-Date"] = new JValue(expiry); session.SaveChanges(); } using (var session = documentStore.OpenSession()) { var company2 = session.Load<Company>(company.Id); Assert.NotNull(company2); var metadata = session.Advanced.GetMetadataFor(company2); var dateAsJsStr = @"\/Date("+(long)( expiry - new DateTime(1970,1,1) ).TotalMilliseconds+@")\/"; Assert.Equal(dateAsJsStr, metadata.Value<string>("Raven-Expiration-Date")); } }
public void WillDeleteIfUserHavePermissions() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { Allow = true, User = UserId, Operation = "Company/Rename" } } });// deny everyone s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Rename"); company.Name = "Stampading Rhinos"; s.Store(company); Assert.DoesNotThrow(() => s.Advanced.DatabaseCommands.Delete(company.Id, null)); } }
public void CanReadDocumentWhichUserHavePermissionsTo() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { User = UserId, Allow = true, Operation = "Company/Bid" } } }); s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Bid"); Assert.NotNull(s.Load<Company>(company.Id)); } }
public void WhenGivingPermissionOnDocumentRoleAndAssociatingUserWithRoleWillAllow() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = userId, Name = "Ayende Rahien", Roles = { "/Raven/Authorization/Roles/Managers" } }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { Allow = true, Operation = operation, Role = "/Raven/Authorization/Roles/Managers" } } }); s.SaveChanges(); } var jsonDocument = server.Database.Get(company.Id, null); var isAllowed = authorizationDecisions.IsAllowed(userId, operation, company.Id, jsonDocument.Metadata, null); Assert.True(isAllowed); }
public void CanGiveUserExplicitPermissionOnDoc() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = userId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { Allow = true, Operation = operation, User = userId } } }); s.SaveChanges(); } var jsonDocument = server.Database.Get(company.Id, null); var isAllowed = authorizationDecisions.IsAllowed(userId, operation, company.Id, jsonDocument.Metadata, null); Assert.True(isAllowed); }
public void GivingPermissionToRoleOnTagAssociatedWithRoleWillAllow_OnClient() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = userId, Name = "Ayende Rahien", Roles = { "Authorization/Roles/Managers" } }); s.Store(new AuthorizationRole { Id = "Authorization/Roles/Managers", Permissions = { new OperationPermission { Allow = true, Operation = operation, Tag = "Fortune 500" } } }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Tags = { "Fortune 500" } }); s.SaveChanges(); } using (var s = store.OpenSession()) { var authorizationUser = s.Load<AuthorizationUser>(userId); Assert.True(s.IsAllowed(authorizationUser, operation)); } }
public void DocumentWithoutPermissionWillBeFilteredOutSiltently() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId, Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization());// deny everyone s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId, "Company/Bid"); Assert.Equal(0, s.Advanced.LuceneQuery<Company>() .WaitForNonStaleResults() .ToList().Count); } }
public void Will_delete_old_revisions() { var company = new Company {Name = "Company #1"}; using (var session = documentStore.OpenSession()) { session.Store(company); session.SaveChanges(); for (int i = 0; i < 10; i++) { company.Name = "Company #" + i + 2; session.SaveChanges(); } } using (var session = documentStore.OpenSession()) { for (int i = 1; i < 6; i++) { Assert.Null(session.Load<Company>(company.Id + "/revisions/" + i)); } for (int i = 6; i < 11; i++) { Assert.NotNull(session.Load<Company>(company.Id + "/revisions/" + i)); } } }
public void Can_get_all_revisions() { var company = new Company { Name = "Company Name" }; using (var session = documentStore.OpenSession()) { session.Store(company); session.SaveChanges(); Assert.Equal(1, session.Advanced.GetMetadataFor(company).Value<int>("Raven-Document-Revision")); } using (var session = documentStore.OpenSession()) { var company3 = session.Load<Company>(company.Id); company3.Name = "Hibernating Rhinos"; session.SaveChanges(); Assert.Equal(2, session.Advanced.GetMetadataFor(company3).Value<int>("Raven-Document-Revision")); } using (var session = documentStore.OpenSession()) { var companiesRevisions = session.Advanced.GetRevisionsFor<Company>(company.Id, 0, 25); Assert.Equal("Company Name", companiesRevisions[0].Name); Assert.Equal("Hibernating Rhinos", companiesRevisions[1].Name); } }
public void Will_automatically_craete_duplicate_on_next_insert() { var company = new Company {Name = "Company Name"}; using (var session = documentStore.OpenSession()) { session.Store(company); session.SaveChanges(); Assert.Equal(1, session.Advanced.GetMetadataFor(company).Value<int>("Raven-Document-Revision")); } using (var session = documentStore.OpenSession()) { var company3 = session.Load<Company>(company.Id); company3.Name = "Hibernating Rhinos"; session.SaveChanges(); Assert.Equal(2, session.Advanced.GetMetadataFor(company3).Value<int>("Raven-Document-Revision")); } using (var session = documentStore.OpenSession()) { var company2 = session.Load<Company>(company.Id + "/revisions/1"); var metadata = session.Advanced.GetMetadataFor(company2); Assert.Equal("Company Name", company2.Name); Assert.Equal("Historical", metadata.Value<string>("Raven-Document-Revision-Status")); Assert.Null(metadata.Value<string>("Raven-Document-Parent-Revision")); company2 = session.Load<Company>(company.Id + "/revisions/2"); metadata = session.Advanced.GetMetadataFor(company2); Assert.Equal("Hibernating Rhinos", company2.Name); Assert.Equal("Historical", metadata.Value<string>("Raven-Document-Revision-Status")); Assert.Equal("companies/1/revisions/1", metadata.Value<string>("Raven-Document-Parent-Revision")); } }
public void WhenThereIsNoAuthorizationWillAllow() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = userId, Name = "Ayende Rahien", }); s.Store(company); s.SaveChanges(); } var jsonDocument = server.Database.Get(company.Id, null); var isAllowed = authorizationDecisions.IsAllowed(userId, operation, company.Id, jsonDocument.Metadata, null); Assert.True(isAllowed); }
public void WhenGivingUserPermissionForParentTagAndTaggingDocumentWillAllow() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = userId, Name = "Ayende Rahien", Permissions = { new OperationPermission { Allow = true, Operation = operation, Tag = "Companies" } } }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Tags = { "Companies/Important" } }); s.SaveChanges(); } var jsonDocument = server.Database.Get(company.Id, null); var isAllowed = authorizationDecisions.IsAllowed(userId, operation, company.Id, jsonDocument.Metadata, null); Assert.True(isAllowed); }
public void WillWriteIfUserHavePermissions_CaseInsensitive() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = UserId.ToUpper(), Name = "Ayende Rahien", }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Permissions = { new DocumentPermission { Allow = true, User = UserId.ToUpper(), Operation = "Company/Rename" } } });// deny everyone s.SaveChanges(); } using (var s = store.OpenSession()) { s.SecureFor(UserId.ToLower(), "Company/Rename"); company.Name = "Stampading Rhinos"; s.Store(company); Assert.DoesNotThrow(s.SaveChanges); } }
public void Will_not_replicate_replicated_documents() { var store1 = CreateStore(); var store2 = CreateStore(); TellFirstInstanceToReplicateToSecondInstance(); TellSecondInstanceToReplicateToFirstInstance(); Company company = null; string etag; string id; using (var session = store1.OpenAsyncSession()) { company = new Company { Name = "Hibernating Rhinos" }; session.Store(company); session.SaveChangesAsync().Wait(); id = company.Id; session.Advanced.Clear(); company = session.LoadAsync<Company>(id).Result; etag = session.Advanced.GetMetadataFor(company).Value<string>("@etag"); } for (int i = 0; i < RetriesCount; i++) { using (var session = store2.OpenAsyncSession()) // waiting for document to show up. { company = session.LoadAsync<Company>(id).Result; if (company != null) break; Thread.Sleep(100); } } Assert.NotNull(company); Assert.Equal("Hibernating Rhinos", company.Name); // assert that the etag haven't changed (we haven't replicated) for (int i = 0; i < 15; i++) { using (var session = store1.OpenAsyncSession()) { company = session.LoadAsync<Company>(id).Result; Assert.Equal(etag, session.Advanced.GetMetadataFor(company).Value<string>("@etag")); } Thread.Sleep(100); } }
public void Can_add_entity_with_expiry_but_will_not_be_able_to_read_it_after_expiry() { var company = new Company { Name = "Company Name" }; var expiry = DateTime.UtcNow.AddMinutes(5); using (var session = documentStore.OpenSession()) { session.Store(company); session.Advanced.GetMetadataFor(company)["Raven-Expiration-Date"] = new RavenJValue(expiry); session.SaveChanges(); } ExpirationReadTrigger.GetCurrentUtcDate = () => DateTime.UtcNow.AddMinutes(10); using (var session = documentStore.OpenSession()) { var company2 = session.Load<Company>(company.Id); Assert.Null(company2); } }
public void GivingPermissionForAllowAndDenyOnSameLevelWithReturnDeny() { var company = new Company { Name = "Hibernating Rhinos" }; using (var s = store.OpenSession()) { s.Store(new AuthorizationUser { Id = userId, Name = "Ayende Rahien", Roles = { "Authorization/Roles/Managers" }, Permissions = { new OperationPermission { Allow = false, Operation = operation, Tag = "Important" } } }); s.Store(company); s.SetAuthorizationFor(company, new DocumentAuthorization { Tags = { "Important" }, Permissions = { new DocumentPermission { Allow = true, Operation = operation, Role = "Authorization/Roles/Managers" } } }); s.SaveChanges(); } var jsonDocument = server.Database.Get(company.Id, null); var isAllowed = authorizationDecisions.IsAllowed(userId, operation, company.Id, jsonDocument.Metadata, null); Assert.False(isAllowed); }