Skip to content

Commit a5633be

Browse files
committed
优化测试
1 parent 7e3f7c6 commit a5633be

File tree

7 files changed

+183
-4
lines changed

7 files changed

+183
-4
lines changed

hsweb-authorization/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
package org.hswebframework.web.entity.authorization;
2020

21+
import org.hibernate.validator.constraints.NotBlank;
2122
import org.hswebframework.web.commons.entity.GenericEntity;
2223
import org.hswebframework.web.commons.entity.RecordCreationEntity;
24+
import org.hswebframework.web.validator.group.CreateGroup;
2325

2426
/**
2527
* @author zhouhao
@@ -41,6 +43,7 @@ public interface UserEntity extends GenericEntity<String>, RecordCreationEntity
4143

4244
void setUsername(String username);
4345

46+
@NotBlank(groups = CreateGroup.class)
4447
String getName();
4548

4649
void setPassword(String password);

hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<artifactId>h2</artifactId>
7474
<scope>test</scope>
7575
</dependency>
76+
7677
<dependency>
7778
<groupId>com.alibaba</groupId>
7879
<artifactId>druid</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.hswebframework.web.authorization.starter;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.test.context.web.WebAppConfiguration;
5+
6+
/**
7+
* @author zhouhao
8+
* @since 3.0.0-RC
9+
*/
10+
@SpringBootApplication
11+
@WebAppConfiguration
12+
public class TestApplication {
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package org.hswebframework.web.authorization.starter
2+
3+
import com.alibaba.fastjson.JSON
4+
import org.hswebframework.web.authorization.Authentication
5+
import org.hswebframework.web.authorization.AuthenticationInitializeService
6+
import org.junit.runner.RunWith
7+
import org.springframework.beans.factory.annotation.Autowired
8+
import org.springframework.boot.test.context.SpringBootTest
9+
import org.springframework.context.ConfigurableApplicationContext
10+
import org.springframework.http.MediaType
11+
import org.springframework.test.context.ContextConfiguration
12+
import org.springframework.test.context.junit4.SpringRunner
13+
import org.springframework.test.context.web.WebAppConfiguration
14+
import org.springframework.test.web.servlet.MockMvc
15+
import org.springframework.test.web.servlet.MvcResult
16+
import org.springframework.test.web.servlet.ResultHandler
17+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
18+
import org.springframework.test.web.servlet.setup.MockMvcBuilders
19+
import spock.lang.Shared
20+
import spock.lang.Specification
21+
22+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
23+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
26+
27+
/**
28+
* @author zhouhao
29+
* @since
30+
*/
31+
@WebAppConfiguration
32+
@ContextConfiguration
33+
@SpringBootTest(classes = [TestApplication.class], properties = ["classpath:application.yml"])
34+
class UserSettingControllerTest extends Specification {
35+
@Autowired
36+
private ConfigurableApplicationContext context;
37+
38+
@Shared
39+
private MockMvc mockMvc;
40+
41+
@Autowired
42+
private AuthenticationInitializeService initializeService;
43+
44+
void setup() {
45+
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
46+
}
47+
48+
def "Add Permission"() {
49+
def permissions = [
50+
[
51+
"id" : "user",
52+
"name" : "用户管理",
53+
"actions": [["action": "query", "describe": "查询"], ["action": "update", "describe": "修改"]]
54+
55+
],
56+
[
57+
"id" : "role",
58+
"name" : "角色管理",
59+
"actions": [["action": "query", "describe": "查询"], ["action": "get", "describe": "查看详情"]]
60+
61+
]
62+
]
63+
permissions.forEach({ permission ->
64+
//添加权限
65+
mockMvc.perform(post("/permission")
66+
.contentType(MediaType.APPLICATION_JSON)
67+
.content(JSON.toJSONString(permission)))
68+
.andExpect(status().is(201))
69+
})
70+
}
71+
72+
def "Add User"() {
73+
//添加用户先
74+
String result = mockMvc
75+
.perform(
76+
post("/user")
77+
.contentType(MediaType.APPLICATION_JSON)
78+
.content(
79+
"""
80+
{
81+
"name":"admin",
82+
"username":"admin",
83+
"password":"admin"
84+
}
85+
"""
86+
))
87+
.andExpect(status().is(201))
88+
.andReturn()
89+
.getResponse()
90+
.getContentAsString()
91+
return JSON.parseObject(result).getString("result")
92+
}
93+
94+
def "Add User Authentication Setting"() {
95+
setup:
96+
"Add Permission"()
97+
def userId = "Add User"()
98+
//添加用户权限
99+
mockMvc.perform(
100+
post("/autz-setting")
101+
.contentType(MediaType.APPLICATION_JSON)
102+
.content(JSON.toJSONString
103+
([
104+
type : "user", //设置类型:user
105+
settingFor: userId, //设置给具体的user
106+
describe : "测试",
107+
details :
108+
[
109+
[
110+
permissionId: "user", //赋予user权限
111+
actions : ["query", "update"],
112+
status : 1
113+
],
114+
[
115+
permissionId: "role", //赋予role权限
116+
actions : ["query", "get"],
117+
status : 1
118+
]
119+
],
120+
menus :
121+
[
122+
[
123+
menuId: "user-menu"
124+
],
125+
[
126+
menuId: "role-menu"
127+
]
128+
]
129+
])
130+
)).andExpect(status().is(201))
131+
expect:
132+
userId != null
133+
def autz = initializeService.initUserAuthorization(userId)
134+
autz != null
135+
autz.hasPermission("user", "query")
136+
autz.hasPermission("role", "query", "get")
137+
138+
}
139+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ hsweb:
1313
version: 3.0.0
1414
authorize:
1515
sync: true
16-
auto-parse: true
16+
auto-parse: true
17+
logging:
18+
level:
19+
org.springframework: WARN
20+
org.hswebframework: WARN
21+
org.hswebframework.web: DEBUG

hsweb-tests/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727
<artifactId>druid</artifactId>
2828
<version>1.0.26</version>
2929
</dependency>
30+
31+
<dependency>
32+
<groupId>org.spockframework</groupId>
33+
<artifactId>spock-core</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.spockframework</groupId>
38+
<artifactId>spock-spring</artifactId>
39+
</dependency>
40+
<!-- https://mvnrepository.com/artifact/com.athaydes/spock-reports -->
41+
<dependency>
42+
<groupId>com.athaydes</groupId>
43+
<artifactId>spock-reports</artifactId>
44+
<version>1.2.13</version>
45+
<!--<scope>test</scope>-->
46+
</dependency>
3047
<dependency>
3148
<groupId>org.hswebframework.web</groupId>
3249
<artifactId>hsweb-spring-boot-starter</artifactId>

0 commit comments

Comments
 (0)