-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backend] Fix password reset for users who are not logged in (#1963)
- Loading branch information
1 parent
f257e8e
commit 3430fe2
Showing
18 changed files
with
150 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ | |
|
||
import static io.openbas.utils.JsonUtils.asJsonString; | ||
import static io.openbas.utils.fixtures.UserFixture.EMAIL; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
@@ -11,10 +16,15 @@ | |
import io.openbas.database.model.User; | ||
import io.openbas.database.repository.UserRepository; | ||
import io.openbas.rest.user.form.login.LoginUserInput; | ||
import io.openbas.rest.user.form.login.ResetUserInput; | ||
import io.openbas.rest.user.form.user.CreateUserInput; | ||
import io.openbas.service.MailingService; | ||
import io.openbas.utils.fixtures.UserFixture; | ||
import java.util.List; | ||
import org.junit.jupiter.api.*; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
@@ -28,6 +38,8 @@ class UserApiTest extends IntegrationTest { | |
|
||
@Autowired private UserRepository userRepository; | ||
|
||
@MockBean private MailingService mailingService; | ||
|
||
@BeforeAll | ||
public void setup() { | ||
// Create user | ||
|
@@ -143,4 +155,45 @@ void given_known_create_user_in_uppercase_input_should_return_conflict() throws | |
.andExpect(status().isConflict()); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("Reset Password from I forget my pwd option") | ||
class ResetPassword { | ||
@DisplayName("With a known email") | ||
@Test | ||
void resetPassword() throws Exception { | ||
// -- PREPARE -- | ||
ResetUserInput input = UserFixture.getResetUserInput(); | ||
|
||
// -- EXECUTE -- | ||
mvc.perform( | ||
post("/api/reset") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(asJsonString(input))) | ||
.andExpect(status().isOk()); | ||
|
||
// -- ASSERT -- | ||
ArgumentCaptor<List<User>> userCaptor = ArgumentCaptor.forClass(List.class); | ||
verify(mailingService).sendEmail(anyString(), anyString(), userCaptor.capture()); | ||
assertEquals(EMAIL, userCaptor.getValue().get(0).getEmail()); | ||
} | ||
|
||
@DisplayName("With a unknown email") | ||
@Test | ||
void resetPasswordWithUnknownEmail() throws Exception { | ||
// -- PREPARE -- | ||
ResetUserInput input = UserFixture.getResetUserInput(); | ||
input.setLogin("[email protected]"); | ||
|
||
// -- EXECUTE -- | ||
mvc.perform( | ||
post("/api/reset") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(asJsonString(input))) | ||
.andExpect(status().isBadRequest()); | ||
|
||
// -- ASSERT -- | ||
verify(mailingService, never()).sendEmail(anyString(), anyString(), any(List.class)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.