Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 414636c0

Přidáno uživatelem Jakub Schenk před asi 2 roky(ů)

commentary

new comments for testing classes and messages for failed tests

Zobrazit rozdíly:

Backend/BackendTesting/UserManagementTesting.cs
17 17

  
18 18
namespace Core.Services
19 19
{
20
    //class responsible for testing user and backend autentication on login
20 21
    [TestClass]
21 22
    public class UserManagementTesting      //testing of Core/Services/UserService
22 23
    {
24
        //necessary items for new database with my data for teting
23 25
        private static readonly IConfiguration configuration = new ConfigurationBuilder().Build();
24 26
        public DatabaseContext ctx;
25

  
26 27
        public Core.Services.IUserService US;
28
        
29
        //constructor for creating database and filling it with dummy data
27 30
        public UserManagementTesting()
28 31
        {
32
            //creating new database
29 33
            this.ctx = new DatabaseContext(configuration);
30 34
            this.US = new UserServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
31 35

  
36
            //ensure the database is fresh without unwanted users from previous testings
32 37
            ctx.Database.EnsureDeleted();
33 38
            ctx.Database.EnsureCreated();
34 39

  
35
            //vytvoření dat pro testování
40
            //data for testing creation
36 41
            var userList = new List<User>();
37 42
            userList.Add(new User() { Username = "aaa", Name = "aaa", Surname = "aaa", Password = "aaa", Role = Models.Enums.ERole.ANNOTATOR });
38 43
            userList.Add(new User() { Username = "bbb", Name = "bbb", Surname = "bbb", Password = "bbb", Role = Models.Enums.ERole.ANNOTATOR });
......
45 50
            userList.Add(new User() { Username = "iii", Name = "iii", Surname = "iii", Password = "iii", Role = Models.Enums.ERole.ADMINISTRATOR });
46 51
            userList.Add(new User() { Username = "jjj", Name = "jjj", Surname = "jjj", Password = "jjj", Role = Models.Enums.ERole.ADMINISTRATOR });
47 52

  
48
            //nahrani do databaze
53
            //filling up the database
49 54
            foreach (var user in userList)
50 55
            {
51 56
                US.CreateUser(user.Username, user.Name, user.Surname, user.Password, user.Role);
......
98 103
            {
99 104
                if (user.Username == username)
100 105
                {
101
                    //checking, that user with wanted username was not changed
102
                    /*Assert.AreEqual(expected.Username, user.Username);*/
103
                    Assert.AreEqual(name, user.Name);
104
                    Assert.AreEqual(surname, user.Surname);
105
                    Assert.AreNotEqual(password, user.Password);
106
                    Assert.AreEqual(role, user.Role);
106
                    //checking, that user with wanted username was not changed and he is present in database
107
                    //if there were some data changed (not username) - these tests will fail
108
                    Assert.AreEqual(name, user.Name, "names are not same");
109
                    Assert.AreEqual(surname, user.Surname, "surnames are not same");
110
                    Assert.AreNotEqual(password, user.Password, "passwords are same - it should have been hashed");
111
                    Assert.AreEqual(role, user.Role, "roles are not same");
107 112
                    return;
108 113
                }
109 114
            }
110
            //if there was username not found, inserted user must have been changed or wasnt saved into database at all - test fails
115
            //if there was username not found, inserted users username must have been changed or wasnt saved into database at all - test fails
111 116
            Assert.Fail("supposedly created user was not found in database");
112 117
        }
113 118

  
......
115 120
        [DataRow("aaa", "aaa", "aaa", "aaa", "aaa", ERole.ANNOTATOR)]
116 121
        [DataRow("bbb", "bbb", "bbb", "bbb", "bbb", ERole.ANNOTATOR)]
117 122
        [DataRow("ccc", "ccc", "ccc", "ccc", "ccc", ERole.ADMINISTRATOR)]
118
        //method testing, that user searched by username is the one wanted - data are inserted before
123
        //method testing, that user searched by username is the one wanted - data are inserted in constructor
119 124
        public void GetUserByUsername_Test(string insertedUsername, string username, string name, string surname, string password, ERole role)
120 125
        {
121 126
            User? actual = US.GetUserByUsername(insertedUsername);
......
130 135
        [DataRow("aaa", "aaa", "aaa", "aaa", ERole.ANNOTATOR)]
131 136
        [DataRow("bbb", "bbb", "bbb", "bbb", ERole.ANNOTATOR)]
132 137
        [DataRow("ccc", "ccc", "ccc", "ccc", ERole.ADMINISTRATOR)]
138
        //testing method to get user via his ID, using another tested method - GetUserByUsername
133 139
        public void GetUserById_Test(string username, string name, string surname, string password, ERole role)
134 140
        {
135
            //TODO Find id other way then via another tested method
136 141
            User? expected = US.GetUserByUsername(username);
137 142
            if (expected == null)
138 143
            {
139
                Assert.Fail();
144
                Assert.Fail("There is mistake in test data or in GetUserByUsername method");
140 145
                return;
141 146
            }
142

  
143 147
            User? actual = US.GetUserById(expected.Id);
144 148

  
145 149
            //testing whole users
146
            Assert.AreEqual(expected, actual);
150
            Assert.AreEqual(expected, actual, "Users are not same");
147 151

  
148 152
            //testing all parameters
149
            Assert.AreEqual(actual.Username, username);
150
            Assert.AreEqual(actual.Name, name);
151
            Assert.AreEqual(actual.Surname, surname);
152
            Assert.AreNotEqual(actual.Password, password);
153
            Assert.AreEqual(actual.Role, role);
153
            Assert.AreEqual(actual.Username, username, "usernames are not same");
154
            Assert.AreEqual(actual.Name, name, "names are not same");
155
            Assert.AreEqual(actual.Surname, surname, "surnames are not same");
156
            Assert.AreNotEqual(actual.Password, password, "passwords ARE same (the one from database should have been hashed)");
157
            Assert.AreEqual(actual.Role, role, "roles are not same");
154 158
        }
155 159

  
156 160

  
......
164 168
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "bbb", "ccc", ERole.ANNOTATOR)]
165 169
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "xxx", "xxx", ERole.ADMINISTRATOR)]
166 170
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "bbb", "ccc", ERole.ADMINISTRATOR)]
171
        //creating new user and updating him
167 172
        public void UpdateUser_Test(string oldUsername, string oldName, string oldSurname, string password, ERole oldRole, string? username = null, string? name = null, string? surname = null, ERole? role = null)
168 173
        {
174
            //new user creation
169 175
            User? newUser = US.CreateUser(oldUsername, oldName, oldSurname, password, oldRole);
170 176
            if(newUser == null)
171 177
            {
172
                Assert.Fail("user not created");
178
                Assert.Fail("user not created (maybe already user username?)");
173 179
            }
174 180
            User? actual = US.UpdateUser(newUser, username, name, surname, role);
175 181

  
176
            Assert.IsNotNull(actual);
182
            Assert.IsNotNull(actual, "user wasnt updated, null was returned");
177 183
            Assert.AreEqual(username, actual.Username, "wrong username");
178 184
            Assert.AreEqual(name, actual.Name, "wrong name");
179 185
            Assert.AreEqual(surname, actual.Surname, "wrong surname");
......
186 192
        [DataRow("ccc", "aaa")]//admin
187 193
        [DataRow("bbb", "")]//set to none password
188 194
        [DataRow("eee", "eee")]//keep old password
195
        //test of changing password
189 196
        public void ChangePassword_Test(string username, string newPassword) 
190 197
        {
191
            //get user
198
            //get user using GetUserByUsername
192 199
            User? changingUser = US.GetUserByUsername(username);
193 200
            if(changingUser == null)
194 201
            {
......
229 236
        [DataRow("aaa", "aaa")]//anotator
230 237
        [DataRow("fff", "fff")]//anotator
231 238
        [DataRow("iii", "iii")]//admin
239
        //testing login autentication CheckUsernamePassword
232 240
        public void CheckUsernamePassword_Correct(string username, string password)
233 241
        {
234 242
            User? checkedUser = US.CheckUsernamePassword(username, password);
......
244 252
                Assert.Fail("There is mistake in tested data");
245 253
                return;
246 254
            }
247
            Assert.AreEqual(expected, checkedUser);
255
            Assert.AreEqual(expected, checkedUser, "Users are not same (maybe wrong password)");
248 256
        }
249 257

  
258

  
250 259
        [TestMethod]
251 260
        [DataRow("aaa", "bbb")]//changedPassword?
252 261
        [DataRow("fff", "fgh")]//anotator
......
256 265
            User? checkedUser = US.CheckUsernamePassword(username, password);
257 266
            if (checkedUser != null)
258 267
            {
259
                //there are just correct users - this should have returned user
268
                //there are just incorrect users - this should not have returned user
260 269
                Assert.Fail("there are just incorrect users-passwords - this should not return user");
261 270
                return;
262 271
            }
......
265 274
        [TestMethod]
266 275
        [DataRow("fff", "fff", "123")]//anotator
267 276
        [DataRow("iii", "iii", "456")]//admin
277
        //more complex test of the same as the above
278
        //passwords are changed and then there is login
268 279
        public void CheckUsernamePassword_ChangedPassword(string username, string oldPassword, string newPassword)
269 280
        {
270 281
            //get user from database
271 282
            User? oldUser = US.CheckUsernamePassword(username, oldPassword);
272 283
            if (oldUser == null) 
273 284
            {
274
                Assert.Fail("This should have returned user - there are wrong input data");
285
                Assert.Fail("This should have returned user - there are wrong input data or CheckUsernamePassword isnt working properly");
275 286
                return;
276 287
            }
277 288
            //change users password
......
301 312
            }
302 313
        }
303 314
        [TestMethod]
315
        //I dont really know how to test this
304 316
        public void GetUsers_Test()
305 317
        {
306 318
            var userList = US.GetUsers();
......
310 322

  
311 323

  
312 324
        [TestMethod]
313
        //correct
325
        //login from AuthService testing with correct username-password
314 326
        [DataRow("aaa", "aaa", ERole.ANNOTATOR)]//anotator
315 327
        [DataRow("fff", "fff", ERole.ANNOTATOR)]//anotator
316 328
        [DataRow("iii", "iii", ERole.ADMINISTRATOR)]//admin
317 329
        public void AuthService_Login_Correct(string username, string password, ERole role)
318 330
        {
331
            //creating new instance of AuthService
319 332
            var jwt = TestingJwtUtils.GetJwtUtils();
320 333
            var AS = new AuthService(US, jwt);
321
         //   var AS = new AuthServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
334
            //try login
322 335
            var loginResults = AS.Login(username, password);
336
            //check results
323 337
            Assert.IsNotNull(loginResults);
324 338
            Assert.IsTrue(loginResults.Ok);
325 339
            Assert.IsNotNull(loginResults.Token);
326 340
            DateTime exp = loginResults.Expiration;
341
            //expiration is in the future
327 342
            var now = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
328 343
            Assert.IsTrue(exp.CompareTo(now) > 0);//if compare is >0 -> exp is in the future
329 344
            Assert.AreEqual(role, loginResults.Role);
330 345
        }
331 346

  
332
        //incorrect
347
        //login from AuthService testing with incorrect username-password
333 348
        [TestMethod]
334 349
        [DataRow("aaa", "bbb")]//changedPassword?
335 350
        [DataRow("fff", "fgh")]//anotator
336 351
        [DataRow("iii", "123")]//admin
337 352
        public void AuthService_Login_Incorrect(string username, string password)
338 353
        {
354
            //creating new instance of AuthService
339 355
            var jwt = TestingJwtUtils.GetJwtUtils();
340 356
            var AS = new AuthService(US, jwt);
341
            //   var AS = new AuthServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
342 357
            var loginResults = AS.Login(username, password);
358
            //checking if login results are null or there is false in OK and there is none obtained token
343 359
            if (loginResults != null)
344 360
            {
345 361
                Assert.IsFalse(loginResults.Ok);
Backend/FrontendTesting/LoginTesting.cs
16 16
    [TestClass]
17 17
    public class LoginTesting
18 18
    {
19
        //defining variables needed for testing
19 20
        public string url = "http://localhost:3000/login";
20 21
        public By usernameField = By.Id("login_username");
21 22
        public By passwordField = By.Id("login_password");
......
26 27
        public string[] passwords = { "aaa", "bbb", "ccc" };
27 28

  
28 29

  
30
        /**
31
         Names of tests should be self-explanatory
32
        tests are separated, becouse there were problems with multiple tests runs in one method
33
        tests contain thread.sleep becouse of waiting on components to load (if I find better and reasonable solution, I will change it)
34
         */
35

  
36

  
29 37
        [TestMethod]
38
        //dummy method for constructing tests
30 39
        public void Login_Dummy()
31 40
        {
32 41
            var driver = Drivers.cDriver;
......
50 59
        public void Login_Correct_Annotator_Firefox()
51 60
        {
52 61
            var driver = new FirefoxDriver();
53
            driver.Navigate().GoToUrl(url);                             //zakladni stranka
62
            driver.Navigate().GoToUrl(url);                                     //zakladni stranka
54 63
            Thread.Sleep(3000);
55
            driver.FindElement(usernameField).SendKeys(usernames[0]);          //enter username
56
            driver.FindElement(passwordField).SendKeys(passwords[0]);          //enter password
57
            driver.FindElement(loginButton).Click();                    //click on login
64
            driver.FindElement(usernameField).SendKeys(usernames[0]);           //enter username
65
            driver.FindElement(passwordField).SendKeys(passwords[0]);           //enter password
66
            driver.FindElement(loginButton).Click();                            //click on login
58 67
            Thread.Sleep(3000);
59
            Assert.AreEqual(driver.Url, "http://localhost:3000/documents/annotator");
60
            driver.Navigate().GoToUrl(url);
68
            Assert.AreEqual(driver.Url, "http://localhost:3000/documents/annotator");   //check if your url is changed on this
69
            driver.Navigate().GoToUrl(url);                                     //return back on login screen (effectively logout, becouse logout button was too hard to click)
61 70
            driver.Close();
62 71
            driver.Quit();
63 72
        }
......
71 80
            driver.FindElement(passwordField).SendKeys(passwords[2]);          //enter password
72 81
            driver.FindElement(loginButton).Click();                    //click on login
73 82
            Thread.Sleep(3000);
74
            Assert.AreEqual(driver.Url, "http://localhost:3000/documents/admin");
75
            driver.Navigate().GoToUrl(url);
83
            Assert.AreEqual(driver.Url, "http://localhost:3000/documents/admin");   //check if url changed
84
            driver.Navigate().GoToUrl(url);                                     //return back on login screen (effectively logout, becouse logout button was too hard to click)
76 85
            driver.Close();
77 86
            driver.Quit();
78 87
        }
......
86 95
            driver.FindElement(passwordField).SendKeys(passwords[0]);          //enter password
87 96
            driver.FindElement(loginButton).Click();                    //click on login
88 97
            Thread.Sleep(3000);
89
            Assert.AreEqual(driver.Url, "http://localhost:3000/documents/annotator");
90
            driver.Navigate().GoToUrl(url);
98
            Assert.AreEqual(driver.Url, "http://localhost:3000/documents/annotator");   //check if url changed
99
            driver.Navigate().GoToUrl(url);                                     //return back on login screen (effectively logout, becouse logout button was too hard to click)
91 100
            driver.Close();
92 101
            driver.Quit();
93 102
        }
......
101 110
            driver.FindElement(passwordField).SendKeys(passwords[2]);          //enter password
102 111
            driver.FindElement(loginButton).Click();                    //click on login
103 112
            Thread.Sleep(3000);
104
            Assert.AreEqual(driver.Url, "http://localhost:3000/documents/admin");
105
            driver.Navigate().GoToUrl(url);
113
            Assert.AreEqual(driver.Url, "http://localhost:3000/documents/admin");   //check if url changed
114
            driver.Navigate().GoToUrl(url);                                     //return back on login screen (effectively logout, becouse logout button was too hard to click)
106 115
            driver.Close();
107 116
            driver.Quit();
108 117
        }
......
118 127
            driver.FindElement(passwordField).SendKeys("abc");          //enter password
119 128
            driver.FindElement(loginButton).Click();                    //click on login
120 129
            Thread.Sleep(3000);
121
            Assert.AreEqual(driver.Url, "http://localhost:3000/login");
130
            Assert.AreEqual(driver.Url, "http://localhost:3000/login"); //check if url didn change and user was not logged in
122 131
            Thread.Sleep(3000);
123 132
            driver.Close();
124 133
            driver.Quit();
......
133 142
            driver.FindElement(passwordField).SendKeys("abc");          //enter password
134 143
            driver.FindElement(loginButton).Click();                    //click on login
135 144
            Thread.Sleep(3000);
136
            Assert.AreEqual(driver.Url, "http://localhost:3000/login");
145
            Assert.AreEqual(driver.Url, "http://localhost:3000/login"); //check if url didn change and user was not logged in
137 146
            Thread.Sleep(3000);
138 147
            driver.Close();
139 148
            driver.Quit();
......
148 157
            driver.FindElement(passwordField).SendKeys("abc");          //enter password
149 158
            driver.FindElement(loginButton).Click();                    //click on login
150 159
            Thread.Sleep(3000);
151
            Assert.AreEqual(driver.Url, "http://localhost:3000/login");
160
            Assert.AreEqual(driver.Url, "http://localhost:3000/login"); //check if url didn change and user was not logged in
152 161
            Thread.Sleep(3000);
153 162
            driver.Close();
154 163
            driver.Quit();
......
163 172
            driver.FindElement(passwordField).SendKeys("abc");          //enter password
164 173
            driver.FindElement(loginButton).Click();                    //click on login
165 174
            Thread.Sleep(3000);
166
            Assert.AreEqual(driver.Url, "http://localhost:3000/login");
175
            Assert.AreEqual(driver.Url, "http://localhost:3000/login"); //check if url didn change and user was not logged in
167 176
            Thread.Sleep(3000);
168 177
            driver.Close();
169 178
            driver.Quit();
......
178 187
            driver.FindElement(passwordField).SendKeys("abc");          //enter password
179 188
            driver.FindElement(loginButton).Click();                    //click on login
180 189
            Thread.Sleep(3000);
181
            Assert.AreEqual(driver.Url, "http://localhost:3000/login");
190
            Assert.AreEqual(driver.Url, "http://localhost:3000/login"); //check if url didn change and user was not logged in
182 191
            Thread.Sleep(3000);
183 192
            driver.Close();
184 193
            driver.Quit();
......
193 202
            driver.FindElement(passwordField).SendKeys("abc");          //enter password
194 203
            driver.FindElement(loginButton).Click();                    //click on login
195 204
            Thread.Sleep(3000);
196
            Assert.AreEqual(driver.Url, "http://localhost:3000/login");
205
            Assert.AreEqual(driver.Url, "http://localhost:3000/login"); //check if url didn change and user was not logged in
197 206
            Thread.Sleep(3000);
198 207
            driver.Close();
199 208
            driver.Quit();
......
208 217
            driver.FindElement(passwordField).SendKeys("");          //enter password
209 218
            driver.FindElement(loginButton).Click();                    //click on login
210 219
            Thread.Sleep(3000);
211
            Assert.AreEqual(driver.Url, "http://localhost:3000/login");
220
            Assert.AreEqual(driver.Url, "http://localhost:3000/login"); //check if url didn change and user was not logged in
212 221
            Thread.Sleep(3000);
213 222
            driver.Close();
214 223
            driver.Quit();
......
223 232
            driver.FindElement(passwordField).SendKeys("");          //enter password
224 233
            driver.FindElement(loginButton).Click();                    //click on login
225 234
            Thread.Sleep(3000);
226
            Assert.AreEqual(driver.Url, "http://localhost:3000/login");
235
            Assert.AreEqual(driver.Url, "http://localhost:3000/login"); //check if url didn change and user was not logged in
227 236
            Thread.Sleep(3000);
228 237
            driver.Close();
229 238
            driver.Quit();
Backend/FrontendTesting/MainTestingClass.cs
1
using Microsoft.VisualStudio.TestTools.UnitTesting;
2

  
3
namespace FrontendTesting
4
{
5
    [TestClass]
6
    public class MainTestingClass
7
    {
8
        [TestMethod]
9
        public void RunAll()
10
        {
11
            
12
        }
13
    }
14
}
Backend/FrontendTesting/UnitTest1.cs
1
using Microsoft.VisualStudio.TestTools.UnitTesting;
2

  
3
namespace FrontendTesting
4
{
5
    [TestClass]
6
    public class UnitTest1
7
    {
8
        [TestMethod]
9
        public void TestMethod1()
10
        {
11
        }
12
    }
13
}

Také k dispozici: Unified diff