Projekt

Obecné

Profil

Stáhnout (17.2 KB) Statistiky
| Větev: | Tag: | Revize:
1
using Microsoft.VisualStudio.TestTools.UnitTesting;
2
using Models.Enums;
3
using Core.Entities;
4
using Serilog;
5

    
6
using System;
7
using System.Collections.Generic;
8
using System.Linq;
9
using System.Text;
10
using System.Threading.Tasks;
11
using Microsoft.EntityFrameworkCore;
12
using AutoMapper;
13
using Core.MapperProfiles;
14
using Core.Contexts;
15
using Microsoft.Extensions.Configuration;
16
using Models.Users;
17

    
18
namespace Core.Services
19
{
20
    //class responsible for testing user and backend autentication on login
21
    [TestClass]
22
    public class UserManagementTesting      //testing of Core/Services/UserService
23
    {
24
        //necessary items for new database with my data for teting
25
        private static readonly IConfiguration configuration = new ConfigurationBuilder().Build();
26
        public DatabaseContext ctx;
27
        public Core.Services.IUserService US;
28
        
29
        //constructor for creating database and filling it with dummy data
30
        public UserManagementTesting()
31
        {
32
            //creating new database
33
            this.ctx = new DatabaseContext(configuration);
34
            this.US = new UserServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
35

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

    
40
            //data for testing creation
41
            var userList = new List<User>();
42
            userList.Add(new User() { Username = "aaa", Name = "aaa", Surname = "aaa", Password = "aaa", Role = Models.Enums.ERole.ANNOTATOR });
43
            userList.Add(new User() { Username = "bbb", Name = "bbb", Surname = "bbb", Password = "bbb", Role = Models.Enums.ERole.ANNOTATOR });
44
            userList.Add(new User() { Username = "ccc", Name = "ccc", Surname = "ccc", Password = "ccc", Role = Models.Enums.ERole.ADMINISTRATOR });
45
            userList.Add(new User() { Username = "ddd", Name = "ddd", Surname = "ddd", Password = "ddd", Role = Models.Enums.ERole.ANNOTATOR });
46
            userList.Add(new User() { Username = "eee", Name = "eee", Surname = "eee", Password = "eee", Role = Models.Enums.ERole.ANNOTATOR });
47
            userList.Add(new User() { Username = "fff", Name = "fff", Surname = "fff", Password = "fff", Role = Models.Enums.ERole.ANNOTATOR });
48
            userList.Add(new User() { Username = "ggg", Name = "ggg", Surname = "ggg", Password = "ggg", Role = Models.Enums.ERole.ANNOTATOR });
49
            userList.Add(new User() { Username = "hhh", Name = "hhh", Surname = "hhh", Password = "hhh", Role = Models.Enums.ERole.ADMINISTRATOR });
50
            userList.Add(new User() { Username = "iii", Name = "iii", Surname = "iii", Password = "iii", Role = Models.Enums.ERole.ADMINISTRATOR });
51
            userList.Add(new User() { Username = "jjj", Name = "jjj", Surname = "jjj", Password = "jjj", Role = Models.Enums.ERole.ADMINISTRATOR });
52

    
53
            //filling up the database
54
            foreach (var user in userList)
55
            {
56
                US.CreateUser(user.Username, user.Name, user.Surname, user.Password, user.Role);
57
            }
58
            ctx.SaveChanges();
59
        }
60

    
61
        [TestMethod]
62
        [DataRow("kkk", "kkk", "kkk", "kkk", ERole.ANNOTATOR, true)]
63
        [DataRow("lll", "lll", "lll", "", ERole.ANNOTATOR, true)]
64
        [DataRow("mmm", "mmm", "", "mmm", ERole.ANNOTATOR, true)]
65
        [DataRow("nnn", "", "nnn", "nnn", ERole.ANNOTATOR, true)]
66
        [DataRow("", "ooo", "ooo", "ooo", ERole.ADMINISTRATOR, false)]
67
        //method tests saving user into database, retrieving all users checking if the one saved is between them
68
        public void CreateUser_Test(string username, string name, string surname, string password, ERole role, bool shouldBeRigh)
69
        {
70
            //creation of new user
71
            var output = US.CreateUser(username, name, surname, password, role);
72
            ctx.SaveChanges();
73
            if (output == null && !shouldBeRigh)
74
            {
75
                //trying to insert user with taken username resolves with refusing 
76
                return;
77
            }
78

    
79
            //check for empty atributes
80
            if((username == null || username == "")&& output!=null)
81
            {
82
                Assert.Fail("user with no username created");
83
                return;
84
            }
85
            if ((name == null || name == "") && output != null)
86
            {
87
                Assert.Fail("user with no name created");
88
                return;
89
            }
90
            if ((surname == null || surname == "") && output != null)
91
            {
92
                Assert.Fail("user with no surname created");
93
                return;
94
            }
95
            if ((password == null || password == "") && output != null)
96
            {
97
                Assert.Fail("user with no password created");
98
                return;
99
            }
100

    
101
            //retrieving all users checking via username
102
            foreach (var user in ctx.Users)
103
            {
104
                if (user.Username == username)
105
                {
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");
112
                    return;
113
                }
114
            }
115
            //if there was username not found, inserted users username must have been changed or wasnt saved into database at all - test fails
116
            Assert.Fail("supposedly created user was not found in database");
117
        }
118

    
119
        [TestMethod]
120
        [DataRow("aaa", "aaa", "aaa", "aaa", "aaa", ERole.ANNOTATOR)]
121
        [DataRow("bbb", "bbb", "bbb", "bbb", "bbb", ERole.ANNOTATOR)]
122
        [DataRow("ccc", "ccc", "ccc", "ccc", "ccc", ERole.ADMINISTRATOR)]
123
        //method testing, that user searched by username is the one wanted - data are inserted in constructor
124
        public void GetUserByUsername_Test(string insertedUsername, string username, string name, string surname, string password, ERole role)
125
        {
126
            User? actual = US.GetUserByUsername(insertedUsername);
127
            Assert.AreEqual(username, actual.Username, "wrong username");
128
            Assert.AreEqual(name, actual.Name, "wrong name");
129
            Assert.AreEqual(surname, actual.Surname, "wrong surname");
130
            Assert.AreNotEqual(password, actual.Password, "same password");
131
            Assert.AreEqual(role, actual.Role, "wrong role");
132
        }
133

    
134
        [TestMethod]
135
        [DataRow("aaa", "aaa", "aaa", "aaa", ERole.ANNOTATOR)]
136
        [DataRow("bbb", "bbb", "bbb", "bbb", ERole.ANNOTATOR)]
137
        [DataRow("ccc", "ccc", "ccc", "ccc", ERole.ADMINISTRATOR)]
138
        //testing method to get user via his ID, using another tested method - GetUserByUsername
139
        public void GetUserById_Test(string username, string name, string surname, string password, ERole role)
140
        {
141
            User? expected = US.GetUserByUsername(username);
142
            if (expected == null)
143
            {
144
                Assert.Fail("There is mistake in test data or in GetUserByUsername method");
145
                return;
146
            }
147
            User? actual = US.GetUserById(expected.Id);
148

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

    
152
            //testing all parameters
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");
158
        }
159

    
160

    
161

    
162

    
163
        [TestMethod]
164
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "uuu", "xxx", "xxx", ERole.ANNOTATOR)]
165
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "uuu", "xxx", ERole.ANNOTATOR)]
166
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "xxx", "uuu", ERole.ANNOTATOR)]
167
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "aaa", "bbb", "ccc", ERole.ANNOTATOR)]
168
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "bbb", "ccc", ERole.ANNOTATOR)]
169
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "xxx", "xxx", ERole.ADMINISTRATOR)]
170
        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "bbb", "ccc", ERole.ADMINISTRATOR)]
171
        //creating new user and updating him
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)
173
        {
174
            //new user creation
175
            User? newUser = US.CreateUser(oldUsername, oldName, oldSurname, password, oldRole);
176
            if(newUser == null)
177
            {
178
                Assert.Fail("user not created (maybe already user username?)");
179
            }
180
            User? actual = US.UpdateUser(newUser, username, name, surname, role);
181

    
182
            Assert.IsNotNull(actual, "user wasnt updated, null was returned");
183
            Assert.AreEqual(username, actual.Username, "wrong username");
184
            Assert.AreEqual(name, actual.Name, "wrong name");
185
            Assert.AreEqual(surname, actual.Surname, "wrong surname");
186
            Assert.AreEqual(role, actual.Role, "wrong role");
187
            Assert.AreNotEqual(password, actual.Password, "same password");
188

    
189
        }
190
        [TestMethod]
191
        [DataRow("aaa", "bbb")]//anotator
192
        [DataRow("ccc", "aaa")]//admin
193
        [DataRow("bbb", "")]//set to none password
194
        [DataRow("eee", "eee")]//keep old password
195
        //test of changing password
196
        public void ChangePassword_Test(string username, string newPassword) 
197
        {
198
            //get user using GetUserByUsername
199
            User? changingUser = US.GetUserByUsername(username);
200
            if(changingUser == null)
201
            {
202
                Assert.Fail("user not found");
203
                return;
204
            }
205
            //get old password hash
206
            var oldPassword = changingUser.Password;
207

    
208
            //change password
209
            User? changed = US.ChangePassword(changingUser, newPassword);
210
            //new password is not empty and database returned null for user (user had to be there)
211
            if(changed == null && (newPassword!="" || newPassword != null))
212
            {
213
                Assert.Fail("user not returned");
214
                return;
215
            }
216
            //new password is empty
217
            if((newPassword == "" || newPassword == null) && changed != null)
218
            {
219
                Assert.Fail("empty password was accepted");
220
                return;
221
            }
222
            //no user was returned back after ChangePassword
223
            if(changed == null)
224
            {
225
                Assert.Fail("user not returned or found or there was mistake in password change");
226
                return;
227
            }
228

    
229
            //get new password hash
230
            var renewedPassword = changed.Password;
231

    
232
            Assert.AreNotEqual(oldPassword, renewedPassword, "password was not changed or it was changed on same value");
233

    
234
        }
235
        [TestMethod]
236
        [DataRow("aaa", "aaa")]//anotator
237
        [DataRow("fff", "fff")]//anotator
238
        [DataRow("iii", "iii")]//admin
239
        //testing login autentication CheckUsernamePassword
240
        public void CheckUsernamePassword_Correct(string username, string password)
241
        {
242
            User? checkedUser = US.CheckUsernamePassword(username, password);
243
            if(checkedUser == null)
244
            {
245
                //there are just correct users - this should have returned user
246
                Assert.Fail("there are just correct users - this should have returned user");
247
                return;
248
            }
249
            User? expected = US.GetUserByUsername(username);
250
            if(expected == null)
251
            {
252
                Assert.Fail("There is mistake in tested data");
253
                return;
254
            }
255
            Assert.AreEqual(expected, checkedUser, "Users are not same (maybe wrong password)");
256
        }
257

    
258

    
259
        [TestMethod]
260
        [DataRow("aaa", "bbb")]//changedPassword?
261
        [DataRow("fff", "fgh")]//anotator
262
        [DataRow("iii", "123")]//admin
263
        public void CheckUsernamePassword_Incorrect(string username, string password)
264
        {
265
            User? checkedUser = US.CheckUsernamePassword(username, password);
266
            if (checkedUser != null)
267
            {
268
                //there are just incorrect users - this should not have returned user
269
                Assert.Fail("there are just incorrect users-passwords - this should not return user");
270
                return;
271
            }
272
        }
273

    
274
        [TestMethod]
275
        [DataRow("fff", "fff", "123")]//anotator
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
279
        public void CheckUsernamePassword_ChangedPassword(string username, string oldPassword, string newPassword)
280
        {
281
            //get user from database
282
            User? oldUser = US.CheckUsernamePassword(username, oldPassword);
283
            if (oldUser == null) 
284
            {
285
                Assert.Fail("This should have returned user - there are wrong input data or CheckUsernamePassword isnt working properly");
286
                return;
287
            }
288
            //change users password
289
            User? changedUser = US.ChangePassword(oldUser, newPassword);
290
            //update database so changes are recognisable
291
            ctx.SaveChanges();
292
            if (changedUser == null)
293
            {
294
                Assert.Fail("There is mistake in another method \"ChangePassword\" test it!");
295
            }
296
            //check if new password is in place
297
            User? newUser = US.CheckUsernamePassword(username, newPassword);
298
            if(newUser == null)
299
            {
300
                Assert.Fail("No user was returned - password probably wasnt changed");
301
            }
302
            Assert.AreEqual(oldUser.Username, newUser.Username, "returned users usernames are not same");
303
            Assert.AreEqual(oldUser.Name, newUser.Name, "returned users names are not same");
304
            Assert.AreEqual(oldUser.Surname, newUser.Surname, "returned users surnames are not same");
305
            Assert.AreEqual(oldUser.Role, newUser.Role, "returned users roles are not same");
306

    
307
            //check if there is no way to login with old password
308
            User? oldPasswordTester = US.CheckUsernamePassword(username, oldPassword);
309
            if(oldPasswordTester != null)
310
            {
311
                Assert.Fail("old password is still viable after password change");
312
            }
313
        }
314
        [TestMethod]
315
        //I dont really know how to test this
316
        public void GetUsers_Test()
317
        {
318
            var userList = US.GetUsers();
319
            Assert.AreEqual(ctx.Users.Count(), userList.Users.Count, "I dont know if this is even test");
320
        }
321

    
322

    
323

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

    
347
        //login from AuthService testing with incorrect username-password
348
        [TestMethod]
349
        [DataRow("aaa", "bbb")]//changedPassword?
350
        [DataRow("fff", "fgh")]//anotator
351
        [DataRow("iii", "123")]//admin
352
        public void AuthService_Login_Incorrect(string username, string password)
353
        {
354
            //creating new instance of AuthService
355
            var jwt = TestingJwtUtils.GetJwtUtils();
356
            var AS = new AuthService(US, jwt);
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
359
            if (loginResults != null)
360
            {
361
                Assert.IsFalse(loginResults.Ok);
362
                Assert.IsNull(loginResults.Token);
363
            }
364
            else
365
            {
366
                Assert.IsNull(loginResults);
367
            }
368
        }
369
    }
370
}
(10-10/11)