-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-1971 Improve Request Validation
- Loading branch information
Showing
7 changed files
with
160 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,10 +127,14 @@ private PostUsersRequest() { | |
@Schema(example = "1") | ||
public Long staffId; | ||
@Schema(example = "[2,3]") | ||
public List<String> roles; | ||
public List<Long> roles; | ||
@Schema(example = "[2,3]") | ||
public List<Long> clients; | ||
@Schema(example = "true") | ||
public Boolean sendPasswordToEmail; | ||
@Schema(example = "true") | ||
public Boolean passwordNeverExpires; | ||
@Schema(example = "true") | ||
public Boolean isSelfServiceUser; | ||
} | ||
|
||
|
@@ -156,10 +160,26 @@ private PutUsersUserIdRequest() { | |
|
||
@Schema(example = "Test") | ||
public String firstname; | ||
@Schema(example = "window75") | ||
@Schema(example = "User") | ||
public String lastname; | ||
@Schema(example = "[email protected]") | ||
public String email; | ||
@Schema(example = "1") | ||
public Long officeId; | ||
@Schema(example = "1") | ||
public Long staffId; | ||
@Schema(example = "[2,3]") | ||
public List<Long> roles; | ||
@Schema(example = "[2,3]") | ||
public List<Long> clients; | ||
@Schema(example = "password") | ||
public String password; | ||
@Schema(example = "window75") | ||
@Schema(example = "repeatPassword") | ||
public String repeatPassword; | ||
@Schema(example = "true") | ||
public Boolean sendPasswordToEmail; | ||
@Schema(example = "true") | ||
public Boolean isSelfServiceUser; | ||
} | ||
|
||
@Schema(description = "PutUsersUserIdResponse") | ||
|
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 |
---|---|---|
|
@@ -65,7 +65,7 @@ public void setUp() { | |
PostUsersRequest createUserRequest = new PostUsersRequest().username(username) | ||
.firstname(Utils.randomStringGenerator("NotificationFN", 4)).lastname(Utils.randomStringGenerator("NotificationLN", 4)) | ||
.email("[email protected]").password(password).repeatPassword(password).sendPasswordToEmail(false) | ||
.roles(List.of(Long.toString(SUPER_USER_ROLE_ID))).officeId(headOffice.getId()); | ||
.roles(List.of(SUPER_USER_ROLE_ID)).officeId(headOffice.getId()); | ||
|
||
PostUsersResponse userCreationResponse = UserHelper.createUser(requestSpec, responseSpec, createUserRequest); | ||
Assertions.assertNotNull(userCreationResponse.getResourceId()); | ||
|
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 |
---|---|---|
|
@@ -27,7 +27,16 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.fineract.client.models.GetOfficesResponse; | ||
import org.apache.fineract.client.models.GetUsersUserIdResponse; | ||
import org.apache.fineract.client.models.PostUsersRequest; | ||
import org.apache.fineract.client.models.PostUsersResponse; | ||
import org.apache.fineract.client.models.PutUsersUserIdRequest; | ||
import org.apache.fineract.client.models.PutUsersUserIdResponse; | ||
import org.apache.fineract.client.util.CallFailedRuntimeException; | ||
import org.apache.fineract.integrationtests.client.IntegrationTest; | ||
import org.apache.fineract.integrationtests.common.ClientHelper; | ||
import org.apache.fineract.integrationtests.common.OfficeHelper; | ||
import org.apache.fineract.integrationtests.common.Utils; | ||
import org.apache.fineract.integrationtests.common.organisation.StaffHelper; | ||
import org.apache.fineract.integrationtests.useradministration.roles.RolesHelper; | ||
|
@@ -40,7 +49,7 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class UserAdministrationTest { | ||
public class UserAdministrationTest extends IntegrationTest { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(UserAdministrationTest.class); | ||
private ResponseSpecification responseSpec; | ||
|
@@ -171,4 +180,67 @@ public void testModifySystemUser() { | |
|
||
final List errors = (List) UserHelper.updateUser(this.requestSpec, expectStatusCode(403), userId, "systemtest", "errors"); | ||
} | ||
|
||
@Test | ||
public void testApplicationUserCanChangeOwnPassword() { | ||
// Admin creates a new user with an empty role | ||
Integer roleId = RolesHelper.createRole(requestSpec, responseSpec); | ||
String originalPassword = "aA1qwerty56"; | ||
String simpleUsername = Utils.uniqueRandomStringGenerator("NotificationUser", 4); | ||
GetOfficesResponse headOffice = OfficeHelper.getHeadOffice(requestSpec, responseSpec); | ||
PostUsersRequest createUserRequest = new PostUsersRequest().username(simpleUsername) | ||
.firstname(Utils.randomStringGenerator("NotificationFN", 4)).lastname(Utils.randomStringGenerator("NotificationLN", 4)) | ||
.email("[email protected]").password(originalPassword).repeatPassword(originalPassword).sendPasswordToEmail(false) | ||
.officeId(headOffice.getId()).roles(List.of(Long.valueOf(roleId))); | ||
|
||
PostUsersResponse userCreationResponse = UserHelper.createUser(requestSpec, responseSpec, createUserRequest); | ||
Long userId = userCreationResponse.getResourceId(); | ||
Assertions.assertNotNull(userId); | ||
|
||
// User updates its own password | ||
String updatedPassword = "aA1qwerty56!"; | ||
PutUsersUserIdResponse putUsersUserIdResponse = ok(newFineract(simpleUsername, originalPassword).users.update26(userId, | ||
new PutUsersUserIdRequest().password(updatedPassword).repeatPassword(updatedPassword))); | ||
Assertions.assertNotNull(putUsersUserIdResponse.getResourceId()); | ||
|
||
// From then on the originalPassword is not working anymore | ||
CallFailedRuntimeException callFailedRuntimeException = Assertions.assertThrows(CallFailedRuntimeException.class, () -> { | ||
ok(newFineract(simpleUsername, originalPassword).users.retrieveOne31(userId)); | ||
}); | ||
Assertions.assertEquals(401, callFailedRuntimeException.getResponse().raw().code()); | ||
Assertions.assertTrue(callFailedRuntimeException.getMessage().contains("Unauthorized")); | ||
|
||
// The update password is still working perfectly | ||
GetUsersUserIdResponse ok = ok(newFineract(simpleUsername, updatedPassword).users.retrieveOne31(userId)); | ||
} | ||
|
||
@Test | ||
public void testApplicationUserShallNotBeAbleToChangeItsOwnRoles() { | ||
// Admin creates a new user with one role assigned | ||
Integer roleId = RolesHelper.createRole(requestSpec, responseSpec); | ||
String password = "aA1qwerty56"; | ||
String simpleUsername = Utils.uniqueRandomStringGenerator("NotificationUser", 4); | ||
GetOfficesResponse headOffice = OfficeHelper.getHeadOffice(requestSpec, responseSpec); | ||
PostUsersRequest createUserRequest = new PostUsersRequest().username(simpleUsername) | ||
.firstname(Utils.randomStringGenerator("NotificationFN", 4)).lastname(Utils.randomStringGenerator("NotificationLN", 4)) | ||
.email("[email protected]").password(password).repeatPassword(password).sendPasswordToEmail(false) | ||
.officeId(headOffice.getId()).roles(List.of(Long.valueOf(roleId))); | ||
|
||
PostUsersResponse userCreationResponse = UserHelper.createUser(requestSpec, responseSpec, createUserRequest); | ||
Long userId = userCreationResponse.getResourceId(); | ||
Assertions.assertNotNull(userId); | ||
|
||
// Admin creates a second role | ||
Integer roleId2 = RolesHelper.createRole(requestSpec, responseSpec); | ||
|
||
// User tries to update it's own roles | ||
CallFailedRuntimeException callFailedRuntimeException = Assertions.assertThrows(CallFailedRuntimeException.class, () -> { | ||
ok(newFineract(simpleUsername, password).users.update26(userId, | ||
new PutUsersUserIdRequest().roles(List.of(Long.valueOf(roleId2))))); | ||
}); | ||
|
||
Assertions.assertEquals(400, callFailedRuntimeException.getResponse().raw().code()); | ||
Assertions.assertTrue(callFailedRuntimeException.getMessage().contains("not.enough.permission.to.update.fields")); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -149,7 +149,7 @@ public static RequestSpecification getSimpleUserWithoutBypassPermission(final Re | |
PostUsersRequest createUserRequest = new PostUsersRequest().username(simpleUsername) | ||
.firstname(Utils.randomStringGenerator("NotificationFN", 4)).lastname(Utils.randomStringGenerator("NotificationLN", 4)) | ||
.email("[email protected]").password(password).repeatPassword(password).sendPasswordToEmail(false) | ||
.roles(List.of(simpleRoleId)).officeId(headOffice.getId()); | ||
.roles(List.of(Long.valueOf(simpleRoleId))).officeId(headOffice.getId()); | ||
|
||
PostUsersResponse userCreationResponse = UserHelper.createUser(requestSpec, responseSpec, createUserRequest); | ||
Assertions.assertNotNull(userCreationResponse.getResourceId()); | ||
|