示例#1
0
        public void InitializeMocks(T loggerClass)
        {
            _loggerMock = LoggerMock.SetupMock(loggerClass);
            _logger     = _loggerMock.Object;
            BaseMock.ShouldThrowException = false;

            _httpContextMock = HttpContextMock.SetupHttpContextMock();
            _httpContext     = _httpContextMock.Object;

            _dateTimeUtil = new DateTimeUtilMock();
            _tokenHandler = JwtSecurityTokenHandlerMock.SetupMock().Object;

            DbContextMock.ShouldThrowException = false;
            DbContextMock.SaveChangesResult    = 1;
            _dbContextMock = DbContextMock.SetupDbContext <IApplicationDbContext>();
            _dbContext     = _dbContextMock.Object;

            _tokenRepositoryMock = TokenRepositoryMock.SetupMock(_dbContext, _dateTimeUtil);
            _tokenRepository     = _tokenRepositoryMock.Object;

            _usersRepositoryMock = UsersRepositoryMock.SetupMock(_dbContext, _dateTimeUtil);
            _usersRepository     = _usersRepositoryMock.Object;

            _authServiceMock = AuthServiceMock.SetupMock(_usersRepository,
                                                         _tokenRepository,
                                                         _dateTimeUtil,
                                                         _logger as IEMSLogger <AuthService>,
                                                         _tokenHandler);
            _authService = _authServiceMock.Object;

            _usersServiceMock = UsersServiceMock.SetupMock(_usersRepository,
                                                           _logger as IEMSLogger <UsersService>, _dateTimeUtil);
            _usersService = _usersServiceMock.Object;
        }
示例#2
0
        public async Task LogoffFlowTest(string token, string device, bool callCloseCount)
        {
            var tokenRep = TokenRepositoryMock.CreateRepository();

            try
            {
                await Logoff.Execute(tokenRep, token, device);

                Assert.AreEqual(tokenRep.CloseCount, callCloseCount ? 1 : 0);
            }
            catch
            {
                Assert.AreEqual(tokenRep.CloseCount, callCloseCount ? 1 : 0);
            }
        }
示例#3
0
        public async Task LogoffCodeTest(string token, string device, bool exception, Type exceptionType = null)
        {
            var tokenRep = TokenRepositoryMock.CreateRepository();

            try
            {
                await Logoff.Execute(tokenRep, token, device);

                Assert.IsFalse(exception);
            }
            catch (Exception ex)
            {
                Assert.IsTrue((exception) && (exceptionType.FullName == ex.GetType().FullName));
            }
        }
示例#4
0
        public async Task LoginCodeTest(string appKey, string userName, string password, string device, bool exception, Type exceptionType = null)
        {
            var userRep      = UserRepositoryMock.CreateRepository();
            var tokenRep     = TokenRepositoryMock.CreateRepository();
            var tokenCreator = TokenCreatorMock.Create();

            try
            {
                await Login.Execute(tokenRep, userRep, tokenCreator, appKey, userName, password, device);

                Assert.IsFalse(exception);
            }
            catch (Exception ex)
            {
                Assert.IsTrue((exception) && (exceptionType.FullName == ex.GetType().FullName));
            }
        }
示例#5
0
        public async Task ChangePasswordCodeTest(string token, string currentPassword, string newPassword, string appKey,
                                                 string device, bool exception, Type exceptionType = null)
        {
            var userRep         = UserRepositoryMock.CreateRepository();
            var tokenRepository = TokenRepositoryMock.CreateRepository();

            try
            {
                await ChangeMyPassword.Execute(tokenRepository, userRep, token, currentPassword, newPassword, appKey, device);

                Assert.IsFalse(exception);
            }
            catch (Exception ex)
            {
                Assert.IsTrue((exception) && (exceptionType.FullName == ex.GetType().FullName));
            }
        }
示例#6
0
        public async Task CreateUserFlowTest(string token, string appKey, string device, bool callGetUserIdByTokenCount, bool callGetByIdCount)
        {
            var tokenRep      = TokenRepositoryMock.CreateRepository();
            var userRep       = UserRepositoryMock.CreateRepository();
            var tokenExtactor = TokenInfoExtractorMock.Create();

            try
            {
                await GetTokenInfo.Execute(tokenRep, userRep, tokenExtactor, token, appKey, device);

                Assert.AreEqual(userRep.GetByIdCount, callGetByIdCount ? 1 : 0);
                Assert.AreEqual(tokenRep.GetUserIdByTokenCount, callGetUserIdByTokenCount ? 1 : 0);
            }
            catch
            {
                Assert.AreEqual(userRep.GetByIdCount, callGetByIdCount ? 1 : 0);
                Assert.AreEqual(tokenRep.GetUserIdByTokenCount, callGetUserIdByTokenCount ? 1 : 0);
            }
        }
示例#7
0
        public async Task LoginFlowTest(string appKey = "", string userName = "", string password = "", string device = "", bool callGetByKeyToAppCount = false, bool callCreateToken = false, bool callTokenCreator = false)
        {
            var userRep      = UserRepositoryMock.CreateRepository();
            var tokenRep     = TokenRepositoryMock.CreateRepository();
            var tokenCreator = TokenCreatorMock.Create();

            try
            {
                await Login.Execute(tokenRep, userRep, tokenCreator, appKey, userName, password, device);

                Assert.AreEqual(userRep.GetByKeyToAppCount, callGetByKeyToAppCount ? 1 : 0);
                Assert.AreEqual(tokenRep.CreateCount, callCreateToken ? 1 : 0);
                Assert.AreEqual(tokenCreator.CreateCount, callTokenCreator ? 1 : 0);
            }
            catch
            {
                Assert.AreEqual(userRep.GetByKeyToAppCount, callGetByKeyToAppCount ? 1 : 0);
                Assert.AreEqual(tokenRep.CreateCount, callCreateToken ? 1 : 0);
                Assert.AreEqual(tokenCreator.CreateCount, callTokenCreator ? 1 : 0);
            }
        }
示例#8
0
        public async Task ChangeMyPasswordFlowTest(string token, string currentPassword, string newPassword, string appKey,
                                                   string device, bool callGetUserIdByToken, bool callGetById, bool callChangePassword)
        {
            var userRep         = UserRepositoryMock.CreateRepository();
            var tokenRepository = TokenRepositoryMock.CreateRepository();

            try
            {
                await ChangeMyPassword.Execute(tokenRepository, userRep, token, currentPassword, newPassword, appKey, device);

                Assert.AreEqual(userRep.GetByIdCount, callGetById ? 1 : 0);
                Assert.AreEqual(userRep.ChangePasswordCount, callChangePassword ? 1 : 0);
                Assert.AreEqual(tokenRepository.GetUserIdByTokenCount, callGetUserIdByToken ? 1 : 0);
            }
            catch
            {
                Assert.AreEqual(userRep.GetByIdCount, callGetById ? 1 : 0);
                Assert.AreEqual(userRep.ChangePasswordCount, callChangePassword ? 1 : 0);
                Assert.AreEqual(tokenRepository.GetUserIdByTokenCount, callGetUserIdByToken ? 1 : 0);
            }
        }
示例#9
0
        public async Task LogoffInvalidParametersTest(bool isTokenRepNull, string token, string device)
        {
            var tokenRep = isTokenRepNull ? null : TokenRepositoryMock.CreateRepository();

            try
            {
                await Logoff.Execute(tokenRep, token, device);

                Assert.Fail();
            }
            catch (Exception ex)
            {
                if (isTokenRepNull)
                {
                    Assert.AreEqual(ex.GetType(), typeof(ArgumentNullException));
                }
                else
                {
                    Assert.AreEqual(ex.GetType(), typeof(ArgumentException));
                }
            }
        }
示例#10
0
        public async Task LoginInvalidParametersTest(bool isTokenRepNull, bool isUserRepNull, bool isTokenCreatorNull, string key = "", string userName = "", string password = "", string device = "")
        {
            var tokenRep     = isTokenRepNull ? null : TokenRepositoryMock.CreateRepository();
            var userRep      = isUserRepNull ? null : UserRepositoryMock.CreateRepository();
            var tokenCreator = isTokenCreatorNull ? null : TokenCreatorMock.Create();

            try
            {
                await Login.Execute(tokenRep, userRep, tokenCreator, key, userName, password, device);

                Assert.Fail();
            }
            catch (Exception ex)
            {
                if (isTokenRepNull || isUserRepNull || isTokenCreatorNull)
                {
                    Assert.AreEqual(ex.GetType(), typeof(ArgumentNullException));
                }
                else
                {
                    Assert.AreEqual(ex.GetType(), typeof(ArgumentException));
                }
            }
        }
示例#11
0
        public async Task GetTokenInfoInvalidParametersTest(bool isTokenRepNull, bool isUserRepNull, bool isTokenExtractorNull, string token, string appKey, string device)
        {
            var tokenRep       = isTokenRepNull ? null : TokenRepositoryMock.CreateRepository();
            var userRep        = isUserRepNull ? null : UserRepositoryMock.CreateRepository();
            var tokenExytactor = isTokenExtractorNull ? null : TokenInfoExtractorMock.Create();

            try
            {
                await GetTokenInfo.Execute(tokenRep, userRep, tokenExytactor, token, appKey, device);

                Assert.Fail();
            }
            catch (Exception ex)
            {
                if (isTokenRepNull || isUserRepNull || isTokenExtractorNull)
                {
                    Assert.AreEqual(ex.GetType(), typeof(ArgumentNullException));
                }
                else
                {
                    Assert.AreEqual(ex.GetType(), typeof(ArgumentException));
                }
            }
        }
示例#12
0
        public async Task ChangeMyPasswordInvalidParametersTest(bool isUserRepNull, bool isTokenRepNull, string token,
                                                                string currentPassword, string newPassword, string appKey, string device)
        {
            var userRep         = isUserRepNull ? null : UserRepositoryMock.CreateRepository();
            var tokenRepository = isTokenRepNull ? null : TokenRepositoryMock.CreateRepository();

            try
            {
                await ChangeMyPassword.Execute(tokenRepository, userRep, token, currentPassword, newPassword, appKey, device);

                Assert.Fail();
            }
            catch (Exception ex)
            {
                if (isUserRepNull || isTokenRepNull)
                {
                    Assert.AreEqual(ex.GetType(), typeof(ArgumentNullException));
                }
                else
                {
                    Assert.AreEqual(ex.GetType(), typeof(ArgumentException));
                }
            }
        }