优化tests

This commit is contained in:
zhouhao
2018-09-25 11:50:40 +08:00
parent dba67fa0c0
commit e7bb73af6c
7 changed files with 97 additions and 8 deletions

View File

@@ -78,7 +78,6 @@ public class AuthorizingHandlerAutoConfiguration {
}
@Bean
@ConditionalOnProperty("hsweb.authorize.allows")
public UserAllowPermissionHandler userAllowPermissionHandler() {
return new UserAllowPermissionHandler();
}

View File

@@ -3,6 +3,7 @@ package org.hswebframework.web.authorization.basic.handler;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.web.authorization.define.AuthorizingContext;
import org.hswebframework.web.authorization.define.HandleType;
import org.hswebframework.web.authorization.listener.event.AuthorizingHandleBeforeEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.event.EventListener;
@@ -39,11 +40,13 @@ public class UserAllowPermissionHandler {
@EventListener
public void handEvent(AuthorizingHandleBeforeEvent event) {
AuthorizingContext context = event.getContext();
if (allows.isEmpty()) {
if (allows.isEmpty() || event.getHandleType() == HandleType.DATA) {
return;
}
// package.method
AuthorizingContext context = event.getContext();
// class full name.method
String path = ClassUtils.getUserClass(context.getParamContext()
.getTarget())
.getName().concat(".")

View File

@@ -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;

View File

@@ -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

View File

@@ -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();
}
}

View File

@@ -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()
}
}

View File

@@ -15,6 +15,10 @@ hsweb:
app:
name: hsweb-oauth2 客户端示例
version: 3.0.0
authorize:
allows:
users:
admin: "**.TestController.*"
users:
admin:
name: 超级管理员