use of com.github.nkonev.aaa.dto.EditUserDTO 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.dto.EditUserDTO in project videochat by nkonev.
the class RegistrationControllerTest method testConfirmationSuccess.
@Test
public void testConfirmationSuccess() throws Exception {
final String email = "newbie@example.com";
final String username = "newbie";
final String password = "password";
EditUserDTO createUserDTO = new EditUserDTO(username, null, null, password, email);
// register
MvcResult createAccountRequest = mockMvc.perform(MockMvcRequestBuilders.post(Constants.Urls.API + Constants.Urls.REGISTER).content(objectMapper.writeValueAsString(createUserDTO)).contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).with(csrf())).andExpect(status().isOk()).andReturn();
String createAccountStr = createAccountRequest.getResponse().getContentAsString();
LOGGER.info(createAccountStr);
// login unconfirmed fail
mockMvc.perform(MockMvcRequestBuilders.post(SecurityConfig.API_LOGIN_URL).contentType(MediaType.APPLICATION_FORM_URLENCODED).param(SecurityConfig.USERNAME_PARAMETER, username).param(SecurityConfig.PASSWORD_PARAMETER, password).with(csrf())).andExpect(status().isUnauthorized());
// user lost email and reissues token
{
long tokenCountBeforeResend = userConfirmationTokenRepository.count();
mockMvc.perform(post(Constants.Urls.API + Constants.Urls.RESEND_CONFIRMATION_EMAIL + "?email=" + email).with(csrf())).andExpect(status().isOk());
Assertions.assertEquals(tokenCountBeforeResend + 1, userConfirmationTokenRepository.count());
}
// http://www.icegreen.com/greenmail/javadocs/com/icegreen/greenmail/util/Retriever.html
try (Retriever r = new Retriever(greenMail.getImap())) {
Message[] messages = r.getMessages(email);
Assertions.assertEquals(2, messages.length, "backend should sent two email: a) during registration; b) during confirmation token reissue");
IMAPMessage imapMessage = (IMAPMessage) messages[1];
String content = (String) imapMessage.getContent();
String parsedUrl = UrlParser.parseUrlFromMessage(content);
String tokenUuidString = UriComponentsBuilder.fromUri(new URI(parsedUrl)).build().getQueryParams().get(Constants.Urls.UUID).get(0);
Assertions.assertTrue(userConfirmationTokenRepository.existsById(tokenUuidString));
// perform confirm
mockMvc.perform(get(parsedUrl)).andExpect(status().isOk());
Assertions.assertFalse(userConfirmationTokenRepository.existsById(tokenUuidString));
}
// login confirmed ok
mockMvc.perform(post(SecurityConfig.API_LOGIN_URL).contentType(MediaType.APPLICATION_FORM_URLENCODED).param(SecurityConfig.USERNAME_PARAMETER, username).param(SecurityConfig.PASSWORD_PARAMETER, password).with(csrf())).andExpect(status().isOk());
// resend for already confirmed does nothing
{
long tokenCountBeforeResend = userConfirmationTokenRepository.count();
mockMvc.perform(post(Constants.Urls.API + Constants.Urls.RESEND_CONFIRMATION_EMAIL + "?email=" + email).with(csrf())).andExpect(status().isOk());
Assertions.assertEquals(tokenCountBeforeResend, userConfirmationTokenRepository.count());
}
}
use of com.github.nkonev.aaa.dto.EditUserDTO 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.dto.EditUserDTO 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());
}
use of com.github.nkonev.aaa.dto.EditUserDTO in project videochat by nkonev.
the class RegistrationControllerTest method testRegistrationUserWithSameLoginAlreadyPresent.
@Test
public void testRegistrationUserWithSameLoginAlreadyPresent() throws Exception {
final String email = "newbie@example.com";
final String username = TestConstants.USER_ALICE;
final String password = "password";
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().isForbidden()).andExpect(jsonPath("$.error").value("user already present")).andExpect(jsonPath("$.message").value("User with login 'alice' is already present")).andReturn();
String stringResponse = createAccountResult.getResponse().getContentAsString();
LOGGER.info(stringResponse);
}
Aggregations