Skip to content

Commit c083129

Browse files
authored
AYS-412 | Create Password Service Automation (#184)
1 parent f5fac63 commit c083129

File tree

8 files changed

+224
-1
lines changed

8 files changed

+224
-1
lines changed

src/test/java/org/ays/auth/datasource/UserDataSource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,23 @@ public static List<String> findAllRoleIdsById(String id) {
174174
}
175175
}
176176

177+
public static String findPasswordIdByUserId(String userId) {
178+
String query = "SELECT ID FROM AYS_USER_PASSWORD WHERE USER_ID = ?";
179+
180+
try (Connection connection = AysDataSource.createConnection();
181+
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
182+
183+
preparedStatement.setString(1, userId);
184+
try (ResultSet resultSet = preparedStatement.executeQuery()) {
185+
if (resultSet.next()) {
186+
return resultSet.getString("ID");
187+
}
188+
}
189+
190+
throw new RuntimeException("No password id found for the given userId: " + userId);
191+
} catch (SQLException exception) {
192+
throw new RuntimeException(exception);
193+
}
194+
}
195+
177196
}

src/test/java/org/ays/auth/endpoints/AuthEndpoints.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.restassured.response.Response;
44
import lombok.experimental.UtilityClass;
55
import org.ays.auth.payload.LoginPayload;
6+
import org.ays.auth.payload.PasswordCreatePayload;
67
import org.ays.auth.payload.PasswordForgotPayload;
78
import org.ays.auth.payload.TokenInvalidatePayload;
89
import org.ays.auth.payload.TokenRefreshPayload;
@@ -60,4 +61,17 @@ public static Response forgotPassword(PasswordForgotPayload passwordForgotPayloa
6061

6162
}
6263

64+
public static Response createPassword(String id, PasswordCreatePayload passwordCreatePayload) {
65+
66+
AysRestAssuredPayload restAssuredRequest = AysRestAssuredPayload.builder()
67+
.httpMethod(HttpMethod.POST)
68+
.url("/api/v1/authentication/password/{id}")
69+
.pathParameter(Map.of("id", id))
70+
.body(passwordCreatePayload)
71+
.build();
72+
73+
return AysRestAssured.perform(restAssuredRequest);
74+
75+
}
76+
6377
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.ays.auth.payload;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.ays.common.util.AysRandomUtil;
6+
7+
@Getter
8+
@Setter
9+
public class PasswordCreatePayload {
10+
11+
private String password;
12+
private String passwordRepeat;
13+
14+
public static PasswordCreatePayload generate() {
15+
PasswordCreatePayload passwordCreatePayload = new PasswordCreatePayload();
16+
String generatedPassword = AysRandomUtil.generatePassword();
17+
passwordCreatePayload.setPassword(generatedPassword);
18+
passwordCreatePayload.setPasswordRepeat(generatedPassword);
19+
return passwordCreatePayload;
20+
}
21+
22+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package org.ays.auth.tests;
2+
3+
import io.restassured.response.Response;
4+
import org.ays.auth.datasource.PermissionDataSource;
5+
import org.ays.auth.datasource.RoleDataSource;
6+
import org.ays.auth.datasource.UserDataSource;
7+
import org.ays.auth.endpoints.AuthEndpoints;
8+
import org.ays.auth.endpoints.RoleEndpoints;
9+
import org.ays.auth.endpoints.UserEndpoints;
10+
import org.ays.auth.model.enums.Permission;
11+
import org.ays.auth.model.enums.SourcePage;
12+
import org.ays.auth.payload.LoginPayload;
13+
import org.ays.auth.payload.PasswordCreatePayload;
14+
import org.ays.auth.payload.PasswordForgotPayload;
15+
import org.ays.auth.payload.RoleCreatePayload;
16+
import org.ays.auth.payload.UserCreatePayload;
17+
import org.ays.common.model.enums.AysErrorMessage;
18+
import org.ays.common.util.AysConfigurationProperty;
19+
import org.ays.common.util.AysDataProvider;
20+
import org.ays.common.util.AysRandomUtil;
21+
import org.ays.common.util.AysResponseSpecs;
22+
import org.testng.annotations.Test;
23+
24+
import java.util.Collections;
25+
import java.util.List;
26+
27+
import static org.hamcrest.Matchers.equalTo;
28+
29+
public class PasswordCreateTest {
30+
31+
@Test(groups = {"Smoke", "Regression"})
32+
public void createPasswordFirstTime() {
33+
LoginPayload loginPayload = LoginPayload.generateAsTestDisasterFoundationAdmin();
34+
String accessToken = this.loginAndGetAccessToken(loginPayload);
35+
36+
List<String> permissionsIds = Collections.singletonList(PermissionDataSource
37+
.findPermissionIdByName(Permission.INSTITUTION_PAGE.getPermission()));
38+
39+
RoleCreatePayload roleCreatePayload = RoleCreatePayload.generate(permissionsIds);
40+
RoleEndpoints.create(roleCreatePayload, accessToken);
41+
String roleId = RoleDataSource.findLastCreatedRoleIdByInstitutionId(AysConfigurationProperty.TestDisasterFoundation.ID);
42+
43+
UserCreatePayload userCreatePayload = UserCreatePayload.generateUserWithARole(roleId);
44+
UserEndpoints.create(userCreatePayload, accessToken);
45+
String userId = UserDataSource.findLastCreatedUserIdByInstitutionId(AysConfigurationProperty.TestDisasterFoundation.ID);
46+
47+
String passwordId = UserDataSource.findPasswordIdByUserId(userId);
48+
PasswordCreatePayload passwordCreatePayload = PasswordCreatePayload.generate();
49+
Response response = AuthEndpoints.createPassword(passwordId, passwordCreatePayload);
50+
response.then()
51+
.spec(AysResponseSpecs.expectSuccessResponseSpec());
52+
53+
loginPayload.setEmailAddress(userCreatePayload.getEmailAddress());
54+
loginPayload.setPassword(passwordCreatePayload.getPassword());
55+
loginPayload.setSourcePage(SourcePage.INSTITUTION);
56+
57+
Response loginCheckResponse = AuthEndpoints.token(loginPayload);
58+
loginCheckResponse.then()
59+
.spec(AysResponseSpecs.expectSuccessResponseSpec())
60+
.spec(AysResponseSpecs.expectGetTokenResponseSpec());
61+
}
62+
63+
@Test(groups = {"Smoke", "Regression"})
64+
public void createPasswordAfterForgotPasswordCalling() {
65+
LoginPayload loginPayload = LoginPayload.generateAsTestDisasterFoundationAdmin();
66+
String accessToken = this.loginAndGetAccessToken(loginPayload);
67+
68+
List<String> permissionsIds = Collections.singletonList(PermissionDataSource
69+
.findPermissionIdByName(Permission.INSTITUTION_PAGE.getPermission()));
70+
71+
RoleCreatePayload roleCreatePayload = RoleCreatePayload.generate(permissionsIds);
72+
RoleEndpoints.create(roleCreatePayload, accessToken);
73+
String roleId = RoleDataSource.findLastCreatedRoleIdByInstitutionId(AysConfigurationProperty.TestDisasterFoundation.ID);
74+
75+
UserCreatePayload userCreatePayload = UserCreatePayload.generateUserWithARole(roleId);
76+
UserEndpoints.create(userCreatePayload, accessToken);
77+
78+
PasswordForgotPayload passwordForgotPayload = new PasswordForgotPayload();
79+
String emailAddress = userCreatePayload.getEmailAddress();
80+
passwordForgotPayload.setEmailAddress(emailAddress);
81+
AuthEndpoints.forgotPassword(passwordForgotPayload);
82+
83+
String userId = UserDataSource.findLastCreatedUserIdByInstitutionId(AysConfigurationProperty.TestDisasterFoundation.ID);
84+
85+
String passwordId = UserDataSource.findPasswordIdByUserId(userId);
86+
PasswordCreatePayload passwordCreatePayload = PasswordCreatePayload.generate();
87+
Response response = AuthEndpoints.createPassword(passwordId, passwordCreatePayload);
88+
response.then()
89+
.spec(AysResponseSpecs.expectSuccessResponseSpec());
90+
91+
loginPayload.setEmailAddress(emailAddress);
92+
loginPayload.setPassword(passwordCreatePayload.getPassword());
93+
loginPayload.setSourcePage(SourcePage.INSTITUTION);
94+
95+
Response loginCheckResponse = AuthEndpoints.token(loginPayload);
96+
loginCheckResponse.then()
97+
.spec(AysResponseSpecs.expectSuccessResponseSpec())
98+
.spec(AysResponseSpecs.expectGetTokenResponseSpec());
99+
}
100+
101+
@Test(groups = {"Regression"}, dataProvider = "invalidIdFormat", dataProviderClass = AysDataProvider.class)
102+
public void createPasswordUsingInvalidPasswordId(String passwordId, AysErrorMessage errorMessage, String field, String type) {
103+
PasswordCreatePayload passwordCreatePayload = PasswordCreatePayload.generate();
104+
Response response = AuthEndpoints.createPassword(passwordId, passwordCreatePayload);
105+
response.then()
106+
.spec(AysResponseSpecs.expectBadRequestResponseSpec())
107+
.spec(AysResponseSpecs.subErrorsSpec(errorMessage, field, type));
108+
}
109+
110+
@Test(groups = {"Regression"}, dataProvider = "invalidPassword", dataProviderClass = AysDataProvider.class)
111+
public void createPasswordWithInvalidPasswordData(String password, AysErrorMessage errorMessage, String field, String type) {
112+
PasswordCreatePayload passwordCreatePayload = PasswordCreatePayload.generate();
113+
passwordCreatePayload.setPassword(password);
114+
115+
String userId = UserDataSource.findLastCreatedUserIdByInstitutionId(AysConfigurationProperty.TestDisasterFoundation.ID);
116+
117+
String passwordId = UserDataSource.findPasswordIdByUserId(userId);
118+
Response response = AuthEndpoints.createPassword(passwordId, passwordCreatePayload);
119+
response.then()
120+
.spec(AysResponseSpecs.expectBadRequestResponseSpec())
121+
.spec(AysResponseSpecs.subErrorsSpec(errorMessage, field, type));
122+
}
123+
124+
@Test(groups = {"Regression"}, dataProvider = "invalidPasswordRepeat", dataProviderClass = AysDataProvider.class)
125+
public void createPasswordWithInvalidPasswordRepeatData(String password, AysErrorMessage errorMessage, String field, String type) {
126+
PasswordCreatePayload passwordCreatePayload = PasswordCreatePayload.generate();
127+
passwordCreatePayload.setPasswordRepeat(password);
128+
129+
String userId = UserDataSource.findLastCreatedUserIdByInstitutionId(AysConfigurationProperty.TestDisasterFoundation.ID);
130+
131+
String passwordId = UserDataSource.findPasswordIdByUserId(userId);
132+
Response response = AuthEndpoints.createPassword(passwordId, passwordCreatePayload);
133+
response.then()
134+
.spec(AysResponseSpecs.expectBadRequestResponseSpec())
135+
.spec(AysResponseSpecs.subErrorsSpec(errorMessage, field, type));
136+
}
137+
138+
@Test(groups = {"Regression"})
139+
public void createPasswordWithMismatchedPasswordAndPasswordRepeatData() {
140+
PasswordCreatePayload passwordCreatePayload = PasswordCreatePayload.generate();
141+
passwordCreatePayload.setPasswordRepeat(AysRandomUtil.generatePassword());
142+
143+
String userId = UserDataSource.findLastCreatedUserIdByInstitutionId(AysConfigurationProperty.TestDisasterFoundation.ID);
144+
145+
String passwordId = UserDataSource.findPasswordIdByUserId(userId);
146+
Response response = AuthEndpoints.createPassword(passwordId, passwordCreatePayload);
147+
response.then()
148+
.spec(AysResponseSpecs.expectBadRequestResponseSpec())
149+
.body("subErrors[0].message", equalTo(AysErrorMessage.PASSWORDS_MUST_BE_EQUAL.getMessage()));
150+
}
151+
152+
private String loginAndGetAccessToken(LoginPayload loginPayload) {
153+
return AuthEndpoints.token(loginPayload).jsonPath().getString("response.accessToken");
154+
}
155+
}

src/test/java/org/ays/common/model/enums/AysErrorMessage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public enum AysErrorMessage {
3636
ADMIN_REGISTRATION_APPLICATION_ALREADY_APPROVED_OR_REJECTED("admin registration application was already approved or rejected!"),
3737
ADMIN_REGISTRATION_APPLICATION_ALREADY_REJECTED("admin registration application was already rejected!"),
3838
ADMIN_REGISTRATION_APPLICATION_IS_NOT_COMPLETE("admin registration application is not complete!"),
39-
SOURCE_CITY_DISTRICT_AND_TARGET_CITY_DISTRICT_MUST_BE_DIFFERENT("source city/district and target city/district must be different");
39+
SOURCE_CITY_DISTRICT_AND_TARGET_CITY_DISTRICT_MUST_BE_DIFFERENT("source city/district and target city/district must be different"),
40+
PASSWORDS_MUST_BE_EQUAL("passwords must be equal");
4041

4142
private final String message;
4243

src/test/java/org/ays/common/util/AysDataProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ public Object[][] invalidPassword() {
250250
};
251251
}
252252

253+
@org.testng.annotations.DataProvider(name = "invalidPasswordRepeat")
254+
public Object[][] invalidPasswordRepeat() {
255+
return new Object[][]{
256+
{"", AysErrorMessage.MUST_NOT_BE_BLANK, "passwordRepeat", "String"},
257+
{null, AysErrorMessage.MUST_NOT_BE_BLANK, "passwordRepeat", "String"},
258+
{AysRandomUtil.generatePassword(1, 7), AysErrorMessage.SIZE_BETWEEN_8_128, "passwordRepeat", "String"},
259+
{AysRandomUtil.generatePassword(129, 135), AysErrorMessage.SIZE_BETWEEN_8_128, "passwordRepeat", "String"}
260+
};
261+
}
262+
253263
@org.testng.annotations.DataProvider(name = "invalidPhoneNumberDataForRegistrationComplete")
254264
public static Object[][] invalidPhoneNumberDataForRegistrationComplete() {
255265
return new Object[][]{

src/test/resources/testsuite/RegressionSuite.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<class name="org.ays.auth.tests.TokenInvalidateTest"/>
1212
<class name="org.ays.auth.tests.TokenRefreshTest"/>
1313
<class name="org.ays.auth.tests.TokenTest"/>
14+
<class name="org.ays.auth.tests.PasswordCreateTest"/>
1415
<class name="org.ays.auth.tests.PasswordForgotTest"/>
1516
<class name="org.ays.auth.tests.PermissionsListTest"/>
1617
<class name="org.ays.auth.tests.RoleActivateTest"/>

src/test/resources/testsuite/SmokeSuite.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<class name="org.ays.auth.tests.TokenRefreshTest"/>
1313
<class name="org.ays.auth.tests.TokenTest"/>
1414
<class name="org.ays.auth.tests.InvalidTokensTest"/>
15+
<class name="org.ays.auth.tests.PasswordCreateTest"/>
1516
<class name="org.ays.auth.tests.PasswordForgotTest"/>
1617
<class name="org.ays.auth.tests.PermissionsListTest"/>
1718
<class name="org.ays.auth.tests.RoleActivateTest"/>

0 commit comments

Comments
 (0)