]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9356 skipOnboardingTutorial updates user flag
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 12 Jun 2017 09:16:11 +0000 (11:16 +0200)
committerStas Vilchik <stas.vilchik@sonarsource.com>
Tue, 20 Jun 2017 11:10:53 +0000 (04:10 -0700)
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java
server/sonar-server/src/main/java/org/sonar/server/user/ws/SkipOnboardingTutorialAction.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/SkipOnboardingTutorialActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersWsParameters.java

index f45573f7dc54789fe31980c0d9063c2953a9f9d6..ecc767b006ff73adbfc0ecc088d1963f6127f65e 100644 (file)
@@ -45,6 +45,7 @@ public class UserDao implements Dao {
     this.system2 = system2;
   }
 
+  @CheckForNull
   public UserDto selectUserById(DbSession session, int userId) {
     return mapper(session).selectUser(userId);
   }
index 39359a9f153bcb6df22f3768a708b0223638a676..003208121d2bc3127f136b95465e685813df9f72 100644 (file)
@@ -22,22 +22,52 @@ package org.sonar.server.user.ws;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.user.UserSession;
+
+import static com.google.common.base.Preconditions.checkState;
+import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_SKIP_ONBOARDING_TUTORIAL;
 
 public class SkipOnboardingTutorialAction implements UsersWsAction {
 
+  private final UserSession userSession;
+  private final DbClient dbClient;
+  private final System2 system2;
+
+  public SkipOnboardingTutorialAction(UserSession userSession, DbClient dbClient, System2 system2) {
+    this.userSession = userSession;
+    this.dbClient = dbClient;
+    this.system2 = system2;
+  }
+
   @Override
   public void define(WebService.NewController context) {
-    context.createAction("skipOnboardingTutorial")
+    context.createAction(ACTION_SKIP_ONBOARDING_TUTORIAL)
       .setPost(true)
       .setInternal(true)
-      .setDescription("Stores that the user has skipped the onboarding tutorial and does not want to see it after future logins." +
-        " Calling this webservice several times will silently ignore subsequent requests.")
+      .setDescription("Stores that the user has skipped the onboarding tutorial and does not want to see it after future logins.<br/>" +
+        "Requires authentication.")
       .setSince("6.5")
       .setHandler(this);
   }
 
   @Override
   public void handle(Request request, Response response) throws Exception {
+    userSession.checkLoggedIn();
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      String userLogin = userSession.getLogin();
+      UserDto userDto = dbClient.userDao().selectActiveUserByLogin(dbSession, userLogin);
+      checkState(userDto != null, "User login '%s' cannot be found", userLogin);
+      if (!userDto.isOnboarded()) {
+        userDto.setOnboarded(true);
+        userDto.setUpdatedAt(system2.now());
+        dbClient.userDao().update(dbSession, userDto);
+        dbSession.commit();
+      }
+    }
     response.noContent();
   }
 }
index 85985b76ec05bb65a27bbc3a3c83c08860410e9a..df97cd664deb7217d455870f4ca0e6bb256f2c48 100644 (file)
  */
 package org.sonar.server.user.ws;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.db.DbTester;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 
@@ -28,21 +35,100 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 public class SkipOnboardingTutorialActionTest {
 
+  private final static long PAST = 100_000_000_000L;
+  private final static long NOW = 500_000_000_000L;
+
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+
+  @Rule
+  public DbTester db = DbTester.create();
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private TestSystem2 system2 = new TestSystem2().setNow(NOW);
+
+  private WsActionTester ws = new WsActionTester(new SkipOnboardingTutorialAction(userSession, db.getDbClient(), system2));
+
+  @Test
+  public void mark_user_as_onboarded() {
+    UserDto user = db.users().insertUser(u -> u
+      .setOnboarded(false)
+      .setUpdatedAt(PAST));
+    userSession.logIn(user);
+
+    call();
+
+    UserDto userDto = selectUser(user.getLogin());
+    assertThat(userDto.isOnboarded()).isEqualTo(true);
+    assertThat(userDto.getUpdatedAt()).isEqualTo(NOW);
+  }
+
+  @Test
+  public void does_nothing_if_user_already_onboarded() {
+    UserDto user = db.users().insertUser(u -> u
+      .setOnboarded(true)
+      .setUpdatedAt(PAST));
+    userSession.logIn(user);
+
+    call();
+
+    UserDto userDto = selectUser(user.getLogin());
+    assertThat(userDto.isOnboarded()).isEqualTo(true);
+    assertThat(userDto.getUpdatedAt()).isEqualTo(PAST);
+  }
+
+  @Test
+  public void fail_for_anonymous() {
+    userSession.anonymous();
+    expectedException.expect(UnauthorizedException.class);
+    expectedException.expectMessage("Authentication is required");
+
+    call();
+  }
+
+  @Test
+  public void fail_with_ISE_when_user_login_in_db_does_not_exist() {
+    db.users().insertUser(usert -> usert.setLogin("another"));
+    userSession.logIn("obiwan.kenobi");
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("User login 'obiwan.kenobi' cannot be found");
+
+    call();
+  }
+
   @Test
-  public void should_have_a_good_definition() {
-    WsActionTester ws = new WsActionTester(new SkipOnboardingTutorialAction());
+  public void response_has_no_content() {
+    UserDto user = db.users().insertUser(u -> u.setOnboarded(false));
+    userSession.logIn(user);
+
+    TestResponse response = call();
+
+    assertThat(response.getStatus()).isEqualTo(204);
+    assertThat(response.getInput()).isEmpty();
+  }
+
+  @Test
+  public void test_definition() {
+    WsActionTester ws = new WsActionTester(new SkipOnboardingTutorialAction(userSession, db.getDbClient(), system2));
     WebService.Action def = ws.getDef();
     assertThat(def.isPost()).isTrue();
     assertThat(def.isInternal()).isTrue();
     assertThat(def.since()).isEqualTo("6.5");
     assertThat(def.params()).isEmpty();
+    assertThat(def.changelog()).isEmpty();
   }
 
-  @Test
-  public void should_return_silently() {
-    WsActionTester ws = new WsActionTester(new SkipOnboardingTutorialAction());
-    TestResponse response = ws.newRequest().setMethod("POST").execute();
-    assertThat(response.getStatus()).isEqualTo(204);
-    assertThat(response.getInput()).isEmpty();
+  private TestResponse call() {
+    return ws.newRequest().setMethod("POST").execute();
+  }
+
+  private UserDto selectUser(String userLogin) {
+    UserDto userDto = db.getDbClient().userDao().selectByLogin(db.getSession(), userLogin);
+    assertThat(userDto).isNotNull();
+    return userDto;
   }
+
 }
index c79994c1a40a1f511526ac6568454a584428eca3..6ffecd751b0ce0f1604f0c9c4e15b1f44afbcab7 100644 (file)
@@ -27,7 +27,7 @@ public class UsersWsParameters {
   public static final String ACTION_CREATE = "create";
   public static final String ACTION_UPDATE = "update";
   public static final String ACTION_GROUPS = "groups";
-  public static final String ACTION_SKIP_ONBOARDING_TUTORIAL = "skipOnboardingTutorial";
+  public static final String ACTION_SKIP_ONBOARDING_TUTORIAL = "skip_onboarding_tutorial";
 
   public static final String PARAM_ORGANIZATION = "organization";
   public static final String PARAM_LOGIN = "login";