import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.api.web.UserRole;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.source.SourceService;
+import org.sonar.server.user.UserSession;
import java.util.List;
@Override
public void handle(Request request, Response response) {
String fileKey = request.mandatoryParam("key");
+ UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
+
int from = Math.max(request.mandatoryParamAsInt("from"), 1);
int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.web.UserRole;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.component.ComponentTesting;
import org.sonar.server.component.db.ComponentDao;
import org.sonar.server.db.DbClient;
+import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.source.SourceService;
+import org.sonar.server.user.MockUserSession;
import org.sonar.server.ws.WsTester;
import static com.google.common.collect.Lists.newArrayList;
@Test
public void show_source() throws Exception {
String fileKey = "src/Foo.java";
+ MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
when(componentDao.getByKey(session, fileKey)).thenReturn(file);
when(sourceService.getLinesAsHtml(eq(file.uuid()), anyInt(), anyInt())).thenReturn(newArrayList(
"/*",
@Test
public void show_source_with_from_and_to_params() throws Exception {
String fileKey = "src/Foo.java";
+ MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
when(componentDao.getByKey(session, fileKey)).thenReturn(file);
when(sourceService.getLinesAsHtml(file.uuid(), 3, 5)).thenReturn(newArrayList(
" */",
@Test
public void show_source_accept_from_less_than_one() throws Exception {
String fileKey = "src/Foo.java";
+ MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "polop", fileKey);
when(componentDao.getByKey(session, fileKey)).thenReturn(file);
when(sourceService.getLinesAsHtml(file.uuid(), 1, 5)).thenReturn(newArrayList(
" */",
verify(sourceService).getLinesAsHtml(file.uuid(), 1, 5);
}
+ @Test(expected = ForbiddenException.class)
+ public void require_code_viewer() throws Exception {
+ String fileKey = "src/Foo.java";
+ MockUserSession.set();
+ tester.newGetRequest("api/sources", "show").setParam("key", fileKey).execute();
+ }
}