コード例 #1
0
        public async void GetCategoriesByType_WhenExists_ReturnsDistinctedWithStatus200()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**", Role.GetCategoriesByType)))
            {
                await sut.CreateManyAsync(new List <Exam>()
                {
                    new ExamBuilder("First Exam", ExamType.GeneralKnowledge, "Kardiologia").Build(),
                    new ExamBuilder("Second Exam", ExamType.GeneralKnowledge, "Interna").Build(),
                    new ExamBuilder("Third Exam", ExamType.Specialization, "Kardiologia").Build(),
                    new ExamBuilder("Fourth Exam", ExamType.GeneralKnowledge, "Kardiologia").Build(),
                    new ExamBuilder("Fifth Exam", ExamType.GeneralKnowledge, "Interna").Build(),
                    new ExamBuilder("Sixth Exam", ExamType.Specialization, "Urologia").Build(),
                    new ExamBuilder("Seventh Exam", ExamType.GeneralKnowledge, "Nefrologia").Build()
                });

                var url    = $"{Url}/{nameof(ExamType.GeneralKnowledge)}/categories";
                var result = sut.HttpGet <CategoriesResult>(url);

                result.IsSuccess.Should().BeTrue();
                result.Content.Should().BeEquivalentTo(new CategoriesResult(
                                                           new []
                {
                    new CategoriesResult.Category("Kardiologia"),
                    new CategoriesResult.Category("Interna"),
                    new CategoriesResult.Category("Nefrologia"),
                })
                                                       );
            }
        }
コード例 #2
0
        public async Task RegisterUser_WhenEmailDoesNotExists_ShouldCreateCorrectUser()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**")))
            {
                var command = new RegisterCommand
                {
                    Email           = "*****@*****.**",
                    Password        = "******",
                    ConfirmPassword = "******"
                };

                var url    = $"{Url}/register";
                var result = sut.HttpPost(url, command);

                var user = await sut.GetUserByEmailAsync(command.Email);

                result.IsSuccess.Should().BeTrue();
                user.Id.Should().NotBeEmpty();
                user.Email.Should().Be(command.Email);
                user.IsAdmin.Should().BeFalse();
                user.Roles.Should().BeEmpty();
                user.PasswordHash.Should().NotBe(command.Password);
                user.PasswordSalt.Should().NotBe(command.Password);
                user.PasswordHash.Should().NotBe(user.PasswordSalt);
            }
        }
コード例 #3
0
        public async void GetExamById_WhenExamsExists_ExamIsReturnedWithStatus200()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**", Role.GetExamById)))
            {
                var exams = await sut.CreateManyAsync(new List <Exam>()
                {
                    new ExamBuilder("First Exam", ExamType.GeneralKnowledge, "Kardiologia")
                    .WithQuestion("e1q1", CorrectAnswer.A)
                    .WithQuestion("e1q2", CorrectAnswer.B)
                    .Build(),
                    new ExamBuilder("Second Exam", ExamType.GeneralKnowledge, "Interna")
                    .WithQuestion("e2q1", CorrectAnswer.C)
                    .WithQuestion("e2q2", CorrectAnswer.D)
                    .Build(),
                    new ExamBuilder("Third Exam", ExamType.Specialization, "Kardiologia")
                    .WithQuestion("e3q1", CorrectAnswer.B)
                    .WithQuestion("e3q2", CorrectAnswer.D)
                    .Build()
                });

                var expected = sut.Mapper.Map <ExamWithQuestionsResult>(exams[1]);

                var result = sut.HttpGet <ExamWithQuestionsResult>($"{Url}/{exams[1].Id}");

                result.IsSuccess.Should().BeTrue();
                result.Content.Should().BeEquivalentTo(expected, o => o.Excluding(x => x.Questions));
                result.Content.Questions.Should().BeEquivalentTo(expected.Questions, o => o.Excluding(x => x.Id));
            }
        }
コード例 #4
0
        public async void AddExamWithQuestions_WhenCalled_CorrectlyCreatesObjectsWithStatus202()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**", Role.AddExam)))
            {
                var exam = new ExamBuilder("Some-exam", ExamType.Specialization, "Some-category")
                           .WithQuestion("Q1", CorrectAnswer.A)
                           .WithQuestion("Q2", CorrectAnswer.B)
                           .WithQuestion("Q3", CorrectAnswer.C)
                           .Build();
                var command = sut.Mapper.Map <AddExamCommand>(exam);

                var commandResult = sut.HttpPost(Url, command);
                var allExams      = await sut.GetAllExamsAsync();

                var result = await sut.GetExamByIdAsync(allExams.First().Id);

                commandResult.IsSuccess.Should().BeTrue();
                allExams.Count.Should().Be(1);
                result.CreatedDate.Should().BeAfter(exam.CreatedDate);
                result.Should().BeEquivalentTo(
                    exam,
                    options => options
                    .Excluding(p => p.Id)
                    .Excluding(p => p.CreatedDate)
                    .Excluding(p => p.CreatedBy)
                    .Excluding(p => p.Questions)
                    );
                result.Questions.Should().BeEquivalentTo(
                    exam.Questions,
                    options => options
                    .Excluding(p => p.Id)
                    .Excluding(p => p.Exam)
                    );
            }
        }
コード例 #5
0
        public void GetExamById_WhenExamNotExists_EmptyValueIsReturnedWithStatus204()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**", Role.GetExamById)))
            {
                var result = sut.HttpGet <ExamWithQuestionsResult>(Url, Guid.NewGuid());

                result.Content.Should().BeNull();
                result.IsSuccess.Should().BeFalse();
                result.Message.Should().BeEquivalentTo("Exam with given Id does not exists.");
            }
        }
コード例 #6
0
        public async Task GetUserByEmail_WhenUserHasDifferentEmailButIsAdmin_ShouldReturnCompletlyResult()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateAdmin("*****@*****.**")))
            {
                var user = UsersFactory.CreateUser("*****@*****.**");
                await sut.CreateAsync(user);

                var url    = $"{Url}/[email protected]";
                var result = sut.HttpGet <UserResult>(url);

                result.IsSuccess.Should().BeTrue();
            }
        }
コード例 #7
0
        public async Task GetUserByEmail_WhenUserHasDifferentEmailAndIsNotAdmin_ShouldReturnAccessDenied()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**")))
            {
                var user = UsersFactory.CreateUser("*****@*****.**");
                await sut.CreateAsync(user);

                var url    = $"{Url}/[email protected]";
                var result = sut.HttpGet <UserResult>(url);

                result.IsSuccess.Should().BeFalse();
                result.Message.Should().BeEquivalentTo("Access denied.");
            }
        }
コード例 #8
0
        public async Task SignInUser_WhenGivenCredentialsAreNotValid_ReturnFailure()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**")))
            {
                await sut.CreateAsync(
                    new User(Guid.NewGuid(), "*****@*****.**", false, new [] { Role.AddExam }, "zaq1@WSX", "pass-salt", DateTime.Now)
                    );

                var command = new SignInCommand("*****@*****.**", "bad-password");

                var url    = $"{Url}/sign-in";
                var result = sut.HttpPost(url, command);

                result.IsSuccess.Should().BeFalse();
                result.Message.Should().BeEquivalentTo("Given email or password are not valid.");
            }
        }
コード例 #9
0
        public async Task SignInUser_WhenGivenCredentialsAreValid_ReturnSuccess()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**")))
            {
                var user = UsersFactory.CreateUser("*****@*****.**");
                await sut.CreateAsync(user);

                var command = new SignInCommand(user.Email, UsersFactory.Password);

                var url    = $"{Url}/sign-in";
                var result = sut.HttpPost(url, command);
                var token  = JsonConvert.DeserializeObject <TokenResult>(result.Message);

                result.IsSuccess.Should().BeTrue();
                token.Token.Should().NotBeNullOrEmpty();
                token.Expiry.Should().BeAfter(DateTime.UtcNow);
            }
        }
コード例 #10
0
        public async void GetExamsByTypeAndCategory_WhenExists_ReturnsResultWithStatus200()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**", Role.GetExamByTypeAndCategory)))
            {
                var exams = await sut.CreateManyAsync(new List <Exam>()
                {
                    new ExamBuilder("First Exam", ExamType.GeneralKnowledge, "Kardiologia").Build(),
                    new ExamBuilder("Second Exam", ExamType.GeneralKnowledge, "Interna").Build(),
                    new ExamBuilder("Third Exam", ExamType.Specialization, "Kardiologia").Build()
                });

                var expected = new ExamsResult(new [] { sut.Mapper.Map <ExamsResult.Exam>(exams[1]) });

                var url    = $"{Url}/{nameof(ExamType.GeneralKnowledge)}/Interna";
                var result = sut.HttpGet <ExamsResult>(url);

                result.IsSuccess.Should().BeTrue();
                result.Content.Should().BeEquivalentTo(expected);
            }
        }
コード例 #11
0
        public async void RemoveExamWithQuestions_WhenExamExists_DeleteExamsAndQuestionsWithStatus202()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**", Role.DeleteExam)))
            {
                var exam = await sut.CreateAsync(new ExamBuilder("Some-exam", ExamType.Specialization, "Some-category")
                                                 .WithQuestion("Q1", CorrectAnswer.B)
                                                 .WithQuestion("Q2", CorrectAnswer.D)
                                                 .Build()
                                                 );

                var addedExam = await sut.GetExamByIdAsync(exam.Id);

                var commandResult = sut.HttpDelete(Url, addedExam.Id);
                var result        = await sut.GetExamByIdAsync(addedExam.Id);

                commandResult.IsSuccess.Should().BeTrue();
                addedExam.Should().NotBeNull();
                result.Should().BeNull();
            }
        }
コード例 #12
0
        public async Task RegisterUser_WhenEmailAlreadyExists_ShouldThrowAnException()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**")))
            {
                var user = new User(Guid.NewGuid(), "*****@*****.**", true, Enumerable.Empty <Role>(), "zaq1@WSX-hash", "password-salt", DateTime.Now);
                await sut.CreateAsync(user);

                var command = new RegisterCommand
                {
                    Email           = "*****@*****.**",
                    Password        = "******",
                    ConfirmPassword = "******"
                };

                var url    = $"{Url}/register";
                var result = sut.HttpPost(url, command);

                result.IsSuccess.Should().BeFalse();
                result.Message.Should().BeEquivalentTo("Email already exists.");
            }
        }
コード例 #13
0
        public async void EditExamWithQuestions_WhenExamExists_CorrectlyModifyExamAndQuestionsWithStatus202()
        {
            using (var sut = new SystemUnderTest(UsersFactory.CreateUser("*****@*****.**", Role.EditExam)))
            {
                await sut.CreateAsync(new ExamBuilder("Some-exam", ExamType.Specialization, "Some-category")
                                      .WithQuestion("Q1", CorrectAnswer.A)
                                      .WithQuestion("Q2", CorrectAnswer.B)
                                      .WithQuestion("Q3", CorrectAnswer.C)
                                      .Build()
                                      );

                var allExams = await sut.GetAllExamsAsync();

                var original = await sut.GetExamByIdAsync(allExams.First().Id);

                var modified = original.DeepClone();

                modified.SetName("Different name");
                modified.SetCategory("Interna");
                modified.ChangeType(ExamType.GeneralKnowledge);

                modified.RemoveQuestion(modified.Questions.Last().Id);
                modified.AddQuestion(3, "text", "a", "b", "c", "d", CorrectAnswer.D, "expl");
                modified.AddQuestion(4, "text-second", "e", "f", "g", "h", CorrectAnswer.A, "testing");
                modified.Questions.First().SetText("Changed text");

                var command = sut.Mapper.Map <EditExamCommand>(modified);

                var commandResult = sut.HttpPut(Url, modified.Id, command);
                var result        = await sut.GetExamByIdAsync(modified.Id);

                commandResult.IsSuccess.Should().BeTrue();
                result.Should().NotBe(original);
                result.Questions.Count.Should().Be(4);
                result.Questions.Should().NotBeEquivalentTo(original.Questions);
            }
        }