Browse Source

优化测试

zhouhao 6 years ago
parent
commit
a5633beee2

+ 3 - 3
hsweb-authorization/README.md

@@ -3,7 +3,7 @@
 
 # 目录介绍
 1. [hsweb-authorization-api](hsweb-authorization-api):权限控制API
-1. [hsweb-authorization-oauth2](hsweb-authorization-oauth2):oauth2支持
-1. [hsweb-authorization-basic](hsweb-authorization-basic):权限控制基础实现
-1. [hsweb-authorization-jwt](hsweb-authorization-jwt):权限控制jwt拓展
+2. [hsweb-authorization-oauth2](hsweb-authorization-oauth2):oauth2支持
+3. [hsweb-authorization-basic](hsweb-authorization-basic):权限控制基础实现
+4. [hsweb-authorization-jwt](hsweb-authorization-jwt):权限控制jwt拓展
 

+ 3 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-api/src/main/java/org/hswebframework/web/entity/authorization/UserEntity.java

@@ -18,8 +18,10 @@
 
 package org.hswebframework.web.entity.authorization;
 
+import org.hibernate.validator.constraints.NotBlank;
 import org.hswebframework.web.commons.entity.GenericEntity;
 import org.hswebframework.web.commons.entity.RecordCreationEntity;
+import org.hswebframework.web.validator.group.CreateGroup;
 
 /**
  * @author zhouhao
@@ -41,6 +43,7 @@ public interface UserEntity extends GenericEntity<String>, RecordCreationEntity
 
     void setUsername(String username);
 
+    @NotBlank(groups = CreateGroup.class)
     String getName();
 
     void setPassword(String password);

+ 1 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/pom.xml

@@ -73,6 +73,7 @@
             <artifactId>h2</artifactId>
             <scope>test</scope>
         </dependency>
+
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid</artifactId>

+ 14 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/java/org/hswebframework/web/authorization/starter/TestApplication.java

@@ -0,0 +1,14 @@
+package org.hswebframework.web.authorization.starter;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+/**
+ * @author zhouhao
+ * @since 3.0.0-RC
+ */
+@SpringBootApplication
+@WebAppConfiguration
+public class TestApplication {
+
+}

+ 139 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/java/org/hswebframework/web/authorization/starter/UserSettingControllerTest.groovy

@@ -0,0 +1,139 @@
+package org.hswebframework.web.authorization.starter
+
+import com.alibaba.fastjson.JSON
+import org.hswebframework.web.authorization.Authentication
+import org.hswebframework.web.authorization.AuthenticationInitializeService
+import org.junit.runner.RunWith
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.context.SpringBootTest
+import org.springframework.context.ConfigurableApplicationContext
+import org.springframework.http.MediaType
+import org.springframework.test.context.ContextConfiguration
+import org.springframework.test.context.junit4.SpringRunner
+import org.springframework.test.context.web.WebAppConfiguration
+import org.springframework.test.web.servlet.MockMvc
+import org.springframework.test.web.servlet.MvcResult
+import org.springframework.test.web.servlet.ResultHandler
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers
+import org.springframework.test.web.servlet.setup.MockMvcBuilders
+import spock.lang.Shared
+import spock.lang.Specification
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+
+/**
+ * @author zhouhao
+ * @since
+ */
+@WebAppConfiguration
+@ContextConfiguration
+@SpringBootTest(classes = [TestApplication.class], properties = ["classpath:application.yml"])
+class UserSettingControllerTest extends Specification {
+    @Autowired
+    private ConfigurableApplicationContext context;
+
+    @Shared
+    private MockMvc mockMvc;
+
+    @Autowired
+    private AuthenticationInitializeService initializeService;
+
+    void setup() {
+        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
+    }
+
+    def "Add Permission"() {
+        def permissions = [
+                [
+                        "id"     : "user",
+                        "name"   : "用户管理",
+                        "actions": [["action": "query", "describe": "查询"], ["action": "update", "describe": "修改"]]
+
+                ],
+                [
+                        "id"     : "role",
+                        "name"   : "角色管理",
+                        "actions": [["action": "query", "describe": "查询"], ["action": "get", "describe": "查看详情"]]
+
+                ]
+        ]
+        permissions.forEach({ permission ->
+            //添加权限
+            mockMvc.perform(post("/permission")
+                    .contentType(MediaType.APPLICATION_JSON)
+                    .content(JSON.toJSONString(permission)))
+                    .andExpect(status().is(201))
+        })
+    }
+
+    def "Add User"() {
+        //添加用户先
+        String result = mockMvc
+                .perform(
+                post("/user")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content(
+                        """
+                        {
+                            "name":"admin",
+                            "username":"admin",
+                            "password":"admin"
+                        }
+                        """
+                ))
+                .andExpect(status().is(201))
+                .andReturn()
+                .getResponse()
+                .getContentAsString()
+        return JSON.parseObject(result).getString("result")
+    }
+
+    def "Add User Authentication Setting"() {
+        setup:
+        "Add Permission"()
+        def userId = "Add User"()
+        //添加用户权限
+        mockMvc.perform(
+                post("/autz-setting")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content(JSON.toJSONString
+                        ([
+                                type      : "user", //设置类型:user
+                                settingFor: userId, //设置给具体的user
+                                describe  : "测试",
+                                details   :
+                                        [
+                                                [
+                                                        permissionId: "user", //赋予user权限
+                                                        actions     : ["query", "update"],
+                                                        status      : 1
+                                                ],
+                                                [
+                                                        permissionId: "role", //赋予role权限
+                                                        actions     : ["query", "get"],
+                                                        status      : 1
+                                                ]
+                                        ],
+                                menus     :
+                                        [
+                                                [
+                                                        menuId: "user-menu"
+                                                ],
+                                                [
+                                                        menuId: "role-menu"
+                                                ]
+                                        ]
+                        ])
+                )).andExpect(status().is(201))
+        expect:
+        userId != null
+        def autz = initializeService.initUserAuthorization(userId)
+        autz != null
+        autz.hasPermission("user", "query")
+        autz.hasPermission("role", "query", "get")
+
+    }
+}

+ 6 - 1
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/resources/application.yml

@@ -13,4 +13,9 @@ hsweb:
       version: 3.0.0
     authorize:
       sync: true
-      auto-parse: true
+      auto-parse: true
+logging:
+  level:
+    org.springframework: WARN
+    org.hswebframework: WARN
+    org.hswebframework.web: DEBUG

+ 17 - 0
hsweb-tests/pom.xml

@@ -27,6 +27,23 @@
             <artifactId>druid</artifactId>
             <version>1.0.26</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.spockframework</groupId>
+            <artifactId>spock-core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.spockframework</groupId>
+            <artifactId>spock-spring</artifactId>
+        </dependency>
+        <!-- https://mvnrepository.com/artifact/com.athaydes/spock-reports -->
+        <dependency>
+            <groupId>com.athaydes</groupId>
+            <artifactId>spock-reports</artifactId>
+            <version>1.2.13</version>
+            <!--<scope>test</scope>-->
+        </dependency>
         <dependency>
             <groupId>org.hswebframework.web</groupId>
             <artifactId>hsweb-spring-boot-starter</artifactId>