public class AuthorizationDaoTest extends AbstractDaoTestCase {
private static final int USER = 100;
- private static final Long PROJECT_ID = 300L, EMPTY_PROJECT_ID = 400L;
+ private static final Long PROJECT_ID = 300L, PROJECT_ID_WITHOUT_SNAPSHOT = 400L;
private static final String PROJECT = "pj-w-snapshot";
+ private static final String PROJECT_WIHOUT_SNAPSHOT = "pj-wo-snapshot";
DbSession session;
+ AuthorizationDao authorization;
+
@Before
public void setUp() throws Exception {
session = getMyBatis().openSession(false);
+ authorization = new AuthorizationDao(getMyBatis());
}
@After
// but user is not in an authorized group
setupData("user_should_be_authorized");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<Long> componentIds = authorization.keepAuthorizedProjectIds(session,
- Sets.newHashSet(PROJECT_ID, EMPTY_PROJECT_ID),
+ Sets.newHashSet(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT),
USER, "user");
- assertThat(componentIds).containsOnly(PROJECT_ID, EMPTY_PROJECT_ID);
+ assertThat(componentIds).containsOnly(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT);
// user does not have the role "admin"
componentIds = authorization.keepAuthorizedProjectIds(session,
USER, "admin")).isEmpty();
}
+ @Test
+ public void keep_authorized_project_ids_for_user() {
+ setupData("keep_authorized_project_ids_for_user");
+
+ assertThat(authorization.keepAuthorizedProjectIds(session, Sets.newHashSet(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT), USER, "user")).containsOnly(PROJECT_ID);
+
+ // user does not have the role "admin"
+ assertThat(authorization.keepAuthorizedProjectIds(session, Sets.newHashSet(PROJECT_ID), USER, "admin")).isEmpty();
+
+ // Empty list
+ assertThat(authorization.keepAuthorizedProjectIds(session, Collections.<Long>emptySet(), USER, "admin")).isEmpty();
+ }
+
+ @Test
+ public void keep_authorized_project_ids_for_group() {
+ setupData("keep_authorized_project_ids_for_group");
+
+ assertThat(authorization.keepAuthorizedProjectIds(session, Sets.newHashSet(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT), USER, "user")).containsOnly(PROJECT_ID);
+
+ // user does not have the role "admin"
+ assertThat(authorization.keepAuthorizedProjectIds(session, Sets.newHashSet(PROJECT_ID), USER, "admin")).isEmpty();
+
+ // Empty list
+ assertThat(authorization.keepAuthorizedProjectIds(session, Collections.<Long>emptySet(), USER, "admin")).isEmpty();
+ }
+
+ @Test
+ public void keep_authorized_project_ids_for_anonymous() {
+ setupData("keep_authorized_project_ids_for_anonymous");
+
+ assertThat(authorization.keepAuthorizedProjectIds(session, Sets.newHashSet(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT), null, "user")).containsOnly(PROJECT_ID);
+
+ // user does not have the role "admin"
+ assertThat(authorization.keepAuthorizedProjectIds(session, Sets.newHashSet(PROJECT_ID), null, "admin")).isEmpty();
+
+ // Empty list
+ assertThat(authorization.keepAuthorizedProjectIds(session, Collections.<Long>emptySet(), null, "admin")).isEmpty();
+ }
+
@Test
public void is_authorized_component_key_for_user() {
- // but user is not in an authorized group
- setupData("user_should_be_authorized");
+ setupData("keep_authorized_project_ids_for_user");
+
+ assertThat(authorization.isAuthorizedComponentKey(PROJECT, USER, "user")).isTrue();
+ assertThat(authorization.isAuthorizedComponentKey(PROJECT_WIHOUT_SNAPSHOT, USER, "user")).isFalse();
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
+ // user does not have the role "admin"
+ assertThat(authorization.isAuthorizedComponentKey(PROJECT, USER, "admin")).isFalse();
+ }
+
+ @Test
+ public void is_authorized_component_key_for_group() {
+ setupData("keep_authorized_project_ids_for_group");
assertThat(authorization.isAuthorizedComponentKey(PROJECT, USER, "user")).isTrue();
+ assertThat(authorization.isAuthorizedComponentKey(PROJECT_WIHOUT_SNAPSHOT, USER, "user")).isFalse();
// user does not have the role "admin"
assertThat(authorization.isAuthorizedComponentKey(PROJECT, USER, "admin")).isFalse();
@Test
public void is_authorized_component_key_for_anonymous() {
- setupData("anonymous_should_be_authorized");
-
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
+ setupData("keep_authorized_project_ids_for_anonymous");
assertThat(authorization.isAuthorizedComponentKey(PROJECT, null, "user")).isTrue();
+ assertThat(authorization.isAuthorizedComponentKey(PROJECT_WIHOUT_SNAPSHOT, null, "user")).isFalse();
assertThat(authorization.isAuthorizedComponentKey(PROJECT, null, "admin")).isFalse();
}
// user is in an authorized group
setupData("group_should_be_authorized");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
- Collection<Long> componentIds = authorization.keepAuthorizedProjectIds(session,
- Sets.newHashSet(PROJECT_ID, EMPTY_PROJECT_ID),
- USER, "user");
-
- assertThat(componentIds).containsOnly(PROJECT_ID, EMPTY_PROJECT_ID);
-
- // group does not have the role "admin"
- componentIds = authorization.keepAuthorizedProjectIds(session,
- Sets.newHashSet(PROJECT_ID, EMPTY_PROJECT_ID),
- USER, "admin");
- assertThat(componentIds).isEmpty();
- }
-
- @Test
- public void group_should_have_global_authorization() {
- // user is in a group that has authorized access to all projects
- setupData("group_should_have_global_authorization");
-
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<Long> componentIds = authorization.keepAuthorizedProjectIds(session,
- Sets.newHashSet(PROJECT_ID, EMPTY_PROJECT_ID),
+ Sets.newHashSet(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT),
USER, "user");
- assertThat(componentIds).containsOnly(PROJECT_ID, EMPTY_PROJECT_ID);
+ assertThat(componentIds).containsOnly(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT);
// group does not have the role "admin"
componentIds = authorization.keepAuthorizedProjectIds(session,
- Sets.newHashSet(PROJECT_ID, EMPTY_PROJECT_ID),
+ Sets.newHashSet(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT),
USER, "admin");
assertThat(componentIds).isEmpty();
}
public void anonymous_should_be_authorized() {
setupData("anonymous_should_be_authorized");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<Long> componentIds = authorization.keepAuthorizedProjectIds(session,
- Sets.newHashSet(PROJECT_ID, EMPTY_PROJECT_ID),
+ Sets.newHashSet(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT),
null, "user");
- assertThat(componentIds).containsOnly(PROJECT_ID, EMPTY_PROJECT_ID);
+ assertThat(componentIds).containsOnly(PROJECT_ID, PROJECT_ID_WITHOUT_SNAPSHOT);
// group does not have the role "admin"
componentIds = authorization.keepAuthorizedProjectIds(session,
public void should_return_root_project_keys_for_user() {
setupData("should_return_root_project_keys_for_user");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<String> rootProjectIds = authorization.selectAuthorizedRootProjectsKeys(USER, "user");
assertThat(rootProjectIds).containsOnly(PROJECT);
// but user is not in an authorized group
setupData("should_return_root_project_keys_for_group");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<String> rootProjectIds = authorization.selectAuthorizedRootProjectsKeys(USER, "user");
assertThat(rootProjectIds).containsOnly(PROJECT);
public void should_return_root_project_keys_for_anonymous() {
setupData("should_return_root_project_keys_for_anonymous");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<String> rootProjectIds = authorization.selectAuthorizedRootProjectsKeys(null, "user");
assertThat(rootProjectIds).containsOnly(PROJECT);
public void should_return_root_project_uuids_for_user() {
setupData("should_return_root_project_keys_for_user");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<String> rootProjectUuids = authorization.selectAuthorizedRootProjectsUuids(USER, "user");
assertThat(rootProjectUuids).containsOnly("ABCD");
// but user is not in an authorized group
setupData("should_return_root_project_keys_for_group");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<String> rootProjectUuids = authorization.selectAuthorizedRootProjectsUuids(USER, "user");
assertThat(rootProjectUuids).containsOnly("ABCD");
public void should_return_root_project_uuids_for_anonymous() {
setupData("should_return_root_project_keys_for_anonymous");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
Collection<String> rootProjectUuids = authorization.selectAuthorizedRootProjectsUuids(null, "user");
assertThat(rootProjectUuids).containsOnly("ABCD");
public void should_return_user_global_permissions() {
setupData("should_return_user_global_permissions");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
assertThat(authorization.selectGlobalPermissions("john")).containsOnly("user", "admin");
assertThat(authorization.selectGlobalPermissions("arthur")).containsOnly("user");
assertThat(authorization.selectGlobalPermissions("none")).isEmpty();
public void should_return_group_global_permissions() {
setupData("should_return_group_global_permissions");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
assertThat(authorization.selectGlobalPermissions("john")).containsOnly("user", "admin");
assertThat(authorization.selectGlobalPermissions("arthur")).containsOnly("user");
assertThat(authorization.selectGlobalPermissions("none")).isEmpty();
public void should_return_global_permissions_for_anonymous() {
setupData("should_return_global_permissions_for_anonymous");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
assertThat(authorization.selectGlobalPermissions(null)).containsOnly("user", "admin");
}
public void should_return_global_permissions_for_group_anyone() throws Exception {
setupData("should_return_global_permissions_for_group_anyone");
- AuthorizationDao authorization = new AuthorizationDao(getMyBatis());
assertThat(authorization.selectGlobalPermissions("anyone_user")).containsOnly("user", "profileadmin");
}