diff --git a/HwProj.CoursesService/CourseService.IntegrationTests/CourseService.IntegrationTests.csproj b/HwProj.CoursesService/CourseService.IntegrationTests/CourseService.IntegrationTests.csproj new file mode 100644 index 000000000..03e4bdb3f --- /dev/null +++ b/HwProj.CoursesService/CourseService.IntegrationTests/CourseService.IntegrationTests.csproj @@ -0,0 +1,26 @@ + + + + netcoreapp2.2 + + false + + + + + + + + + + + + + + + + + + + + diff --git a/HwProj.CoursesService/CourseService.IntegrationTests/CourseServiceTests.cs b/HwProj.CoursesService/CourseService.IntegrationTests/CourseServiceTests.cs new file mode 100644 index 000000000..fa55cc675 --- /dev/null +++ b/HwProj.CoursesService/CourseService.IntegrationTests/CourseServiceTests.cs @@ -0,0 +1,761 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Security.Claims; +using System.Threading.Tasks; +using AutoFixture; +using FluentAssertions; +using HwProj.AuthService.Client; +using HwProj.CoursesService.Client; +using HwProj.Models.AuthService.ViewModels; +using HwProj.Models.CoursesService.ViewModels; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Options; +using Moq; +using NUnit.Framework; + +namespace CourseService.IntegrationTests +{ + public class Tests + { + private RegisterViewModel GenerateRegisterViewModel() + { + var password = new Fixture().Create(); + var fixture = new Fixture().Build() + .With(vm => vm.Password, password) + .With(vm => vm.PasswordConfirm, password); + var viewModel = fixture.Create(); + viewModel.Email += "@mail.ru"; + return viewModel; + } + + private CreateCourseViewModel GenerateCreateCourseViewModel() + { + var fixture = new Fixture().Build() + .With(cvm => cvm.IsOpen, true); + return fixture.Create(); + } + + private UpdateCourseViewModel GenerateUpdateCourseViewModel() + { + var fixture = new Fixture().Build() + .With(cvm => cvm.IsOpen, true); + return fixture.Create(); + } + + private CreateHomeworkViewModel GenerateCreateHomeworkViewModel() + { + var fixture = new Fixture().Build() + .With(hvm => hvm.Tasks, new List()); + return fixture.Create(); + } + + private CreateTaskViewModel GenerateCreateTaskViewModel() + { + return new Fixture().Build().Create(); + } + + private CoursesServiceClient CreateCourseServiceClient(string userId) + { + var mockIConfiguration = new Mock(); + mockIConfiguration.Setup(x => x.GetSection("Services")["Courses"]).Returns("http://localhost:5002"); + var mockClientFactory = new Mock(); + mockClientFactory.Setup(x => x.CreateClient(Options.DefaultName)).Returns(new HttpClient()); + var mockHttpContextAccessor = new Mock(); + mockHttpContextAccessor.Setup(x => x.HttpContext.User.FindFirst("_id")).Returns(new Claim("", userId)); + return new CoursesServiceClient(mockClientFactory.Object, mockHttpContextAccessor.Object, + mockIConfiguration.Object); + } + + private AuthServiceClient CreateAuthServiceClient() + { + var mockIConfiguration = new Mock(); + mockIConfiguration.Setup(x => x.GetSection("Services")["Auth"]).Returns("http://localhost:5001"); + var mockClientFactory = new Mock(); + mockClientFactory.Setup(x => x.CreateClient(Options.DefaultName)).Returns(new HttpClient()); + return new AuthServiceClient(mockClientFactory.Object, mockIConfiguration.Object); + } + + private async Task<(string, string)> CreateAndRegisterUser() + { + var authClient = CreateAuthServiceClient(); + var userData = GenerateRegisterViewModel(); + await authClient.Register(userData); + var userId = await authClient.FindByEmailAsync(userData.Email); + return (userId, userData.Email); + } + + private async Task<(string, string)> CreateAndRegisterLecture() + { + var (userId, mail) = await CreateAndRegisterUser(); + var authClient = CreateAuthServiceClient(); + await authClient.InviteNewLecturer(new InviteLecturerViewModel() {Email = mail}); + return (userId, mail); + } + + + [Test] + public async Task TestCreateCourse() + { + // Arrange + var (userId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(userId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + // Act + var courseId = await courseClient.CreateCourse(newCourseViewModel, userId); + // Assert + var courseFromServer = await courseClient.GetCourseById(courseId, userId); + courseFromServer.Should().BeEquivalentTo(newCourseViewModel); + } + + [Test] + public async Task TestDeleteCourse() + { + // Arrange + var (userId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(userId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + // Act + var courseId = await courseClient.CreateCourse(newCourseViewModel, userId); + await courseClient.DeleteCourse(courseId); + // Assert + var coursesFromServer = await courseClient.GetAllCourses(); + coursesFromServer.Should().NotContain(c => c.Id == courseId); + } + + [Test] + public async Task TestUpdateCourse() + { + // Arrange + var (userId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(userId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + // Act + var courseId = await courseClient.CreateCourse(newCourseViewModel, userId); + var updateCourse = GenerateUpdateCourseViewModel(); + await courseClient.UpdateCourse(updateCourse, courseId); + // Assert + var courseFromServer = await courseClient.GetCourseById(courseId, userId); + courseFromServer.Should().BeEquivalentTo(updateCourse); + } + + [Test] + public async Task TestSignInAndAcceptStudent() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var (studentId, _) = await CreateAndRegisterUser(); + var lectureCourseClient = CreateCourseServiceClient(lectureId); + var studentCourseClient = CreateCourseServiceClient(studentId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + // Act + var courseId = await lectureCourseClient.CreateCourse(newCourseViewModel, lectureId); + await studentCourseClient.SignInCourse(courseId, studentId); + var courseBeforeAcceptStudent = await lectureCourseClient.GetCourseById(courseId, lectureId); + await lectureCourseClient.AcceptStudent(courseId, studentId); + var courseAfterAcceptStudent = await lectureCourseClient.GetCourseById(courseId, lectureId); + // Assert + courseBeforeAcceptStudent.CourseMates.Should().Contain(s => s.StudentId == studentId).Which.IsAccepted + .Should().BeFalse(); + courseAfterAcceptStudent.CourseMates.Should().Contain(s => s.StudentId == studentId).Which.IsAccepted + .Should().BeTrue(); + } + + [Test] + public async Task TestSignInAndRejectStudent() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var (studentId, _) = await CreateAndRegisterUser(); + var lectureCourseClient = CreateCourseServiceClient(lectureId); + var studentCourseClient = CreateCourseServiceClient(studentId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + // Act + var courseId = await lectureCourseClient.CreateCourse(newCourseViewModel, lectureId); + await studentCourseClient.SignInCourse(courseId, studentId); + var courseBeforeRejectStudent = await lectureCourseClient.GetCourseById(courseId, lectureId); + await lectureCourseClient.RejectStudent(courseId, studentId); + var courseAfterRejectStudent = await lectureCourseClient.GetCourseById(courseId, lectureId); + // Assert + courseBeforeRejectStudent.CourseMates.Should().Contain(s => s.StudentId == studentId).Which.IsAccepted + .Should().BeFalse(); + courseAfterRejectStudent.CourseMates.Should().NotContain(s => s.StudentId == studentId); + } + + [Test] + public async Task TestAddHomeworkToCourse() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(lectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + // Act + var courseId = await courseClient.CreateCourse(newCourseViewModel, lectureId); + var homeworkId = (await courseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + var course = await courseClient.GetCourseById(courseId, lectureId); + var homework = await courseClient.GetHomework(homeworkId); + // Assert + homework.Should().BeEquivalentTo(newHomeworkViewModel); + course.Homeworks.Should().Contain(h => h.Id == homeworkId).Which.Should() + .BeEquivalentTo(newHomeworkViewModel); + } + + [Test] + public async Task TestUpdateHomework() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(lectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var courseId = await courseClient.CreateCourse(newCourseViewModel, lectureId); + var homeworkId = (await courseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + var updateHomeworkViewModel = GenerateCreateHomeworkViewModel(); + // Act + await courseClient.UpdateHomework(updateHomeworkViewModel, homeworkId); + var course = await courseClient.GetCourseById(courseId, lectureId); + var homework = await courseClient.GetHomework(homeworkId); + // Assert + homework.Should().BeEquivalentTo(updateHomeworkViewModel); + course.Homeworks.Should().Contain(h => h.Id == homeworkId).Which.Should() + .BeEquivalentTo(updateHomeworkViewModel); + } + + [Test] + public async Task TestDeleteHomework() + { + var (lectureId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(lectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var courseId = await courseClient.CreateCourse(newCourseViewModel, lectureId); + var homeworkId = (await courseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + // Act + await courseClient.DeleteHomework(homeworkId); + var course = await courseClient.GetCourseById(courseId, lectureId); + // Assert + course.Homeworks.Should().NotContain(h => h.Id == homeworkId); + } + + [Test] + public async Task TestAddTask() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(lectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var newTaskViewModel = GenerateCreateTaskViewModel(); + var courseId = await courseClient.CreateCourse(newCourseViewModel, lectureId); + var homeworkId = (await courseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + // Act + var taskId = (await courseClient.AddTask(newTaskViewModel, homeworkId)).Value; + var course = await courseClient.GetCourseById(courseId, lectureId); + var homework = await courseClient.GetHomework(homeworkId); + var task = await courseClient.GetTask(taskId); + // Assert + course.Homeworks.Should().Contain(h => h.Id == homeworkId) + .Which.Tasks.Should().Contain(t => t.Id == taskId) + .Which.Should().BeEquivalentTo(newTaskViewModel); + homework.Tasks.Should().Contain(t => t.Id == taskId) + .Which.Should().BeEquivalentTo(newTaskViewModel); + task.Should().BeEquivalentTo(newTaskViewModel); + task.HomeworkId.Should().Be(homeworkId); + } + + [Test] + public async Task TestUpdateTask() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(lectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var newTaskViewModel = GenerateCreateTaskViewModel(); + var updateTaskViewModel = GenerateCreateTaskViewModel(); + var courseId = await courseClient.CreateCourse(newCourseViewModel, lectureId); + var homeworkId = (await courseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + var taskId = (await courseClient.AddTask(newTaskViewModel, homeworkId)).Value; + // Act + await courseClient.UpdateTask(updateTaskViewModel, taskId); + var course = await courseClient.GetCourseById(courseId, lectureId); + var homework = await courseClient.GetHomework(homeworkId); + var task = await courseClient.GetTask(taskId); + // Assert + course.Homeworks.Should().Contain(h => h.Id == homeworkId) + .Which.Tasks.Should().Contain(t => t.Id == taskId) + .Which.Should().BeEquivalentTo(updateTaskViewModel); + homework.Tasks.Should().Contain(t => t.Id == taskId) + .Which.Should().BeEquivalentTo(updateTaskViewModel); + task.Should().BeEquivalentTo(updateTaskViewModel); + } + + [Test] + public async Task TestDeleteTask() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var courseClient = CreateCourseServiceClient(lectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var newTaskViewModel = GenerateCreateTaskViewModel(); + var courseId = await courseClient.CreateCourse(newCourseViewModel, lectureId); + var homeworkId = (await courseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + var taskId = (await courseClient.AddTask(newTaskViewModel, homeworkId)).Value; + // Act + await courseClient.DeleteTask(taskId); + var course = await courseClient.GetCourseById(courseId, lectureId); + var homework = await courseClient.GetHomework(homeworkId); + // Assert + course.Homeworks.Should().Contain(h => h.Id == homeworkId) + .Which.Tasks.Should().NotContain(t => t.Id == taskId); + homework.Tasks.Should().NotContain(t => t.Id == taskId); + } + + [Test] + public async Task TestAcceptLecture() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var (anotherLectureId, anotherLectureMail) = await CreateAndRegisterLecture(); + var lectureClient = CreateCourseServiceClient(lectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var courseId = await lectureClient.CreateCourse(newCourseViewModel, lectureId); + // Act + await lectureClient.AcceptLecturer(courseId, anotherLectureMail); + var course = await lectureClient.GetCourseById(courseId, lectureId); + // Assert + course.MentorIds.Should().Contain(anotherLectureId); + } + + [Test] + public async Task TestStudentCannotBeAcceptedAsLecture() + { + var (lectureId, _) = await CreateAndRegisterLecture(); + var (studentId, studentEmail) = await CreateAndRegisterUser(); + var lectureClient = CreateCourseServiceClient(lectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var courseId = await lectureClient.CreateCourse(newCourseViewModel, lectureId); + // Act + await lectureClient.AcceptLecturer(courseId, studentEmail); + var course = await lectureClient.GetCourseById(courseId, lectureId); + // Assert + course.MentorIds.Should().NotContain(studentId); + } + + [Test] + public async Task TestLectureWhenWasAcceptedShouldNotBeContainedInCourseMates() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var (anotherLectureId, anotherLectureMail) = await CreateAndRegisterLecture(); + var lectureClient = CreateCourseServiceClient(lectureId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var courseId = await lectureClient.CreateCourse(newCourseViewModel, lectureId); + // Act + await anotherLectureClient.SignInCourse(courseId, anotherLectureId); + await lectureClient.AcceptLecturer(courseId, anotherLectureMail); + var course = await lectureClient.GetCourseById(courseId, lectureId); + // Assert + course.MentorIds.Should().Contain(anotherLectureId); + course.CourseMates.Should().NotContain(m => m.StudentId == anotherLectureId); + } + + [Test] + public async Task TestPublicationDateForTask() + { + // Arrange + var (lectureId, _) = await CreateAndRegisterLecture(); + var (studentId, _) = await CreateAndRegisterUser(); + var lectureCourseClient = CreateCourseServiceClient(lectureId); + var studentCourseClient = CreateCourseServiceClient(studentId); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var firstTaskViewModel = GenerateCreateTaskViewModel(); + firstTaskViewModel.PublicationDate = DateTime.UtcNow.AddHours(3); + var secondTaskViewModel = GenerateCreateTaskViewModel(); + secondTaskViewModel.PublicationDate = DateTime.UtcNow.AddHours(4); + var courseId = await lectureCourseClient.CreateCourse(newCourseViewModel, lectureId); + var homeworkId = (await lectureCourseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + var task1Id = (await lectureCourseClient.AddTask(firstTaskViewModel, homeworkId)).Value; + var task2Id = (await lectureCourseClient.AddTask(secondTaskViewModel, homeworkId)).Value; + var courseFromStudent = await studentCourseClient.GetCourseById(courseId, studentId); + var courseFromLecture = await lectureCourseClient.GetCourseById(courseId, lectureId); + // Assert + var hwFromStudent = courseFromStudent.Homeworks.First(h => h.Id == homeworkId); + var hwFromLecture = courseFromLecture.Homeworks.First(h => h.Id == homeworkId); + hwFromStudent.Tasks.Should().Contain(t => t.Id == task1Id); + hwFromStudent.Tasks.Should().NotContain(t => t.Id == task2Id); + hwFromLecture.Tasks.Should().Contain(t => t.Id == task1Id); + hwFromLecture.Tasks.Should().Contain(t => t.Id == task2Id); + } + + [Test] + public async Task TestUpdateCourseOnlyForCourseMentor() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var updateViewModel = GenerateUpdateCourseViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + // Act + var responseForStudentInCourse = await studentInCourseClient.UpdateCourse(updateViewModel, courseId); + var responseForAnotherStudent = await anotherStudentClient.UpdateCourse(updateViewModel, courseId); + var responseForAnotherLecture = await anotherLectureClient.UpdateCourse(updateViewModel, courseId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.UpdateCourse(updateViewModel, courseId); + // Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestDeleteCourseOnlyForCourseMentor() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + // Act + var responseForStudentInCourse = await studentInCourseClient.DeleteCourse(courseId); + var responseForAnotherStudent = await anotherStudentClient.DeleteCourse(courseId); + var responseForAnotherLecture = await anotherLectureClient.DeleteCourse(courseId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.DeleteCourse(courseId); + // Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestAddHomeworkOnlyForCourseMentor() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + // Act + var responseForStudentInCourse = await studentInCourseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId); + var responseForAnotherStudent = + await anotherStudentClient.AddHomeworkToCourse(newHomeworkViewModel, courseId); + var responseForAnotherLecture = await anotherLectureClient.AddHomeworkToCourse(newHomeworkViewModel, courseId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.AddHomeworkToCourse(newHomeworkViewModel, courseId); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestUpdateHomeworkOnlyForCourseMentor() + { + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var updateHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + var homeworkId = (await courseLectureClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + // Act + var responseForStudentInCourse = await studentInCourseClient.UpdateHomework(updateHomeworkViewModel, homeworkId); + var responseForAnotherStudent = + await anotherStudentClient.UpdateHomework(updateHomeworkViewModel, homeworkId); + var responseForAnotherLecture = await anotherLectureClient.UpdateHomework(updateHomeworkViewModel, homeworkId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.UpdateHomework(updateHomeworkViewModel, homeworkId); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestDeleteHomeworkOnlyForCourseMentor() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + var homeworkId = (await courseLectureClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + // Act + var responseForStudentInCourse = await studentInCourseClient.DeleteHomework(homeworkId); + var responseForAnotherStudent = + await anotherStudentClient.DeleteHomework(homeworkId); + var responseForAnotherLecture = await anotherLectureClient.DeleteHomework(homeworkId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.DeleteHomework(homeworkId); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestAddTaskForCourseMentorOnly() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var newTaskViewModel = GenerateCreateTaskViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + var homeworkId = (await courseLectureClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + // Act + var responseForStudentInCourse = await studentInCourseClient.AddTask(newTaskViewModel, homeworkId); + var responseForAnotherStudent = await anotherStudentClient.AddTask(newTaskViewModel, homeworkId); + var responseForAnotherLecture = await anotherLectureClient.AddTask(newTaskViewModel, homeworkId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.AddTask(newTaskViewModel, homeworkId); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestUpdateTaskForCourseMentorOnly() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var newTaskViewModel = GenerateCreateTaskViewModel(); + var updateTaskViewModel = GenerateCreateTaskViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + var homeworkId = (await courseLectureClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + var taskId = (await courseLectureClient.AddTask(newTaskViewModel, homeworkId)).Value; + // Act + var responseForStudentInCourse = await studentInCourseClient.UpdateTask(updateTaskViewModel, taskId); + var responseForAnotherStudent = await anotherStudentClient.UpdateTask(updateTaskViewModel, taskId); + var responseForAnotherLecture = await anotherLectureClient.UpdateTask(updateTaskViewModel, taskId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.UpdateTask(updateTaskViewModel, taskId); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestDeleteTaskForCourseMentorOnly() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var newHomeworkViewModel = GenerateCreateHomeworkViewModel(); + var newTaskViewModel = GenerateCreateTaskViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + var homeworkId = (await courseLectureClient.AddHomeworkToCourse(newHomeworkViewModel, courseId)).Value; + var taskId = (await courseLectureClient.AddTask(newTaskViewModel, homeworkId)).Value; + // Act + var responseForStudentInCourse = await studentInCourseClient.DeleteTask(taskId); + var responseForAnotherStudent = await anotherStudentClient.DeleteTask(taskId); + var responseForAnotherLecture = await anotherLectureClient.DeleteTask(taskId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.DeleteTask(taskId); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestAcceptStudentForCourseMentorOnly() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + // Act + await anotherStudentClient.SignInCourse(courseId, anotherStudentId); + var responseForStudentInCourse = await studentInCourseClient.AcceptStudent(courseId, anotherStudentId); + var responseForAnotherStudent = await anotherStudentClient.AcceptStudent(courseId, anotherStudentId); + var responseForAnotherLecture = await anotherLectureClient.AcceptStudent(courseId, anotherStudentId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.AcceptStudent(courseId, anotherStudentId); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestRejectStudentForCourseMentorOnly() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, _) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + // Act + await anotherStudentClient.SignInCourse(courseId, anotherStudentId); + var responseForStudentInCourse = await studentInCourseClient.RejectStudent(courseId, anotherStudentId); + var responseForAnotherStudent = await anotherStudentClient.RejectStudent(courseId, anotherStudentId); + var responseForAnotherLecture = await anotherLectureClient.RejectStudent(courseId, anotherStudentId); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.RejectStudent(courseId, anotherStudentId); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + + [Test] + public async Task TestAcceptLectureForCourseMentorOnly() + { + // Arrange + var (courseLectureId, _) = await CreateAndRegisterLecture(); + var (studentInCourseId, _) = await CreateAndRegisterUser(); + var (anotherLectureId, anotherLectureEmail) = await CreateAndRegisterLecture(); + var (anotherStudentId, _) = await CreateAndRegisterUser(); + var (anotherLectureInCourseId, anotherLectureInCourseEmail) = await CreateAndRegisterLecture(); + var newCourseViewModel = GenerateCreateCourseViewModel(); + var courseLectureClient = CreateCourseServiceClient(courseLectureId); + var studentInCourseClient = CreateCourseServiceClient(studentInCourseId); + var anotherLectureClient = CreateCourseServiceClient(anotherLectureId); + var anotherStudentClient = CreateCourseServiceClient(anotherStudentId); + var anotherLectureInCourseClient = CreateCourseServiceClient(anotherLectureInCourseId); + var courseId = await courseLectureClient.CreateCourse(newCourseViewModel, courseLectureId); + await studentInCourseClient.SignInCourse(courseId, studentInCourseId); + await courseLectureClient.AcceptStudent(courseId, studentInCourseId); + await courseLectureClient.AcceptLecturer(courseId, anotherLectureInCourseEmail); + // Act + var responseForStudentInCourse = await studentInCourseClient.AcceptLecturer(courseId, anotherLectureEmail); + var responseForAnotherStudent = await anotherStudentClient.AcceptLecturer(courseId, anotherLectureEmail); + var responseForAnotherLecture = await anotherLectureClient.AcceptLecturer(courseId, anotherLectureEmail); + var responseForAnotherLectureInCourse = await anotherLectureInCourseClient.AcceptLecturer(courseId, anotherLectureEmail); + //Assert + responseForAnotherLecture.Succeeded.Should().BeFalse(); + responseForAnotherStudent.Succeeded.Should().BeFalse(); + responseForStudentInCourse.Succeeded.Should().BeFalse(); + responseForAnotherLectureInCourse.Succeeded.Should().BeTrue(); + } + } +} \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CoursesController.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CoursesController.cs index f9c5e8ed0..b7735d93e 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CoursesController.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CoursesController.cs @@ -25,6 +25,7 @@ public CoursesController(ICoursesService coursesService, IMapper mapper) { _coursesService = coursesService; _mapper = mapper; + } [HttpGet] diff --git a/HwProj.sln b/HwProj.sln index 786e01d33..1e6af9362 100644 --- a/HwProj.sln +++ b/HwProj.sln @@ -64,6 +64,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HwProj.SolutionsService.Cli EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HwProj.Exceptions", "HwProj.Common\HwProj.Exceptions\HwProj.Exceptions.csproj", "{51463655-7668-4C7D-9FDE-D4D7CDAA82B8}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CourseService.IntegrationTests", "HwProj.CoursesService\CourseService.IntegrationTests\CourseService.IntegrationTests.csproj", "{F09D809A-39FB-4746-9D1F-7B2D051643EE}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HwProj.AuthService.IntegrationTests", "HwProj.AuthService\HwProj.AuthService.IntegrationTests\HwProj.AuthService.IntegrationTests.csproj", "{EA822D2F-88C2-4B82-AA20-DD07FF0A9A1E}" EndProject Global @@ -152,6 +153,10 @@ Global {51463655-7668-4C7D-9FDE-D4D7CDAA82B8}.Debug|Any CPU.Build.0 = Debug|Any CPU {51463655-7668-4C7D-9FDE-D4D7CDAA82B8}.Release|Any CPU.ActiveCfg = Release|Any CPU {51463655-7668-4C7D-9FDE-D4D7CDAA82B8}.Release|Any CPU.Build.0 = Release|Any CPU + {F09D809A-39FB-4746-9D1F-7B2D051643EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F09D809A-39FB-4746-9D1F-7B2D051643EE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F09D809A-39FB-4746-9D1F-7B2D051643EE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F09D809A-39FB-4746-9D1F-7B2D051643EE}.Release|Any CPU.Build.0 = Release|Any CPU {EA822D2F-88C2-4B82-AA20-DD07FF0A9A1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {EA822D2F-88C2-4B82-AA20-DD07FF0A9A1E}.Debug|Any CPU.Build.0 = Debug|Any CPU {EA822D2F-88C2-4B82-AA20-DD07FF0A9A1E}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -181,6 +186,7 @@ Global {0CBD72C5-1CCF-4C13-8046-732398816160} = {1EAEB779-E7C8-4EF9-B9A9-22CB8E3C246D} {C1ACAB32-4100-4C19-AE08-57FA9BA75DF2} = {A85A3030-2878-4923-B450-9683832CDAC1} {51463655-7668-4C7D-9FDE-D4D7CDAA82B8} = {77D857A8-45C6-4432-B4BF-A2F2C9ECA7FE} + {F09D809A-39FB-4746-9D1F-7B2D051643EE} = {CB37F9AC-88CB-425C-8200-1B40F372F38D} {EA822D2F-88C2-4B82-AA20-DD07FF0A9A1E} = {D7F8BC73-A6CB-4E07-984D-2D65B740BC69} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution