FINERACT-802-restrict-client-to-have-single-self-service-user (#664)

This commit is contained in:
Awasum Yannick
2020-03-05 09:40:13 +01:00
committed by GitHub
parent 867579d321
commit d4002f22a8
4 changed files with 59 additions and 0 deletions
@@ -27,6 +27,7 @@ import com.jayway.restassured.specification.ResponseSpecification;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.fineract.integrationtests.common.ClientHelper;
import org.apache.fineract.integrationtests.common.Utils;
import org.apache.fineract.integrationtests.common.organisation.StaffHelper;
import org.apache.fineract.integrationtests.useradministration.roles.RolesHelper;
@@ -36,6 +37,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class UserAdministrationTest {
private ResponseSpecification responseSpec;
@@ -64,6 +66,7 @@ public class UserAdministrationTest {
@Test
public void testCreateNewUserBlocksDuplicateUsername() {
final Integer roleId = RolesHelper.createRole(this.requestSpec, this.responseSpec);
Assert.assertNotNull(roleId);
@@ -122,5 +125,25 @@ public class UserAdministrationTest {
Assert.assertEquals("User with username alphabet already exists.", reason.get("defaultUserMessage"));
Assert.assertEquals("error.msg.user.duplicate.username", reason.get("userMessageGlobalisationCode"));
}
@Test
public void testCreateNewUserBlocksDuplicateClientId() {
final Integer roleId = RolesHelper.createRole(this.requestSpec, this.responseSpec);
Assert.assertNotNull(roleId);
final Integer staffId = StaffHelper.createStaff(this.requestSpec, this.responseSpec);
Assert.assertNotNull(staffId);
final Integer clientId = ClientHelper.createClient(this.requestSpec, this.responseSpec);
Assert.assertNotNull(clientId);
final Integer userId = (Integer) UserHelper.createUserForSelfService(this.requestSpec, this.responseSpec, roleId, staffId, clientId, "resourceId");
Assert.assertNotNull(userId);
this.transientUsers.add(userId);
final List errors = (List) UserHelper.createUserForSelfService(this.requestSpec, expectStatusCode(403), roleId, staffId, clientId, "errors");
Map reason = (Map) errors.get(0);
Assert.assertEquals("Self Service User Id is already created. Go to Admin->Users to edit or delete the self-service user.", reason.get("defaultUserMessage"));
}
}
@@ -34,6 +34,9 @@ public class UserHelper {
public static Object createUser(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, int roleId, int staffId, String username, String attribute) {
return Utils.performServerPost(requestSpec, responseSpec, CREATE_USER_URL, getTestCreateUserAsJSON(roleId, staffId, username), attribute);
}
public static Object createUserForSelfService(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, int roleId, int staffId, int clientId, String attribute) {
return Utils.performServerPost(requestSpec, responseSpec, CREATE_USER_URL, getTestCreateUserAsJSONForSelfService(roleId, staffId, clientId), attribute);
}
public static String getTestCreateUserAsJSON(int roleId, int staffId) {
return "{ \"username\": \"" + Utils.randomNameGenerator("User_Name_", 3)
@@ -56,6 +59,15 @@ public class UserHelper {
+ "\", \"firstname\": \"Test\", \"lastname\": \"User\", \"email\": \"whatever@mifos.org\","
+ " \"officeId\": \"1\"}";
}
public static String getTestCreateUserAsJSONForSelfService(int roleId, int staffId, int clientId) {
return "{ \"username\": \"" + Utils.randomNameGenerator("User_Name_", 3)
+ "\", \"firstname\": \"Test\", \"lastname\": \"User\", \"email\": \"whatever@mifos.org\","
+ " \"officeId\": \"1\", \"staffId\": " + "\""
+ staffId +"\",\"roles\": [\""
+ roleId + "\"], \"sendPasswordToEmail\": false,"
+"\"isSelfServiceUser\" : true,"
+"\"clients\" : [\""+clientId+"\"]}";
}
public static Integer deleteUser(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final Integer userId) {
return Utils.performServerDelete(requestSpec, responseSpec, createRoleOperationURL(userId), "resourceId");
@@ -347,6 +347,10 @@ public class AppUserWritePlatformServiceJpaRepositoryImpl implements AppUserWrit
username);
}
if (realCause.getMessage().contains("'unique_self_client'")) {
throw new PlatformDataIntegrityException("error.msg.user.self.service.user.already.exist", "Self Service User Id is already created. Go to Admin->Users to edit or delete the self-service user.");
}
logger.error(dve.getMessage(), dve);
throw new PlatformDataIntegrityException("error.msg.unknown.data.integrity.issue", "Unknown data integrity issue with resource.");
}
@@ -0,0 +1,20 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you 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.
--
ALTER TABLE `m_selfservice_user_client_mapping` ADD CONSTRAINT `unique_self_client` UNIQUE (`client_id`);