Skip to content

Commit 5c5c9f0

Browse files
committed
Update forgotpwd with pattern checks
1 parent edc74c1 commit 5c5c9f0

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

core/src/main/java/io/aiven/klaw/controller/UsersTeamsController.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.aiven.klaw.controller;
22

3+
import static io.aiven.klaw.helpers.KwConstants.PASSWORD_REGEX;
4+
35
import io.aiven.klaw.error.KlawException;
46
import io.aiven.klaw.error.KlawNotAuthorizedException;
57
import io.aiven.klaw.model.ApiResponse;
@@ -16,6 +18,7 @@
1618
import io.aiven.klaw.service.UsersTeamsControllerService;
1719
import io.aiven.klaw.validation.PermissionAllowed;
1820
import jakarta.validation.Valid;
21+
import jakarta.validation.constraints.Pattern;
1922
import java.util.List;
2023
import org.springframework.beans.factory.annotation.Autowired;
2124
import org.springframework.http.HttpStatus;
@@ -276,7 +279,7 @@ public ResponseEntity<ResetPasswordInfo> resetToken(@RequestParam("username") St
276279
produces = {MediaType.APPLICATION_JSON_VALUE})
277280
public ResponseEntity<ResetPasswordInfo> resetPasswordWithToken(
278281
@RequestParam("token") String token,
279-
@RequestParam("password") String password,
282+
@Pattern(regexp = PASSWORD_REGEX) @RequestParam("password") String password,
280283
@RequestParam("username") String username)
281284
throws KlawNotAuthorizedException {
282285
return new ResponseEntity<>(

core/src/main/resources/static/js/forgotPassword.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ app.controller("forgotPwdCtrl", function($scope, $http, $location, $window) {
120120
}).error(
121121
function(error)
122122
{
123-
$scope.alert = 'Unable to update your password. Please check your token has not expired and your user name is spelt correctly.'
123+
if(error != null && error.detail != null && error.detail.includes("Validation failure")){
124+
$scope.alert = error.detail + ": Password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one number, and one special character.";
125+
$scope.alertnote = $scope.alert;
126+
$scope.showAlertToast();
127+
} else {
128+
$scope.alert = 'Unable to update your password. Please check your token has not expired and your user name is spelt correctly.'
129+
$scope.alertnote = error;
130+
$scope.showAlertToast();
131+
}
124132
}
125133
);
126134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.aiven.klaw.controller;
2+
3+
import static org.mockito.ArgumentMatchers.anyString;
4+
import static org.mockito.Mockito.when;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
6+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
7+
8+
import io.aiven.klaw.model.response.ResetPasswordInfo;
9+
import io.aiven.klaw.service.UsersTeamsControllerService;
10+
import java.nio.charset.StandardCharsets;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.MethodOrderer;
13+
import org.junit.jupiter.api.Order;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.TestMethodOrder;
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
import org.junit.jupiter.params.ParameterizedTest;
18+
import org.junit.jupiter.params.provider.CsvSource;
19+
import org.springframework.boot.test.mock.mockito.MockBean;
20+
import org.springframework.http.MediaType;
21+
import org.springframework.test.context.junit.jupiter.SpringExtension;
22+
import org.springframework.test.util.ReflectionTestUtils;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
25+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
26+
27+
@ExtendWith(SpringExtension.class)
28+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
29+
public class UserTeamsControllerTest {
30+
31+
@MockBean private UsersTeamsControllerService usersTeamsControllerService;
32+
33+
private MockMvc mvc;
34+
35+
@BeforeEach
36+
public void setUp() {
37+
UsersTeamsController usersTeamsController = new UsersTeamsController();
38+
mvc = MockMvcBuilders.standaloneSetup(usersTeamsController).dispatchOptions(true).build();
39+
ReflectionTestUtils.setField(
40+
usersTeamsController, "usersTeamsControllerService", usersTeamsControllerService);
41+
}
42+
43+
@ParameterizedTest
44+
@Order(1)
45+
@CsvSource({
46+
"'invalidpwd', 400", // Invalid password -> Expect 4xx Client Error
47+
"'Invalidpwd321@', 200" // Valid password -> Expect 200 OK
48+
})
49+
public void resetPasswordWithTokenTest(String password, int expectedStatus) throws Exception {
50+
ResetPasswordInfo passwordReset = new ResetPasswordInfo();
51+
when(usersTeamsControllerService.resetPassword(anyString(), anyString(), anyString()))
52+
.thenReturn(passwordReset);
53+
54+
mvc.perform(
55+
MockMvcRequestBuilders.post("/reset/password")
56+
.param("token", "token")
57+
.param("password", password)
58+
.param("username", "username")
59+
.contentType(MediaType.APPLICATION_JSON)
60+
.characterEncoding(StandardCharsets.UTF_8)
61+
.accept(MediaType.APPLICATION_JSON))
62+
.andDo(print())
63+
.andExpect(status().is(expectedStatus));
64+
}
65+
66+
}

core/src/test/java/io/aiven/klaw/service/UsersTeamsControllerServiceTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void updateUserNotAuthorizedToUpdateSuperAdmin() throws KlawException {
207207

208208
@Test
209209
public void resetPassword_withSuccess() throws KlawException, KlawNotAuthorizedException {
210-
String newPW = "newPW";
210+
String newPW = "newPW321@";
211211
String resetToken = UUID.randomUUID().toString();
212212
when(handleDbRequests.getUsersInfo(eq(OCTOPUS))).thenReturn(generateUser(OCTOPUS));
213213
when(manageDatabase.getTeamNameFromTeamId(eq(101), eq(10))).thenReturn("Octo");

openapi.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@
767767
"in" : "query",
768768
"required" : true,
769769
"schema" : {
770-
"type" : "string"
770+
"type" : "string",
771+
"pattern" : "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[\\W_]).{8,}"
771772
}
772773
}, {
773774
"name" : "username",

0 commit comments

Comments
 (0)