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
using Core.Services;
19

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

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

    
42
            //data for testing creation
43
            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

    
55
            //filling up the database
56
            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

    
63
        [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
        {
72
            //creation of new user
73
            var output = US.CreateUser(username, name, surname, password, role);
74
            ctx.SaveChanges();
75
            if (output == null && !shouldBeRigh)
76
            {
77
                //trying to insert user with taken username resolves with refusing 
78
                return;
79
            }
80

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

    
103
            //retrieving all users checking via username
104
            foreach (var user in ctx.Users)
105
            {
106
                if (user.Username == username)
107
                {
108
                    //checking, that user with wanted username was not changed and he is present in database
109
                    //if there were some data changed (not username) - these tests will fail
110
                    Assert.AreEqual(name, user.Name, "names are not same");
111
                    Assert.AreEqual(surname, user.Surname, "surnames are not same");
112
                    Assert.AreNotEqual(password, user.Password, "passwords are same - it should have been hashed");
113
                    Assert.AreEqual(role, user.Role, "roles are not same");
114
                    return;
115
                }
116
            }
117
            //if there was username not found, inserted users username must have been changed or wasnt saved into database at all - test fails
118
            Assert.Fail("supposedly created user was not found in database");
119
        }
120

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

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

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

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

    
162

    
163

    
164

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

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

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

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

    
231
            //get new password hash
232
            var renewedPassword = changed.Password;
233

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

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

    
260

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

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

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

    
324

    
325

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

    
349
        //login from AuthService testing with incorrect username-password
350
        [TestMethod]
351
        [DataRow("aaa", "bbb")]//changedPassword?
352
        [DataRow("fff", "fgh")]//anotator
353
        [DataRow("iii", "123")]//admin
354
        public void AuthService_Login_Incorrect(string username, string password)
355
        {
356
            //creating new instance of AuthService
357
            var jwt = TestingJwtUtils.GetJwtUtils();
358
            var AS = new AuthService(US, jwt);
359
            var loginResults = AS.Login(username, password);
360
            //checking if login results are null or there is false in OK and there is none obtained token
361
            if (loginResults != null)
362
            {
363
                Assert.IsFalse(loginResults.Ok);
364
                Assert.IsNull(loginResults.Token);
365
            }
366
            else
367
            {
368
                Assert.IsNull(loginResults);
369
            }
370
        }
371
    }
372
}
(10-10/11)