commit ba13a48a6970ad6164cf7a1b271218b63b138b88
Author: schenkj <schenkj@students.zcu.cz>
Date:   Sun Apr 17 20:12:10 2022 +0200

    backend login
    
    login on backend tested (JwtUtils in Utils inicialization)

diff --git a/Backend/BackendTesting/UserManagementTesting.cs b/Backend/BackendTesting/UserManagementTesting.cs
index 61afbeb..6f204f9 100644
--- a/Backend/BackendTesting/UserManagementTesting.cs
+++ b/Backend/BackendTesting/UserManagementTesting.cs
@@ -301,11 +301,54 @@ namespace Core.Services
             }
         }
         [TestMethod]
-        [DataRow(10)]//expected number of users in database
-        public void GetUsers_Test(int expected)
+        public void GetUsers_Test()
         {
             var userList = US.GetUsers();
-            Assert.AreEqual(expected, userList.Users.Count, "I counted poorly probably");
+            Assert.AreEqual(ctx.Users.Count(), userList.Users.Count, "I dont know if this is even test");
+        }
+
+
+
+        [TestMethod]
+        //correct
+        [DataRow("aaa", "aaa", ERole.ANNOTATOR)]//anotator
+        [DataRow("fff", "fff", ERole.ANNOTATOR)]//anotator
+        [DataRow("iii", "iii", ERole.ADMINISTRATOR)]//admin
+        public void AuthService_Login_Correct(string username, string password, ERole role)
+        {
+            var jwt = TestingJwtUtils.GetJwtUtils();
+            var AS = new AuthService(US, jwt);
+         //   var AS = new AuthServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
+            var loginResults = AS.Login(username, password);
+            Assert.IsNotNull(loginResults);
+            Assert.IsTrue(loginResults.Ok);
+            Assert.IsNotNull(loginResults.Token);
+            DateTime exp = loginResults.Expiration;
+            var now = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
+            Assert.IsTrue(exp.CompareTo(now) > 0);//if compare is >0 -> exp is in the future
+            Assert.AreEqual(role, loginResults.Role);
+        }
+
+        //incorrect
+        [TestMethod]
+        [DataRow("aaa", "bbb")]//changedPassword?
+        [DataRow("fff", "fgh")]//anotator
+        [DataRow("iii", "123")]//admin
+        public void AuthService_Login_Incorrect(string username, string password)
+        {
+            var jwt = TestingJwtUtils.GetJwtUtils();
+            var AS = new AuthService(US, jwt);
+            //   var AS = new AuthServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
+            var loginResults = AS.Login(username, password);
+            if (loginResults != null)
+            {
+                Assert.IsFalse(loginResults.Ok);
+                Assert.IsNull(loginResults.Token);
+            }
+            else
+            {
+                Assert.IsNull(loginResults);
+            }
         }
     }
 }
diff --git a/Backend/BackendTesting/Utils.cs b/Backend/BackendTesting/Utils.cs
index 3e3351e..1bb088c 100644
--- a/Backend/BackendTesting/Utils.cs
+++ b/Backend/BackendTesting/Utils.cs
@@ -13,6 +13,8 @@ using AutoMapper;
 using Core.MapperProfiles;
 using Microsoft.Extensions.Configuration;
 using Core.Contexts;
+using Microsoft.Extensions.Options;
+using Core.Authentication;
 
 namespace Core.Services
 {
@@ -52,4 +54,17 @@ namespace Core.Services
             return new UserServiceEF(new DatabaseContext(configuration), TestingLogger.GetLogger(), TestingMapper.GetMapper()); 
         }
     }
+
+    public static class TestingJwtUtils
+    {
+        public static IOptions<JwtConfig> jwtConfig = Options.Create<JwtConfig>(new JwtConfig() { Issuer = "asd", Secret = "WTaDxVUWTaDxVU3AXX9YqM2ukx98mKFWTaDxVU3AXX9YqM2ukx98mKFWTaDxVU3AXX9YqM2ukx98mKF3AXX9YqM2ukx98mKF" });
+        public static IJwtUtils GetJwtUtils()
+        {
+            ConfigurationManager m = new ConfigurationManager();
+            m["JwtConfig:Secret"] = "WTaDxVUWTaDxVU3AXX9YqM2ukx98mKFWTaDxVU3AXX9YqM2ukx98mKFWTaDxVU3AXX9YqM2ukx98mKF3AXX9YqM2ukx98mKF";
+            m["JwtConfig:Issuer"] = "asd";
+            var tokenValidationParams = JwtUtils.GetTokenValidationParameters(m);
+            return new JwtUtils(jwtConfig, tokenValidationParams);
+        }
+    }
 }
