This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move PasswordResetToken endpoints into their own folder and controller (
#483)
- Loading branch information
1 parent
bfa626a
commit 453e614
Showing
23 changed files
with
341 additions
and
182 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
20 changes: 17 additions & 3 deletions
20
...est/team/password/PasswordResetToken.java → ...sword_reset_token/PasswordResetToken.java
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
52 changes: 52 additions & 0 deletions
52
...main/java/com/ford/labs/retroquest/password_reset_token/PasswordResetTokenController.java
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2022 Ford Motor Company | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.ford.labs.retroquest.password_reset_token; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping(value = "/api/password-reset-token") | ||
@Tag(name = "Password Reset Token Controller", description = "The controller that manages password reset tokens") | ||
public class PasswordResetTokenController { | ||
|
||
private final PasswordResetTokenRepository passwordResetRepository; | ||
|
||
public PasswordResetTokenController(PasswordResetTokenRepository passwordResetRepository) { | ||
this.passwordResetRepository = passwordResetRepository; | ||
} | ||
|
||
@GetMapping("/lifetime-in-seconds") | ||
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) | ||
@Operation(description = "Get the number of seconds before a password reset token expires") | ||
public ResponseEntity<Integer> getResetTokenValiditySeconds(@Value("${retroquest.password.reset.token-lifetime-seconds:600}") int tokenSeconds){ | ||
return ResponseEntity.ok(tokenSeconds); | ||
} | ||
|
||
@GetMapping("/{passwordResetToken}/is-valid") | ||
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) | ||
@Operation(description = "Check if the reset token is valid") | ||
public boolean checkResetTokenStatus(@PathVariable("passwordResetToken") String passwordResetToken) { | ||
PasswordResetToken token = passwordResetRepository.findByResetToken(passwordResetToken); | ||
return token != null && !token.isExpired(); | ||
} | ||
} |
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
122 changes: 122 additions & 0 deletions
122
api/src/test/java/com/ford/labs/retroquest/api/PasswordResetTokenApiTest.java
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (c) 2022 Ford Motor Company | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.ford.labs.retroquest.api; | ||
|
||
import com.ford.labs.retroquest.api.setup.ApiTestBase; | ||
import com.ford.labs.retroquest.password_reset_token.PasswordResetToken; | ||
import com.ford.labs.retroquest.password_reset_token.PasswordResetTokenRepository; | ||
import com.ford.labs.retroquest.team.Team; | ||
import com.ford.labs.retroquest.team.TeamRepository; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@Tag("api") | ||
class PasswordResetTokenApiTest extends ApiTestBase { | ||
|
||
@Autowired | ||
private PasswordResetTokenRepository passwordResetRepository; | ||
|
||
@Autowired | ||
private TeamRepository teamRepository; | ||
|
||
@Autowired | ||
PasswordEncoder passwordEncoder; | ||
|
||
private final String passwordResetTokenRootPath = "/api/password-reset-token"; | ||
|
||
private Team team; | ||
|
||
@BeforeEach | ||
void beforeClass() { | ||
clean(); | ||
team = new Team("teamuri", "TeamName", "%$&357", "[email protected]"); | ||
teamRepository.save(team); | ||
} | ||
|
||
@AfterEach | ||
void clean() { | ||
passwordResetRepository.deleteAllInBatch(); | ||
teamRepository.deleteAllInBatch(); | ||
assertThat(passwordResetRepository.count()).isZero(); | ||
assertThat(teamRepository.count()).isZero(); | ||
} | ||
|
||
@Test | ||
public void get_reset_token_validity_seconds__shouldReportCorrectPasswordTokenExpirationTime() throws Exception { | ||
String tokenLifeTimePath = passwordResetTokenRootPath + "/lifetime-in-seconds"; | ||
MvcResult mvcResult = mockMvc.perform(get(tokenLifeTimePath)).andExpect(status().isOk()).andReturn(); | ||
assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo("600"); | ||
} | ||
|
||
@Test | ||
void check_reset_token_status__should_return_true_when_password_reset_token_is_valid() throws Exception { | ||
PasswordResetToken passwordResetToken = new PasswordResetToken(); | ||
passwordResetToken.setTeam(team); | ||
passwordResetRepository.save(passwordResetToken); | ||
|
||
var mvcResult = mockMvc.perform(get(getIsResetTokenValidPath(passwordResetToken))) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn(); | ||
|
||
assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo("true"); | ||
} | ||
|
||
@Test | ||
void check_reset_token_status__should_return_false_when_password_reset_token_is_expired() throws Exception { | ||
PasswordResetToken passwordResetToken = new PasswordResetToken(); | ||
passwordResetToken.setDateCreated(LocalDateTime.MIN); | ||
passwordResetToken.setTeam(team); | ||
passwordResetRepository.save(passwordResetToken); | ||
|
||
var mvcResult = mockMvc.perform(get(getIsResetTokenValidPath(passwordResetToken))) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn(); | ||
|
||
assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo("false"); | ||
} | ||
|
||
@Test | ||
void check_reset_token_status__should_return_false_when_password_reset_token_does_not_exist() throws Exception { | ||
PasswordResetToken passwordResetToken = new PasswordResetToken(); | ||
passwordResetToken.setDateCreated(LocalDateTime.MIN); | ||
passwordResetToken.setTeam(team); | ||
passwordResetRepository.save(passwordResetToken); | ||
|
||
var mvcResult = mockMvc.perform(get(getIsResetTokenValidPath(passwordResetToken))) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn(); | ||
|
||
assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo("false"); | ||
} | ||
|
||
private String getIsResetTokenValidPath(PasswordResetToken passwordResetToken) { | ||
return passwordResetTokenRootPath + "/" + passwordResetToken.getResetToken() + "/is-valid"; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -20,8 +20,8 @@ | |
import com.ford.labs.retroquest.api.setup.ApiTestBase; | ||
import com.ford.labs.retroquest.column.ColumnRepository; | ||
import com.ford.labs.retroquest.team.*; | ||
import com.ford.labs.retroquest.team.password.PasswordResetToken; | ||
import com.ford.labs.retroquest.team.password.PasswordResetTokenRepository; | ||
import com.ford.labs.retroquest.password_reset_token.PasswordResetToken; | ||
import com.ford.labs.retroquest.password_reset_token.PasswordResetTokenRepository; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import org.junit.jupiter.api.*; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
|
@@ -210,77 +210,6 @@ void should_create_team_with_valid_name_and_password_and_two_emails() throws Exc | |
assertThat(mvcResult.getResponse().getContentAsString()).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void shouldReportCorrectPasswordTokenExpirationTime() throws Exception { | ||
MvcResult mvcResult = mockMvc.perform(get("/api/password/reset/token-lifetime-seconds")).andExpect(status().isOk()).andReturn(); | ||
assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo("600"); | ||
} | ||
|
||
@Test | ||
void should_return_true_when_password_reset_token_is_valid() throws Exception { | ||
Team team = new Team("teamuri", "TeamName", "%$&357", "[email protected]"); | ||
teamRepository.save(team); | ||
PasswordResetToken passwordResetToken = new PasswordResetToken(); | ||
passwordResetToken.setTeam(team); | ||
passwordResetRepository.save(passwordResetToken); | ||
|
||
var mvcResult = mockMvc.perform( | ||
post("/api/password/reset/is-valid") | ||
.contentType(APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsBytes( | ||
new ResetTokenStatusRequest(passwordResetToken.getResetToken())) | ||
) | ||
) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn(); | ||
|
||
assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo("true"); | ||
} | ||
|
||
@Test | ||
void should_return_false_when_password_reset_token_is_expired() throws Exception { | ||
Team team = new Team("teamuri", "TeamName", "%$&357", "[email protected]"); | ||
teamRepository.save(team); | ||
PasswordResetToken passwordResetToken = new PasswordResetToken(); | ||
passwordResetToken.setDateCreated(LocalDateTime.MIN); | ||
passwordResetToken.setTeam(team); | ||
passwordResetRepository.save(passwordResetToken); | ||
|
||
var mvcResult = mockMvc.perform( | ||
post("/api/password/reset/is-valid") | ||
.contentType(APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsBytes( | ||
new ResetTokenStatusRequest(passwordResetToken.getResetToken())) | ||
) | ||
) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn(); | ||
|
||
assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo("false"); | ||
} | ||
|
||
@Test | ||
void should_return_false_when_password_reset_token_does_not_exist() throws Exception { | ||
Team team = new Team("teamuri", "TeamName", "%$&357", "[email protected]"); | ||
teamRepository.save(team); | ||
PasswordResetToken passwordResetToken = new PasswordResetToken(); | ||
passwordResetToken.setDateCreated(LocalDateTime.MIN); | ||
passwordResetToken.setTeam(team); | ||
passwordResetRepository.save(passwordResetToken); | ||
|
||
var mvcResult = mockMvc.perform( | ||
post("/api/password/reset/is-valid") | ||
.contentType(APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsBytes( | ||
new ResetTokenStatusRequest(UUID.randomUUID().toString())) | ||
) | ||
) | ||
.andExpect(status().is2xxSuccessful()) | ||
.andReturn(); | ||
|
||
assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo("false"); | ||
} | ||
|
||
@Test | ||
void should_change_password_and_consume_token_when_reset_token_is_valid() throws Exception { | ||
Team expectedResetTeam = new Team("teamuri", "TeamName", "%$&357", "[email protected]"); | ||
|
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.