import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
-import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserTokenDto;
@Override
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction(ACTION_SEARCH)
- .setDescription("List the access tokens of a user. <br />" +
- "The login must exist and active.<br />" +
- "It requires administration permissions.")
+ .setDescription("List the access tokens of a user.<br>" +
+ "The login must exist and active.<br>" +
+ "If the login is set, it requires administration permissions. Otherwise, a token is generated for the authenticated user.")
.setResponseExample(getClass().getResource("search-example.json"))
.setSince("5.3")
.setHandler(this);
action.createParam(PARAM_LOGIN)
- .setRequired(true)
.setDescription("User login")
.setExampleValue("g.hopper");
}
}
private SearchWsResponse doHandle(SearchWsRequest request) {
- userSession.checkLoggedIn().checkPermission(GlobalPermissions.SYSTEM_ADMIN);
+ TokenPermissionsValidator.validate(userSession, request.getLogin());
DbSession dbSession = dbClient.openSession(false);
try {
}
}
- private static SearchWsRequest toSearchWsRequest(Request request) {
- return new SearchWsRequest()
- .setLogin(request.mandatoryParam(PARAM_LOGIN));
+ private SearchWsRequest toSearchWsRequest(Request request) {
+ SearchWsRequest searchWsRequest = new SearchWsRequest().setLogin(request.param(PARAM_LOGIN));
+ if (searchWsRequest.getLogin() == null) {
+ searchWsRequest.setLogin(userSession.getLogin());
+ }
+ return searchWsRequest;
}
private static SearchWsResponse buildResponse(String login, List<UserTokenDto> userTokensDto) {
import com.google.common.base.Throwables;
import java.io.IOException;
+import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.user.UserDbTester;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
import org.sonar.test.DbTests;
import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.WsUserTokens.SearchWsResponse;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.db.user.UserTokenTesting.newUserToken;
import static org.sonar.test.JsonAssert.assertJson;
public class SearchActionTest {
static final String GRACE_HOPPER = "grace.hopper";
static final String ADA_LOVELACE = "ada.lovelace";
- static final String TOKEN_NAME = "token-name";
@Rule
public ExpectedException expectedException = ExpectedException.none();
.setName("Project scan on Travis")
.setLogin(ADA_LOVELACE));
dbSession.commit();
+
String response = ws.newRequest()
.setParam(PARAM_LOGIN, GRACE_HOPPER)
.execute().getInput();
}
@Test
- public void fail_when_login_does_not_exist() {
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("User with login 'unknown-login' not found");
+ public void a_user_can_search_its_own_token() {
+ userSession.login(GRACE_HOPPER).setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
+ dbClient.userTokenDao().insert(dbSession, newUserToken()
+ .setCreatedAt(1448523067221L)
+ .setName("Project scan on Travis")
+ .setLogin(GRACE_HOPPER));
+ db.commit();
- newRequest("unknown-login");
+ SearchWsResponse response = newRequest(null);
+
+ assertThat(response.getUserTokensCount()).isEqualTo(1);
}
@Test
- public void fail_when_not_logged_in() {
- userSession.anonymous();
- expectedException.expect(UnauthorizedException.class);
+ public void fail_when_login_does_not_exist() {
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("User with login 'unknown-login' not found");
- newRequest(GRACE_HOPPER);
+ newRequest("unknown-login");
}
@Test
newRequest(GRACE_HOPPER);
}
- private SearchWsResponse newRequest(String login) {
- TestResponse response = ws.newRequest()
- .setMediaType(MediaTypes.PROTOBUF)
- .setParam(PARAM_LOGIN, login)
- .execute();
+ private SearchWsResponse newRequest(@Nullable String login) {
+ TestRequest testRequest = ws.newRequest()
+ .setMediaType(MediaTypes.PROTOBUF);
+ if (login != null) {
+ testRequest.setParam(PARAM_LOGIN, login);
+ }
+
+ TestResponse response = testRequest.execute();
+
try {
return SearchWsResponse.parseFrom(response.getInputStream());
} catch (IOException e) {