2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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.
11 * SonarQube 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.
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.
21 package org.sonar.server.computation.ws;
23 import org.apache.commons.io.IOUtils;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.ClassRule;
27 import org.junit.Test;
28 import org.sonar.api.security.DefaultGroups;
29 import org.sonar.api.web.UserRole;
30 import org.sonar.core.activity.Activity;
31 import org.sonar.core.component.ComponentDto;
32 import org.sonar.core.computation.db.AnalysisReportDto;
33 import org.sonar.core.permission.GlobalPermissions;
34 import org.sonar.core.permission.PermissionFacade;
35 import org.sonar.core.persistence.DbSession;
36 import org.sonar.core.persistence.MyBatis;
37 import org.sonar.core.user.UserDto;
38 import org.sonar.server.activity.ActivityService;
39 import org.sonar.server.component.ComponentTesting;
40 import org.sonar.server.computation.AnalysisReportLog;
41 import org.sonar.server.computation.AnalysisReportQueue;
42 import org.sonar.server.computation.ComputationService;
43 import org.sonar.server.db.DbClient;
44 import org.sonar.server.exceptions.ForbiddenException;
45 import org.sonar.server.tester.ServerTester;
46 import org.sonar.server.user.MockUserSession;
47 import org.sonar.server.ws.WsTester;
49 import java.util.List;
51 import static org.fest.assertions.Assertions.assertThat;
53 public class AnalysisReportHistorySearchActionMediumTest {
54 private static final String DEFAULT_PROJECT_KEY = "DefaultProjectKey";
55 private static final String DEFAULT_PROJECT_NAME = "DefaultProjectName";
56 private static final String DEFAULT_REPORT_DATA = "default-project";
59 public static ServerTester tester = new ServerTester();
61 private DbClient dbClient;
62 private DbSession session;
63 private WsTester wsTester;
64 private AnalysisReportQueue queue;
65 private MockUserSession userSession;
66 private ComputationService computationService;
67 private ActivityService activityService;
70 public void before() {
71 tester.clearDbAndIndexes();
72 dbClient = tester.get(DbClient.class);
73 wsTester = tester.get(WsTester.class);
74 session = dbClient.openSession(false);
75 queue = tester.get(AnalysisReportQueue.class);
76 activityService = tester.get(ActivityService.class);
78 UserDto connectedUser = new UserDto().setLogin("gandalf").setName("Gandalf");
79 dbClient.userDao().insert(session, connectedUser);
81 userSession = MockUserSession.set()
82 .setLogin(connectedUser.getLogin())
83 .setUserId(connectedUser.getId().intValue())
84 .setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
89 MyBatis.closeQuietly(session);
93 public void add_and_try_to_retrieve_activities() throws Exception {
94 insertPermissionsForProject(DEFAULT_PROJECT_KEY);
95 queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
96 queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
97 queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
99 List<AnalysisReportDto> reports = queue.all();
100 ComponentDto project = ComponentTesting.newProjectDto()
101 .setName(DEFAULT_PROJECT_NAME)
102 .setKey(DEFAULT_PROJECT_KEY);
103 for (AnalysisReportDto report : reports) {
105 activityService.write(session, Activity.Type.ANALYSIS_REPORT, new AnalysisReportLog(report, project));
109 userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
111 WsTester.TestRequest request = wsTester.newGetRequest(AnalysisReportWebService.API_ENDPOINT, AnalysisReportHistorySearchAction.SEARCH_ACTION);
112 WsTester.Result result = request.execute();
114 assertThat(result).isNotNull();
115 result.assertJson(getClass(), "list_history_reports.json", false);
118 private ComponentDto insertPermissionsForProject(String projectKey) {
119 ComponentDto project = new ComponentDto().setKey(projectKey).setId(1L);
120 dbClient.componentDao().insert(session, project);
122 tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), DefaultGroups.ANYONE, UserRole.USER, session);
123 userSession.addProjectPermissions(UserRole.ADMIN, project.key());
124 userSession.addProjectPermissions(UserRole.USER, project.key());
131 @Test(expected = ForbiddenException.class)
132 public void user_rights_is_not_enough_throw_ForbiddenException() throws Exception {
133 insertPermissionsForProject(DEFAULT_PROJECT_KEY);
134 queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
136 AnalysisReportDto report = queue.all().get(0);
138 queue.remove(report);
139 userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
141 WsTester.TestRequest sut = wsTester.newGetRequest(AnalysisReportWebService.API_ENDPOINT, AnalysisReportHistorySearchAction.SEARCH_ACTION);