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);

Také k dispozici: Unified diff