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