|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2020 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.
|
|
19 | 19 |
|
20 | 20 | import org.junit.*;
|
21 | 21 | import org.springframework.context.ApplicationEventPublisher;
|
| 22 | +import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent; |
22 | 23 | import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
|
23 | 24 | import org.springframework.security.authentication.event.AuthenticationFailureCredentialsExpiredEvent;
|
24 | 25 | import org.springframework.security.authentication.event.AuthenticationFailureDisabledEvent;
|
@@ -138,6 +139,47 @@ public void unknownFailureExceptionIsIgnored() {
|
138 | 139 | verifyZeroInteractions(appPublisher);
|
139 | 140 | }
|
140 | 141 |
|
| 142 | + @Test(expected = IllegalArgumentException.class) |
| 143 | + public void emptyMapCausesException() { |
| 144 | + Map<Class<? extends AuthenticationException>, |
| 145 | + Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>(); |
| 146 | + publisher = new DefaultAuthenticationEventPublisher(); |
| 147 | + publisher.setAdditionalExceptionMappings(mappings); |
| 148 | + } |
| 149 | + |
| 150 | + @Test(expected = IllegalArgumentException.class) |
| 151 | + public void missingExceptionClassCausesException() { |
| 152 | + Map<Class<? extends AuthenticationException>, |
| 153 | + Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>(); |
| 154 | + mappings.put(null, AuthenticationFailureLockedEvent.class); |
| 155 | + publisher = new DefaultAuthenticationEventPublisher(); |
| 156 | + publisher.setAdditionalExceptionMappings(mappings); |
| 157 | + } |
| 158 | + |
| 159 | + @Test(expected = IllegalArgumentException.class) |
| 160 | + public void missingEventClassAsMapValueCausesException() { |
| 161 | + Map<Class<? extends AuthenticationException>, |
| 162 | + Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>(); |
| 163 | + mappings.put(LockedException.class, null); |
| 164 | + publisher = new DefaultAuthenticationEventPublisher(); |
| 165 | + publisher.setAdditionalExceptionMappings(mappings); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void additionalExceptionMappingsUsingMapAreSupported() { |
| 170 | + publisher = new DefaultAuthenticationEventPublisher(); |
| 171 | + Map<Class<? extends AuthenticationException>, |
| 172 | + Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>(); |
| 173 | + mappings.put(MockAuthenticationException.class,AuthenticationFailureDisabledEvent.class); |
| 174 | + publisher.setAdditionalExceptionMappings(mappings); |
| 175 | + ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class); |
| 176 | + |
| 177 | + publisher.setApplicationEventPublisher(appPublisher); |
| 178 | + publisher.publishAuthenticationFailure(new MockAuthenticationException("test"), |
| 179 | + mock(Authentication.class)); |
| 180 | + verify(appPublisher).publishEvent(isA(AuthenticationFailureDisabledEvent.class)); |
| 181 | + } |
| 182 | + |
141 | 183 | @Test(expected = IllegalArgumentException.class)
|
142 | 184 | public void defaultAuthenticationFailureEventClassSetNullThen() {
|
143 | 185 | publisher = new DefaultAuthenticationEventPublisher();
|
|
0 commit comments