1 package com.atlassian.asap.core.client.jersey;
2
3 import com.atlassian.asap.api.Jwt;
4 import com.atlassian.asap.api.JwtBuilder;
5 import com.atlassian.asap.api.client.http.AuthorizationHeaderGenerator;
6 import com.atlassian.asap.api.exception.CannotRetrieveKeyException;
7 import org.glassfish.jersey.client.ClientRequest;
8 import org.hamcrest.Description;
9 import org.hamcrest.TypeSafeMatcher;
10 import org.junit.Before;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.mockito.Mock;
14 import org.mockito.junit.MockitoJUnit;
15 import org.mockito.junit.MockitoRule;
16
17 import javax.ws.rs.core.HttpHeaders;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.core.Response;
20
21 import static com.atlassian.asap.core.client.jersey.AsapAuthenticationFilter.CLIENT_ASAP_TOKEN_ERROR_MESSAGE;
22 import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Matchers.anyString;
25 import static org.mockito.Matchers.argThat;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 public class AsapAuthenticationFilterTest {
32 private static final String AUDIENCE = "my-audience";
33 private static final String ISSUER = "my-issuer";
34 private static final String KEY_ID = "my-key-id";
35 private static final String AUTHORIZATION_HEADER_VALUE = "some-value";
36
37 @Rule
38 public MockitoRule mockitoJunitRule = MockitoJUnit.rule();
39
40 @Mock
41 private AuthorizationHeaderGenerator authorizationHeaderGenerator;
42 @Mock
43 private ClientRequest clientRequest;
44 @Mock
45 private MultivaluedMap<String, Object> headers;
46
47 private AsapAuthenticationFilter asapAuthenticationFilter;
48
49 @Before
50 public void setup() {
51 when(clientRequest.getHeaders()).thenReturn(headers);
52 asapAuthenticationFilter = new AsapAuthenticationFilter(
53 JwtBuilder.newJwt()
54 .audience(AUDIENCE)
55 .issuer(ISSUER)
56 .keyId(KEY_ID)
57 .build(),
58 authorizationHeaderGenerator);
59 }
60
61 @Test
62 public void shouldAddHeaderIfDoesNotExist() throws Exception {
63 when(headers.containsKey(HttpHeaders.AUTHORIZATION)).thenReturn(false);
64 when(authorizationHeaderGenerator.generateAuthorizationHeader(any(Jwt.class))).thenReturn(AUTHORIZATION_HEADER_VALUE);
65
66 asapAuthenticationFilter.filter(clientRequest);
67
68 verify(headers).add(eq(HttpHeaders.AUTHORIZATION), eq(AUTHORIZATION_HEADER_VALUE));
69 verify(authorizationHeaderGenerator).generateAuthorizationHeader(any(Jwt.class));
70 }
71
72 @Test
73 public void shouldNotAddHeaderIfItExists() throws Exception {
74 when(headers.containsKey(HttpHeaders.AUTHORIZATION)).thenReturn(true);
75
76 asapAuthenticationFilter.filter(clientRequest);
77
78 verify(headers, never()).add(anyString(), anyString());
79 verify(authorizationHeaderGenerator, never()).generateAuthorizationHeader(any(Jwt.class));
80 }
81
82 @Test
83 public void shouldReturnUnauthorizedResponseIfTokenCanNotBeGenerated() throws Exception {
84 when(headers.containsKey(HttpHeaders.AUTHORIZATION)).thenReturn(false);
85 when(authorizationHeaderGenerator.generateAuthorizationHeader(any(Jwt.class))).thenThrow(new CannotRetrieveKeyException(""));
86
87 asapAuthenticationFilter.filter(clientRequest);
88
89 verify(headers, never()).add(anyString(), anyString());
90 verify(clientRequest).abortWith(argThat(new UnauthorizedResponseMatcher()));
91 verify(authorizationHeaderGenerator).generateAuthorizationHeader(any(Jwt.class));
92 }
93
94 private static class UnauthorizedResponseMatcher extends TypeSafeMatcher<Response> {
95 @Override
96 public void describeTo(Description description) {
97 description.appendText("UNAUTHORIZED Response");
98 }
99
100 @Override
101 protected boolean matchesSafely(Response response) {
102 return UNAUTHORIZED.equals(Response.Status.fromStatusCode(response.getStatus())) &&
103 CLIENT_ASAP_TOKEN_ERROR_MESSAGE.equals(response.getEntity());
104 }
105 }
106 }