df6c4f41b5bc757905058598abb4489791c3221b
[aaa.git] / aaa-authn-basic / src / test / java / org / opendaylight / aaa / basic / HttpBasicAuthTest.java
1 /*
2  * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
3  * All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.aaa.basic;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import java.io.UnsupportedEncodingException;
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.aaa.ClaimBuilder;
25 import org.opendaylight.aaa.PasswordCredentialBuilder;
26 import org.opendaylight.aaa.api.AuthenticationException;
27 import org.opendaylight.aaa.api.Claim;
28 import org.opendaylight.aaa.api.CredentialAuth;
29
30 import com.sun.jersey.core.util.Base64;
31
32 public class HttpBasicAuthTest {
33     private static final String USERNAME = "admin";
34     private static final String PASSWORD = "odl";
35     private HttpBasicAuth auth;
36
37     @SuppressWarnings("unchecked")
38     @Before
39     public void setup() {
40         auth = new HttpBasicAuth();
41         auth.ca = mock(CredentialAuth.class);
42         when(
43                 auth.ca.authenticate(new PasswordCredentialBuilder()
44                         .setUserName(USERNAME).setPassword(PASSWORD).build(),
45                         null)).thenReturn(
46                 new ClaimBuilder().setUser("admin").addRole("admin").build());
47         when(
48                 auth.ca.authenticate(new PasswordCredentialBuilder()
49                         .setUserName(USERNAME).setPassword("bozo").build(),
50                         null)).thenThrow(new AuthenticationException("barf"));
51     }
52
53     @Test
54     public void testValidateOk() throws UnsupportedEncodingException {
55         String data = USERNAME + ":" + PASSWORD;
56         Map<String, List<String>> headers = new HashMap<>();
57         headers.put(
58                 "Authorization",
59                 Arrays.asList("Basic "
60                         + new String(Base64.encode(data.getBytes("utf-8")))));
61         Claim claim = auth.validate(headers);
62         assertNotNull(claim);
63         assertEquals(USERNAME, claim.user());
64         assertEquals("admin", claim.roles().iterator().next());
65     }
66
67     @Test(expected = AuthenticationException.class)
68     public void testValidateBadPassword() throws UnsupportedEncodingException {
69         String data = USERNAME + ":bozo";
70         Map<String, List<String>> headers = new HashMap<>();
71         headers.put(
72                 "Authorization",
73                 Arrays.asList("Basic "
74                         + new String(Base64.encode(data.getBytes("utf-8")))));
75         auth.validate(headers);
76     }
77 }