Merge pull request #1410 from sharmapankaj2512/MIFOSX-1935
MIFOSX-1935: role with no active user attached will get deleted
This commit is contained in:
+38
@@ -6,6 +6,7 @@
|
|||||||
package org.mifosplatform.integrationtests;
|
package org.mifosplatform.integrationtests;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@@ -13,7 +14,9 @@ import org.junit.Assert;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mifosplatform.integrationtests.common.Utils;
|
import org.mifosplatform.integrationtests.common.Utils;
|
||||||
|
import org.mifosplatform.integrationtests.common.organisation.StaffHelper;
|
||||||
import org.mifosplatform.integrationtests.useradministration.roles.RolesHelper;
|
import org.mifosplatform.integrationtests.useradministration.roles.RolesHelper;
|
||||||
|
import org.mifosplatform.integrationtests.useradministration.users.UserHelper;
|
||||||
|
|
||||||
import com.jayway.restassured.builder.RequestSpecBuilder;
|
import com.jayway.restassured.builder.RequestSpecBuilder;
|
||||||
import com.jayway.restassured.builder.ResponseSpecBuilder;
|
import com.jayway.restassured.builder.ResponseSpecBuilder;
|
||||||
@@ -113,4 +116,39 @@ public class RolesTest {
|
|||||||
final Integer deleteRoleId = RolesHelper.deleteRole(this.requestSpec, this.responseSpec, roleId);
|
final Integer deleteRoleId = RolesHelper.deleteRole(this.requestSpec, this.responseSpec, roleId);
|
||||||
assertEquals(deleteRoleId, roleId);
|
assertEquals(deleteRoleId, roleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRoleShouldGetDeletedIfNoActiveUserExists() {
|
||||||
|
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 userId = UserHelper.createUser(this.requestSpec, this.responseSpec, roleId, staffId);
|
||||||
|
Assert.assertNotNull(userId);
|
||||||
|
|
||||||
|
final Integer deletedUserId = UserHelper.deleteUser(this.requestSpec, this.responseSpec, userId);
|
||||||
|
Assert.assertEquals(deletedUserId, userId);
|
||||||
|
|
||||||
|
final Integer deletedRoleId = RolesHelper.deleteRole(this.requestSpec, this.responseSpec, roleId);
|
||||||
|
assertEquals(deletedRoleId, roleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRoleShouldNotGetDeletedIfActiveUserExists() {
|
||||||
|
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 userId = UserHelper.createUser(this.requestSpec, this.responseSpec, roleId, staffId);
|
||||||
|
Assert.assertNotNull(userId);
|
||||||
|
|
||||||
|
this.responseSpec = new ResponseSpecBuilder().expectStatusCode(403).build();
|
||||||
|
final Integer deletedRoleId = RolesHelper.deleteRole(this.requestSpec, this.responseSpec, roleId);
|
||||||
|
assertNotEquals(deletedRoleId, roleId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
* You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
package org.mifosplatform.integrationtests.useradministration.users;
|
||||||
|
|
||||||
|
import com.jayway.restassured.specification.RequestSpecification;
|
||||||
|
import com.jayway.restassured.specification.ResponseSpecification;
|
||||||
|
import org.mifosplatform.integrationtests.common.Utils;
|
||||||
|
|
||||||
|
|
||||||
|
public class UserHelper {
|
||||||
|
private static final String CREATE_USER_URL = "/mifosng-provider/api/v1/users?" + Utils.TENANT_IDENTIFIER;
|
||||||
|
private static final String USER_URL = "/mifosng-provider/api/v1/users";
|
||||||
|
|
||||||
|
public static Integer createUser(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, int roleId, int staffId) {
|
||||||
|
return Utils.performServerPost(requestSpec, responseSpec, CREATE_USER_URL, getTestCreateUserAsJSON(roleId, staffId), "resourceId");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getTestCreateUserAsJSON(int roleId, int staffId) {
|
||||||
|
String json = "{ \"username\": \"" + Utils.randomNameGenerator("User_Name_", 3)
|
||||||
|
+ "\", \"firstname\": \"Test\", \"lastname\": \"User\", \"email\": \"whatever@mifos.org\","
|
||||||
|
+ " \"officeId\": \"1\", \"staffId\": " + "\""
|
||||||
|
+ Integer.toString(staffId)+"\",\"roles\": [\""
|
||||||
|
+ Integer.toString(roleId) + "\"], \"sendPasswordToEmail\": false}";
|
||||||
|
System.out.println(json);
|
||||||
|
return json;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer deleteUser(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, final Integer userId) {
|
||||||
|
return Utils.performServerDelete(requestSpec, responseSpec, createRoleOperationURL(userId), "resourceId");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String createRoleOperationURL(final Integer userId) {
|
||||||
|
return USER_URL + "/" + userId + "?" + Utils.TENANT_IDENTIFIER;
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
@@ -296,6 +296,7 @@ public class AppUser extends AbstractPersistable<Long> implements PlatformUser {
|
|||||||
this.accountNonExpired = false;
|
this.accountNonExpired = false;
|
||||||
this.firstTimeLoginRemaining = true;
|
this.firstTimeLoginRemaining = true;
|
||||||
this.username = getId() + "_DELETED_" + this.username;
|
this.username = getId() + "_DELETED_" + this.username;
|
||||||
|
this.roles.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDeleted() {
|
public boolean isDeleted() {
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ import org.springframework.data.repository.query.Param;
|
|||||||
|
|
||||||
public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificationExecutor<Role> {
|
public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificationExecutor<Role> {
|
||||||
|
|
||||||
@Query("SELECT COUNT(*) FROM AppUser a JOIN a.roles r WHERE r.id = :roleId")
|
@Query("SELECT COUNT(*) FROM AppUser a JOIN a.roles r WHERE r.id = :roleId AND a.deleted = false")
|
||||||
Integer getCountOfRolesAssociatedWithUsers(@Param("roleId") Long roleId);
|
Integer getCountOfRolesAssociatedWithUsers(@Param("roleId") Long roleId);
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user