2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
2019-03-13 12:07:18 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
2019-03-13 12:07:18 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2019-03-13 12:07:18 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2019-03-13 12:07:18 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2017-02-26 13:19:35 +01:00
|
|
|
*/
|
2020-09-17 15:31:47 +02:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
package sonia.scm.security;
|
|
|
|
|
|
2019-01-23 15:00:48 +01:00
|
|
|
import com.google.common.collect.Lists;
|
2017-02-26 13:19:35 +01:00
|
|
|
import org.junit.Before;
|
2019-01-23 15:00:48 +01:00
|
|
|
import org.junit.Test;
|
2017-06-25 19:01:33 +02:00
|
|
|
import sonia.scm.HandlerEventType;
|
2017-02-26 13:19:35 +01:00
|
|
|
import sonia.scm.group.Group;
|
|
|
|
|
import sonia.scm.group.GroupEvent;
|
|
|
|
|
import sonia.scm.group.GroupModificationEvent;
|
2020-09-17 15:31:47 +02:00
|
|
|
import sonia.scm.repository.Namespace;
|
|
|
|
|
import sonia.scm.repository.NamespaceModificationEvent;
|
2017-02-26 13:19:35 +01:00
|
|
|
import sonia.scm.repository.Repository;
|
|
|
|
|
import sonia.scm.repository.RepositoryEvent;
|
2019-01-23 15:00:48 +01:00
|
|
|
import sonia.scm.repository.RepositoryModificationEvent;
|
|
|
|
|
import sonia.scm.repository.RepositoryPermission;
|
2017-02-26 13:19:35 +01:00
|
|
|
import sonia.scm.repository.RepositoryTestData;
|
|
|
|
|
import sonia.scm.user.User;
|
|
|
|
|
import sonia.scm.user.UserEvent;
|
|
|
|
|
import sonia.scm.user.UserModificationEvent;
|
|
|
|
|
import sonia.scm.user.UserTestData;
|
|
|
|
|
|
2019-01-23 15:00:48 +01:00
|
|
|
import static java.util.Arrays.asList;
|
|
|
|
|
import static java.util.Collections.singletonList;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
|
|
|
|
* Unit tests for {@link AuthorizationChangedEventProducer}.
|
2019-03-13 12:07:18 +01:00
|
|
|
*
|
2017-02-26 13:19:35 +01:00
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
|
|
|
|
public class AuthorizationChangedEventProducerTest {
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private StoringAuthorizationChangedEventProducer producer;
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
@Before
|
|
|
|
|
public void setUpProducer() {
|
|
|
|
|
producer = new StoringAuthorizationChangedEventProducer();
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
|
|
|
|
* Tests {@link AuthorizationChangedEventProducer#onEvent(sonia.scm.user.UserEvent)}.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testOnUserEvent()
|
|
|
|
|
{
|
|
|
|
|
User user = UserTestData.createDent();
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new UserEvent(HandlerEventType.BEFORE_CREATE, user));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new UserEvent(HandlerEventType.CREATE, user));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertUserEventIsFired("dent");
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private void assertEventIsNotFired(){
|
|
|
|
|
assertNull(producer.event);
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private void assertUserEventIsFired(String username){
|
|
|
|
|
assertNotNull(producer.event);
|
|
|
|
|
assertTrue(producer.event.isEveryUserAffected());
|
|
|
|
|
assertEquals(username, producer.event.getNameOfAffectedUser());
|
|
|
|
|
}
|
2019-01-23 15:00:48 +01:00
|
|
|
|
|
|
|
|
private void assertGlobalEventIsFired(){
|
|
|
|
|
assertNotNull(producer.event);
|
|
|
|
|
assertFalse(producer.event.isEveryUserAffected());
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
|
|
|
|
* Tests {@link AuthorizationChangedEventProducer#onEvent(sonia.scm.user.UserEvent)} with modified user.
|
|
|
|
|
*/
|
2019-03-13 12:07:18 +01:00
|
|
|
@Test
|
2017-02-26 13:19:35 +01:00
|
|
|
public void testOnUserModificationEvent()
|
|
|
|
|
{
|
|
|
|
|
User user = UserTestData.createDent();
|
|
|
|
|
User userModified = UserTestData.createDent();
|
|
|
|
|
userModified.setDisplayName("Super Dent");
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new UserModificationEvent(HandlerEventType.BEFORE_CREATE, userModified, user));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new UserModificationEvent(HandlerEventType.CREATE, userModified, user));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
|
|
|
|
userModified.setActive(false);
|
|
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new UserModificationEvent(HandlerEventType.BEFORE_CREATE, userModified, user));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new UserModificationEvent(HandlerEventType.CREATE, userModified, user));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertUserEventIsFired("dent");
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
|
|
|
|
* Tests {@link AuthorizationChangedEventProducer#onEvent(sonia.scm.group.GroupEvent)}.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testOnGroupEvent()
|
|
|
|
|
{
|
|
|
|
|
Group group = new Group("xml", "base");
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new GroupEvent(HandlerEventType.BEFORE_CREATE, group));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new GroupEvent(HandlerEventType.CREATE, group));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
|
|
|
|
* Tests {@link AuthorizationChangedEventProducer#onEvent(sonia.scm.group.GroupEvent)} with modified groups.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testOnGroupModificationEvent()
|
|
|
|
|
{
|
|
|
|
|
Group group = new Group("xml", "base");
|
|
|
|
|
Group modifiedGroup = new Group("xml", "base");
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new GroupModificationEvent(HandlerEventType.BEFORE_MODIFY, modifiedGroup, group));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new GroupModificationEvent(HandlerEventType.MODIFY, modifiedGroup, group));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
modifiedGroup.add("test");
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new GroupModificationEvent(HandlerEventType.MODIFY, modifiedGroup, group));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
|
|
|
|
* Tests {@link AuthorizationChangedEventProducer#onEvent(sonia.scm.repository.RepositoryEvent)}.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testOnRepositoryEvent()
|
|
|
|
|
{
|
|
|
|
|
Repository repository = RepositoryTestData.createHeartOfGold();
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new RepositoryEvent(HandlerEventType.BEFORE_CREATE, repository));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-06-25 19:01:33 +02:00
|
|
|
producer.onEvent(new RepositoryEvent(HandlerEventType.CREATE, repository));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
2019-03-13 12:07:18 +01:00
|
|
|
* Tests {@link AuthorizationChangedEventProducer#onEvent(sonia.scm.repository.RepositoryEvent)} with modified
|
2017-02-26 13:19:35 +01:00
|
|
|
* repository.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testOnRepositoryModificationEvent()
|
|
|
|
|
{
|
2019-01-23 15:00:48 +01:00
|
|
|
Repository repositoryModified = RepositoryTestData.createHeartOfGold();
|
|
|
|
|
repositoryModified.setName("test123");
|
|
|
|
|
repositoryModified.setPermissions(Lists.newArrayList(new RepositoryPermission("test", singletonList("read"), false)));
|
|
|
|
|
|
|
|
|
|
Repository repository = RepositoryTestData.createHeartOfGold();
|
|
|
|
|
repository.setPermissions(Lists.newArrayList(new RepositoryPermission("test", singletonList("read"), false)));
|
|
|
|
|
|
|
|
|
|
producer.onEvent(new RepositoryModificationEvent(HandlerEventType.BEFORE_CREATE, repositoryModified, repository));
|
|
|
|
|
assertEventIsNotFired();
|
|
|
|
|
|
|
|
|
|
producer.onEvent(new RepositoryModificationEvent(HandlerEventType.CREATE, repositoryModified, repository));
|
|
|
|
|
assertEventIsNotFired();
|
|
|
|
|
|
|
|
|
|
repositoryModified.setPermissions(Lists.newArrayList(new RepositoryPermission("test", singletonList("read"), false)));
|
|
|
|
|
producer.onEvent(new RepositoryModificationEvent(HandlerEventType.CREATE, repositoryModified, repository));
|
|
|
|
|
assertEventIsNotFired();
|
|
|
|
|
|
|
|
|
|
repositoryModified.setPermissions(Lists.newArrayList(new RepositoryPermission("test123", singletonList("read"), false)));
|
|
|
|
|
producer.onEvent(new RepositoryModificationEvent(HandlerEventType.CREATE, repositoryModified, repository));
|
|
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
|
|
|
|
|
resetStoredEvent();
|
|
|
|
|
|
|
|
|
|
repositoryModified.setPermissions(
|
|
|
|
|
Lists.newArrayList(new RepositoryPermission("test", singletonList("read"), true))
|
|
|
|
|
);
|
|
|
|
|
producer.onEvent(new RepositoryModificationEvent(HandlerEventType.CREATE, repositoryModified, repository));
|
|
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
|
|
|
|
|
resetStoredEvent();
|
|
|
|
|
|
|
|
|
|
repositoryModified.setPermissions(
|
|
|
|
|
Lists.newArrayList(new RepositoryPermission("test", asList("read", "write"), false))
|
|
|
|
|
);
|
|
|
|
|
producer.onEvent(new RepositoryModificationEvent(HandlerEventType.CREATE, repositoryModified, repository));
|
|
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
|
|
|
|
|
resetStoredEvent();
|
|
|
|
|
repository.setPermissions(Lists.newArrayList(new RepositoryPermission("test", asList("read", "write"), false)));
|
|
|
|
|
|
|
|
|
|
repositoryModified.setPermissions(
|
|
|
|
|
Lists.newArrayList(new RepositoryPermission("test", asList("write", "read"), false))
|
|
|
|
|
);
|
|
|
|
|
producer.onEvent(new RepositoryModificationEvent(HandlerEventType.CREATE, repositoryModified, repository));
|
|
|
|
|
assertEventIsNotFired();
|
2017-02-26 13:19:35 +01:00
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private void resetStoredEvent(){
|
|
|
|
|
producer.event = null;
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
/**
|
2019-01-16 16:03:02 +01:00
|
|
|
* Tests {@link AuthorizationChangedEventProducer#onEvent(AssignedPermissionEvent)}.
|
2017-02-26 13:19:35 +01:00
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testOnStoredAssignedPermissionEvent()
|
|
|
|
|
{
|
|
|
|
|
StoredAssignedPermission groupPermission = new StoredAssignedPermission(
|
|
|
|
|
"123", new AssignedPermission("_authenticated", true, "repository:read:*")
|
|
|
|
|
);
|
2019-01-16 16:03:02 +01:00
|
|
|
producer.onEvent(new AssignedPermissionEvent(HandlerEventType.BEFORE_CREATE, groupPermission));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2019-01-16 16:03:02 +01:00
|
|
|
producer.onEvent(new AssignedPermissionEvent(HandlerEventType.CREATE, groupPermission));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertGlobalEventIsFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
resetStoredEvent();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
StoredAssignedPermission userPermission = new StoredAssignedPermission(
|
|
|
|
|
"123", new AssignedPermission("trillian", false, "repository:read:*")
|
|
|
|
|
);
|
2019-01-16 16:03:02 +01:00
|
|
|
producer.onEvent(new AssignedPermissionEvent(HandlerEventType.BEFORE_CREATE, userPermission));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertEventIsNotFired();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
resetStoredEvent();
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2019-01-16 16:03:02 +01:00
|
|
|
producer.onEvent(new AssignedPermissionEvent(HandlerEventType.CREATE, userPermission));
|
2017-02-26 13:19:35 +01:00
|
|
|
assertUserEventIsFired("trillian");
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2020-09-17 15:31:47 +02:00
|
|
|
@Test
|
|
|
|
|
public void testOnNamespaceModificationEvent()
|
|
|
|
|
{
|
|
|
|
|
Namespace namespaceModified = new Namespace("hitchhiker");
|
|
|
|
|
namespaceModified.setPermissions(Lists.newArrayList(new RepositoryPermission("test", singletonList("read"), false)));
|
|
|
|
|
|
|
|
|
|
Namespace namespace = new Namespace("hitchhiker");
|
|
|
|
|
namespace.setPermissions(Lists.newArrayList(new RepositoryPermission("test", singletonList("read"), false)));
|
|
|
|
|
|
|
|
|
|
producer.onEvent(new NamespaceModificationEvent(HandlerEventType.BEFORE_CREATE, namespaceModified, namespace));
|
|
|
|
|
assertEventIsNotFired();
|
|
|
|
|
|
|
|
|
|
producer.onEvent(new NamespaceModificationEvent(HandlerEventType.CREATE, namespaceModified, namespace));
|
|
|
|
|
assertEventIsNotFired();
|
|
|
|
|
|
|
|
|
|
namespaceModified.setPermissions(Lists.newArrayList(new RepositoryPermission("test", singletonList("read"), false)));
|
|
|
|
|
producer.onEvent(new NamespaceModificationEvent(HandlerEventType.CREATE, namespaceModified, namespace));
|
|
|
|
|
assertEventIsNotFired();
|
|
|
|
|
|
|
|
|
|
namespaceModified.setPermissions(Lists.newArrayList(new RepositoryPermission("test123", singletonList("read"), false)));
|
|
|
|
|
producer.onEvent(new NamespaceModificationEvent(HandlerEventType.CREATE, namespaceModified, namespace));
|
|
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
|
|
|
|
|
resetStoredEvent();
|
|
|
|
|
|
|
|
|
|
namespaceModified.setPermissions(
|
|
|
|
|
Lists.newArrayList(new RepositoryPermission("test", singletonList("read"), true))
|
|
|
|
|
);
|
|
|
|
|
producer.onEvent(new NamespaceModificationEvent(HandlerEventType.CREATE, namespaceModified, namespace));
|
|
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
|
|
|
|
|
resetStoredEvent();
|
|
|
|
|
|
|
|
|
|
namespaceModified.setPermissions(
|
|
|
|
|
Lists.newArrayList(new RepositoryPermission("test", asList("read", "write"), false))
|
|
|
|
|
);
|
|
|
|
|
producer.onEvent(new NamespaceModificationEvent(HandlerEventType.CREATE, namespaceModified, namespace));
|
|
|
|
|
assertGlobalEventIsFired();
|
|
|
|
|
|
|
|
|
|
resetStoredEvent();
|
|
|
|
|
namespace.setPermissions(Lists.newArrayList(new RepositoryPermission("test", asList("read", "write"), false)));
|
|
|
|
|
|
|
|
|
|
namespaceModified.setPermissions(
|
|
|
|
|
Lists.newArrayList(new RepositoryPermission("test", asList("write", "read"), false))
|
|
|
|
|
);
|
|
|
|
|
producer.onEvent(new NamespaceModificationEvent(HandlerEventType.CREATE, namespaceModified, namespace));
|
|
|
|
|
assertEventIsNotFired();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private static class StoringAuthorizationChangedEventProducer extends AuthorizationChangedEventProducer {
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
private AuthorizationChangedEvent event;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void sendEvent(AuthorizationChangedEvent event) {
|
|
|
|
|
this.event = event;
|
|
|
|
|
}
|
2019-03-13 12:07:18 +01:00
|
|
|
|
2017-02-26 13:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-16 16:03:02 +01:00
|
|
|
}
|