Merge pull request #1410 from sharmapankaj2512/MIFOSX-1935

MIFOSX-1935: role with no active user attached will get deleted
This commit is contained in:
Vishwas Babu A J
2015-06-16 07:22:12 -07:00
4 changed files with 80 additions and 2 deletions
@@ -6,6 +6,7 @@
package org.mifosplatform.integrationtests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.util.HashMap;
@@ -13,7 +14,9 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
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.users.UserHelper;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.builder.ResponseSpecBuilder;
@@ -113,4 +116,39 @@ public class RolesTest {
final Integer deleteRoleId = RolesHelper.deleteRole(this.requestSpec, this.responseSpec, 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);
}
}
@@ -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;
}
}
@@ -296,6 +296,7 @@ public class AppUser extends AbstractPersistable<Long> implements PlatformUser {
this.accountNonExpired = false;
this.firstTimeLoginRemaining = true;
this.username = getId() + "_DELETED_" + this.username;
this.roles.clear();
}
public boolean isDeleted() {
@@ -11,8 +11,8 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
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);
}