Projekt

Obecné

Profil

Stáhnout (18 KB) Statistiky
| Větev: | Tag: | Revize:
1 782b6d38 Vojtěch Bartička
using Microsoft.VisualStudio.TestTools.UnitTesting;
2
using Models.Enums;
3
using Core.Entities;
4
using Serilog;
5
6
using System;
7 773d1205 schenkj
using System.Collections.Generic;
8
using System.Linq;
9
using System.Text;
10
using System.Threading.Tasks;
11 782b6d38 Vojtěch Bartička
using Microsoft.EntityFrameworkCore;
12
using AutoMapper;
13
using Core.MapperProfiles;
14
using Core.Contexts;
15
using Microsoft.Extensions.Configuration;
16 ea53e0e1 schenkj
using Models.Users;
17 773d1205 schenkj
18 065f6462 schenkj
using Core.Services;
19
20
namespace BackendTesting
21 773d1205 schenkj
{
22 414636c0 schenkj
    //class responsible for testing user and backend autentication on login
23 ea53e0e1 schenkj
    [TestClass]
24
    public class UserManagementTesting      //testing of Core/Services/UserService
25 773d1205 schenkj
    {
26 414636c0 schenkj
        //necessary items for new database with my data for teting
27 ea53e0e1 schenkj
        private static readonly IConfiguration configuration = new ConfigurationBuilder().Build();
28
        public DatabaseContext ctx;
29
        public Core.Services.IUserService US;
30 414636c0 schenkj
        
31
        //constructor for creating database and filling it with dummy data
32 ea53e0e1 schenkj
        public UserManagementTesting()
33
        {
34 414636c0 schenkj
            //creating new database
35 ea53e0e1 schenkj
            this.ctx = new DatabaseContext(configuration);
36
            this.US = new UserServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
37
38 414636c0 schenkj
            //ensure the database is fresh without unwanted users from previous testings
39 ea53e0e1 schenkj
            ctx.Database.EnsureDeleted();
40
            ctx.Database.EnsureCreated();
41
42 414636c0 schenkj
            //data for testing creation
43 ea53e0e1 schenkj
            var userList = new List<User>();
44
            userList.Add(new User() { Username = "aaa", Name = "aaa", Surname = "aaa", Password = "aaa", Role = Models.Enums.ERole.ANNOTATOR });
45
            userList.Add(new User() { Username = "bbb", Name = "bbb", Surname = "bbb", Password = "bbb", Role = Models.Enums.ERole.ANNOTATOR });
46
            userList.Add(new User() { Username = "ccc", Name = "ccc", Surname = "ccc", Password = "ccc", Role = Models.Enums.ERole.ADMINISTRATOR });
47
            userList.Add(new User() { Username = "ddd", Name = "ddd", Surname = "ddd", Password = "ddd", Role = Models.Enums.ERole.ANNOTATOR });
48
            userList.Add(new User() { Username = "eee", Name = "eee", Surname = "eee", Password = "eee", Role = Models.Enums.ERole.ANNOTATOR });
49
            userList.Add(new User() { Username = "fff", Name = "fff", Surname = "fff", Password = "fff", Role = Models.Enums.ERole.ANNOTATOR });
50
            userList.Add(new User() { Username = "ggg", Name = "ggg", Surname = "ggg", Password = "ggg", Role = Models.Enums.ERole.ANNOTATOR });
51
            userList.Add(new User() { Username = "hhh", Name = "hhh", Surname = "hhh", Password = "hhh", Role = Models.Enums.ERole.ADMINISTRATOR });
52
            userList.Add(new User() { Username = "iii", Name = "iii", Surname = "iii", Password = "iii", Role = Models.Enums.ERole.ADMINISTRATOR });
53
            userList.Add(new User() { Username = "jjj", Name = "jjj", Surname = "jjj", Password = "jjj", Role = Models.Enums.ERole.ADMINISTRATOR });
54 782b6d38 Vojtěch Bartička
55 414636c0 schenkj
            //filling up the database
56 ea53e0e1 schenkj
            foreach (var user in userList)
57
            {
58
                US.CreateUser(user.Username, user.Name, user.Surname, user.Password, user.Role);
59
            }
60
            ctx.SaveChanges();
61
        }
62 782b6d38 Vojtěch Bartička
63 ea53e0e1 schenkj
        [TestMethod]
64
        [DataRow("kkk", "kkk", "kkk", "kkk", ERole.ANNOTATOR, true)]
65
        [DataRow("lll", "lll", "lll", "", ERole.ANNOTATOR, true)]
66
        [DataRow("mmm", "mmm", "", "mmm", ERole.ANNOTATOR, true)]
67
        [DataRow("nnn", "", "nnn", "nnn", ERole.ANNOTATOR, true)]
68
        [DataRow("", "ooo", "ooo", "ooo", ERole.ADMINISTRATOR, false)]
69
        //method tests saving user into database, retrieving all users checking if the one saved is between them
70
        public void CreateUser_Test(string username, string name, string surname, string password, ERole role, bool shouldBeRigh)
71 782b6d38 Vojtěch Bartička
        {
72 ea53e0e1 schenkj
            //creation of new user
73
            var output = US.CreateUser(username, name, surname, password, role);
74
            ctx.SaveChanges();
75 b2594803 schenkj
76
77
            if (output == null && ((password == null || password == "") || (surname == null || surname == "") || (name == null || name == "") || (username == null || username == "")))
78
            {
79
                Assert.IsTrue(true, "Invalid registration creditals were input -> null output was expected");
80
                return;
81
            }
82
83 ea53e0e1 schenkj
            if (output == null && !shouldBeRigh)
84
            {
85
                //trying to insert user with taken username resolves with refusing 
86
                return;
87
            }
88
89
            //check for empty atributes
90
            if((username == null || username == "")&& output!=null)
91
            {
92
                Assert.Fail("user with no username created");
93
                return;
94
            }
95
            if ((name == null || name == "") && output != null)
96
            {
97
                Assert.Fail("user with no name created");
98
                return;
99
            }
100
            if ((surname == null || surname == "") && output != null)
101
            {
102
                Assert.Fail("user with no surname created");
103
                return;
104
            }
105
            if ((password == null || password == "") && output != null)
106
            {
107
                Assert.Fail("user with no password created");
108
                return;
109
            }
110
111 b2594803 schenkj
112 ea53e0e1 schenkj
            //retrieving all users checking via username
113
            foreach (var user in ctx.Users)
114
            {
115
                if (user.Username == username)
116
                {
117 414636c0 schenkj
                    //checking, that user with wanted username was not changed and he is present in database
118
                    //if there were some data changed (not username) - these tests will fail
119
                    Assert.AreEqual(name, user.Name, "names are not same");
120
                    Assert.AreEqual(surname, user.Surname, "surnames are not same");
121
                    Assert.AreNotEqual(password, user.Password, "passwords are same - it should have been hashed");
122
                    Assert.AreEqual(role, user.Role, "roles are not same");
123 ea53e0e1 schenkj
                    return;
124
                }
125
            }
126 414636c0 schenkj
            //if there was username not found, inserted users username must have been changed or wasnt saved into database at all - test fails
127 ea53e0e1 schenkj
            Assert.Fail("supposedly created user was not found in database");
128 782b6d38 Vojtěch Bartička
        }
129
130 ea53e0e1 schenkj
        [TestMethod]
131
        [DataRow("aaa", "aaa", "aaa", "aaa", "aaa", ERole.ANNOTATOR)]
132
        [DataRow("bbb", "bbb", "bbb", "bbb", "bbb", ERole.ANNOTATOR)]
133
        [DataRow("ccc", "ccc", "ccc", "ccc", "ccc", ERole.ADMINISTRATOR)]
134 414636c0 schenkj
        //method testing, that user searched by username is the one wanted - data are inserted in constructor
135 ea53e0e1 schenkj
        public void GetUserByUsername_Test(string insertedUsername, string username, string name, string surname, string password, ERole role)
136
        {
137
            User? actual = US.GetUserByUsername(insertedUsername);
138
            Assert.AreEqual(username, actual.Username, "wrong username");
139
            Assert.AreEqual(name, actual.Name, "wrong name");
140
            Assert.AreEqual(surname, actual.Surname, "wrong surname");
141
            Assert.AreNotEqual(password, actual.Password, "same password");
142
            Assert.AreEqual(role, actual.Role, "wrong role");
143
        }
144
145
        [TestMethod]
146
        [DataRow("aaa", "aaa", "aaa", "aaa", ERole.ANNOTATOR)]
147
        [DataRow("bbb", "bbb", "bbb", "bbb", ERole.ANNOTATOR)]
148
        [DataRow("ccc", "ccc", "ccc", "ccc", ERole.ADMINISTRATOR)]
149 414636c0 schenkj
        //testing method to get user via his ID, using another tested method - GetUserByUsername
150 ea53e0e1 schenkj
        public void GetUserById_Test(string username, string name, string surname, string password, ERole role)
151
        {
152
            User? expected = US.GetUserByUsername(username);
153
            if (expected == null)
154
            {
155 414636c0 schenkj
                Assert.Fail("There is mistake in test data or in GetUserByUsername method");
156 ea53e0e1 schenkj
                return;
157
            }
158
            User? actual = US.GetUserById(expected.Id);
159
160
            //testing whole users
161 414636c0 schenkj
            Assert.AreEqual(expected, actual, "Users are not same");
162 ea53e0e1 schenkj
163
            //testing all parameters
164 414636c0 schenkj
            Assert.AreEqual(actual.Username, username, "usernames are not same");
165
            Assert.AreEqual(actual.Name, name, "names are not same");
166
            Assert.AreEqual(actual.Surname, surname, "surnames are not same");
167
            Assert.AreNotEqual(actual.Password, password, "passwords ARE same (the one from database should have been hashed)");
168
            Assert.AreEqual(actual.Role, role, "roles are not same");
169 ea53e0e1 schenkj
        }
170
171
172 782b6d38 Vojtěch Bartička
173 ea53e0e1 schenkj
174
        [TestMethod]
175
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "uuu", "xxx", "xxx", ERole.ANNOTATOR)]
176
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "uuu", "xxx", ERole.ANNOTATOR)]
177
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "xxx", "uuu", ERole.ANNOTATOR)]
178
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "aaa", "bbb", "ccc", ERole.ANNOTATOR)]
179
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "bbb", "ccc", ERole.ANNOTATOR)]
180
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "xxx", "xxx", ERole.ADMINISTRATOR)]
181
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "bbb", "ccc", ERole.ADMINISTRATOR)]
182 414636c0 schenkj
        //creating new user and updating him
183 ea53e0e1 schenkj
        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)
184
        {
185 414636c0 schenkj
            //new user creation
186 ea53e0e1 schenkj
            User? newUser = US.CreateUser(oldUsername, oldName, oldSurname, password, oldRole);
187
            if(newUser == null)
188
            {
189 414636c0 schenkj
                Assert.Fail("user not created (maybe already user username?)");
190 ea53e0e1 schenkj
            }
191
            User? actual = US.UpdateUser(newUser, username, name, surname, role);
192
193 414636c0 schenkj
            Assert.IsNotNull(actual, "user wasnt updated, null was returned");
194 ea53e0e1 schenkj
            Assert.AreEqual(username, actual.Username, "wrong username");
195
            Assert.AreEqual(name, actual.Name, "wrong name");
196
            Assert.AreEqual(surname, actual.Surname, "wrong surname");
197
            Assert.AreEqual(role, actual.Role, "wrong role");
198
            Assert.AreNotEqual(password, actual.Password, "same password");
199
200
        }
201
        [TestMethod]
202
        [DataRow("aaa", "bbb")]//anotator
203
        [DataRow("ccc", "aaa")]//admin
204
        [DataRow("bbb", "")]//set to none password
205
        [DataRow("eee", "eee")]//keep old password
206 414636c0 schenkj
        //test of changing password
207 ea53e0e1 schenkj
        public void ChangePassword_Test(string username, string newPassword) 
208
        {
209 414636c0 schenkj
            //get user using GetUserByUsername
210 ea53e0e1 schenkj
            User? changingUser = US.GetUserByUsername(username);
211
            if(changingUser == null)
212
            {
213
                Assert.Fail("user not found");
214
                return;
215
            }
216
            //get old password hash
217
            var oldPassword = changingUser.Password;
218
219
            //change password
220 b2594803 schenkj
            try
221 ea53e0e1 schenkj
            {
222 b2594803 schenkj
                User? changed = US.ChangePassword(changingUser, newPassword);
223
                //new password is not empty and database returned null for user (user had to be there)
224
                if (changed == null && (newPassword != "" || newPassword != null))
225
                {
226
                    Assert.Fail("user not returned");
227
                    return;
228
                }
229
                //new password is empty
230
                if ((newPassword == "" || newPassword == null) && changed != null)
231
                {
232
                    Assert.Fail("empty password was accepted");
233
                    return;
234
                }
235
                //no user was returned back after ChangePassword
236
                if (changed == null)
237
                {
238
                    Assert.Fail("user not returned or found or there was mistake in password change");
239
                    return;
240
                }
241
242
                //get new password hash
243
                var renewedPassword = changed.Password;
244
245
                Assert.AreNotEqual(oldPassword, renewedPassword, "password was not changed or it was changed on same value");
246 ea53e0e1 schenkj
            }
247 b2594803 schenkj
            catch(Exception ex)
248 ea53e0e1 schenkj
            {
249 b2594803 schenkj
                if (newPassword == null || newPassword == "") Assert.IsTrue(true,"Try to change password on empty resulted in exception - expected and wanted outcome");
250 ea53e0e1 schenkj
            }
251 b2594803 schenkj
            
252 ea53e0e1 schenkj
253
        }
254
        [TestMethod]
255
        [DataRow("aaa", "aaa")]//anotator
256
        [DataRow("fff", "fff")]//anotator
257
        [DataRow("iii", "iii")]//admin
258 414636c0 schenkj
        //testing login autentication CheckUsernamePassword
259 ea53e0e1 schenkj
        public void CheckUsernamePassword_Correct(string username, string password)
260
        {
261
            User? checkedUser = US.CheckUsernamePassword(username, password);
262
            if(checkedUser == null)
263
            {
264
                //there are just correct users - this should have returned user
265
                Assert.Fail("there are just correct users - this should have returned user");
266
                return;
267
            }
268
            User? expected = US.GetUserByUsername(username);
269
            if(expected == null)
270
            {
271
                Assert.Fail("There is mistake in tested data");
272
                return;
273
            }
274 414636c0 schenkj
            Assert.AreEqual(expected, checkedUser, "Users are not same (maybe wrong password)");
275 ea53e0e1 schenkj
        }
276
277 414636c0 schenkj
278 ea53e0e1 schenkj
        [TestMethod]
279
        [DataRow("aaa", "bbb")]//changedPassword?
280
        [DataRow("fff", "fgh")]//anotator
281
        [DataRow("iii", "123")]//admin
282
        public void CheckUsernamePassword_Incorrect(string username, string password)
283
        {
284
            User? checkedUser = US.CheckUsernamePassword(username, password);
285
            if (checkedUser != null)
286
            {
287 414636c0 schenkj
                //there are just incorrect users - this should not have returned user
288 ea53e0e1 schenkj
                Assert.Fail("there are just incorrect users-passwords - this should not return user");
289
                return;
290
            }
291
        }
292
293
        [TestMethod]
294
        [DataRow("fff", "fff", "123")]//anotator
295
        [DataRow("iii", "iii", "456")]//admin
296 414636c0 schenkj
        //more complex test of the same as the above
297
        //passwords are changed and then there is login
298 ea53e0e1 schenkj
        public void CheckUsernamePassword_ChangedPassword(string username, string oldPassword, string newPassword)
299
        {
300
            //get user from database
301
            User? oldUser = US.CheckUsernamePassword(username, oldPassword);
302
            if (oldUser == null) 
303
            {
304 414636c0 schenkj
                Assert.Fail("This should have returned user - there are wrong input data or CheckUsernamePassword isnt working properly");
305 ea53e0e1 schenkj
                return;
306
            }
307
            //change users password
308
            User? changedUser = US.ChangePassword(oldUser, newPassword);
309
            //update database so changes are recognisable
310
            ctx.SaveChanges();
311
            if (changedUser == null)
312
            {
313
                Assert.Fail("There is mistake in another method \"ChangePassword\" test it!");
314
            }
315
            //check if new password is in place
316
            User? newUser = US.CheckUsernamePassword(username, newPassword);
317
            if(newUser == null)
318
            {
319
                Assert.Fail("No user was returned - password probably wasnt changed");
320
            }
321
            Assert.AreEqual(oldUser.Username, newUser.Username, "returned users usernames are not same");
322
            Assert.AreEqual(oldUser.Name, newUser.Name, "returned users names are not same");
323
            Assert.AreEqual(oldUser.Surname, newUser.Surname, "returned users surnames are not same");
324
            Assert.AreEqual(oldUser.Role, newUser.Role, "returned users roles are not same");
325
326
            //check if there is no way to login with old password
327
            User? oldPasswordTester = US.CheckUsernamePassword(username, oldPassword);
328
            if(oldPasswordTester != null)
329
            {
330
                Assert.Fail("old password is still viable after password change");
331
            }
332
        }
333
        [TestMethod]
334 414636c0 schenkj
        //I dont really know how to test this
335 ba13a48a schenkj
        public void GetUsers_Test()
336 ea53e0e1 schenkj
        {
337
            var userList = US.GetUsers();
338 ba13a48a schenkj
            Assert.AreEqual(ctx.Users.Count(), userList.Users.Count, "I dont know if this is even test");
339
        }
340
341
342
343
        [TestMethod]
344 414636c0 schenkj
        //login from AuthService testing with correct username-password
345 ba13a48a schenkj
        [DataRow("aaa", "aaa", ERole.ANNOTATOR)]//anotator
346
        [DataRow("fff", "fff", ERole.ANNOTATOR)]//anotator
347
        [DataRow("iii", "iii", ERole.ADMINISTRATOR)]//admin
348
        public void AuthService_Login_Correct(string username, string password, ERole role)
349
        {
350 414636c0 schenkj
            //creating new instance of AuthService
351 ba13a48a schenkj
            var jwt = TestingJwtUtils.GetJwtUtils();
352
            var AS = new AuthService(US, jwt);
353 414636c0 schenkj
            //try login
354 ba13a48a schenkj
            var loginResults = AS.Login(username, password);
355 414636c0 schenkj
            //check results
356 ba13a48a schenkj
            Assert.IsNotNull(loginResults);
357
            Assert.IsTrue(loginResults.Ok);
358
            Assert.IsNotNull(loginResults.Token);
359
            DateTime exp = loginResults.Expiration;
360 414636c0 schenkj
            //expiration is in the future
361 ba13a48a schenkj
            var now = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
362
            Assert.IsTrue(exp.CompareTo(now) > 0);//if compare is >0 -> exp is in the future
363
            Assert.AreEqual(role, loginResults.Role);
364
        }
365
366 414636c0 schenkj
        //login from AuthService testing with incorrect username-password
367 ba13a48a schenkj
        [TestMethod]
368
        [DataRow("aaa", "bbb")]//changedPassword?
369
        [DataRow("fff", "fgh")]//anotator
370
        [DataRow("iii", "123")]//admin
371
        public void AuthService_Login_Incorrect(string username, string password)
372
        {
373 414636c0 schenkj
            //creating new instance of AuthService
374 ba13a48a schenkj
            var jwt = TestingJwtUtils.GetJwtUtils();
375
            var AS = new AuthService(US, jwt);
376
            var loginResults = AS.Login(username, password);
377 414636c0 schenkj
            //checking if login results are null or there is false in OK and there is none obtained token
378 ba13a48a schenkj
            if (loginResults != null)
379
            {
380
                Assert.IsFalse(loginResults.Ok);
381
                Assert.IsNull(loginResults.Token);
382
            }
383
            else
384
            {
385
                Assert.IsNull(loginResults);
386
            }
387 ea53e0e1 schenkj
        }
388 773d1205 schenkj
    }
389
}