1
2
3
4
5
6
7 package waffle.servlet.spi;
8
9 import java.util.Arrays;
10 import java.util.List;
11
12 import javax.servlet.http.HttpServletResponse;
13
14 import mockit.Mocked;
15 import mockit.Verifications;
16
17 import org.junit.jupiter.api.Assertions;
18 import org.junit.jupiter.api.BeforeEach;
19 import org.junit.jupiter.api.Test;
20
21 import waffle.windows.auth.IWindowsAuthProvider;
22
23
24
25
26 class NegotiateSecurityFilterProviderTest {
27
28
29 @Mocked
30 private IWindowsAuthProvider auth;
31
32
33 @Mocked
34 private HttpServletResponse response;
35
36
37 private NegotiateSecurityFilterProvider provider;
38
39
40
41
42 @BeforeEach
43 void setUp() {
44 this.provider = new NegotiateSecurityFilterProvider(this.auth);
45 }
46
47
48
49
50 @Test
51 void testGetProtocolsDefault() {
52 final List<String> protocols = this.provider.getProtocols();
53 Assertions.assertEquals(2, protocols.size());
54 Assertions.assertTrue(protocols.contains("Negotiate"));
55 Assertions.assertTrue(protocols.contains("NTLM"));
56 }
57
58
59
60
61 @Test
62 void testSetProtocols() {
63 final List<String> newProtocols = Arrays.asList("NTLM");
64 this.provider.setProtocols(newProtocols);
65 Assertions.assertEquals(1, this.provider.getProtocols().size());
66 Assertions.assertTrue(this.provider.getProtocols().contains("NTLM"));
67 }
68
69
70
71
72 @Test
73 void testIsSecurityPackageSupportedNegotiate() {
74 Assertions.assertTrue(this.provider.isSecurityPackageSupported("Negotiate"));
75 Assertions.assertTrue(this.provider.isSecurityPackageSupported("negotiate"));
76 Assertions.assertTrue(this.provider.isSecurityPackageSupported("NEGOTIATE"));
77 }
78
79
80
81
82 @Test
83 void testIsSecurityPackageSupportedNtlm() {
84 Assertions.assertTrue(this.provider.isSecurityPackageSupported("NTLM"));
85 Assertions.assertTrue(this.provider.isSecurityPackageSupported("ntlm"));
86 }
87
88
89
90
91 @Test
92 void testIsSecurityPackageSupportedUnsupported() {
93 Assertions.assertFalse(this.provider.isSecurityPackageSupported("Basic"));
94 Assertions.assertFalse(this.provider.isSecurityPackageSupported("Bearer"));
95 }
96
97
98
99
100 @Test
101 void testSendUnauthorized() {
102 this.provider.sendUnauthorized(this.response);
103 new Verifications() {
104 {
105 NegotiateSecurityFilterProviderTest.this.response.addHeader("WWW-Authenticate", "Negotiate");
106 this.times = 1;
107 NegotiateSecurityFilterProviderTest.this.response.addHeader("WWW-Authenticate", "NTLM");
108 this.times = 1;
109 }
110 };
111 }
112
113
114
115
116 @Test
117 void testSendUnauthorizedWithCustomProtocols() {
118 this.provider.setProtocols(Arrays.asList("Negotiate"));
119 this.provider.sendUnauthorized(this.response);
120 new Verifications() {
121 {
122 NegotiateSecurityFilterProviderTest.this.response.addHeader("WWW-Authenticate", "Negotiate");
123 this.times = 1;
124 NegotiateSecurityFilterProviderTest.this.response.addHeader("WWW-Authenticate", "NTLM");
125 this.times = 0;
126 }
127 };
128 }
129
130
131
132
133 @Test
134 void testInitParameterProtocols() {
135 this.provider.initParameter("protocols", "NTLM");
136 Assertions.assertEquals(1, this.provider.getProtocols().size());
137 Assertions.assertTrue(this.provider.getProtocols().contains("NTLM"));
138 }
139
140
141
142
143 @Test
144 void testInitParameterProtocolsWhitespaceSeparated() {
145 this.provider.initParameter("protocols", "Negotiate NTLM");
146 Assertions.assertEquals(2, this.provider.getProtocols().size());
147 }
148
149
150
151
152 @Test
153 void testInitParameterUnsupportedProtocolThrows() {
154 Assertions.assertThrows(RuntimeException.class, () -> this.provider.initParameter("protocols", "Basic"));
155 }
156
157
158
159
160 @Test
161 void testInitParameterUnknownParameterThrows() {
162 Assertions.assertThrows(Exception.class, () -> this.provider.initParameter("unknownParam", "value"));
163 }
164 }