import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.BadRequestException;
@Before
public void setUp() throws Exception {
- org = OrganizationTesting.insert(db, OrganizationTesting.newOrganizationDto());
+ org = db.organizations().insert();
group = db.users().insertGroup(org, "a-group");
project = new ComponentDbTester(db).insertComponent(ComponentTesting.newProjectDto());
}
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
import static org.sonar.db.component.ComponentTesting.newView;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
import static org.sonar.server.permission.ws.AddGroupAction.ACTION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
@Test
public void reference_group_by_its_name_in_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto group = db.users().insertGroup(org, "the-group");
loginAsAdmin(org);
@Test
public void fail_if_administrator_of_other_organization_only() throws Exception {
- OrganizationDto org1 = OrganizationTesting.insert(db, newOrganizationDto());
- OrganizationDto org2 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org1 = db.organizations().insert();
+ OrganizationDto org2 = db.organizations().insert();
GroupDto group = db.users().insertGroup(org1, "the-group");
loginAsAdmin(org2);
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.ForbiddenException;
import static org.sonar.core.permission.GlobalPermissions.QUALITY_GATE_ADMIN;
import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.server.user.ServerUserSession.createForAnonymous;
import static org.sonar.server.user.ServerUserSession.createForUser;
private static final String PROJECT_UUID = "ABCD";
private static final String FILE_KEY = "com.foo:Bar:BarFile.xoo";
private static final String FILE_UUID = "BCDE";
- private static final UserDto ROOT_USER_DTO = new UserDto() {{
- setRoot(true);
- }}.setLogin("root_user");
- private static final UserDto NON_ROOT_USER_DTO = new UserDto() {{
- setRoot(false);
- }}.setLogin("regular_user");
+ private static final UserDto ROOT_USER_DTO = new UserDto() {
+ {
+ setRoot(true);
+ }
+ }.setLogin("root_user");
+ private static final UserDto NON_ROOT_USER_DTO = new UserDto() {
+ {
+ setRoot(false);
+ }
+ }.setLogin("regular_user");
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
public void checkComponentPermission_fails_with_FE_when_project_of_specified_uuid_can_not_be_found() {
ComponentDto project2 = db.components().insertComponent(ComponentTesting.newProjectDto());
ComponentDto file2 = db.components().insertComponent(ComponentTesting.newFileDto(project2, null)
- // Simulate file is linked to an invalid project
- .setProjectUuid("INVALID"));
+ // Simulate file is linked to an invalid project
+ .setProjectUuid("INVALID"));
addProjectPermissions(project, UserRole.USER);
UserSession session = newUserSession(userDto);
@Test
public void hasOrganizationPermission_for_logged_in_user() {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
db.users().insertPermissionOnUser(org, userDto, GlobalPermissions.PROVISIONING);
UserSession session = newUserSession(userDto);
@Test
public void hasOrganizationPermission_for_anonymous_user() {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
db.users().insertPermissionOnAnyone(org, GlobalPermissions.PROVISIONING);
UserSession session = newAnonymousSession();
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_NAME;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_LOGIN;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_ORGANIZATION_KEY;
@Test
public void add_user_to_group_referenced_by_its_name_and_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto group = db.users().insertGroup(org, "a-group");
UserDto user = db.users().insertUser("user_login");
loginAsAdmin(org);
@Test
public void fail_if_administrator_of_another_organization() throws Exception {
- OrganizationDto org1 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org1 = db.organizations().insert();
GroupDto group = db.users().insertGroup(org1, "a-group");
UserDto user = db.users().insertUser("user_login");
- OrganizationDto org2 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org2 = db.organizations().insert();
loginAsAdmin(org2);
expectedException.expect(ForbiddenException.class);
.execute();
}
-
private WsTester.TestRequest newRequest() {
return ws.newPostRequest("api/user_groups", "add_user");
}
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.ServerException;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
public class CreateActionTest {
@Test
public void create_group_on_specific_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
loginAsAdmin(org);
newRequest()
@Test
public void fail_if_administrator_of_another_organization() throws Exception {
- OrganizationDto org1 = OrganizationTesting.insert(db, newOrganizationDto());
- OrganizationDto org2 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org1 = db.organizations().insert();
+ OrganizationDto org2 = db.organizations().insert();
loginAsAdmin(org2);
expectedException.expect(ForbiddenException.class);
@Test
public void fail_if_group_with_same_name_already_exists_in_the_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto group = db.users().insertGroup(org, "the-group");
loginAsAdmin(org);
@Test
public void add_group_with_a_name_that_already_exists_in_another_organization() throws Exception {
String name = "the-group";
- OrganizationDto org1 = OrganizationTesting.insert(db, newOrganizationDto());
- OrganizationDto org2 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org1 = db.organizations().insert();
+ OrganizationDto org2 = db.organizations().insert();
GroupDto group = db.users().insertGroup(org1, name);
loginAsAdmin(org2);
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateTesting;
import org.sonar.db.user.GroupDto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_ID;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_NAME;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_ORGANIZATION_KEY;
@Test
public void delete_by_name_and_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
addAdmin(org);
GroupDto group = db.users().insertGroup(org, "to-delete");
loginAsAdmin(org);
@Test
public void delete_by_name_fails_if_organization_is_not_correct() throws Exception {
- OrganizationDto org = newOrganizationDto();
- OrganizationTesting.insert(db, org);
-
+ OrganizationDto org = db.organizations().insert();
loginAsAdmin(org);
expectedException.expect(NotFoundException.class);
@Test
public void delete_group_of_an_organization_even_if_name_is_default_group_of_default_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
addAdmin(org);
GroupDto group = db.users().insertGroup(org, defaultGroup.getName());
loginAsAdmin(org);
@Test
public void delete_admin_group_fails_if_no_admin_users_left() throws Exception {
// admin users are part of the group to be deleted
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto adminGroup = db.users().insertGroup(org, "admins");
db.users().insertPermissionOnGroup(adminGroup, SYSTEM_ADMIN);
UserDto bigBoss = db.users().insertUser();
@Test
public void delete_admin_group_succeeds_if_other_groups_have_administrators() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto adminGroup1 = db.users().insertGroup(org, "admins");
db.users().insertPermissionOnGroup(adminGroup1, SYSTEM_ADMIN);
GroupDto adminGroup2 = db.users().insertGroup(org, "admins");
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.NotFoundException;
@Test
public void remove_user_by_group_name_in_specific_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, OrganizationTesting.newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto group = db.users().insertGroup(org, "a_group");
UserDto user = db.users().insertUser("user_login");
db.users().insertMember(group, user);
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.UnauthorizedException;
import static org.apache.commons.lang.StringUtils.capitalize;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
import static org.sonar.db.user.GroupTesting.newGroupDto;
public class SearchActionTest {
@Test
public void search_in_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto group = db.users().insertGroup(org, "users");
// the group in default org is not returned
db.users().insertGroup(db.getDefaultOrganization(), "users");
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.ForbiddenException;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
public class UpdateActionTest {
private DefaultOrganizationProviderRule defaultOrganizationProvider = DefaultOrganizationProviderRule.create(db);
private PersistentSettings settings = mock(PersistentSettings.class);
- private WsTester ws = new WsTester(new UserGroupsWs(new UpdateAction(db.getDbClient(), userSession, new GroupWsSupport(db.getDbClient(), defaultOrganizationProvider), settings, defaultOrganizationProvider)));
+ private WsTester ws = new WsTester(
+ new UserGroupsWs(new UpdateAction(db.getDbClient(), userSession, new GroupWsSupport(db.getDbClient(), defaultOrganizationProvider), settings, defaultOrganizationProvider)));
@Before
public void setUp() throws Exception {
@Test
public void do_not_update_default_group_of_default_organization_if_updating_group_on_non_default_organization() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
when(settings.getString(DEFAULT_GROUP_NAME_KEY)).thenReturn(DEFAULT_GROUP_NAME_VALUE);
GroupDto groupInDefaultOrg = db.users().insertGroup(defaultOrganizationProvider.getDto(), DEFAULT_GROUP_NAME_VALUE);
GroupDto group = db.users().insertGroup(org, DEFAULT_GROUP_NAME_VALUE);
@Test
public void fails_if_admin_of_another_organization_only() throws Exception {
- OrganizationDto org1 = OrganizationTesting.insert(db, newOrganizationDto());
- OrganizationDto org2 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org1 = db.organizations().insert();
+ OrganizationDto org2 = db.organizations().insert();
GroupDto group = db.users().insertGroup(org1, "group1");
loginAsAdmin(org2);
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.ws.WsTester.TestRequest;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_ID;
@Test
public void fail_if_admin_of_other_organization_only() throws Exception {
- OrganizationDto org1 = OrganizationTesting.insert(db, newOrganizationDto());
- OrganizationDto org2 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org1 = db.organizations().insert();
+ OrganizationDto org2 = db.organizations().insert();
GroupDto group = db.users().insertGroup(org1, "the-group");
loginAsAdmin(org2);
@Test
public void references_group_by_its_name() throws Exception {
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto group = db.users().insertGroup(org, "the-group");
UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada").setName("Ada Lovelace"));
db.users().insertMember(group, user1);
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.db.component.ComponentDbTester;
+import org.sonar.db.organization.OrganizationDbTester;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.UserDbTester;
private final UserDbTester userTester;
private final ComponentDbTester componentTester;
+ private final OrganizationDbTester organizationTester;
private DbTester(System2 system2, @Nullable String schemaPath) {
this.system2 = system2;
initDbClient();
this.userTester = new UserDbTester(this);
this.componentTester = new ComponentDbTester(this);
+ this.organizationTester = new OrganizationDbTester(this);
}
public static DbTester create(System2 system2) {
return componentTester;
}
+ public OrganizationDbTester organizations() {
+ return organizationTester;
+ }
+
@Override
protected void after() {
if (session != null) {
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.organization;
+
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+
+public class OrganizationDbTester {
+ private final DbTester dbTester;
+
+ public OrganizationDbTester(DbTester dbTester) {
+ this.dbTester = dbTester;
+ }
+
+ /**
+ * Insert an {@link OrganizationDto} and commit the session
+ */
+ public OrganizationDto insert() {
+ return insert(OrganizationTesting.newOrganizationDto());
+ }
+
+ /**
+ * Insert the provided {@link OrganizationDto} and commit the session
+ */
+ public OrganizationDto insert(OrganizationDto dto) {
+ DbSession dbSession = dbTester.getSession();
+ dbTester.getDbClient().organizationDao().insert(dbSession, dto);
+ dbSession.commit();
+ return dto;
+ }
+}
*/
package org.sonar.db.organization;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
public class OrganizationTesting {
// only statics
}
- /**
- * Insert an {@link OrganizationDto} and commit the session
- */
- public static OrganizationDto insert(DbTester dbTester, OrganizationDto dto) {
- DbSession dbSession = dbTester.getSession();
- dbTester.getDbClient().organizationDao().insert(dbSession, dto);
- dbSession.commit();
- return dto;
- }
-
/**
* Creates a new {@link OrganizationDto} with randomly generated field values.
*/
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
public class AuthorizationDaoTest {
@Before
public void setUp() throws Exception {
- org = OrganizationTesting.insert(db, newOrganizationDto());
+ org = db.organizations().insert();
user = db.users().insertUser();
group1 = db.users().insertGroup(org, "group1");
group2 = db.users().insertGroup(org, "group2");
UserDto user4 = db.users().insertUser();
UserDto user5 = db.users().insertUser();
- OrganizationDto org = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org = db.organizations().insert();
GroupDto group1 = db.users().insertGroup(org, "g1");
db.users().insertPermissionOnGroup(group1, "perm1");
db.users().insertPermissionOnGroup(group1, "perm2");
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.GroupDto;
import static java.util.Arrays.asList;
import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
-import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
import static org.sonar.db.user.GroupTesting.newGroupDto;
public class GroupPermissionDaoTest {
- private static final long UNKNOWN_PROJECT_ID = -1L;
- private static final long UNKNOWN_GROUP_ID = -1L;
-
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
+
private DbSession dbSession = db.getSession();
private GroupPermissionDao underTest = new GroupPermissionDao();
@Test
public void selectGlobalPermissionsOfGroup() {
- OrganizationDto org1 = OrganizationTesting.insert(db, newOrganizationDto());
- OrganizationDto org2 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org1 = db.organizations().insert();
+ OrganizationDto org2 = db.organizations().insert();
GroupDto group1 = db.users().insertGroup(org1, "group1");
GroupDto group2 = db.users().insertGroup(org2, "group2");
ComponentDto project = db.components().insertProject();
@Test
public void selectProjectPermissionsOfGroup() {
- OrganizationDto org1 = OrganizationTesting.insert(db, newOrganizationDto());
+ OrganizationDto org1 = db.organizations().insert();
GroupDto group1 = db.users().insertGroup(org1, "group1");
ComponentDto project1 = db.components().insertProject();
ComponentDto project2 = db.components().insertProject();