Skip to content

Commit

Permalink
优化test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Oct 12, 2018
1 parent 1f171a2 commit d053a44
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.hswebframework.web.commons.entity.CloneableEntity;

/**
* TODO 完成注释
*
* @author zhouhao
*/
public class DataAccessEntity implements CloneableEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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.hswebframework.web.authorization.access.DataAccessConfig
import org.hswebframework.web.tests.HswebCrudWebApiSpecification
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
Expand All @@ -27,32 +29,26 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.

/**
* @author zhouhao
* @since
* @since 3.0
*/
@WebAppConfiguration
@ContextConfiguration
@SpringBootTest(classes = [TestApplication.class], properties = ["classpath:application.yml"])
class UserSettingControllerTest extends Specification {
@Autowired
private ConfigurableApplicationContext context;

@Shared
private MockMvc mockMvc;
class UserSettingControllerTest extends HswebCrudWebApiSpecification {

@Autowired
private AuthenticationInitializeService initializeService;

void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
@Override
protected String getBaseApi() {
return "/autz-setting"
}


def "Add Permission"() {
def permissions = [
[
"id" : "user",
"name" : "用户管理",
"actions": [["action": "query", "describe": "查询"], ["action": "update", "describe": "修改"]]

"id" : "user",
"name" : "用户管理",
"actions" : [["action": "query", "describe": "查询"], ["action": "update", "describe": "修改"]],
"supportDataAccessTypes": [DataAccessConfig.DefaultType.DENY_FIELDS]
],
[
"id" : "role",
Expand Down Expand Up @@ -99,45 +95,52 @@ class UserSettingControllerTest extends Specification {
"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 :
doAddRequest(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
permissionId: "user", //赋予user权限
actions : ["query", "update"],
status : 1,
dataAccesses: [ //数据权限,控制查询的时候不能查询password字段
[
type : DataAccessConfig.DefaultType.DENY_FIELDS,
action: "query",
config: """{"fields": ["password"]}"""
]
]
],
menus :
[
[
menuId: "user-menu"
],
[
menuId: "role-menu"
]
permissionId: "role", //赋予role权限
actions : ["query", "get"],
status : 1
]
])
)).andDo({ result -> println result.response.contentAsString })
// .andExpect(status().is(201))
],
menus :
[
[
menuId: "user-menu"
],
[
menuId: "role-menu"
]
]
]))

expect:
userId != null
def autz = initializeService.initUserAuthorization(userId)
autz != null
autz.hasPermission("user", "query")
autz.hasPermission("role", "query", "get")

autz.getPermission("user")
.map({ per -> per.findDenyFields("query") })
.map({ fields -> fields.contains("password") })
.orElse(false)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package org.hswebframework.web.tests

import com.alibaba.fastjson.JSON
import org.hswebframework.ezorm.core.dsl.Query
import org.hswebframework.web.WebUtil
import org.hswebframework.web.commons.entity.param.QueryParamEntity
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*


/**
* @author zhouhao
* @since 3.0.2
*/
abstract class HswebCrudWebApiSpecification extends HswebSpecification {

protected abstract String getBaseApi();

def doAddRequest(String requestBody) {
def response = mockMvc.perform(post(getBaseApi())
.content(requestBody)
.contentType(MediaType.APPLICATION_JSON))
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}

def doUpdateRequest(String id, String requestBody) {
def response = mockMvc.perform(put("${getBaseApi()}/{id}", id)
.content(requestBody)
.contentType(MediaType.APPLICATION_JSON))
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}

def doDeleteRequest(String id) {
def response = mockMvc
.perform(delete("${getBaseApi()}/{id}", id))
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}

def doGetRequest(String id) {
def response = mockMvc
.perform(get("${getBaseApi()}/{id}", id))
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}


Query createQuery() {
return Query.empty(new QueryParamEntity());
}

def doQueryRequest(Query query) {
MockHttpServletRequestBuilder get = get("${getBaseApi()}")
WebUtil.objectToHttpParameters(query.param)
.forEach({ k, v -> get.param(k, v) })
def response = mockMvc
.perform(get)
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}

def doQueryByIdsRequest(String ids) {
def response = mockMvc
.perform(get("${getBaseApi()}/ids").param("ids", ids))
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}

def doTotalRequest(Query query) {
MockHttpServletRequestBuilder get = get("${getBaseApi()}/total")
WebUtil.objectToHttpParameters(query.param)
.forEach({ k, v -> get.param(k, v) })

def response = mockMvc
.perform(get)
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}

def doCountRequest(Query query) {
MockHttpServletRequestBuilder get = get("${getBaseApi()}/count")
WebUtil.objectToHttpParameters(query.param)
.forEach({ k, v -> get.param(k, v) })
def response = mockMvc
.perform(get)
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}

def doNoPagingRequest(Query query) {
MockHttpServletRequestBuilder get = get("${getBaseApi()}/no-paging")
WebUtil.objectToHttpParameters(query.param)
.forEach({ k, v -> get.param(k, v) })
def response = mockMvc
.perform(get)
.andReturn()
.response
.contentAsString;
return JSON.parseObject(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.hswebframework.web.tests

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.ConfigurableApplicationContext
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.web.WebAppConfiguration
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import spock.lang.Shared
import spock.lang.Specification

/**
* @author zhouhao
* @since 3.0.2
*/
@WebAppConfiguration
@ContextConfiguration
@SpringBootTest(classes = [HswebTestApplication.class], properties = ["classpath:application.yml"])
class HswebSpecification extends Specification {
@Autowired
protected ConfigurableApplicationContext context;

@Shared
protected MockMvc mockMvc;

void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.hswebframework.web.tests;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.web.WebAppConfiguration;

/**
* @author zhouhao
* @since 3.0.2
*/
@SpringBootApplication
@WebAppConfiguration
@Configuration
class HswebTestApplication {
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
* TODO 完成注释
* 已弃用,请使用 {@link HswebSpecification} {@link HswebCrudWebApiSpecification }
*
* @author zhouhao
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SimpleWebApplicationTests.Config.class)
@Deprecated
public class SimpleWebApplicationTests extends AbstractTransactionalJUnit4SpringContextTests {

protected MockMvc mvc;
Expand Down
Loading

0 comments on commit d053a44

Please sign in to comment.