4c2a19e426f1a88194b5f223a4245b2e67f3fd27
[controller.git] / opendaylight / usermanager / api / src / test / java / org / opendaylight / controller / usermanager / AuthorizationUserConfigTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.usermanager;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
19 import org.opendaylight.controller.sal.authorization.UserLevel;
20 import org.opendaylight.controller.usermanager.AuthResponse;
21 import org.opendaylight.controller.usermanager.AuthorizationConfig;
22 import org.opendaylight.controller.usermanager.UserConfig;
23
24 /*
25  * This test case includes tests for UserConfig and the extending class AuthorizationConfig
26  */
27 public class AuthorizationUserConfigTest {
28
29     @Test
30     public void authorizationConfigTest() {
31         AuthorizationConfig authConfig;
32         List<String> roles = new ArrayList<String>();
33
34         // test isValid
35         roles.add(UserLevel.SYSTEMADMIN.toString());
36         authConfig = new AuthorizationConfig(null, roles);
37         assertFalse(authConfig.validate().isSuccess());
38         authConfig = new AuthorizationConfig("admin", new ArrayList<String>());
39         assertFalse(authConfig.validate().isSuccess());
40         authConfig = new AuthorizationConfig("admin", roles);
41         assertTrue(authConfig.validate().isSuccess());
42     }
43
44     @Test
45     public void userConfigTest() {
46         UserConfig userConfig;
47         List<String> roles = new ArrayList<String>();
48
49         roles.add(UserLevel.SYSTEMADMIN.toString());
50         userConfig = new UserConfig(null, "cisco", roles);
51         assertFalse(userConfig.validate().isSuccess());
52
53         roles.clear();
54         roles.add("cisco");
55         userConfig = new UserConfig("uname", "", roles);
56         assertFalse(userConfig.validate().isSuccess());
57
58         roles.clear();
59         roles.add(UserLevel.NETWORKOPERATOR.toString());
60         userConfig = new UserConfig("uname", "ciscocisco", roles);
61         assertTrue(userConfig.validate().isSuccess());
62
63         // currentPassword mismatch
64         assertFalse(userConfig.update("Cisco", "cisco123", roles).isSuccess());
65
66         // Role change only
67         roles.clear();
68         roles.add(UserLevel.NETWORKADMIN.toString());
69         assertTrue(userConfig.update("ciscocisco", null, roles).isSuccess());
70
71         // Role change and same new password
72         roles.clear();
73         roles.add(UserLevel.NETWORKOPERATOR.toString());
74         assertTrue(userConfig.update("ciscocisco", "ciscocisco", roles)
75                 .isSuccess());
76
77         // New Password = null, No change in password
78         assertTrue(userConfig.getPassword().equals(UserConfig.hash("ciscocisco")));
79
80         // Password changed successfully, no change in user role
81         assertTrue(userConfig.update("ciscocisco", "cisco123", roles)
82                 .isSuccess());
83         assertTrue(userConfig.getPassword().equals(UserConfig.hash("cisco123")));
84         assertTrue(userConfig.getRoles().get(0).equals(
85                 UserLevel.NETWORKOPERATOR.toString()));
86
87         // Password not changed, role changed successfully
88         roles.clear();
89         roles.add(UserLevel.SYSTEMADMIN.toString());
90         assertTrue(userConfig.update("cisco123", "cisco123", roles)
91                 .isSuccess());
92         assertTrue(userConfig.getPassword().equals(UserConfig.hash("cisco123")));
93         assertTrue(userConfig.getRoles().get(0)
94                 .equals(UserLevel.SYSTEMADMIN.toString()));
95
96         // Password and role changed successfully
97         assertTrue(userConfig.update("cisco123", "ciscocisco", roles)
98                 .isSuccess());
99         assertTrue(userConfig.getPassword().equals(UserConfig.hash("ciscocisco")));
100         assertTrue(userConfig.getRoles().get(0)
101                 .equals(UserLevel.SYSTEMADMIN.toString()));
102
103         String username = userConfig.getUser();
104         assertTrue(username.equals("uname"));
105
106         // test authenticate
107         AuthResponse authresp = userConfig.authenticate("ciscocisco");
108         assertTrue(authresp.getStatus().equals(AuthResultEnum.AUTH_ACCEPT_LOC));
109         authresp = userConfig.authenticate("wrongPassword");
110         assertTrue(authresp.getStatus().equals(AuthResultEnum.AUTH_REJECT_LOC));
111
112         // test equals()
113         roles.clear();
114         roles.add(UserLevel.NETWORKOPERATOR.toString());
115         userConfig = new UserConfig("uname", "ciscocisco", roles);
116         assertEquals(userConfig, userConfig);
117         UserConfig userConfig2 = new UserConfig("uname", "ciscocisco", roles);
118         assertEquals(userConfig, userConfig2);
119     }
120 }