public async Task GetAdminQuizList()
        {
            List <Quiz> result;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager  = Mock.Of <ILogManager>();
                var userManager = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>(), logManager);
                var sut         = new QuizManager(context, new QuizAttemptManager(context, Mock.Of <IQuestionManager>(), logManager), logManager);
                var quiz        = new Quiz
                {
                    Title              = "title",
                    Intro              = "intro",
                    TimeConstraint     = true,
                    TimeLimitInSeconds = 40,
                    AvailableTo        = DateTime.Now.AddDays(1)
                };

                var userId = await userManager.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true);

                await sut.InsertQuizInternalAsync(userId, ManagerTestHelper.CreateQuiz(0));

                await sut.InsertQuizInternalAsync(userId, ManagerTestHelper.CreateQuiz(1));

                await context.SaveChangesAsync();

                result = await sut.GetAdminQuizList(userId);
            }

            Assert.AreEqual(2, result.Count);
        }
Exemple #2
0
        public async Task UpdatePasswordWithTokenWrongType()
        {
            bool         result;
            const string password    = "******";
            const string email       = "*****@*****.**";
            const string newPassword = "******";
            const string tokenString = "token";

            Models.DbModels.User updatedUser;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await ManagerTestHelper.AddUserAsync(context, 0);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, false);

                await ManagerTestHelper.AddUserAsync(context, 2);

                await ManagerTestHelper.AddOneTimeTokenAsync(context, email, OneTimeTokenType.AccountVerification, tokenString, true);

                await context.SaveChangesAsync();

                result = await sut.UpdatePassword(tokenString, newPassword);

                updatedUser = await context.Users.FindAsync(user.Id);
            }

            Assert.IsFalse(result);
            Assert.IsTrue(AuthenticationHelper.CompareByteArrays(AuthenticationHelper.EncryptPassword(password), updatedUser.PasswordHash));
        }
        public async Task GenerateTokenByIdExistingToken()
        {
            const string password    = "******";
            const string email       = "*****@*****.**";
            const string deviceId    = "mydevice";
            const string tokenString = "randomtokenstring12345";
            AuthToken    token;
            UserToken    existingToken;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetAuthManager(context);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password);

                await ManagerTestHelper.AddAuthTokenAsync(context, user.Id, deviceId, tokenString, true);

                await context.SaveChangesAsync();

                token = await sut.GenerateTokenAsync(user.Id, deviceId);

                existingToken = await context.UserTokens.FindAsync(user.Id, deviceId);
            }

            Assert.AreNotEqual(tokenString, existingToken.Token);
            Assert.AreEqual(token.Token, existingToken.Token);
        }
Exemple #4
0
        public async Task SendAccountVerificationEmailUserNotFound()
        {
            bool         result;
            const string password         = "******";
            const string email            = "*****@*****.**";
            const string nonExistingEmail = "*****@*****.**";
            OneTimeToken token;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(1, email, password), false);

                await context.SaveChangesAsync();

                result = await sut.SendAccountVerificationEmail(nonExistingEmail);

                token = await context.OneTimeTokens.FirstOrDefaultAsync(t => t.Email == nonExistingEmail && t.TokenType == (byte)OneTimeTokenType.AccountVerification);
            }

            Assert.IsFalse(result);
            Assert.IsNull(token);
        }
        public async Task VerifyAccessTokenUserIsVerified()
        {
            const string password    = "******";
            const string email       = "*****@*****.**";
            const string deviceId    = "mydevice";
            const string tokenString = "randomtokenstring12345";
            AuthToken    token;
            User         user;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetAuthManager(context);

                user = await ManagerTestHelper.AddUserAsync(context, 1, email, password);

                await ManagerTestHelper.AddAuthTokenAsync(context, user.Id, deviceId, tokenString, true);

                await context.SaveChangesAsync();

                token = await sut.VerifyAccessToken(tokenString);
            }

            Assert.AreEqual(tokenString, token.Token);
            Assert.AreEqual(user.Id, token.UserId);
            Assert.IsTrue(token.IsVerified);
        }
Exemple #6
0
        public async Task VerifyAccountAlreadyVerified()
        {
            AuthToken    result;
            const string password    = "******";
            const string email       = "*****@*****.**";
            const string tokenString = "token";

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(1, email, password), true);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(2), true);

                await ManagerTestHelper.AddOneTimeTokenAsync(context, email, OneTimeTokenType.AccountVerification, tokenString, true);

                await context.SaveChangesAsync();

                result = await sut.VerifyAccount(new OneTimeTokenRequest
                {
                    Token    = tokenString,
                    DeviceId = "device"
                });
            }

            Assert.IsNull(result);
        }
Exemple #7
0
        public async Task InsertUserInvalidPassword()
        {
            GenericManagerResponse <AuthToken, InsertUserResponse> result;
            const string password = "******";
            const string email    = "*****@*****.**";

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(1, email, password), false);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(2), true);

                await context.SaveChangesAsync();

                result = await sut.InsertUserAsync(new Models.TransferObjects.User
                {
                    DeviceId   = "device",
                    FirstName  = "FirstName",
                    LastName   = "LastName",
                    Email      = "*****@*****.**",
                    PictureUrl = "",
                    Password   = "******"
                });
            }

            Assert.AreEqual(result.Response, InsertUserResponse.PasswordCriteriaNotSatisfied);
        }
Exemple #8
0
        public async Task UpdateUserInvalidUser()
        {
            bool         result;
            const string password      = "******";
            const string email         = "*****@*****.**";
            const string newFirstName  = "newName";
            const string newLastName   = "newLastName";
            const string newPictureUrl = "newUrl";

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await ManagerTestHelper.AddUserAsync(context, 0);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, false);

                await context.SaveChangesAsync();

                result = await sut.UpdateUserAsync(user.Id + 1, new Models.TransferObjects.User
                {
                    DeviceId   = "device",
                    FirstName  = newFirstName,
                    LastName   = newLastName,
                    Email      = email,
                    PictureUrl = newPictureUrl,
                    Password   = "******"
                });
            }

            Assert.IsFalse(result);
        }
Exemple #9
0
        public async Task DeleteUser()
        {
            bool         result;
            const string password = "******";
            const string email    = "*****@*****.**";

            Models.DbModels.User deletedUser;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await ManagerTestHelper.AddUserAsync(context, 0);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, false);

                await ManagerTestHelper.AddUserAsync(context, 2);

                await context.SaveChangesAsync();

                result = await sut.DeleteUserAsync(user.Id);

                deletedUser = await context.Users.FindAsync(user.Id);
            }

            Assert.IsTrue(result);
            Assert.IsNull(deletedUser);
        }
        public async Task GenerateTokenEmailFail()
        {
            const string password = "******";
            const string email    = "*****@*****.**";
            AuthToken    token;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetAuthManager(context);

                var userManager = ManagerTestHelper.GetUserManager(context, sut);
                await userManager.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true);

                await userManager.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(1, email, password), true);

                await userManager.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(2), true);

                await context.SaveChangesAsync();

                token = await sut.GenerateTokenAsync(new TokenRequest
                {
                    DeviceId = "deviceId",
                    Email    = "*****@*****.**",
                    Password = password
                });
            }

            Assert.IsNull(token);
        }
Exemple #11
0
        public async Task UpdatePasswordInvalidPassword()
        {
            bool         result;
            const string password    = "******";
            const string email       = "*****@*****.**";
            const string newPassword = "******";

            Models.DbModels.User updatedUser;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await ManagerTestHelper.AddUserAsync(context, 0);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, true);

                await ManagerTestHelper.AddUserAsync(context, 2);

                await context.SaveChangesAsync();

                result = await sut.UpdatePassword(user.Id, newPassword);

                updatedUser = await context.Users.FindAsync(user.Id);
            }

            Assert.IsFalse(result);
            Assert.IsTrue(AuthenticationHelper.CompareByteArrays(AuthenticationHelper.EncryptPassword(password), updatedUser.PasswordHash));
        }
        public async Task GenerateTokenPasswordMatches()
        {
            const string password = "******";
            const string email    = "*****@*****.**";
            AuthToken    token;
            User         user;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetAuthManager(context);

                await ManagerTestHelper.AddUserAsync(context, 0);

                user = await ManagerTestHelper.AddUserAsync(context, 1, email, password);

                await ManagerTestHelper.AddUserAsync(context, 2);

                await context.SaveChangesAsync();

                token = await sut.GenerateTokenAsync(new TokenRequest
                {
                    DeviceId = "deviceId",
                    Email    = email,
                    Password = password
                });
            }

            Assert.IsNotNull(token);
            Assert.AreEqual(token.UserId, user.Id);
        }
Exemple #13
0
        public async Task SendPasswordResetEmailVerified()
        {
            bool         result;
            OneTimeToken token;
            const string password = "******";
            const string email    = "*****@*****.**";

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(1, email, password), true);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(2), true);

                await context.SaveChangesAsync();

                result = await sut.SendPasswordResetEmail(email);

                token = await context.OneTimeTokens.FirstOrDefaultAsync(u => u.Email == email);
            }

            Assert.IsFalse(result);
            Assert.IsNull(token);
        }
        public async Task GetUserQuizList()
        {
            List <Quiz> result;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var sut        = new QuizManager(context, new QuizAttemptManager(context, Mock.Of <IQuestionManager>(), logManager), logManager);
                var quiz       = new Quiz
                {
                    Title              = "title",
                    Intro              = "intro",
                    TimeConstraint     = true,
                    TimeLimitInSeconds = 40,
                    AvailableTo        = DateTime.Now.AddDays(1)
                };

                var testData = await ManagerTestHelper.CreateAndAssignQuizAsync(context, quiz, true);

                var attempt = await context.QuizAttempts.AddAsync(
                    new QuizAttempt
                {
                    QuizId    = testData.QuizId,
                    UserId    = testData.UserId,
                    Status    = QuizAttemptStatus.Incomplete,
                    StartDate = DateTime.Now
                });

                await context.SaveChangesAsync();

                result = await sut.GetUserQuizList(testData.UserId);
            }

            Assert.IsNotNull(result[0].LastAttempt);
        }
Exemple #15
0
        public async Task InsertAnswerUnauthorized()
        {
            UpdateQuizAttemptStatusResult result;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var quizData   = await ManagerTestHelper.CreateQuizAsync(context, 3, 5);

                var quiz = await context.Quizes.FindAsync(quizData.QuizId);

                var userId = await ManagerTestHelper.AssignQuizAsync(context, quiz.QuizIdentityId);

                var sut = new QuizAttemptManager(context, new QuestionManager(context, logManager), logManager);

                result = await sut.InsertAnswerAsync(userId, 8,
                                                     new Models.TransferObjects.Answer
                {
                    QuestionId = quizData.QuestionIds[0],
                    Options    = quizData.OptionIds.Take(2),
                    TimeSpent  = 40
                });
            }

            Assert.AreEqual(UpdateQuizAttemptStatusResult.NotAuthorized, result);
        }
Exemple #16
0
        public async Task InsertAnswer()
        {
            UpdateQuizAttemptStatusResult result;
            QuizAttempt attempt;
            List <int>  optionIds;
            List <int>  answers;
            List <int>  answersOtherQuestion;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var quizData   = await ManagerTestHelper.CreateQuizAsync(context, 3, 5);

                var quiz = await context.Quizes.FindAsync(quizData.QuizId);

                var userId = await ManagerTestHelper.AssignQuizAsync(context, quiz.QuizIdentityId);

                var sut = new QuizAttemptManager(context, new QuestionManager(context, logManager), logManager);
                var createAttemptResult = await sut.CreateAttempt(userId, quiz.Id);

                attempt   = createAttemptResult.Attempt;
                optionIds = quizData.OptionIds.Skip(2).Take(2).ToList();

                await sut.InsertAnswerAsync(userId, attempt.Id,
                                            new Models.TransferObjects.Answer
                {
                    QuestionId = quizData.QuestionIds[0],
                    Options    = quizData.OptionIds.Take(2),
                    TimeSpent  = 40
                });

                result = await sut.InsertAnswerAsync(userId, attempt.Id,
                                                     new Models.TransferObjects.Answer
                {
                    QuestionId = quizData.QuestionIds[0],
                    Options    = optionIds,
                    TimeSpent  = 40
                });

                answers = context.Answers
                          .Where(a => a.AttemptId == attempt.Id && a.QuestionId == quizData.QuestionIds[0])
                          .Select(a => a.OptionId)
                          .ToList();

                answersOtherQuestion = context.Answers
                                       .Where(a => a.AttemptId == attempt.Id && a.QuestionId == quizData.QuestionIds[1])
                                       .Select(a => a.OptionId)
                                       .ToList();
            }

            Assert.AreEqual(UpdateQuizAttemptStatusResult.Success, result);
            Assert.AreEqual(0, optionIds.Except(answers).ToList().Count);
            Assert.AreEqual(0, answers.Except(optionIds).ToList().Count);
            Assert.AreEqual(0, answersOtherQuestion.Count);
            Assert.AreEqual(40, attempt.TimeSpent);
        }
        public async Task GetAccessTokenReturnsNull()
        {
            UserToken token;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetAuthManager(context);

                token = await sut.GetAccessToken("somestring");
            }

            Assert.IsNull(token);
        }
        public async Task IsOwner(bool expected, int userId, int ownerId)
        {
            bool result;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = new QuizManager(context, Mock.Of <IQuizAttemptManager>(), Mock.Of <ILogManager>());

                var insertResult = await sut.InsertQuizInternalAsync(ownerId, ManagerTestHelper.CreateQuiz(0));

                result = await sut.IsOwner(userId, insertResult.QuizIdentityId);
            }

            Assert.AreEqual(expected, result);
        }
Exemple #19
0
        public async Task VerifyAccount()
        {
            AuthToken    result;
            const string password    = "******";
            const string email       = "*****@*****.**";
            const string tokenString = "token";
            OneTimeToken token;

            Models.DbModels.User user;
            Models.DbModels.User updatedUser;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var authManager = new Mock <IAuthManager>();
                authManager
                .Setup(c => c.GenerateTokenAsync(It.IsAny <int>(), It.IsAny <string>()))
                .Returns((int u, string d) => Task.FromResult(new AuthToken
                {
                    UserId = u
                }));
                var sut = ManagerTestHelper.GetUserManager(context, authManager.Object);

                await ManagerTestHelper.AddUserAsync(context, 0);

                user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, false);

                await ManagerTestHelper.AddUserAsync(context, 2);

                await ManagerTestHelper.AddOneTimeTokenAsync(context, email, OneTimeTokenType.AccountVerification, tokenString, true);

                await context.SaveChangesAsync();

                result = await sut.VerifyAccount(new OneTimeTokenRequest
                {
                    Token    = tokenString,
                    DeviceId = "device"
                });

                token = await context.OneTimeTokens.FirstOrDefaultAsync(t => t.Email == email && t.TokenType == (byte)OneTimeTokenType.AccountVerification);

                updatedUser = await context.Users.FindAsync(user.Id);
            }

            Assert.AreEqual(result.UserId, user.Id);
            Assert.IsNull(token);
            Assert.IsTrue(updatedUser.IsVerified);
        }
Exemple #20
0
        public async Task GetQuizOptionsNoRows()
        {
            List <Option> options;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var sut        = new OptionManager(context, logManager);

                var testData = await ManagerTestHelper.CreateQuizAsync(context, 0, 0);

                options = await sut.GetQuizOptions(testData.QuizId);
            }

            Assert.IsNotNull(options);
            Assert.AreEqual(0, options.Count);
        }
        public async Task GenerateTokenByIdNoUser()
        {
            const string password = "******";
            const string email    = "*****@*****.**";
            const string deviceId = "mydevice";
            AuthToken    token;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetAuthManager(context);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password);

                await context.SaveChangesAsync();

                token = await sut.GenerateTokenAsync(user.Id + 1, deviceId); // +1 to simply specify a different user.
            }

            Assert.IsNull(token);
        }
Exemple #22
0
        public async Task DeleteUserNotFound()
        {
            bool         result;
            const string password = "******";
            const string email    = "*****@*****.**";

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>());

                await ManagerTestHelper.AddUserAsync(context, 0);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password, false);

                await context.SaveChangesAsync();

                result = await sut.DeleteUserAsync(user.Id + 1);
            }

            Assert.IsFalse(result);
        }
Exemple #23
0
        public async Task UpdateStatusAvailableIntervalPassed()
        {
            UpdateQuizAttemptResponse result;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var sut        = new QuizAttemptManager(context, new QuestionManager(context, logManager), logManager);
                var quiz       = new Quiz
                {
                    Title              = "title",
                    Intro              = "intro",
                    TimeConstraint     = true,
                    TimeLimitInSeconds = 40,
                    AvailableTo        = DateTime.Now.AddDays(-1)
                };

                var testData = await ManagerTestHelper.CreateAndAssignQuizAsync(context, quiz, true);

                var attempt = await context.QuizAttempts.AddAsync(
                    new QuizAttempt
                {
                    QuizId    = testData.QuizId,
                    UserId    = testData.UserId,
                    Status    = QuizAttemptStatus.Incomplete,
                    StartDate = DateTime.Now
                });

                await context.SaveChangesAsync();

                result = await sut.UpdateStatus(testData.UserId, attempt.Entity.Id, new UpdateQuizAttemptStatus
                {
                    TimeSpent = 30,
                    EndQuiz   = false
                });
            }

            Assert.AreEqual(UpdateQuizAttemptStatusResult.DateError, result.Result);
            Assert.AreNotEqual(QuizAttemptStatus.Incomplete, result.Attempt.Status);
        }
Exemple #24
0
        public async Task CreateAttemptQuizRepeatable()
        {
            CreateAttemptResponse result;
            QuizAttempt           previousAttempt;

            var quiz = new Quiz
            {
                Title      = "title",
                Intro      = "intro",
                Repeatable = true
            };

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var testData   = await ManagerTestHelper.CreateAndAssignQuizAsync(context, quiz, true);

                var attempt = await context.QuizAttempts.AddAsync(
                    new QuizAttempt
                {
                    QuizId    = testData.QuizId,
                    UserId    = testData.UserId,
                    StartDate = DateTime.Now,
                    Status    = QuizAttemptStatus.Incomplete,
                    TimeSpent = 10
                });

                await context.SaveChangesAsync();

                var sut = new QuizAttemptManager(context, new QuestionManager(context, logManager), logManager);
                result = await sut.CreateAttempt(testData.UserId, testData.QuizId);

                previousAttempt = await context.QuizAttempts.FindAsync(attempt.Entity.Id);
            }

            Assert.AreEqual(CreateAttemptResult.Success, result.Result);
            Assert.AreEqual(QuizAttemptStatus.Completed, previousAttempt.Status);
            Assert.AreEqual(QuizAttemptStatus.Incomplete, result.Attempt.Status);
        }
Exemple #25
0
        public async Task CreateAttemptQuizNotAssigned()
        {
            CreateAttemptResponse result;

            var quiz = new Quiz
            {
                Title = "title",
                Intro = "intro"
            };

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var testData   = await ManagerTestHelper.CreateAndAssignQuizAsync(context, quiz, false);

                var sut = new QuizAttemptManager(context, new QuestionManager(context, logManager), logManager);
                result = await sut.CreateAttempt(testData.UserId, testData.QuizId);
            }

            Assert.AreEqual(CreateAttemptResult.NotAuthorized, result.Result);
            Assert.IsNull(result.Attempt);
        }
Exemple #26
0
        public async Task CreateAttemptQuizNotAvailable()
        {
            CreateAttemptResponse result;

            var quiz = new Quiz
            {
                Title       = "title",
                Intro       = "intro",
                AvailableTo = DateTime.Now.AddDays(-1)
            };

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var testData   = await ManagerTestHelper.CreateAndAssignQuizAsync(context, quiz, true);

                var sut = new QuizAttemptManager(context, new QuestionManager(context, logManager), logManager);
                result = await sut.CreateAttempt(testData.UserId, testData.QuizId);
            }

            Assert.AreEqual(CreateAttemptResult.DateError, result.Result);
            Assert.IsNull(result.Attempt);
        }
Exemple #27
0
        public async Task FinishQuiz()
        {
            CreateAttemptResponse result;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var logManager = Mock.Of <ILogManager>();
                var quizData   = await ManagerTestHelper.CreateQuizAsync(context, 3, 5);

                var quiz = await context.Quizes.FindAsync(quizData.QuizId);

                var userId = await ManagerTestHelper.AssignQuizAsync(context, quiz.QuizIdentityId);

                var sut = new QuizAttemptManager(context, new QuestionManager(context, logManager), logManager);
                result = await sut.CreateAttempt(userId, quiz.Id);

                await sut.FinishQuizAsync(result.Attempt, quiz.PassScore, 30);
            }

            Assert.AreEqual(CreateAttemptResult.Success, result.Result);
            Assert.AreEqual(QuizAttemptStatus.Completed, result.Attempt.Status);
            Assert.IsNotNull(result.Attempt.EndDate);
        }
        private async Task <UserToken> GetAccessTokenInternal(bool isValid)
        {
            const string password    = "******";
            const string email       = "*****@*****.**";
            const string deviceId    = "mydevice";
            const string tokenString = "randomtokenstring12345";
            UserToken    token;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetAuthManager(context);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password);

                await ManagerTestHelper.AddAuthTokenAsync(context, user.Id, deviceId, tokenString, isValid);

                await context.SaveChangesAsync();

                token = await sut.GetAccessToken(tokenString);
            }

            return(token);
        }
        public async Task VerifyAccessTokenTokenNotFound()
        {
            const string password    = "******";
            const string email       = "*****@*****.**";
            const string deviceId    = "mydevice";
            const string tokenString = "randomtokenstring12345";
            AuthToken    token;

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetAuthManager(context);

                var user = await ManagerTestHelper.AddUserAsync(context, 1, email, password);

                await ManagerTestHelper.AddAuthTokenAsync(context, user.Id, deviceId, tokenString + "somesuffix", true);

                await context.SaveChangesAsync();

                token = await sut.VerifyAccessToken(tokenString);
            }

            Assert.IsNull(token);
        }
Exemple #30
0
        public async Task SendPasswordResetEmailCreatesToken()
        {
            const string password = "******";
            const string email    = "*****@*****.**";
            bool         result;
            OneTimeToken token;
            var          isLogManagerCalled = false;

            var logManager = new Mock <ILogManager>();

            logManager.Setup(c => c.AddLog(LogCategory.Email, It.IsAny <string>(), LogEventLevel.Information, It.IsAny <object[]>()))
            .Callback(() =>
            {
                isLogManagerCalled = true;
            });

            using (var context = new QuizContext(ManagerTestHelper.Options))
            {
                var sut = ManagerTestHelper.GetUserManager(context, Mock.Of <IAuthManager>(), logManager.Object);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(0), true);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(1, email, password), false);

                await sut.InsertUserInternalAsync(ManagerTestHelper.CreateUserTo(2), true);

                await context.SaveChangesAsync();

                result = await sut.SendPasswordResetEmail(email);

                token = await context.OneTimeTokens.FirstOrDefaultAsync(u => u.Email == email);
            }

            Assert.IsTrue(result);
            Assert.IsNotNull(token);
            Assert.IsTrue(isLogManagerCalled);
        }