@Test public void create() { String avatar = underTest.create(newUserDto("john", "John", "john@doo.com")); assertThat(avatar).isEqualTo("9297bfb538f650da6143b604e82a355d"); }
@Test public void return_true_when_jwt_token_is_set() throws Exception { when(jwtHttpHandler.validateToken(request, response)).thenReturn(Optional.of(newUserDto())); when(basicAuthentication.authenticate(request)).thenReturn(Optional.empty()); underTest.doFilter(request, response, chain); verify(response).setContentType(MediaTypes.JSON); JsonAssert.assertJson(stringWriter.toString()).isSimilarTo("{\"valid\":true}"); }
private UserDto insertUser(boolean active) { UserDto dto = newUserDto().setActive(active); underTest.insert(session, dto); return dto; }
@Test public void redirect_when_failing_because_of_EmailAlreadyExistException() throws Exception { UserDto existingUser = newUserDto().setEmail("john@email.com").setExternalLogin("john.bitbucket").setExternalIdentityProvider("bitbucket"); FailWithEmailAlreadyExistException identityProvider = new FailWithEmailAlreadyExistException(existingUser); when(request.getRequestURI()).thenReturn("/oauth2/callback/" + identityProvider.getKey()); identityProviderRepository.addIdentityProvider(identityProvider); underTest.doFilter(request, response, chain); verify(response).sendRedirect("/sessions/email_already_exists?email=john%40email.com&login=john.github&provider=failing&existingLogin=john.bitbucket&existingProvider=bitbucket"); verify(oAuthRedirection).delete(eq(request), eq(response)); }
@Test public void execute_makes_user_with_specified_login_not_root_when_it_is() { UserDto otherUser = insertRootUser(newUserDto()); insertRootUser(newUserDto(SOME_LOGIN, "name", "email")); logInAsRoot(); executeRequest(SOME_LOGIN); assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isFalse(); assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isTrue(); }
@Test public void change_assignee() { UserDto user = newUserDto().setLogin("emmerik").setName("Emmerik"); issue.setAssigneeUuid("user_uuid"); boolean updated = underTest.assign(issue, user, context); assertThat(updated).isTrue(); assertThat(issue.assignee()).isEqualTo(user.getUuid()); assertThat(issue.mustSendNotifications()).isTrue(); FieldDiffs.Diff diff = issue.currentChange().get(ASSIGNEE); assertThat(diff.oldValue()).isEqualTo(UNUSED); assertThat(diff.newValue()).isEqualTo(user.getName()); }
@Test public void execute_has_no_effect_when_user_is_already_not_root() { UserDto otherUser = insertRootUser(newUserDto()); insertNonRootUser(newUserDto(SOME_LOGIN, "name", "email")); logInAsRoot(); executeRequest(SOME_LOGIN); assertThat(userDao.selectByLogin(dbSession, SOME_LOGIN).isRoot()).isFalse(); assertThat(userDao.selectByLogin(dbSession, otherUser.getLogin()).isRoot()).isTrue(); }
private UserDto insertUser() { return db.users().insertUser(newUserDto() .setActive(true) .setEmail("john@email.com") .setLogin(USER_LOGIN) .setName("John") .setScmAccounts(singletonList("jn"))); }
public static UserDto newDisabledUser() { return newUserDto() .setActive(false) // All these fields are reset when disabling a user .setScmAccounts((String) null) .setEmail(null) .setCryptedPassword(null) .setSalt(null); }
@Test public void null_hash_should_throw_AuthenticationException() { UserDto user = newUserDto(); expectedException.expect(AuthenticationException.class); expectedException.expectMessage("null hash method"); underTest.authenticate(db.getSession(), user, "whatever", AuthenticationEvent.Method.BASIC); }
public static UserDto newLocalUser(String login, String name, @Nullable String email) { return newUserDto() .setLocal(true) .setName(name) .setEmail(email) .setLogin(login) .setExternalId(login) .setExternalLogin(login) .setExternalIdentityProvider("sonarqube"); }
@Test public void clean_user_homepage() { UserDto user = newUserDto().setHomepageType("RANDOM").setHomepageParameter("any-string"); underTest.insert(session, user); session.commit(); underTest.cleanHomepage(session, user); UserDto reloaded = underTest.selectUserById(session, user.getId()); assertThat(reloaded.getUpdatedAt()).isEqualTo(NOW); assertThat(reloaded.getHomepageType()).isNull(); assertThat(reloaded.getHomepageParameter()).isNull(); }
@Test public void authenticate_local_user() { insertUser(newUserDto() .setLogin(LOGIN) .setCryptedPassword(ENCRYPTED_PASSWORD) .setHashMethod(CredentialsLocalAuthentication.HashMethod.SHA1.name()) .setSalt(SALT) .setLocal(true)); UserDto userDto = executeAuthenticate(BASIC); assertThat(userDto.getLogin()).isEqualTo(LOGIN); verify(authenticationEvent).loginSuccess(request, LOGIN, Source.local(BASIC)); }
@Test public void execute_fails_with_BadRequestException_when_attempting_to_unset_non_root_and_there_is_no_root_at_all() { UserDto userDto1 = newUserDto(SOME_LOGIN, "name", "email"); insertNonRootUser(userDto1); logInAsRoot(); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Last root can't be unset"); executeRequest(userDto1.getLogin()); }
public static UserDto newExternalUser(String login, String name, @Nullable String email) { return newUserDto() .setLocal(false) .setName(name) .setEmail(email) .setLogin(login) .setExternalId(randomAlphanumeric(40)) .setExternalLogin(randomAlphanumeric(40)) .setExternalIdentityProvider(randomAlphanumeric(40)); }
private void insertUsersHavingGlobalPermissions() { UserDto user1 = db.users().insertUser(newUserDto("login-1", "name-1", "email-1")); db.organizations().addMember(db.getDefaultOrganization(), user1); UserDto user2 = db.users().insertUser(newUserDto("login-2", "name-2", "email-2")); db.organizations().addMember(db.getDefaultOrganization(), user2); UserDto user3 = db.users().insertUser(newUserDto("login-3", "name-3", "email-3")); db.organizations().addMember(db.getDefaultOrganization(), user3); db.users().insertPermissionOnUser(user1, SCAN); db.users().insertPermissionOnUser(user2, SCAN); db.users().insertPermissionOnUser(user3, ADMINISTER); }
@Test public void execute_fails_with_NotFoundException_when_user_for_specified_login_is_not_active() { UserDto userDto = UserTesting.newUserDto().setActive(false); userDao.insert(dbSession, userDto); dbSession.commit(); logInAsRoot(); expectedException.expect(NotFoundException.class); expectedException.expectMessage("User with login '" + userDto.getLogin() + "' not found"); executeRequest(userDto.getLogin()); }
@Test public void does_not_delete_user_settings() { UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto()); propertyDb.insertProperties(newUserPropertyDto("foo", "one", user)); propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one")); underTest.deleteGlobalSettings(dbSession, "foo"); assertUserPropertyExists("foo", user); }
@Test public void authentication_with_sha1_with_empty_salt_should_throw_AuthenticationException() { String password = randomAlphanumeric(60); UserDto user = newUserDto() .setHashMethod(SHA1.name()) .setCryptedPassword(DigestUtils.sha1Hex("--0242b0b4c0a93ddfe09dd886de50bc25ba000b51--" + password + "--")) .setSalt(null); expectedException.expect(AuthenticationException.class); expectedException.expectMessage("null salt"); underTest.authenticate(db.getSession(), user, "WHATEVER", AuthenticationEvent.Method.BASIC); }
@Test public void test_response_example() { logInAsRoot(); UserDto user = UserTesting.newUserDto().setLogin("daniel").setName("Daniel").setEmail("daniel@corp.com"); UserDto rootDto = userDao.insert(dbSession, user); userDao.setRoot(dbSession, rootDto.getLogin(), true); dbSession.commit(); TestResponse response = wsTester.newRequest().setMediaType(MediaTypes.JSON).execute(); assertJson(response.getInput()).isSimilarTo(wsTester.getDef().responseExampleAsString()); }