commit ea53e0e1a51f2a85b8f30b22f6f71c5d08e8f15a
Author: schenkj <schenkj@students.zcu.cz>
Date:   Sat Apr 16 10:31:25 2022 +0200

    user testing
    
    first of was user testing mandatory, before testing autentization - with this user should be properly tested

diff --git a/Backend/BackendTesting/UserManagementTesting.cs b/Backend/BackendTesting/UserManagementTesting.cs
index 8060154..61afbeb 100644
--- a/Backend/BackendTesting/UserManagementTesting.cs
+++ b/Backend/BackendTesting/UserManagementTesting.cs
@@ -13,27 +13,299 @@ using AutoMapper;
 using Core.MapperProfiles;
 using Core.Contexts;
 using Microsoft.Extensions.Configuration;
+using Models.Users;
 
 namespace Core.Services
 {
-    public class UserContext : DatabaseContext
+    [TestClass]
+    public class UserManagementTesting      //testing of Core/Services/UserService
     {
-        public UserContext(IConfiguration configuration) : base(configuration) { }
+        private static readonly IConfiguration configuration = new ConfigurationBuilder().Build();
+        public DatabaseContext ctx;
+
+        public Core.Services.IUserService US;
+        public UserManagementTesting()
+        {
+            this.ctx = new DatabaseContext(configuration);
+            this.US = new UserServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
+
+            ctx.Database.EnsureDeleted();
+            ctx.Database.EnsureCreated();
+
+            //vytvoření dat pro testování
+            var userList = new List<User>();
+            userList.Add(new User() { Username = "aaa", Name = "aaa", Surname = "aaa", Password = "aaa", Role = Models.Enums.ERole.ANNOTATOR });
+            userList.Add(new User() { Username = "bbb", Name = "bbb", Surname = "bbb", Password = "bbb", Role = Models.Enums.ERole.ANNOTATOR });
+            userList.Add(new User() { Username = "ccc", Name = "ccc", Surname = "ccc", Password = "ccc", Role = Models.Enums.ERole.ADMINISTRATOR });
+            userList.Add(new User() { Username = "ddd", Name = "ddd", Surname = "ddd", Password = "ddd", Role = Models.Enums.ERole.ANNOTATOR });
+            userList.Add(new User() { Username = "eee", Name = "eee", Surname = "eee", Password = "eee", Role = Models.Enums.ERole.ANNOTATOR });
+            userList.Add(new User() { Username = "fff", Name = "fff", Surname = "fff", Password = "fff", Role = Models.Enums.ERole.ANNOTATOR });
+            userList.Add(new User() { Username = "ggg", Name = "ggg", Surname = "ggg", Password = "ggg", Role = Models.Enums.ERole.ANNOTATOR });
+            userList.Add(new User() { Username = "hhh", Name = "hhh", Surname = "hhh", Password = "hhh", Role = Models.Enums.ERole.ADMINISTRATOR });
+            userList.Add(new User() { Username = "iii", Name = "iii", Surname = "iii", Password = "iii", Role = Models.Enums.ERole.ADMINISTRATOR });
+            userList.Add(new User() { Username = "jjj", Name = "jjj", Surname = "jjj", Password = "jjj", Role = Models.Enums.ERole.ADMINISTRATOR });
 
-        //public static ILoggerFactory logger = LoggerFactory.Create(builder => { builder.AddConsole(); });
-        public ILogger logger = new LoggerConfiguration().WriteTo.Console().CreateLogger(); /* logger */
+            //nahrani do databaze
+            foreach (var user in userList)
+            {
+                US.CreateUser(user.Username, user.Name, user.Surname, user.Password, user.Role);
+            }
+            ctx.SaveChanges();
+        }
 
-        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
+        [TestMethod]
+        [DataRow("kkk", "kkk", "kkk", "kkk", ERole.ANNOTATOR, true)]
+        [DataRow("lll", "lll", "lll", "", ERole.ANNOTATOR, true)]
+        [DataRow("mmm", "mmm", "", "mmm", ERole.ANNOTATOR, true)]
+        [DataRow("nnn", "", "nnn", "nnn", ERole.ANNOTATOR, true)]
+        [DataRow("", "ooo", "ooo", "ooo", ERole.ADMINISTRATOR, false)]
+        //method tests saving user into database, retrieving all users checking if the one saved is between them
+        public void CreateUser_Test(string username, string name, string surname, string password, ERole role, bool shouldBeRigh)
         {
-            base.OnConfiguring(optionsBuilder);
-            optionsBuilder.UseSqlite("Data source=db.sqlite", b => b.MigrationsAssembly("RestAPI"));
-            //optionsBuilder.(logger);
+            //creation of new user
+            var output = US.CreateUser(username, name, surname, password, role);
+            ctx.SaveChanges();
+            if (output == null && !shouldBeRigh)
+            {
+                //trying to insert user with taken username resolves with refusing 
+                return;
+            }
+
+            //check for empty atributes
+            if((username == null || username == "")&& output!=null)
+            {
+                Assert.Fail("user with no username created");
+                return;
+            }
+            if ((name == null || name == "") && output != null)
+            {
+                Assert.Fail("user with no name created");
+                return;
+            }
+            if ((surname == null || surname == "") && output != null)
+            {
+                Assert.Fail("user with no surname created");
+                return;
+            }
+            if ((password == null || password == "") && output != null)
+            {
+                Assert.Fail("user with no password created");
+                return;
+            }
+
+            //retrieving all users checking via username
+            foreach (var user in ctx.Users)
+            {
+                if (user.Username == username)
+                {
+                    //checking, that user with wanted username was not changed
+                    /*Assert.AreEqual(expected.Username, user.Username);*/
+                    Assert.AreEqual(name, user.Name);
+                    Assert.AreEqual(surname, user.Surname);
+                    Assert.AreNotEqual(password, user.Password);
+                    Assert.AreEqual(role, user.Role);
+                    return;
+                }
+            }
+            //if there was username not found, inserted user must have been changed or wasnt saved into database at all - test fails
+            Assert.Fail("supposedly created user was not found in database");
         }
 
-    }
-    [TestClass]
-    public class UserManagementTesting      //testing of Core/Services/UserService
-    {
+        [TestMethod]
+        [DataRow("aaa", "aaa", "aaa", "aaa", "aaa", ERole.ANNOTATOR)]
+        [DataRow("bbb", "bbb", "bbb", "bbb", "bbb", ERole.ANNOTATOR)]
+        [DataRow("ccc", "ccc", "ccc", "ccc", "ccc", ERole.ADMINISTRATOR)]
+        //method testing, that user searched by username is the one wanted - data are inserted before
+        public void GetUserByUsername_Test(string insertedUsername, string username, string name, string surname, string password, ERole role)
+        {
+            User? actual = US.GetUserByUsername(insertedUsername);
+            Assert.AreEqual(username, actual.Username, "wrong username");
+            Assert.AreEqual(name, actual.Name, "wrong name");
+            Assert.AreEqual(surname, actual.Surname, "wrong surname");
+            Assert.AreNotEqual(password, actual.Password, "same password");
+            Assert.AreEqual(role, actual.Role, "wrong role");
+        }
+
+        [TestMethod]
+        [DataRow("aaa", "aaa", "aaa", "aaa", ERole.ANNOTATOR)]
+        [DataRow("bbb", "bbb", "bbb", "bbb", ERole.ANNOTATOR)]
+        [DataRow("ccc", "ccc", "ccc", "ccc", ERole.ADMINISTRATOR)]
+        public void GetUserById_Test(string username, string name, string surname, string password, ERole role)
+        {
+            //TODO Find id other way then via another tested method
+            User? expected = US.GetUserByUsername(username);
+            if (expected == null)
+            {
+                Assert.Fail();
+                return;
+            }
+
+            User? actual = US.GetUserById(expected.Id);
+
+            //testing whole users
+            Assert.AreEqual(expected, actual);
+
+            //testing all parameters
+            Assert.AreEqual(actual.Username, username);
+            Assert.AreEqual(actual.Name, name);
+            Assert.AreEqual(actual.Surname, surname);
+            Assert.AreNotEqual(actual.Password, password);
+            Assert.AreEqual(actual.Role, role);
+        }
+
+
 
+
+        [TestMethod]
+        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "uuu", "xxx", "xxx", ERole.ANNOTATOR)]
+        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "uuu", "xxx", ERole.ANNOTATOR)]
+        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "xxx", "uuu", ERole.ANNOTATOR)]
+        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "aaa", "bbb", "ccc", ERole.ANNOTATOR)]
+        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "bbb", "ccc", ERole.ANNOTATOR)]
+        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "xxx", "xxx", ERole.ADMINISTRATOR)]
+        [DataRow("xxx", "xxx", "xxx", "xxx", ERole.ANNOTATOR, "xxx", "bbb", "ccc", ERole.ADMINISTRATOR)]
+        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)
+        {
+            User? newUser = US.CreateUser(oldUsername, oldName, oldSurname, password, oldRole);
+            if(newUser == null)
+            {
+                Assert.Fail("user not created");
+            }
+            User? actual = US.UpdateUser(newUser, username, name, surname, role);
+
+            Assert.IsNotNull(actual);
+            Assert.AreEqual(username, actual.Username, "wrong username");
+            Assert.AreEqual(name, actual.Name, "wrong name");
+            Assert.AreEqual(surname, actual.Surname, "wrong surname");
+            Assert.AreEqual(role, actual.Role, "wrong role");
+            Assert.AreNotEqual(password, actual.Password, "same password");
+
+        }
+        [TestMethod]
+        [DataRow("aaa", "bbb")]//anotator
+        [DataRow("ccc", "aaa")]//admin
+        [DataRow("bbb", "")]//set to none password
+        [DataRow("eee", "eee")]//keep old password
+        public void ChangePassword_Test(string username, string newPassword) 
+        {
+            //get user
+            User? changingUser = US.GetUserByUsername(username);
+            if(changingUser == null)
+            {
+                Assert.Fail("user not found");
+                return;
+            }
+            //get old password hash
+            var oldPassword = changingUser.Password;
+
+            //change password
+            User? changed = US.ChangePassword(changingUser, newPassword);
+            //new password is not empty and database returned null for user (user had to be there)
+            if(changed == null && (newPassword!="" || newPassword != null))
+            {
+                Assert.Fail("user not returned");
+                return;
+            }
+            //new password is empty
+            if((newPassword == "" || newPassword == null) && changed != null)
+            {
+                Assert.Fail("empty password was accepted");
+                return;
+            }
+            //no user was returned back after ChangePassword
+            if(changed == null)
+            {
+                Assert.Fail("user not returned or found or there was mistake in password change");
+                return;
+            }
+
+            //get new password hash
+            var renewedPassword = changed.Password;
+
+            Assert.AreNotEqual(oldPassword, renewedPassword, "password was not changed or it was changed on same value");
+
+        }
+        [TestMethod]
+        [DataRow("aaa", "aaa")]//anotator
+        [DataRow("fff", "fff")]//anotator
+        [DataRow("iii", "iii")]//admin
+        public void CheckUsernamePassword_Correct(string username, string password)
+        {
+            User? checkedUser = US.CheckUsernamePassword(username, password);
+            if(checkedUser == null)
+            {
+                //there are just correct users - this should have returned user
+                Assert.Fail("there are just correct users - this should have returned user");
+                return;
+            }
+            User? expected = US.GetUserByUsername(username);
+            if(expected == null)
+            {
+                Assert.Fail("There is mistake in tested data");
+                return;
+            }
+            Assert.AreEqual(expected, checkedUser);
+        }
+
+        [TestMethod]
+        [DataRow("aaa", "bbb")]//changedPassword?
+        [DataRow("fff", "fgh")]//anotator
+        [DataRow("iii", "123")]//admin
+        public void CheckUsernamePassword_Incorrect(string username, string password)
+        {
+            User? checkedUser = US.CheckUsernamePassword(username, password);
+            if (checkedUser != null)
+            {
+                //there are just correct users - this should have returned user
+                Assert.Fail("there are just incorrect users-passwords - this should not return user");
+                return;
+            }
+        }
+
+        [TestMethod]
+        [DataRow("fff", "fff", "123")]//anotator
+        [DataRow("iii", "iii", "456")]//admin
+        public void CheckUsernamePassword_ChangedPassword(string username, string oldPassword, string newPassword)
+        {
+            //get user from database
+            User? oldUser = US.CheckUsernamePassword(username, oldPassword);
+            if (oldUser == null) 
+            {
+                Assert.Fail("This should have returned user - there are wrong input data");
+                return;
+            }
+            //change users password
+            User? changedUser = US.ChangePassword(oldUser, newPassword);
+            //update database so changes are recognisable
+            ctx.SaveChanges();
+            if (changedUser == null)
+            {
+                Assert.Fail("There is mistake in another method \"ChangePassword\" test it!");
+            }
+            //check if new password is in place
+            User? newUser = US.CheckUsernamePassword(username, newPassword);
+            if(newUser == null)
+            {
+                Assert.Fail("No user was returned - password probably wasnt changed");
+            }
+            Assert.AreEqual(oldUser.Username, newUser.Username, "returned users usernames are not same");
+            Assert.AreEqual(oldUser.Name, newUser.Name, "returned users names are not same");
+            Assert.AreEqual(oldUser.Surname, newUser.Surname, "returned users surnames are not same");
+            Assert.AreEqual(oldUser.Role, newUser.Role, "returned users roles are not same");
+
+            //check if there is no way to login with old password
+            User? oldPasswordTester = US.CheckUsernamePassword(username, oldPassword);
+            if(oldPasswordTester != null)
+            {
+                Assert.Fail("old password is still viable after password change");
+            }
+        }
+        [TestMethod]
+        [DataRow(10)]//expected number of users in database
+        public void GetUsers_Test(int expected)
+        {
+            var userList = US.GetUsers();
+            Assert.AreEqual(expected, userList.Users.Count, "I counted poorly probably");
+        }
     }
 }
diff --git a/Backend/BackendTesting/Utils.cs b/Backend/BackendTesting/Utils.cs
index 3b29e22..3e3351e 100644
--- a/Backend/BackendTesting/Utils.cs
+++ b/Backend/BackendTesting/Utils.cs
@@ -11,6 +11,8 @@ using System.Threading.Tasks;
 using Microsoft.EntityFrameworkCore;
 using AutoMapper;
 using Core.MapperProfiles;
+using Microsoft.Extensions.Configuration;
+using Core.Contexts;
 
 namespace Core.Services
 {
@@ -40,4 +42,14 @@ namespace Core.Services
             return new LoggerConfiguration().WriteTo.Console().CreateLogger(); /* logger */
         }
     }
+
+    public static class TestingUserService
+    {
+        private static readonly IConfiguration configuration = new ConfigurationBuilder().Build();
+       
+        public static IUserService GetUserService()
+        {
+            return new UserServiceEF(new DatabaseContext(configuration), TestingLogger.GetLogger(), TestingMapper.GetMapper()); 
+        }
+    }
 }
