UserTest updated
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / dataschema / UserTest.java
1 /*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8 package ch.bfh.ti.srs.dataschema;
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.Set;
15
16 import org.junit.Test;
17
18 import ch.bfh.ti.srs.extern.Password;
19
20 /**
21 * @author Michael Grossen
22 */
23 public class UserTest {
24 // @Rule
25 // public ExpectedException createUserException = ExpectedException.none();
26
27 @Test
28 public void createUserTest() throws Exception {
29 User testUser = new User("TestUser", "pass");
30 assertEquals("TestUser", testUser.getUserName());
31
32 // Zweite Moeglichkeit um Exceptions zu testen, so lange keine Exception
33 // erwartet wird reicht es
34 // eine allfaellige mit 'throws Exception' an junit weiterzuleiten.
35 // --------------------------------------------------------------------------------------------
36 /*
37 * createUserException.expect(IllegalArgumentException.class);
38 * createUserException.expectMessage("Invalid username"); User
39 * wrongArgumentUser1 = new User(null, "1234");
40 * createUserException.expectMessage("Invalid password"); User
41 * wrongArgumentUser2 = new User("TestUser2", null);
42 */
43 }
44
45 @Test
46 public void createExistingTest() throws Exception {
47 User testUser = new User("TestUser", "pass", "grosm9@bfh.ch", "Test", "Dummy");
48 assertEquals("TestUser", testUser.getUserName());
49 assertEquals("pass", testUser.getEncryptedPassword());
50 assertEquals("grosm9@bfh.ch", testUser.getMail());
51 assertEquals("Test", testUser.getName());
52 assertEquals("Dummy", testUser.getLastname());
53 }
54
55 @Test
56 public void getEncryptedPasswordTest() throws Exception {
57 String encryptedPass = Password.getSaltedHash("testPassword");
58 User testUser = new User("TestUser", encryptedPass, "grosm9@bfh.ch", "Test", "Dummy");
59 assertEquals(encryptedPass, testUser.getEncryptedPassword());
60 }
61
62 @Test(expected = IllegalArgumentException.class)
63 public void createUserInvalidUsernameTest() throws Exception {
64 User testUser = new User(null, "pass");
65 }
66
67 @Test(expected = IllegalArgumentException.class)
68 public void createUserTooShortUsernameTest() throws Exception {
69 User testUser = new User(new String(), "pass");
70 User testUser2 = new User(new String("tes"), "pass");
71 }
72
73 @Test(expected = IllegalArgumentException.class)
74 public void createUserInvalidPasswordTest() throws Exception {
75 User testUser = new User("TestUser", null);
76 }
77
78 @Test
79 public void checkPasswordTest() throws Exception {
80 User testUser = new User("TestUser", "pass");
81 assertTrue(testUser.checkPassword("pass"));
82 assertFalse(testUser.checkPassword("notmypass"));
83 }
84
85 @Test(expected = IllegalArgumentException.class)
86 public void checkPasswordInvalidTest() throws Exception {
87 User testUser = new User("TestUser", "pass");
88 assertTrue(testUser.checkPassword(null));
89 }
90
91 @Test
92 public void changePasswordTest() throws Exception {
93 User testUser = new User("TestUser", "pass");
94 assertTrue(testUser.checkPassword("pass"));
95 testUser.changePassword("newpass");
96 assertTrue(testUser.checkPassword("newpass"));
97 assertFalse(testUser.checkPassword("pass"));
98 assertFalse(testUser.checkPassword("notmypass"));
99 }
100
101 @Test(expected = IllegalArgumentException.class)
102 public void changePasswordInvalid1Test() throws Exception {
103 User testUser = new User("TestUser", "pass");
104 testUser.changePassword(null);
105 }
106
107 @Test(expected = IllegalArgumentException.class)
108 public void changePasswordInvalid2Test() throws Exception {
109 User testUser = new User("TestUser", "pass");
110 testUser.changePassword("pass");
111 }
112
113 @Test
114 public void setMailTest() throws Exception {
115 User testUser = new User("TestUser", "pass");
116 testUser.setMail("grosm9@bfh.ch");
117 }
118
119 @Test(expected = IllegalArgumentException.class)
120 public void setMailIllegal1Test() throws Exception {
121 User testUser = new User("TestUser", "pass");
122 testUser.setMail("fda.ds");
123
124 }
125
126 @Test(expected = IllegalArgumentException.class)
127 public void setMailIllegal2Test() throws Exception {
128 User testUser = new User("TestUser", "pass");
129 testUser.setMail("adfiug@fs");
130 }
131
132 @Test(expected = IllegalArgumentException.class)
133 public void setMailIllegal3Test() throws Exception {
134 User testUser = new User("TestUser", "pass");
135 testUser.setMail(null);
136 }
137
138 @Test
139 public void getMailTest() throws Exception {
140 User testUser = new User("TestUser", "pass");
141 String mail = "grosm9@bfh.ch";
142
143 testUser.setMail(mail);
144 assertEquals(mail, testUser.getMail());
145 }
146
147 @Test
148 public void setAndGetNameTest() throws Exception {
149 User testUser = new User("TestUser", "pass");
150 testUser.setName("Tester");
151 assertEquals("Tester", testUser.getName());
152 testUser.setName(null);
153 assertEquals(null, testUser.getName());
154 }
155
156 @Test
157 public void setAndGetLastnameTest() throws Exception {
158 User testUser = new User("TestUser", "pass");
159 testUser.setLastname("Dummy");
160 assertEquals("Dummy", testUser.getLastname());
161 testUser.setLastname(null);
162 assertEquals(null, testUser.getLastname());
163 }
164
165 @Test
166 public void RoleTest() throws Exception{
167 User testUser = new User("TestUser", "pass");
168 Set<Role> roles = testUser.getRoles();
169 assertTrue(roles.contains(Role.User));
170 assertFalse(roles.contains(Role.ResourceManager));
171 assertFalse(roles.contains(Role.Admin));
172
173 testUser.addRole(Role.ResourceManager);
174 roles = testUser.getRoles();
175 assertTrue(roles.contains(Role.User));
176 assertTrue(roles.contains(Role.ResourceManager));
177 assertFalse(roles.contains(Role.Admin));
178
179 testUser.removeRole(Role.User);
180 roles = testUser.getRoles();
181 assertFalse(roles.contains(Role.User));
182 assertTrue(roles.contains(Role.ResourceManager));
183 assertFalse(roles.contains(Role.Admin));
184
185 testUser.removeRole(Role.Admin);
186 roles = testUser.getRoles();
187 assertFalse(roles.contains(Role.User));
188 assertTrue(roles.contains(Role.ResourceManager));
189 assertFalse(roles.contains(Role.Admin));
190 }
191
192 @Test
193 public void cloneTest() throws Exception{
194 User testUser = new User("TestUser", "pass");
195 User clonedUser = testUser.clone();
196 assertEquals(testUser.getUserName(), clonedUser.getUserName());
197 }
198 }