]> source.dussan.org Git - sonarqube.git/blob
fd054edc264f3bcc3db2701061548e5c0c395425
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.qualityprofile;
21
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.ClassRule;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.permission.GlobalPermissions;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.permission.PermissionRepository;
33 import org.sonar.db.qualityprofile.QualityProfileDto;
34 import org.sonar.db.user.UserDto;
35 import org.sonar.server.tester.MockUserSession;
36 import org.sonar.server.tester.ServerTester;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.user.UserSession;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41
42 public class QProfileProjectOperationsMediumTest {
43
44   @ClassRule
45   public static ServerTester tester = new ServerTester().withEsIndexes();
46   @Rule
47   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
48
49   DbClient db;
50   DbSession dbSession;
51   QProfileFactory factory;
52   QProfileProjectOperations projectOperations;
53   ComponentDto project;
54   QualityProfileDto profile;
55   static final String PROJECT_KEY = "SonarQube";
56   static final String PROJECT_UUID = "ABCD";
57
58   UserSession authorizedProfileAdminUserSession = new MockUserSession("john").setName("John").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
59   UserSession authorizedProjectAdminUserSession = new MockUserSession("john").setName("John").addProjectPermissions(UserRole.ADMIN, PROJECT_KEY);
60
61   @Before
62   public void before() {
63     tester.clearDbAndIndexes();
64     db = tester.get(DbClient.class);
65     dbSession = db.openSession(false);
66     factory = tester.get(QProfileFactory.class);
67     projectOperations = tester.get(QProfileProjectOperations.class);
68
69     project = new ComponentDto()
70       .setUuid(PROJECT_UUID)
71       .setKey(PROJECT_KEY)
72       .setName("SonarQube")
73       .setLongName("SonarQube")
74       .setQualifier("TRK")
75       .setScope("PRJ")
76       .setEnabled(true);
77     db.componentDao().insert(dbSession, project);
78
79     profile = QProfileTesting.newXooP1();
80     db.qualityProfileDao().insert(dbSession, profile);
81
82     dbSession.commit();
83   }
84
85   @After
86   public void after() {
87     dbSession.close();
88   }
89
90   @Test
91   public void add_project() {
92     projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
93
94     assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
95   }
96
97   @Test
98   public void add_project_with_only_project_admin_permission() {
99     projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProjectAdminUserSession);
100
101     assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
102   }
103
104   @Test
105   public void remove_project_from_project_id() {
106     projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
107     assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
108
109     projectOperations.removeProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
110     assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNull();
111   }
112
113   @Test
114   public void remove_project_from_language() {
115     projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
116     assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
117
118     projectOperations.removeProject(profile.getLanguage(), project.getId(), authorizedProfileAdminUserSession);
119     assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNull();
120   }
121
122   @Test
123   public void remove_all_projects() {
124     ComponentDto project1 = new ComponentDto()
125       .setUuid("BCDE")
126       .setKey("project1")
127       .setName("project1")
128       .setLongName("project1")
129       .setQualifier("TRK")
130       .setScope("PRJ")
131       .setEnabled(true);
132     ComponentDto project2 = new ComponentDto()
133       .setUuid("CDEF")
134       .setKey("project2")
135       .setName("project2")
136       .setLongName("project2")
137       .setQualifier("TRK")
138       .setScope("PRJ")
139       .setEnabled(true);
140     db.componentDao().insert(dbSession, project1);
141     db.componentDao().insert(dbSession, project2);
142
143     // Create a user having user permission on the two projects and the global quality profile admin permission
144     UserDto user = new UserDto().setLogin("john").setName("John").setEmail("jo@hn.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis());
145     db.userDao().insert(dbSession, user);
146     tester.get(PermissionRepository.class).insertUserPermission(project1.getId(), user.getId(), UserRole.USER, dbSession);
147     tester.get(PermissionRepository.class).insertUserPermission(project2.getId(), user.getId(), UserRole.USER, dbSession);
148     UserSession userSession = userSessionRule.login("john").setUserId(user.getId().intValue()).setName("John")
149       .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
150
151     dbSession.commit();
152
153     projectOperations.addProject(profile.getKey(), project1.uuid(), userSession);
154     projectOperations.addProject(profile.getKey(), project2.uuid(), userSession);
155     assertThat(tester.get(QProfileProjectLookup.class).projects(profile.getId())).hasSize(2);
156
157     projectOperations.removeAllProjects(profile.getKey(), userSession);
158     assertThat(tester.get(QProfileProjectLookup.class).projects(profile.getId())).isEmpty();
159   }
160 }