Skip to content

Commit

Permalink
feat-全局权限管理-增加功能权限对应的单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Oct 7, 2024
1 parent 46fd6ca commit a698d21
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public List<String> getAllowedActions(String userID, String module) {
select v_alias
from platform.app_module_action
where id_at_app_module = (select id from platform.app_module where app_module.v_alias = ?)
order by i_order
""", module);

return result.stream().map(it -> it.get("v_alias").toString()).collect(Collectors.toList());
Expand Down
162 changes: 162 additions & 0 deletions muyun-boot/src/test/java/net/ximatai/muyun/test/auth/TestAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package net.ximatai.muyun.test.auth;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import net.ximatai.muyun.authorization.AuthorizationService;
import net.ximatai.muyun.database.IDatabaseOperationsStd;
import net.ximatai.muyun.model.AuthorizedResource;
import net.ximatai.muyun.platform.controller.ModuleController;
import net.ximatai.muyun.platform.controller.RoleActionController;
import net.ximatai.muyun.test.testcontainers.PostgresTestResource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

@QuarkusTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true)
public class TestAuth {

@Inject
IDatabaseOperationsStd db;

@Inject
AuthorizationService authService;

@Inject
ModuleController moduleController;

@Inject
RoleActionController roleActionController;

String userID;
String role1, role2;
String module1, module2;

@BeforeAll
void setUp() {
userID = db.insertItem("platform", "auth_user", Map.of(
"v_username", "test"
));

role1 = db.insertItem("platform", "auth_role", Map.of(
"v_name", "role1"
));
role2 = db.insertItem("platform", "auth_role", Map.of(
"v_name", "role2"
));

db.insertItem("platform", "auth_user_role", Map.of(
"id_at_auth_user", userID,
"id_at_auth_role", role1
));

db.insertItem("platform", "auth_user_role", Map.of(
"id_at_auth_user", userID,
"id_at_auth_role", role2
));

module1 = moduleController.create(Map.of(
"v_name", "module1",
"v_alias", "module1"
));

module2 = moduleController.create(Map.of(
"v_name", "module2",
"v_alias", "module2"
));

List<Map> actionList1 = moduleController.getChildTableList(module1, "app_module_action", null);

List<Map> actionList2 = moduleController.getChildTableList(module2, "app_module_action", null);

roleActionController.create(Map.of(
"id_at_auth_role", role1,
"id_at_app_module_action", actionList1.get(0).get("id")
));

roleActionController.create(Map.of(
"id_at_auth_role", role1,
"id_at_app_module_action", actionList1.get(1).get("id")
));

roleActionController.create(Map.of(
"id_at_auth_role", role1,
"id_at_app_module_action", actionList1.get(2).get("id")
));

roleActionController.create(Map.of(
"id_at_auth_role", role1,
"id_at_app_module_action", actionList2.get(0).get("id")
));

roleActionController.create(Map.of(
"id_at_auth_role", role2,
"id_at_app_module_action", actionList1.get(0).get("id")
));
}

@Test
void testGetUserAvailableRoles() {
List<String> roles = authService.getUserAvailableRoles(userID);
assertEquals(2, roles.size());
assertTrue(roles.contains(role1));
assertTrue(roles.contains(role2));
}

@Test
void testGetAllowedActions() {
List<String> actions = authService.getAllowedActions(userID, "module1");
assertEquals(3, actions.size());

List<String> actions2 = authService.getAllowedActions(userID, "module2");
assertEquals(1, actions2.size());
}

@Test
void testGetAllowedActionsForSuper() {
List<String> actions = authService.getAllowedActions("1", "module1");
assertEquals(6, actions.size());

List<String> actions2 = authService.getAllowedActions("1", "module2");
assertEquals(6, actions2.size());
}

@Test
void testGetAuthorizedResources() {
Set<AuthorizedResource> resources = authService.getAuthorizedResources(userID);
assertEquals(4, resources.size());
}

@Test
void testGetAuthorizedResourcesForSuper() {
Set<AuthorizedResource> resources = authService.getAuthorizedResources("1");
assertEquals(12, resources.size());
}

@Test
void testIsAuthorized() {
assertTrue(authService.isAuthorized(userID, "module1", "view"));
assertTrue(authService.isAuthorized(userID, "module1", "menu"));
assertTrue(authService.isAuthorized(userID, "module1", "export"));
assertTrue(authService.isAuthorized(userID, "module2", "menu"));
assertFalse(authService.isAuthorized(userID, "module2", "view"));
}

@Test
void testIsAuthorizedForSuper() {
assertTrue(authService.isAuthorized("1", "module1", "view"));
assertTrue(authService.isAuthorized("1", "module1", "menu"));
assertTrue(authService.isAuthorized("1", "module1", "export"));
assertTrue(authService.isAuthorized("1", "module2", "menu"));
assertTrue(authService.isAuthorized("1", "module2", "view"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public void fitOutDefaultValue(Map body) {
super.fitOutDefaultValue(body);

Objects.requireNonNull(body.get("id_at_auth_role"));
Objects.requireNonNull(body.get("id_at_app_module"));
Objects.requireNonNull(body.get("id_at_app_module_action"));

Map<String, Object> action = getDB().row("select v_alias,id_at_app_module from platform.app_module_action where id = ?", body.get("id_at_app_module_action"));
body.put("v_alias_at_app_module_action", action.get("v_alias"));
body.put("id_at_app_module", action.get("id_at_app_module"));

Map<String, Object> module = getDB().row("select v_alias from platform.app_module where id = ?", body.get("id_at_app_module"));
body.put("v_alias_at_app_module", module.get("v_alias"));

Map<String, Object> action = getDB().row("select v_alias from platform.app_module_action where id = ?", body.get("id_at_app_module_action"));
body.put("v_alias_at_app_module_action", action.get("v_alias"));
}
}

0 comments on commit a698d21

Please sign in to comment.