mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-05-31 09:53:21 +08:00
优化tests
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package org.hswebframework.web.authorization.basic.embed;
|
||||
package org.hswebframework.web.authorization;
|
||||
|
||||
import org.hswebframework.web.authorization.basic.configuration.AopAuthorizeAutoConfiguration;
|
||||
import org.hswebframework.web.authorization.basic.configuration.AuthorizingHandlerAutoConfiguration;
|
||||
import org.hswebframework.web.authorization.basic.configuration.EnableAopAuthorize;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.hswebframework.web.authorization.basic.embed
|
||||
|
||||
import org.hswebframework.web.authorization.Authentication
|
||||
import org.hswebframework.web.authorization.AuthenticationManager
|
||||
import org.hswebframework.web.authorization.TestApplication
|
||||
import org.hswebframework.web.authorization.simple.PlainTextUsernamePasswordAuthenticationRequest
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.hswebframework.web.authorization.basic.handler;
|
||||
|
||||
import org.hswebframework.web.authorization.annotation.Authorize;
|
||||
import org.hswebframework.web.controller.message.ResponseMessage;
|
||||
|
||||
/**
|
||||
* @author zhouhao
|
||||
* @since 3.0.1
|
||||
*/
|
||||
public class TestController {
|
||||
|
||||
public ResponseMessage<String> query() {
|
||||
return ResponseMessage.ok();
|
||||
}
|
||||
|
||||
public ResponseMessage<String> update() {
|
||||
return ResponseMessage.ok();
|
||||
}
|
||||
|
||||
public ResponseMessage<String> delete() {
|
||||
return ResponseMessage.ok();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.hswebframework.web.authorization.basic.handler
|
||||
|
||||
import org.hswebframework.web.authorization.Authentication
|
||||
import org.hswebframework.web.authorization.AuthenticationManager
|
||||
import org.hswebframework.web.authorization.TestApplication
|
||||
import org.hswebframework.web.authorization.basic.define.EmptyAuthorizeDefinition
|
||||
import org.hswebframework.web.authorization.define.AuthorizeDefinition
|
||||
import org.hswebframework.web.authorization.define.AuthorizingContext
|
||||
import org.hswebframework.web.authorization.define.HandleType
|
||||
import org.hswebframework.web.authorization.listener.event.AuthorizingHandleBeforeEvent
|
||||
import org.hswebframework.web.authorization.simple.PlainTextUsernamePasswordAuthenticationRequest
|
||||
import org.hswebframework.web.boost.aop.context.MethodInterceptorContext
|
||||
import org.hswebframework.web.boost.aop.context.MethodInterceptorHolder
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
import org.springframework.test.context.web.WebAppConfiguration
|
||||
import spock.lang.Specification
|
||||
|
||||
/**
|
||||
* @author zhouhao
|
||||
* @since 3.0.1
|
||||
*/
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration
|
||||
@SpringBootTest(classes = [TestApplication.class], properties = ["classpath:application.yml"])
|
||||
class UserAllowPermissionHandlerTest extends Specification {
|
||||
|
||||
@Autowired
|
||||
UserAllowPermissionHandler handler;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationManager manager;
|
||||
|
||||
def createMethodInterceptorContext(TestController controller, String name) {
|
||||
return new MethodInterceptorHolder(
|
||||
"test"
|
||||
, TestController.class.getMethod(name)
|
||||
, controller
|
||||
, new HashMap<String, Object>())
|
||||
.createParamContext()
|
||||
}
|
||||
|
||||
def "Test"() {
|
||||
setup:
|
||||
def authentication = manager.authenticate(new PlainTextUsernamePasswordAuthenticationRequest("admin", "admin"));
|
||||
def definition = EmptyAuthorizeDefinition.instance;
|
||||
def controller = new TestController();
|
||||
def context = createMethodInterceptorContext(controller, "query");
|
||||
def authorizingContext = new AuthorizingContext(
|
||||
authentication: authentication
|
||||
, definition: definition
|
||||
, paramContext: context);
|
||||
def event = new AuthorizingHandleBeforeEvent(authorizingContext, HandleType.RBAC);
|
||||
handler.handEvent(event);
|
||||
expect:
|
||||
authentication != null
|
||||
event.isAllow()
|
||||
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,10 @@ hsweb:
|
||||
app:
|
||||
name: hsweb-oauth2 客户端示例
|
||||
version: 3.0.0
|
||||
authorize:
|
||||
allows:
|
||||
users:
|
||||
admin: "**.TestController.*"
|
||||
users:
|
||||
admin:
|
||||
name: 超级管理员
|
||||
|
||||
Reference in New Issue
Block a user