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
|
|
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
|
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
|
|
112
|
//retrieving all users checking via username
|
113
|
foreach (var user in ctx.Users)
|
114
|
{
|
115
|
if (user.Username == username)
|
116
|
{
|
117
|
//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
|
return;
|
124
|
}
|
125
|
}
|
126
|
//if there was username not found, inserted users username must have been changed or wasnt saved into database at all - test fails
|
127
|
Assert.Fail("supposedly created user was not found in database");
|
128
|
}
|
129
|
|
130
|
[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
|
//method testing, that user searched by username is the one wanted - data are inserted in constructor
|
135
|
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
|
//testing method to get user via his ID, using another tested method - GetUserByUsername
|
150
|
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
|
Assert.Fail("There is mistake in test data or in GetUserByUsername method");
|
156
|
return;
|
157
|
}
|
158
|
User? actual = US.GetUserById(expected.Id);
|
159
|
|
160
|
//testing whole users
|
161
|
Assert.AreEqual(expected, actual, "Users are not same");
|
162
|
|
163
|
//testing all parameters
|
164
|
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
|
}
|
170
|
|
171
|
|
172
|
|
173
|
|
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
|
//creating new user and updating him
|
183
|
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
|
//new user creation
|
186
|
User? newUser = US.CreateUser(oldUsername, oldName, oldSurname, password, oldRole);
|
187
|
if(newUser == null)
|
188
|
{
|
189
|
Assert.Fail("user not created (maybe already user username?)");
|
190
|
}
|
191
|
User? actual = US.UpdateUser(newUser, username, name, surname, role);
|
192
|
|
193
|
Assert.IsNotNull(actual, "user wasnt updated, null was returned");
|
194
|
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
|
//test of changing password
|
207
|
public void ChangePassword_Test(string username, string newPassword)
|
208
|
{
|
209
|
//get user using GetUserByUsername
|
210
|
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
|
try
|
221
|
{
|
222
|
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
|
}
|
247
|
catch(Exception ex)
|
248
|
{
|
249
|
if (newPassword == null || newPassword == "") Assert.IsTrue(true,"Try to change password on empty resulted in exception - expected and wanted outcome");
|
250
|
}
|
251
|
|
252
|
|
253
|
}
|
254
|
[TestMethod]
|
255
|
[DataRow("aaa", "aaa")]//anotator
|
256
|
[DataRow("fff", "fff")]//anotator
|
257
|
[DataRow("iii", "iii")]//admin
|
258
|
//testing login autentication CheckUsernamePassword
|
259
|
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
|
Assert.AreEqual(expected, checkedUser, "Users are not same (maybe wrong password)");
|
275
|
}
|
276
|
|
277
|
|
278
|
[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
|
//there are just incorrect users - this should not have returned user
|
288
|
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
|
//more complex test of the same as the above
|
297
|
//passwords are changed and then there is login
|
298
|
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
|
Assert.Fail("This should have returned user - there are wrong input data or CheckUsernamePassword isnt working properly");
|
305
|
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
|
//I dont really know how to test this
|
335
|
public void GetUsers_Test()
|
336
|
{
|
337
|
var userList = US.GetUsers();
|
338
|
Assert.AreEqual(ctx.Users.Count(), userList.Users.Count, "I dont know if this is even test");
|
339
|
}
|
340
|
|
341
|
|
342
|
|
343
|
[TestMethod]
|
344
|
//login from AuthService testing with correct username-password
|
345
|
[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
|
//creating new instance of AuthService
|
351
|
var jwt = TestingJwtUtils.GetJwtUtils();
|
352
|
var AS = new AuthService(US, jwt);
|
353
|
//try login
|
354
|
var loginResults = AS.Login(username, password);
|
355
|
//check results
|
356
|
Assert.IsNotNull(loginResults);
|
357
|
Assert.IsTrue(loginResults.Ok);
|
358
|
Assert.IsNotNull(loginResults.Token);
|
359
|
DateTime exp = loginResults.Expiration;
|
360
|
//expiration is in the future
|
361
|
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
|
//login from AuthService testing with incorrect username-password
|
367
|
[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
|
//creating new instance of AuthService
|
374
|
var jwt = TestingJwtUtils.GetJwtUtils();
|
375
|
var AS = new AuthService(US, jwt);
|
376
|
var loginResults = AS.Login(username, password);
|
377
|
//checking if login results are null or there is false in OK and there is none obtained token
|
378
|
if (loginResults != null)
|
379
|
{
|
380
|
Assert.IsFalse(loginResults.Ok);
|
381
|
Assert.IsNull(loginResults.Token);
|
382
|
}
|
383
|
else
|
384
|
{
|
385
|
Assert.IsNull(loginResults);
|
386
|
}
|
387
|
}
|
388
|
}
|
389
|
}
|