use of com.github.nkonev.aaa.entity.jdbc.UserAccount in project videochat by nkonev.
the class RegistrationControllerTest method testAttackerCannotStealLockedUserAccount.
@Test
public void testAttackerCannotStealLockedUserAccount() throws Exception {
String bobEmail = "bob@example.com";
UserAccount bob = userAccountRepository.findByEmail(bobEmail).orElseThrow(() -> new RuntimeException("bob not found in test"));
bob = bob.withLocked(true);
bob = userAccountRepository.save(bob);
// attacker
long tokenCountBeforeResend = userConfirmationTokenRepository.count();
mockMvc.perform(post(Constants.Urls.API + Constants.Urls.RESEND_CONFIRMATION_EMAIL + "?email=" + bobEmail).with(csrf())).andExpect(status().isOk());
Assertions.assertEquals(tokenCountBeforeResend, userConfirmationTokenRepository.count(), "new token shouldn't appear when attacker attempts reactivate banned(locked) user");
}
use of com.github.nkonev.aaa.entity.jdbc.UserAccount in project videochat by nkonev.
the class RegistrationControllerTest method testRegistrationUserWithSameEmailAlreadyPresent.
@Test
public void testRegistrationUserWithSameEmailAlreadyPresent() throws Exception {
final String email = "alice@example.com";
final String username = "newbie";
final String password = "password";
UserAccount userAccountBefore = userAccountRepository.findByEmail(email).orElseThrow(() -> new RuntimeException("user account not found in test"));
EditUserDTO createUserDTO = new EditUserDTO(username, null, null, password, email);
// register
MvcResult createAccountResult = mockMvc.perform(post(Constants.Urls.API + Constants.Urls.REGISTER).content(objectMapper.writeValueAsString(createUserDTO)).contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).with(csrf())).andExpect(status().isOk()).andReturn();
String stringResponse = createAccountResult.getResponse().getContentAsString();
LOGGER.info(stringResponse);
UserAccount userAccountAfter = userAccountRepository.findByEmail(email).orElseThrow(() -> new RuntimeException("user account not found in test"));
// check that initial user account is not affected
Assertions.assertEquals(userAccountBefore.id(), userAccountAfter.id());
Assertions.assertEquals(userAccountBefore.avatar(), userAccountAfter.avatar());
Assertions.assertEquals(TestConstants.USER_ALICE, userAccountBefore.username());
Assertions.assertEquals(userAccountBefore.username(), userAccountAfter.username());
Assertions.assertEquals(userAccountBefore.password(), userAccountAfter.password());
Assertions.assertEquals(userAccountBefore.role(), userAccountAfter.role());
}
use of com.github.nkonev.aaa.entity.jdbc.UserAccount in project videochat by nkonev.
the class UserProfileControllerTest method fullyAuthenticatedUserCanChangeHerProfile.
@WithUserDetails(TestConstants.USER_ALICE)
@org.junit.jupiter.api.Test
public void fullyAuthenticatedUserCanChangeHerProfile() throws Exception {
receiver.clear();
UserAccount userAccount = getUserFromBd(TestConstants.USER_ALICE);
final String initialPassword = userAccount.password();
final String newLogin = "new_alice";
EditUserDTO edit = UserAccountConverter.convertToEditUserDto(userAccount);
edit = edit.withLogin(newLogin);
MvcResult mvcResult = mockMvc.perform(post(Constants.Urls.API + Constants.Urls.PROFILE).content(objectMapper.writeValueAsString(edit)).contentType(MediaType.APPLICATION_JSON_UTF8).with(csrf())).andExpect(status().isOk()).andExpect(jsonPath("$.login").value(newLogin)).andExpect(jsonPath("$.password").doesNotExist()).andReturn();
LOGGER.info(mvcResult.getResponse().getContentAsString());
Assertions.assertEquals(initialPassword, getUserFromBd(newLogin).password(), "password shouldn't be affected if there isn't set explicitly");
MvcResult getPostsRequest = mockMvc.perform(get(Constants.Urls.API + Constants.Urls.PROFILE)).andExpect(status().isOk()).andExpect(jsonPath("$.login").value(newLogin)).andExpect(jsonPath("$.password").doesNotExist()).andReturn();
for (int i = 0; i < 10; ++i) {
if (receiver.size() > 0) {
break;
} else {
Uninterruptibles.sleepUninterruptibly(Duration.of(1, ChronoUnit.SECONDS));
}
}
Assertions.assertEquals(1, receiver.size());
final UserAccountDTO userAccountEvent = receiver.getLast();
Assertions.assertEquals(newLogin, userAccountEvent.login());
}
use of com.github.nkonev.aaa.entity.jdbc.UserAccount in project videochat by nkonev.
the class UserProfileControllerTest method createUserForDelete.
private long createUserForDelete(String login) {
UserAccount userAccount = new UserAccount(null, CreationType.REGISTRATION, login, null, null, null, false, false, true, UserRole.ROLE_USER, login + "@example.com", null, null);
userAccount = userAccountRepository.save(userAccount);
return userAccount.id();
}
use of com.github.nkonev.aaa.entity.jdbc.UserAccount in project videochat by nkonev.
the class UserProfileControllerTest method fullyAuthenticatedUserCannotBringForeignEmail.
@WithUserDetails(TestConstants.USER_ALICE)
@Test
public void fullyAuthenticatedUserCannotBringForeignEmail() throws Exception {
UserAccount userAccount = getUserFromBd(TestConstants.USER_ALICE);
final String newEmail = TestConstants.USER_BOB + "@example.com";
final Optional<UserAccount> foreignBobAccountOptional = userAccountRepository.findByEmail(newEmail);
final UserAccount foreignBobAccount = foreignBobAccountOptional.orElseThrow(() -> new RuntimeException("foreign email '" + newEmail + "' must be present"));
final long foreingId = foreignBobAccount.id();
final String foreignPassword = foreignBobAccount.password();
final String foreignEmail = foreignBobAccount.email();
EditUserDTO edit = UserAccountConverter.convertToEditUserDto(userAccount);
edit = edit.withEmail(newEmail);
MvcResult mvcResult = mockMvc.perform(post(Constants.Urls.API + Constants.Urls.PROFILE).content(objectMapper.writeValueAsString(edit)).contentType(MediaType.APPLICATION_JSON_UTF8).with(csrf())).andExpect(// we care for emails
status().isOk()).andReturn();
LOGGER.info(mvcResult.getResponse().getContentAsString());
UserAccount foreignAccountAfter = getUserFromBd(TestConstants.USER_BOB);
Assertions.assertEquals(foreingId, foreignAccountAfter.id().longValue());
Assertions.assertEquals(foreignEmail, foreignAccountAfter.email());
Assertions.assertEquals(foreignPassword, foreignAccountAfter.password());
}
Aggregations