Search in sources :

Example 1 with Database

use of org.exist.Database in project exist by eXist-db.

the class AbstractGroupTest method removeManager_calls_assertCanModifyGroup.

@Test
public void removeManager_calls_assertCanModifyGroup() throws PermissionDeniedException, NoSuchMethodException {
    DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
    AbstractRealm mockRealm = EasyMock.createMock(AbstractRealm.class);
    Subject mockSubject = EasyMock.createMock(Subject.class);
    Database mockDatabase = EasyMock.createMock(Database.class);
    Group partialMockGroup = EasyMock.createMockBuilder(AbstractGroup.class).withConstructor(DBBroker.class, AbstractRealm.class, int.class, String.class, List.class).withArgs(mockBroker, mockRealm, 1, "testGroup", null).addMockedMethod("assertCanModifyGroup", Account.class).addMockedMethod(AbstractGroup.class.getDeclaredMethod("_addManager", Account.class)).createNiceMock();
    // expectations
    expect(mockRealm.getDatabase()).andReturn(mockDatabase);
    expect(mockDatabase.getActiveBroker()).andReturn(mockBroker);
    expect(mockBroker.getCurrentSubject()).andReturn(mockSubject);
    partialMockGroup.assertCanModifyGroup(mockSubject);
    replay(mockRealm, mockDatabase, mockBroker, partialMockGroup);
    // test
    partialMockGroup.removeManager(null);
    verify(mockRealm, mockDatabase, mockBroker, partialMockGroup);
}
Also used : DBBroker(org.exist.storage.DBBroker) Database(org.exist.Database) Test(org.junit.Test)

Example 2 with Database

use of org.exist.Database in project exist by eXist-db.

the class AbstractGroupTest method addManagerWithString_calls_assertCanModifyGroup.

@Test
public void addManagerWithString_calls_assertCanModifyGroup() throws PermissionDeniedException, NoSuchMethodException {
    DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
    AbstractRealm mockRealm = EasyMock.createNiceMock(AbstractRealm.class);
    Subject mockSubject = EasyMock.createMock(Subject.class);
    Database mockDatabase = EasyMock.createMock(Database.class);
    AbstractGroup partialMockGroup = EasyMock.createMockBuilder(AbstractGroup.class).withConstructor(DBBroker.class, AbstractRealm.class, int.class, String.class, List.class).withArgs(mockBroker, mockRealm, 1, "testGroup", null).addMockedMethod("assertCanModifyGroup", Account.class).addMockedMethod(AbstractGroup.class.getDeclaredMethod("_addManager", Account.class)).createNiceMock();
    // expectations
    expect(mockRealm.getDatabase()).andReturn(mockDatabase);
    expect(mockDatabase.getActiveBroker()).andReturn(mockBroker);
    expect(mockBroker.getCurrentSubject()).andReturn(mockSubject);
    partialMockGroup.assertCanModifyGroup(mockSubject);
    replay(mockRealm, mockDatabase, mockBroker, partialMockGroup);
    // test
    partialMockGroup.addManager((String) null);
    verify(mockRealm, mockDatabase, mockBroker, partialMockGroup);
}
Also used : DBBroker(org.exist.storage.DBBroker) Database(org.exist.Database) Test(org.junit.Test)

Example 3 with Database

use of org.exist.Database in project exist by eXist-db.

the class AbstractRealmTest method updateGroup_calls_assertCanModifyGroup.

/*
    @Test
    public void updateAccount_calls_assertCanModifyAccount() throws PermissionDeniedException, EXistException {
        SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
        Configuration mockConfiguration = EasyMock.createMock(Configuration.class);
        Database mockDatabase = EasyMock.createMock(Database.class);
        Subject mockSubject = EasyMock.createMock(Subject.class);

        Account mockAccount = EasyMock.createMockBuilder(AbstractAccount.class)
                .addMockedMethod("getName", new Class[0])
                .addMockedMethod("getGroups", new Class[0])
                .addMockedMethod("assertCanModifyAccount", new Class[]{Account.class})
                .addMockedMethod("getRealm", new Class[0])
                .createNiceMock();
        final String accountName = "someAccount";

        AbstractRealm mockRealm = EasyMock
                .createMockBuilder(AbstractRealm.class)
                .withConstructor(SecurityManager.class, Configuration.class)
                .withArgs(mockSecurityManager, mockConfiguration)
                .addMockedMethod("getDatabase", new Class[0])
                .addMockedMethod("getAccount", new Class[]{Subject.class, String.class})
                .createNiceMock();

        Account mockUpdatingAccount = EasyMock.createMock(Account.class);

        //expectations
        expect(mockRealm.getDatabase()).andReturn(mockDatabase);
        expect(mockDatabase.getCurrentSubject()).andReturn(mockSubject);
        mockAccount.assertCanModifyAccount(mockSubject);
        expect(mockAccount.getName()).andReturn(accountName);
        expect(mockRealm.getAccount(null, accountName)).andReturn(mockUpdatingAccount);
        expect(mockAccount.getGroups()).andReturn(new String[0]);
        expect(mockUpdatingAccount.getGroups()).andReturn(new String[0]);

        replay(mockRealm, mockDatabase, mockSubject, mockUpdatingAccount, mockAccount);

        mockRealm.updateAccount(null, mockAccount);

        verify(mockRealm, mockDatabase, mockSubject, mockUpdatingAccount, mockAccount);
    } */
@Test
public void updateGroup_calls_assertCanModifyGroup() throws PermissionDeniedException, EXistException {
    SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
    Configuration mockConfiguration = EasyMock.createMock(Configuration.class);
    Database mockDatabase = EasyMock.createMock(Database.class);
    DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
    Subject mockSubject = EasyMock.createMock(Subject.class);
    Group mockGroup = EasyMock.createMockBuilder(AbstractGroup.class).addMockedMethod("getName", new Class[0]).addMockedMethod("getManagers", new Class[0]).addMockedMethod("assertCanModifyGroup", new Class[] { Account.class }).addMockedMethod("getMetadataKeys", new Class[0]).createNiceMock();
    final String groupName = "someGroup";
    AbstractRealm mockRealm = EasyMock.createMockBuilder(AbstractRealm.class).withConstructor(SecurityManager.class, Configuration.class).withArgs(mockSecurityManager, mockConfiguration).addMockedMethod("getDatabase", new Class[0]).addMockedMethod("getGroup", new Class[] { String.class }).createNiceMock();
    Group mockUpdatingGroup = EasyMock.createNiceMock(Group.class);
    // expectations
    expect(mockRealm.getDatabase()).andReturn(mockDatabase);
    expect(mockDatabase.getActiveBroker()).andReturn(mockBroker);
    expect(mockBroker.getCurrentSubject()).andReturn(mockSubject);
    mockGroup.assertCanModifyGroup(mockSubject);
    expect(mockGroup.getName()).andReturn(groupName);
    expect(mockRealm.getGroup(groupName)).andReturn(mockUpdatingGroup);
    expect(mockGroup.getManagers()).andReturn(Collections.emptyList());
    expect(mockUpdatingGroup.getManagers()).andReturn(Collections.emptyList());
    expect(mockGroup.getMetadataKeys()).andReturn(Collections.emptySet());
    mockGroup.save();
    replay(mockRealm, mockDatabase, mockBroker, mockGroup, mockSubject, mockUpdatingGroup);
    mockRealm.updateGroup(mockGroup);
    verify(mockRealm, mockDatabase, mockBroker, mockGroup, mockSubject, mockUpdatingGroup);
}
Also used : DBBroker(org.exist.storage.DBBroker) Configuration(org.exist.config.Configuration) Database(org.exist.Database) Test(org.junit.Test)

Example 4 with Database

use of org.exist.Database in project exist by eXist-db.

the class AccountTest method testGroupFallback.

@Ignore
@Test
public void testGroupFallback() throws NoSuchMethodException, PermissionDeniedException {
    // final String mockRealmId = "mock";
    final String testAccountName = "testUser";
    final String testGroupName = "testGroup";
    Database mockDatabase = EasyMock.createMock(Database.class);
    SecurityManagerImpl mockSecurityManager = EasyMock.createMockBuilder(SecurityManagerImpl.class).withConstructor(Database.class).withArgs(mockDatabase).createMock();
    Configuration mockConfiguration = EasyMock.createMock(Configuration.class);
    AbstractRealm mockRealm = EasyMock.createMockBuilder(AbstractRealm.class).withConstructor(SecurityManager.class, Configuration.class).withArgs(mockSecurityManager, mockConfiguration).createMock();
    AccountImpl mockAccountImpl = EasyMock.createMockBuilder(AccountImpl.class).withConstructor(AbstractRealm.class, String.class).withArgs(mockRealm, testAccountName).addMockedMethods(AccountImpl.class.getMethod("getRealm"), AccountImpl.class.getMethod("addGroup", Group.class)).createMock();
    expect(mockAccountImpl.getRealm()).andReturn(mockRealm);
    expect(mockRealm.getGroup(testGroupName)).andReturn(null);
    // expect(mockAccountImpl.getRealm()).andReturn(mockRealm);
    // expect(mockRealm.getSecurityManager()).andReturn(mockSecurityManager);
    replay();
    mockAccountImpl.addGroup(testGroupName);
    verify();
}
Also used : Configuration(org.exist.config.Configuration) Database(org.exist.Database) AccountImpl(org.exist.security.internal.AccountImpl) SecurityManagerImpl(org.exist.security.internal.SecurityManagerImpl) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Database

use of org.exist.Database in project exist by eXist-db.

the class SimpleACLPermissionTest method modify.

@Test
public void modify() throws PermissionDeniedException {
    final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
    final Database mockDatabase = EasyMock.createMock(Database.class);
    final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
    final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
    expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase).times(3);
    expect(mockDatabase.getActiveBroker()).andReturn(mockBroker).times(3);
    expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject).times(3);
    expect(mockCurrentSubject.hasDbaRole()).andReturn(true).times(3);
    replay(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
    SimpleACLPermission permission = new SimpleACLPermission(mockSecurityManager);
    assertEquals(0, permission.getACECount());
    final int userId = 1;
    final int mode = Permission.READ;
    final ACE_ACCESS_TYPE access_type = ACE_ACCESS_TYPE.ALLOWED;
    permission.addUserACE(access_type, userId, mode);
    assertEquals(1, permission.getACECount());
    assertEquals(userId, permission.getACEId(0));
    assertEquals(access_type, permission.getACEAccessType(0));
    assertEquals(ACE_TARGET.USER, permission.getACETarget(0));
    assertEquals(mode, permission.getACEMode(0));
    permission.modifyACE(0, access_type, Permission.WRITE);
    assertEquals(1, permission.getACECount());
    assertEquals(userId, permission.getACEId(0));
    assertEquals(access_type, permission.getACEAccessType(0));
    assertEquals(ACE_TARGET.USER, permission.getACETarget(0));
    assertEquals(Permission.WRITE, permission.getACEMode(0));
    permission.modifyACE(0, ACE_ACCESS_TYPE.DENIED, Permission.READ | Permission.WRITE);
    assertEquals(1, permission.getACECount());
    assertEquals(userId, permission.getACEId(0));
    assertEquals(ACE_TARGET.USER, permission.getACETarget(0));
    assertEquals(ACE_ACCESS_TYPE.DENIED, permission.getACEAccessType(0));
    assertEquals(Permission.READ | Permission.WRITE, permission.getACEMode(0));
    verify(mockSecurityManager, mockDatabase, mockBroker, mockCurrentSubject);
}
Also used : DBBroker(org.exist.storage.DBBroker) ACE_ACCESS_TYPE(org.exist.security.ACLPermission.ACE_ACCESS_TYPE) Database(org.exist.Database) Test(org.junit.Test)

Aggregations

Database (org.exist.Database)42 DBBroker (org.exist.storage.DBBroker)34 Test (org.junit.Test)29 EXistException (org.exist.EXistException)6 SecurityManager (org.exist.security.SecurityManager)5 BrokerPool (org.exist.storage.BrokerPool)4 XmldbURI (org.exist.xmldb.XmldbURI)4 XQuery (org.exist.xquery.XQuery)4 IOException (java.io.IOException)3 Random (java.util.Random)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 CompiledXQuery (org.exist.xquery.CompiledXQuery)3 Ignore (org.junit.Ignore)3 StringWriter (java.io.StringWriter)2 Collection (org.exist.collections.Collection)2 Configuration (org.exist.config.Configuration)2 StreamListener (org.exist.indexing.StreamListener)2 SecurityManagerImpl (org.exist.security.internal.SecurityManagerImpl)2 Serializer (org.exist.storage.serializers.Serializer)2 Configuration (org.exist.util.Configuration)2