DataHandlerForTestsTest added,
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / core / 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.core;
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 org.junit.Rule;
15 import org.junit.Test;
16 import org.junit.rules.ExpectedException;
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 erwartet wird reicht es
33 //eine allfaellige mit 'throws Exception' an junit weiterzuleiten.
34 //--------------------------------------------------------------------------------------------
35 /*createUserException.expect(IllegalArgumentException.class);
36 createUserException.expectMessage("Invalid username");
37 User wrongArgumentUser1 = new User(null, "1234");
38 createUserException.expectMessage("Invalid password");
39 User wrongArgumentUser2 = new User("TestUser2", null);*/
40 }
41
42 @Test
43 public void createExistingTest() throws Exception{
44 User testUser = new User("TestUser", "pass", "grosm9@bfh.ch", "Test", "Dummy");
45 assertEquals("TestUser", testUser.getUserName());
46 assertEquals("pass", testUser.getEncryptedPassword());
47 assertEquals("grosm9@bfh.ch", testUser.getMail());
48 assertEquals("Test", testUser.getName());
49 assertEquals("Dummy", testUser.getLastname());
50 }
51
52 @Test
53 public void getEncryptedPasswordTest() throws Exception{
54 String encryptedPass = Password.getSaltedHash("testPassword");
55 User testUser = new User("TestUser", encryptedPass, "grosm9@bfh.ch", "Test", "Dummy");
56 assertEquals(encryptedPass, testUser.getEncryptedPassword());
57 }
58
59 @Test(expected=IllegalArgumentException.class)
60 public void createUserInvalidUsernameTest() throws Exception{
61 User testUser = new User(null,"pass");
62 }
63
64 @Test(expected=IllegalArgumentException.class)
65 public void createUserTooShortUsernameTest() throws Exception{
66 User testUser = new User(new String(),"pass");
67 User testUser2 = new User(new String("tes"),"pass");
68 }
69
70 @Test(expected=IllegalArgumentException.class)
71 public void createUserInvalidPasswordTest() throws Exception{
72 User testUser = new User("TestUser", null);
73 }
74
75 @Test
76 public void checkPasswordTest() throws Exception{
77 User testUser = new User("TestUser", "pass");
78 assertTrue(testUser.checkPassword("pass"));
79 assertFalse(testUser.checkPassword("notmypass"));
80 }
81
82 @Test(expected=IllegalArgumentException.class)
83 public void checkPasswordInvalidTest() throws Exception{
84 User testUser = new User("TestUser", "pass");
85 assertTrue(testUser.checkPassword(null));
86 }
87
88 @Test
89 public void changePasswordTest() throws Exception{
90 User testUser = new User("TestUser", "pass");
91 assertTrue(testUser.checkPassword("pass"));
92 testUser.changePassword("newpass");
93 assertTrue(testUser.checkPassword("newpass"));
94 assertFalse(testUser.checkPassword("pass"));
95 assertFalse(testUser.checkPassword("notmypass"));
96 }
97
98 @Test(expected=IllegalArgumentException.class)
99 public void changePasswordInvalid1Test() throws Exception{
100 User testUser = new User("TestUser", "pass");
101 testUser.changePassword(null);
102 }
103
104 @Test(expected=IllegalArgumentException.class)
105 public void changePasswordInvalid2Test() throws Exception{
106 User testUser = new User("TestUser", "pass");
107 testUser.changePassword("pass");
108 }
109
110 @Test
111 public void setMailTest() throws Exception{
112 User testUser = new User("TestUser", "pass");
113 testUser.setMail("grosm9@bfh.ch");
114 }
115
116 @Test(expected=IllegalArgumentException.class)
117 public void setMailIllegal1Test() throws Exception{
118 User testUser = new User("TestUser", "pass");
119 testUser.setMail("fda.ds");
120
121 }
122
123 @Test(expected=IllegalArgumentException.class)
124 public void setMailIllegal2Test() throws Exception{
125 User testUser = new User("TestUser", "pass");
126 testUser.setMail("adfiug@fs");
127 }
128
129 @Test(expected=IllegalArgumentException.class)
130 public void setMailIllegal3Test() throws Exception{
131 User testUser = new User("TestUser", "pass");
132 testUser.setMail(null);
133 }
134
135 @Test
136 public void getMailTest() throws Exception{
137 User testUser = new User("TestUser", "pass");
138 String mail = "grosm9@bfh.ch";
139
140 testUser.setMail(mail);
141 assertEquals(mail, testUser.getMail());
142 }
143
144 @Test
145 public void setAndGetNameTest() throws Exception{
146 User testUser = new User("TestUser", "pass");
147 testUser.setName("Tester");
148 assertEquals("Tester",testUser.getName());
149 testUser.setName(null);
150 assertEquals(null, testUser.getName());
151 }
152
153 @Test
154 public void setAndGetLastnameTest() throws Exception{
155 User testUser = new User("TestUser", "pass");
156 testUser.setLastname("Dummy");
157 assertEquals("Dummy", testUser.getLastname());
158 testUser.setLastname(null);
159 assertEquals(null, testUser.getLastname());
160 }
161 }