import org.sonarqube.ws.client.permission.AddUserWsRequest;
import org.sonarqube.ws.client.permission.RemoveGroupWsRequest;
import org.sonarqube.ws.client.usertoken.GenerateWsRequest;
+import org.sonarqube.ws.client.usertoken.RevokeWsRequest;
+import org.sonarqube.ws.client.usertoken.SearchWsRequest;
import org.sonarqube.ws.client.usertoken.UserTokensService;
import static java.lang.String.format;
@Test
public void basic_authentication_based_on_token() {
+ String tokenName = "Validate token based authentication";
WsUserTokens.GenerateWsResponse generateWsResponse = userTokensWsClient.generate(new GenerateWsRequest()
.setLogin(LOGIN)
- .setName("Validate token based authentication"));
+ .setName(tokenName));
WsClient wsClient = new HttpWsClient(new HttpConnector.Builder()
.url(ORCHESTRATOR.getServer().getUrl())
.token(generateWsResponse.getToken()).build());
WsResponse response = wsClient.wsConnector().call(new GetRequest("api/authentication/validate"));
assertThat(response.content()).isEqualTo("{\"valid\":true}");
+
+ WsUserTokens.SearchWsResponse searchResponse = userTokensWsClient.search(new SearchWsRequest().setLogin(LOGIN));
+ assertThat(searchResponse.getUserTokensCount()).isEqualTo(1);
+ userTokensWsClient.revoke(new RevokeWsRequest().setLogin(LOGIN).setName(tokenName));
+ searchResponse = userTokensWsClient.search(new SearchWsRequest().setLogin(LOGIN));
+ assertThat(searchResponse.getUserTokensCount()).isEqualTo(0);
}
/**
@Test
public void run_analysis_with_token_authentication() {
+ String tokenName = "Analyze Project";
WsUserTokens.GenerateWsResponse generateWsResponse = userTokensWsClient.generate(new GenerateWsRequest()
.setLogin(LOGIN)
- .setName("Analyze Project"));
+ .setName(tokenName));
SonarRunner sampleProject = SonarRunner.create(projectDir("shared/xoo-sample"));
sampleProject.setProperties(
"sonar.login", generateWsResponse.getToken(),
BuildResult buildResult = ORCHESTRATOR.executeBuild(sampleProject);
assertThat(buildResult.isSuccess()).isTrue();
+ userTokensWsClient.revoke(new RevokeWsRequest().setLogin(LOGIN).setName(tokenName));
}
@Test
package org.sonarqube.ws.client.usertoken;
import org.sonarqube.ws.WsUserTokens.GenerateWsResponse;
+import org.sonarqube.ws.WsUserTokens.SearchWsResponse;
import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsConnector;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.ACTION_GENERATE;
+import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.ACTION_REVOKE;
+import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.ACTION_SEARCH;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.CONTROLLER;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.PARAM_LOGIN;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.PARAM_NAME;
.setParam(PARAM_NAME, request.getName()),
GenerateWsResponse.parser());
}
+
+ public SearchWsResponse search(SearchWsRequest request) {
+ return call(
+ new GetRequest(path(ACTION_SEARCH)).setParam(PARAM_LOGIN, request.getLogin()),
+ SearchWsResponse.parser());
+ }
+
+ public void revoke(RevokeWsRequest request) {
+ call(
+ new PostRequest(path(ACTION_REVOKE))
+ .setParam(PARAM_LOGIN, request.getLogin())
+ .setParam(PARAM_NAME, request.getName()));
+ }
+
}