|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | import org.springframework.mock.web.MockHttpServletRequest;
|
24 | 24 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
25 | 25 | import org.springframework.security.authorization.AuthorizationDecision;
|
| 26 | +import org.springframework.security.authorization.AuthorizationEventPublisher; |
26 | 27 | import org.springframework.security.core.Authentication;
|
27 | 28 | import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
|
28 | 29 |
|
29 | 30 | import static org.assertj.core.api.Assertions.assertThat;
|
30 | 31 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | +import static org.mockito.Mockito.verify; |
31 | 34 |
|
32 | 35 | /**
|
33 | 36 | * Tests for {@link DelegatingAuthorizationManager}.
|
34 | 37 | *
|
35 | 38 | * @author Evgeniy Cheban
|
| 39 | + * @author Parikshit Dutta |
36 | 40 | */
|
37 | 41 | public class DelegatingAuthorizationManagerTests {
|
38 | 42 |
|
@@ -81,4 +85,40 @@ public void checkWhenMultipleMappingsConfiguredThenDelegatesMatchingManager() {
|
81 | 85 | assertThat(abstain).isNull();
|
82 | 86 | }
|
83 | 87 |
|
| 88 | + @Test |
| 89 | + public void testAuthorizationEventPublisherIsNotNull() { |
| 90 | + DelegatingAuthorizationManager manager = DelegatingAuthorizationManager.builder() |
| 91 | + .add(new MvcRequestMatcher(null, "/grant"), (a, o) -> new AuthorizationDecision(true)).build(); |
| 92 | + assertThatIllegalArgumentException().isThrownBy(() -> manager.setAuthorizationEventPublisher(null)) |
| 93 | + .withMessage("AuthorizationEventPublisher cannot be null"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testAuthorizationSuccessEventWhenAuthorizationGranted() { |
| 98 | + DelegatingAuthorizationManager manager = DelegatingAuthorizationManager.builder() |
| 99 | + .add(new MvcRequestMatcher(null, "/grant"), (a, o) -> new AuthorizationDecision(true)).build(); |
| 100 | + |
| 101 | + AuthorizationEventPublisher authorizationEventPublisher = mock(AuthorizationEventPublisher.class); |
| 102 | + manager.setAuthorizationEventPublisher(authorizationEventPublisher); |
| 103 | + |
| 104 | + Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
| 105 | + |
| 106 | + AuthorizationDecision grant = manager.check(authentication, new MockHttpServletRequest(null, "/grant")); |
| 107 | + verify(authorizationEventPublisher).publishAuthorizationSuccess(grant); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testAuthorizationFailureEventWhenAuthorizationNotGranted() { |
| 112 | + DelegatingAuthorizationManager manager = DelegatingAuthorizationManager.builder() |
| 113 | + .add(new MvcRequestMatcher(null, "/deny"), (a, o) -> new AuthorizationDecision(false)).build(); |
| 114 | + |
| 115 | + AuthorizationEventPublisher authorizationEventPublisher = mock(AuthorizationEventPublisher.class); |
| 116 | + manager.setAuthorizationEventPublisher(authorizationEventPublisher); |
| 117 | + |
| 118 | + Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
| 119 | + |
| 120 | + AuthorizationDecision grant = manager.check(authentication, new MockHttpServletRequest(null, "/deny")); |
| 121 | + verify(authorizationEventPublisher).publishAuthorizationFailure(grant); |
| 122 | + } |
| 123 | + |
84 | 124 | }
|
0 commit comments