]> source.dussan.org Git - sonarqube.git/blob
c45201b93449e70af7b117ecae4dd2eec7a386e1
[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.computation.queue.report;
21
22 import org.apache.commons.io.IOUtils;
23 import org.hamcrest.Description;
24 import org.hamcrest.TypeSafeMatcher;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.core.permission.GlobalPermissions;
29 import org.sonar.db.ce.CeTaskTypes;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.server.component.ComponentService;
32 import org.sonar.server.component.NewComponent;
33 import org.sonar.server.computation.queue.CeQueue;
34 import org.sonar.server.computation.queue.CeQueueImpl;
35 import org.sonar.server.computation.queue.CeTaskSubmit;
36 import org.sonar.server.exceptions.ForbiddenException;
37 import org.sonar.server.permission.PermissionService;
38 import org.sonar.server.tester.UserSessionRule;
39
40 import static org.mockito.Matchers.any;
41 import static org.mockito.Matchers.argThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.verifyZeroInteractions;
45 import static org.mockito.Mockito.when;
46
47 public class ReportSubmitterTest {
48
49   static final String PROJECT_KEY = "MY_PROJECT";
50   static final String PROJECT_UUID = "P1";
51   static final String PROJECT_NAME = "My Project";
52   static final String TASK_UUID = "TASK_1";
53
54   @Rule
55   public ExpectedException thrown = ExpectedException.none();
56
57   @Rule
58   public UserSessionRule userSession = UserSessionRule.standalone();
59
60   CeQueue queue = mock(CeQueueImpl.class);
61   ReportFiles reportFiles = mock(ReportFiles.class);
62   ComponentService componentService = mock(ComponentService.class);
63   PermissionService permissionService = mock(PermissionService.class);
64   ReportSubmitter underTest = new ReportSubmitter(queue, userSession, reportFiles, componentService, permissionService);
65
66   @Test
67   public void submit_a_report_on_existing_project() {
68     userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
69
70     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
71     when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
72
73     underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
74
75     verifyZeroInteractions(permissionService);
76     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
77       @Override
78       protected boolean matchesSafely(CeTaskSubmit submit) {
79         return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals(PROJECT_UUID) &&
80           submit.getUuid().equals(TASK_UUID);
81       }
82
83       @Override
84       public void describeTo(Description description) {
85
86       }
87     }));
88   }
89
90   @Test
91   public void provision_project_if_does_not_exist() throws Exception {
92     userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.PROVISIONING);
93
94     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
95     when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
96     when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
97
98     underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
99
100     verify(permissionService).applyDefaultPermissionTemplate(PROJECT_KEY);
101     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
102       @Override
103       protected boolean matchesSafely(CeTaskSubmit submit) {
104         return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals(PROJECT_UUID) &&
105           submit.getUuid().equals(TASK_UUID);
106       }
107
108       @Override
109       public void describeTo(Description description) {
110
111       }
112     }));
113   }
114
115   @Test
116   public void submit_a_report_on_new_project_with_global_scan_permission() {
117     userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
118
119     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
120     when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
121     when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
122
123     underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
124
125     verify(queue).submit(any(CeTaskSubmit.class));
126   }
127
128   @Test
129   public void submit_a_report_on_existing_project_with_global_scan_permission() {
130     userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
131
132     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
133     when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
134
135     underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
136
137     verify(queue).submit(any(CeTaskSubmit.class));
138   }
139
140   @Test
141   public void submit_a_report_on_existing_project_with_project_scan_permission() {
142     userSession.addProjectPermissions(GlobalPermissions.SCAN_EXECUTION, PROJECT_KEY);
143
144     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
145     when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
146
147     underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
148
149     verify(queue).submit(any(CeTaskSubmit.class));
150   }
151
152   @Test
153   public void fail_with_forbidden_exception_when_no_scan_permission() {
154     userSession.setGlobalPermissions(GlobalPermissions.DASHBOARD_SHARING);
155
156     thrown.expect(ForbiddenException.class);
157     underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
158   }
159
160   @Test
161   public void fail_with_forbidden_exception_on_new_project_when_only_project_scan_permission() {
162     userSession.addProjectPermissions(GlobalPermissions.SCAN_EXECUTION, PROJECT_KEY);
163
164     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
165     when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
166     when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
167
168     thrown.expect(ForbiddenException.class);
169     underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
170   }
171
172 }