]> source.dussan.org Git - sonarqube.git/commitdiff
Create user only when needed in LocalAuthenticationTest
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 4 Sep 2017 13:57:00 +0000 (15:57 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 5 Sep 2017 10:07:45 +0000 (12:07 +0200)
tests/src/test/java/org/sonarqube/tests/user/LocalAuthenticationTest.java

index 716a2a80b2ee9510b38513ab620c62f2932a486a..662c0c225069e778ff0381f21e54cd66cc0a1b82 100644 (file)
@@ -23,7 +23,6 @@ import com.codeborne.selenide.Condition;
 import com.sonar.orchestrator.Orchestrator;
 import java.util.UUID;
 import org.junit.After;
-import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
@@ -32,13 +31,13 @@ import org.sonarqube.pageobjects.Navigation;
 import org.sonarqube.tests.Category4Suite;
 import org.sonarqube.tests.Tester;
 import org.sonarqube.ws.WsUserTokens;
+import org.sonarqube.ws.WsUsers.CreateWsResponse.User;
 import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.HttpConnector;
 import org.sonarqube.ws.client.PostRequest;
 import org.sonarqube.ws.client.WsClient;
 import org.sonarqube.ws.client.WsClientFactories;
 import org.sonarqube.ws.client.WsResponse;
-import org.sonarqube.ws.client.permission.AddUserWsRequest;
 import org.sonarqube.ws.client.usertoken.GenerateWsRequest;
 import org.sonarqube.ws.client.usertoken.RevokeWsRequest;
 import org.sonarqube.ws.client.usertoken.SearchWsRequest;
@@ -51,25 +50,12 @@ import static util.ItUtils.setServerProperty;
 
 public class LocalAuthenticationTest {
 
-  private static final String ADMIN_USER_LOGIN = "admin-user";
-
-  private static final String LOGIN = "george.orwell";
-
   @ClassRule
   public static Orchestrator orchestrator = Category4Suite.ORCHESTRATOR;
 
   @Rule
   public Tester tester = new Tester(orchestrator).disableOrganizations();
 
-  @Before
-  public void setUp() {
-    tester.users().generate(u -> u.setLogin(LOGIN).setPassword("123456"));
-    addUserPermission(LOGIN, "admin");
-
-    tester.users().generate(u -> u.setLogin("simple-user").setPassword("password"));
-    tester.users().generateAdministrator(u -> u.setLogin(ADMIN_USER_LOGIN).setPassword(ADMIN_USER_LOGIN));
-  }
-
   @After
   public void resetProperties() throws Exception {
     resetSettings(orchestrator, null, "sonar.forceAuthentication");
@@ -77,18 +63,20 @@ public class LocalAuthenticationTest {
 
   @Test
   public void log_in_with_correct_credentials_then_log_out() {
+    User user = tester.users().generate();
     Navigation nav = tester.openBrowser();
     nav.shouldNotBeLoggedIn();
-    nav.logIn().submitCredentials(LOGIN, "123456").shouldBeLoggedIn();
+    nav.logIn().submitCredentials(user.getLogin()).shouldBeLoggedIn();
     nav.logOut().shouldNotBeLoggedIn();
   }
 
   @Test
   public void log_in_with_wrong_credentials() {
+    User user = tester.users().generate();
     Navigation nav = tester.openBrowser();
     LoginPage page = nav
       .logIn()
-      .submitWrongCredentials(LOGIN, "wrong");
+      .submitWrongCredentials(user.getLogin(), "wrong");
     page.getErrorMessage().shouldHave(Condition.text("Authentication failed"));
 
     nav.openHome();
@@ -111,10 +99,11 @@ public class LocalAuthenticationTest {
 
   @Test
   public void basic_authentication_based_on_token() {
+    User user = tester.users().generate();
     String tokenName = "Validate token based authentication";
     UserTokensService tokensService = tester.wsClient().userTokens();
     WsUserTokens.GenerateWsResponse generateWsResponse = tokensService.generate(new GenerateWsRequest()
-      .setLogin(LOGIN)
+      .setLogin(user.getLogin())
       .setName(tokenName));
     WsClient wsClient = WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
       .url(orchestrator.getServer().getUrl())
@@ -124,10 +113,10 @@ public class LocalAuthenticationTest {
 
     assertThat(response.content()).isEqualTo("{\"valid\":true}");
 
-    WsUserTokens.SearchWsResponse searchResponse = tokensService.search(new SearchWsRequest().setLogin(LOGIN));
+    WsUserTokens.SearchWsResponse searchResponse = tokensService.search(new SearchWsRequest().setLogin(user.getLogin()));
     assertThat(searchResponse.getUserTokensCount()).isEqualTo(1);
-    tokensService.revoke(new RevokeWsRequest().setLogin(LOGIN).setName(tokenName));
-    searchResponse = tokensService.search(new SearchWsRequest().setLogin(LOGIN));
+    tokensService.revoke(new RevokeWsRequest().setLogin(user.getLogin()).setName(tokenName));
+    searchResponse = tokensService.search(new SearchWsRequest().setLogin(user.getLogin()));
     assertThat(searchResponse.getUserTokensCount()).isEqualTo(0);
   }
 
@@ -168,6 +157,8 @@ public class LocalAuthenticationTest {
 
   @Test
   public void test_authentication_in_ui() {
+    tester.users().generate(u -> u.setLogin("simple-user").setPassword("password"));
+    tester.users().generateAdministrator(u -> u.setLogin("admin-user").setPassword("admin-user"));
     tester.runHtmlTests(
       "/user/LocalAuthenticationTest/login_successful.html",
       "/user/LocalAuthenticationTest/login_wrong_password.html",
@@ -176,6 +167,8 @@ public class LocalAuthenticationTest {
 
   @Test
   public void test_authentication_redirects_in_ui() {
+    tester.users().generate(u -> u.setLogin("simple-user").setPassword("password"));
+    tester.users().generateAdministrator(u -> u.setLogin("admin-user").setPassword("admin-user"));
     tester.runHtmlTests(
     "/user/LocalAuthenticationTest/redirect_to_login_when_not_enough_privilege.html",
       // SONAR-2132
@@ -187,6 +180,8 @@ public class LocalAuthenticationTest {
 
   @Test
   public void force_authentication_in_ui() {
+    tester.users().generate(u -> u.setLogin("simple-user").setPassword("password"));
+    tester.users().generateAdministrator(u -> u.setLogin("admin-user").setPassword("admin-user"));
     setServerProperty(orchestrator, "sonar.forceAuthentication", "true");
 
     tester.runHtmlTests(
@@ -196,16 +191,18 @@ public class LocalAuthenticationTest {
 
   @Test
   public void authentication_with_authentication_ws() {
-    assertThat(checkAuthenticationWithAuthenticateWebService("admin", "admin")).isTrue();
-    assertThat(checkAuthenticationWithAuthenticateWebService("wrong", "admin")).isFalse();
-    assertThat(checkAuthenticationWithAuthenticateWebService("admin", "wrong")).isFalse();
+    User user = tester.users().generate(u -> u.setLogin("test").setPassword("password"));
+
+    assertThat(checkAuthenticationWithAuthenticateWebService("test", "password")).isTrue();
+    assertThat(checkAuthenticationWithAuthenticateWebService("wrong", "password")).isFalse();
+    assertThat(checkAuthenticationWithAuthenticateWebService("test", "wrong")).isFalse();
     assertThat(checkAuthenticationWithAuthenticateWebService(null, null)).isTrue();
 
     setServerProperty(orchestrator, "sonar.forceAuthentication", "true");
 
-    assertThat(checkAuthenticationWithAuthenticateWebService("admin", "admin")).isTrue();
-    assertThat(checkAuthenticationWithAuthenticateWebService("wrong", "admin")).isFalse();
-    assertThat(checkAuthenticationWithAuthenticateWebService("admin", "wrong")).isFalse();
+    assertThat(checkAuthenticationWithAuthenticateWebService("test", "password")).isTrue();
+    assertThat(checkAuthenticationWithAuthenticateWebService("wrong", "password")).isFalse();
+    assertThat(checkAuthenticationWithAuthenticateWebService("test", "wrong")).isFalse();
     assertThat(checkAuthenticationWithAuthenticateWebService(null, null)).isFalse();
   }
 
@@ -214,18 +211,20 @@ public class LocalAuthenticationTest {
    */
   @Test
   public void authentication_with_any_ws() throws Exception {
-    assertThat(checkAuthenticationWithAnyWS("admin", "admin").code()).isEqualTo(200);
-    assertThat(checkAuthenticationWithAnyWS("wrong", "admin").code()).isEqualTo(401);
-    assertThat(checkAuthenticationWithAnyWS("admin", "wrong").code()).isEqualTo(401);
-    assertThat(checkAuthenticationWithAnyWS("admin", null).code()).isEqualTo(401);
+    tester.users().generate(u -> u.setLogin("test").setPassword("password"));
+
+    assertThat(checkAuthenticationWithAnyWS("test", "password").code()).isEqualTo(200);
+    assertThat(checkAuthenticationWithAnyWS("wrong", "password").code()).isEqualTo(401);
+    assertThat(checkAuthenticationWithAnyWS("test", "wrong").code()).isEqualTo(401);
+    assertThat(checkAuthenticationWithAnyWS("test", null).code()).isEqualTo(401);
     assertThat(checkAuthenticationWithAnyWS(null, null).code()).isEqualTo(200);
 
     setServerProperty(orchestrator, "sonar.forceAuthentication", "true");
 
-    assertThat(checkAuthenticationWithAnyWS("admin", "admin").code()).isEqualTo(200);
-    assertThat(checkAuthenticationWithAnyWS("wrong", "admin").code()).isEqualTo(401);
-    assertThat(checkAuthenticationWithAnyWS("admin", "wrong").code()).isEqualTo(401);
-    assertThat(checkAuthenticationWithAnyWS("admin", null).code()).isEqualTo(401);
+    assertThat(checkAuthenticationWithAnyWS("test", "password").code()).isEqualTo(200);
+    assertThat(checkAuthenticationWithAnyWS("wrong", "password").code()).isEqualTo(401);
+    assertThat(checkAuthenticationWithAnyWS("test", "wrong").code()).isEqualTo(401);
+    assertThat(checkAuthenticationWithAnyWS("test", null).code()).isEqualTo(401);
     assertThat(checkAuthenticationWithAnyWS(null, null).code()).isEqualTo(401);
   }
 
@@ -240,10 +239,4 @@ public class LocalAuthenticationTest {
     return wsClient.wsConnector().call(new GetRequest("api/rules/search"));
   }
 
-  private void addUserPermission(String login, String permission) {
-    tester.wsClient().permissions().addUser(new AddUserWsRequest()
-      .setLogin(login)
-      .setPermission(permission));
-  }
-
 }