Completed
Push — master ( 039122...d1b12d )
by Misagh
48:00 queued 22:11
created

handleUnsatisfiedAuthenticationPolicyExceptionByDefault()   A

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
c 1
b 1
f 0
dl 0
loc 13
rs 9.4285
1
package org.apereo.cas.web.flow.actions;
2
3
import org.apereo.cas.authentication.Authentication;
4
import org.apereo.cas.authentication.AuthenticationException;
5
import org.apereo.cas.authentication.ContextualAuthenticationPolicy;
6
import org.apereo.cas.ticket.InvalidTicketException;
7
import org.apereo.cas.ticket.UnsatisfiedAuthenticationPolicyException;
8
import org.apereo.cas.util.CollectionUtils;
9
import org.junit.Test;
10
import org.junit.runner.RunWith;
11
import org.junit.runners.JUnit4;
12
import org.mockito.ArgumentCaptor;
13
import org.springframework.binding.message.DefaultMessageResolver;
14
import org.springframework.webflow.test.MockRequestContext;
15
16
import javax.security.auth.login.AccountLockedException;
17
import javax.security.auth.login.AccountNotFoundException;
18
import java.security.GeneralSecurityException;
19
import java.util.HashMap;
20
import java.util.Map;
21
import java.util.Optional;
22
23
import static org.junit.Assert.*;
24
import static org.mockito.Mockito.*;
25
26
/**
27
 * @author Marvin S. Addison
28
 * @since 4.0.0
29
 */
30
@RunWith(JUnit4.class)
31
public class AuthenticationExceptionHandlerActionTests {
32
 
33
    @Test
34
    public void handleAccountNotFoundExceptionByDefault() {
35
        final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction(
36
                CollectionUtils.wrapSet(AccountLockedException.class,
37
                        AccountNotFoundException.class)
38
        );
39
        final MockRequestContext req = new MockRequestContext();
40
        
41
        final Map<String, Class<? extends Throwable>> map = new HashMap<>();
42
        map.put("notFound", AccountNotFoundException.class);
43
        final String id = handler.handle(new AuthenticationException(map), req);
44
        assertEquals(id, AccountNotFoundException.class.getSimpleName());
45
    }
46
47
    @Test
48
    public void handleUnknownExceptionByDefault() {
49
        final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction();
50
        final MockRequestContext req = new MockRequestContext();
51
        final Map<String, Class<? extends Throwable>> map = new HashMap<>();
52
        map.put("unknown", GeneralSecurityException.class);
53
        final String id = handler.handle(new AuthenticationException(map), req);
54
        assertEquals(id, "UNKNOWN");
55
    }
56
57
    @Test
58
    public void handleUnknownTicketExceptionByDefault() {
59
        final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction();
60
        final MockRequestContext req = new MockRequestContext();
61
62
        final String id = handler.handle(new InvalidTicketException("TGT"), req);
63
        assertEquals(id, "UNKNOWN");
64
        verifyZeroInteractions(req);
65
    }
66
    
67
    @Test
68
    public void handleUnsatisfiedAuthenticationPolicyExceptionByDefault() {
69
        final AuthenticationExceptionHandlerAction handler = new AuthenticationExceptionHandlerAction(
70
                CollectionUtils.wrapSet(UnsatisfiedAuthenticationPolicyException.class,
71
                        AccountNotFoundException.class)
72
        );
73
        final MockRequestContext req = new MockRequestContext();
74
75
        final ContextualAuthenticationPolicy<?> policy = new TestContextualAuthenticationPolicy();
76
        final String id = handler.handle(new UnsatisfiedAuthenticationPolicyException(policy), req);
77
        assertEquals(id, "UnsatisfiedAuthenticationPolicyException");
78
        final ArgumentCaptor<DefaultMessageResolver> message = ArgumentCaptor.forClass(DefaultMessageResolver.class);
79
        assertArrayEquals(new String[]{policy.getCode().get()}, message.getValue().getCodes());
80
    }
81
82
    private static class TestContextualAuthenticationPolicy implements ContextualAuthenticationPolicy<Object> {
83
        @Override
84
        public Optional<String> getCode() {
85
            return Optional.of("CUSTOM_CODE");
86
        }
87
88
        @Override
89
        public Object getContext() {
90
            return null;
91
        }
92
93
        @Override
94
        public boolean isSatisfiedBy(final Authentication authentication) {
95
            return false;
96
        }
97
    }
98
}
99