Search in sources :

Example 1 with EditUserDTO

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());
}
Also used : EditUserDTO(com.github.nkonev.aaa.dto.EditUserDTO) MvcResult(org.springframework.test.web.servlet.MvcResult) UserAccount(com.github.nkonev.aaa.entity.jdbc.UserAccount) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 2 with EditUserDTO

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());
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(javax.mail.Message) IMAPMessage(com.sun.mail.imap.IMAPMessage) EditUserDTO(com.github.nkonev.aaa.dto.EditUserDTO) IMAPMessage(com.sun.mail.imap.IMAPMessage) MvcResult(org.springframework.test.web.servlet.MvcResult) URI(java.net.URI) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 3 with EditUserDTO

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());
}
Also used : UserAccountDTO(com.github.nkonev.aaa.dto.UserAccountDTO) EditUserDTO(com.github.nkonev.aaa.dto.EditUserDTO) MvcResult(org.springframework.test.web.servlet.MvcResult) UserAccount(com.github.nkonev.aaa.entity.jdbc.UserAccount) Test(org.junit.jupiter.api.Test) WithUserDetails(org.springframework.security.test.context.support.WithUserDetails)

Example 4 with EditUserDTO

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());
}
Also used : EditUserDTO(com.github.nkonev.aaa.dto.EditUserDTO) MvcResult(org.springframework.test.web.servlet.MvcResult) UserAccount(com.github.nkonev.aaa.entity.jdbc.UserAccount) Test(org.junit.jupiter.api.Test) WithUserDetails(org.springframework.security.test.context.support.WithUserDetails)

Example 5 with EditUserDTO

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);
}
Also used : EditUserDTO(com.github.nkonev.aaa.dto.EditUserDTO) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

EditUserDTO (com.github.nkonev.aaa.dto.EditUserDTO)11 Test (org.junit.jupiter.api.Test)11 MvcResult (org.springframework.test.web.servlet.MvcResult)11 UserAccount (com.github.nkonev.aaa.entity.jdbc.UserAccount)7 WithUserDetails (org.springframework.security.test.context.support.WithUserDetails)6 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)5 UserAccountDTO (com.github.nkonev.aaa.dto.UserAccountDTO)1 Retriever (com.icegreen.greenmail.util.Retriever)1 IMAPMessage (com.sun.mail.imap.IMAPMessage)1 URI (java.net.URI)1 Map (java.util.Map)1 Message (javax.mail.Message)1