public async Task CreateIssue_WhenModelValid_CreatesIssueInDbAndReturnSuccessResponse() { var claims = new Claim("roles", "ProjectManager"); var token = GetJwtToken(new[] { claims }); AddAuth(token); var issuesCount = IssuesDbSet.Get().Count(); var request = new IssueDto() { ProjectId = ProjectsDbSet.Get().First().Id, MilestoneId = null, Description = "description", Status = Status.Closed, AssignedToUserId = UsersDbSet.Get().First().Id, ReportedByUserId = UsersDbSet.Get().First().Id, Title = "title" }; var httpResponse = await PostAsync(IssueControllerRoutes.CreateIssue, request); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <IssueDto> >(); response.VerifySuccessResponse(); await CheckIssueIsAddedToDatabase(response.Data, issuesCount + 1); response.VerifySuccessResponse(); await ReSeedDatabase(); }
public async Task GetActivitiesForUser_WhenModelValid_ReturnsUserActivities() { var user = UsersDbSet.Get().First(); var request = new ActivitiesRequest() { ProjectId = ProjectsDbSet.Get().First().Id, UserId = user.Id }; var workLogItems = WorklogsDbSet.Get().Where(e => e.UserId.Equals(request.UserId)) .Include(e => e.Issue) .ThenInclude(e => e.Project) .Where(e => e.Issue.ProjectId.Equals(request.ProjectId)); var httpResponse = await GetAsync(WorkLogControllerRoutes.GetUserWorkLogs + "?" + request.ToQueryString()); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <UserActivityDto> >(); response.VerifySuccessResponse(); response.Data.UserId.Should().Be(request.UserId); response.Data.UserName.Should().Be(user.FirstName); response.Data.UserSurname.Should().Be(user.LastName); response.Data.UserEmail.Should().Be(user.Email); response.Data.ProjectName.Should().Be(workLogItems?.Select(e => e.Issue.Project.Name).FirstOrDefault()); response.Data.TotalWorkLogInSeconds.Should().Be((long?)workLogItems?.Sum(e => e.TimeSpent)); response.Data.WorkLogItems.Should() .BeEquivalentTo(workLogItems?.Select(e => GetWorkLogDetails(e)).ToList()); await ReSeedDatabase(); }
public async Task CreateProject_WhenModelValid_CreatesProjectInDbAndReturnsSuccessResponse() { var claims = new Claim("roles", "ProjectManager"); var token = GetJwtToken(new[] { claims }); AddAuth(token); var projectCount = ProjectsDbSet.Get().Count(); var request = new ProjectDto() { Description = "description", InitialRisk = DateTimeOffset.Now.AddDays(-3222), Abbreviation = "AAAA", Name = "name" }; var httpResponse = await PostAsync(ProjectControllerRoutes.CreateProject, request); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <ProjectDto> >(); await CheckProjectsAddedToDatabase(response.Data, projectCount + 1); response.VerifySuccessResponse(); await ReSeedDatabase(); }
public static void SeedData(TimeTrackingDbContext context) { context.Issues.AddRange(IssuesDbSet.Get().ToList()); context.Projects.AddRange(ProjectsDbSet.Get().ToList()); context.Milestones.AddRange(MilestonesDbSet.Get().ToList()); context.Teams.AddRange(TeamsDbSet.Get().ToList()); context.WorkLogs.AddRange(WorklogsDbSet.Get().ToList()); context.Users.AddRange(UsersDbSet.Get().ToList()); context.SaveChanges(); }
public async Task ProjectRepository_GetAllAsync_ReturnsAllValues() { await using var context = new TimeTrackingDbContext(_dbOptions); var projectRepository = new ProjectRepository(context); var expected = ProjectsDbSet.Get(); var actual = await projectRepository.GetAllAsync(); Assert.That(actual.OrderBy(x => x.Id), Is.EqualTo(expected.OrderBy(x => x.Id)) .Using(EqualityComparers.ProjectComparer)); }
public async Task ProjectRepository_GetById_ShouldReturnCorrectItem(string id) { var guidId = Guid.Parse(id); await using var context = new TimeTrackingDbContext(_dbOptions); var expected = ProjectsDbSet.Get().First(x => x.Id == guidId); var projectRepository = new ProjectRepository(context); var actual = await projectRepository.GetByIdAsync(guidId); Assert.That(actual, Is.EqualTo(expected).Using(EqualityComparers.ProjectComparer)); }
public async Task GetProjectById_WhenFound_ReturnsProject() { var expected = ProjectsDbSet.Get().First(); var projectId = expected.Id; var httpResponse = await GetAsync(ProjectControllerRoutes.BaseRoute + "/" + projectId); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <ProjectDetailsDto> >(); response.VerifySuccessResponse(); response.Data.Should().BeEquivalentTo(GetProjectDetails(expected)); }
public async Task ProjectRepository_DeleteAsync_DeletesEntity() { var guidId = Guid.Parse("9245950B-0F82-4B9E-9AF7-DEC9ACF171FA"); var entityToDelete = ProjectsDbSet.Get().First(x => x.Id == guidId); var expectedCount = ProjectsDbSet.Get().ToList().Count - 1; await using var context = new TimeTrackingDbContext(_dbOptions); var projectRepository = new ProjectRepository(context); await projectRepository.DeleteAsync(entityToDelete); context.Milestones.Should().HaveCount(expectedCount); }
public async Task ProjectRepository_UpdateAsync_UpdateEntity() { await using var context = new TimeTrackingDbContext(_dbOptions); var projectRepository = new ProjectRepository(context); var entityToUpdate = ProjectsDbSet.Get().First(); entityToUpdate.Description = "New description"; var actual = await projectRepository.UpdateAsync(entityToUpdate); actual.Should().BeEquivalentTo(entityToUpdate); //Assert.That( actual, Is.EqualTo(entityToUpdate).Using(EqualityComparers.ProjectComparer)); }
public async Task GenerateReport_WhenValidParameters_GeneratesReportAsFile() { var request = new ReportGeneratorRequest() { ProjectId = ProjectsDbSet.Get().First().Id, UserId = UsersDbSet.Get().First().Id }; var httpResponse = await GetAsync(ReportControllerRoutes.BaseRoute + "?" + request.ToQueryString()); httpResponse.EnsureSuccessStatusCode(); httpResponse.Content.Should().NotBeNull(); }
public async Task CreateIssue_WhenAddFailed_ReturnProjectCreationFailedResponse() { var projectPassed = ProjectsDbSet.ProjectBuilder().Create <ProjectDto>(); Project modelAfterMap = new Project(); MockFor <IBaseMapper <Project, ProjectDto> >().Setup(e => e.MapToEntity(It.IsAny <ProjectDto>())) .Returns(modelAfterMap); MockFor <IProjectRepository>().Setup(x => x.AddAsync(modelAfterMap)) .ReturnsAsync((Project)null); var result = await ClassUnderTest.CreateProjectAsync(projectPassed); result.VerifyNotSuccessResponseWithErrorCodeAndMessage(ErrorCode.ProjectCreationFailed); }
public async Task GetActivitiesForUser_WhenUserNotFound_ReturnsUserNotFoundResponse() { var request = new ActivitiesRequest() { ProjectId = ProjectsDbSet.Get().First().Id, UserId = Guid.NewGuid() }; var httpResponse = await GetAsync(WorkLogControllerRoutes.GetUserWorkLogs + "?" + request.ToQueryString()); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <UserActivityDto> >(); response.VerifyNotSuccessResponseWithErrorCodeAndMessage(ErrorCode.UserNotFound); }
public async Task GetAllProjects_WhenRequestValid_ReturnsAllProjects(int page, int size, int expectedPage, int expectedSize) { var expected = ProjectsDbSet.Get().ToList(); var pagedRequest = new PagedRequest() { Page = page, PageSize = size }; var httpResponse = await GetAsync(ProjectControllerRoutes.BaseRoute + "?" + pagedRequest.ToQueryString()); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiPagedResponse <ProjectDetailsDto> >(); response.EnsurePagedResult(expected.Count, expectedSize, expectedPage); }
public async Task ProjectRepository_AddAsync_AddsValueToDatabase() { var expectedCount = ProjectsDbSet.Get().ToList().Count + 1; var entityToAdd = ProjectsDbSet.ProjectBuilder().Create(); await using var context = new TimeTrackingDbContext(_dbOptions); var projectRepository = new ProjectRepository(context); await projectRepository.AddAsync(entityToAdd); context.Projects.Should().HaveCount(expectedCount); var entityFound = await context.Projects.FindAsync(entityToAdd.Id); entityFound.Should().BeEquivalentTo(entityToAdd); // Assert.That(entityFound, Is.EqualTo(entityToAdd).Using(EqualityComparers.ProjectComparer)); }
public async Task CreateIssue_WhenExceptionThrows_ReturnInternalError() { var projectPassed = ProjectsDbSet.ProjectBuilder().Create <ProjectDto>(); Project modelAfterMap = new Project(); MockFor <IBaseMapper <Project, ProjectDto> >().Setup(e => e.MapToEntity(It.IsAny <ProjectDto>())) .Returns(modelAfterMap); MockFor <IBaseMapper <Project, ProjectDto> >().Setup(e => e.MapToModel(It.IsAny <Project>())) .Returns(projectPassed); MockFor <IProjectRepository>().Setup(x => x.AddAsync(modelAfterMap)) .ThrowsAsync(new Exception()); var result = await ClassUnderTest.CreateProjectAsync(projectPassed); result.VerifyInternalError(); }
public async Task CreateIssue_WhenAddSuccess_ReturnProjectMappedResponse() { var projectPassed = ProjectsDbSet.ProjectBuilder().Create <ProjectDto>(); Project modelAfterMap = new Project(); MockFor <IBaseMapper <Project, ProjectDto> >().Setup(e => e.MapToEntity(It.IsAny <ProjectDto>())) .Returns(modelAfterMap); MockFor <IBaseMapper <Project, ProjectDto> >().Setup(e => e.MapToModel(It.IsAny <Project>())) .Returns(projectPassed); MockFor <IProjectRepository>().Setup(x => x.AddAsync(modelAfterMap)) .ReturnsAsync(new Project()); var result = await ClassUnderTest.CreateProjectAsync(projectPassed); result.VerifySuccessResponseWithData(projectPassed); }
public async Task GetAllProjects_WhenRequestValid_ReturnsValidProjects() { var pagedRequest = new PagedRequest() { Page = 1, PageSize = 2 }; var expected = ProjectsDbSet.Get().Take(pagedRequest.PageSize).ToList(); var mappedExpectedResult = expected.Select(GetProjectDetails).ToList(); var httpResponse = await GetAsync(ProjectControllerRoutes.BaseRoute + "?" + pagedRequest.ToQueryString()); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiPagedResponse <ProjectDetailsDto> >(); response.Data.Should().BeEquivalentTo(mappedExpectedResult); }
public async Task CreateIssue_WhenNotValidModelPassed_ReturnsValidationError() { var claims = new Claim("roles", "ProjectManager"); var token = GetJwtToken(new[] { claims }); AddAuth(token); var request = new IssueDto() { ProjectId = ProjectsDbSet.Get().First().Id, MilestoneId = null, AssignedToUserId = UsersDbSet.Get().First().Id, ReportedByUserId = UsersDbSet.Get().First().Id }; var httpResponse = await PostAsync(IssueControllerRoutes.CreateIssue, request); var response = await httpResponse.BodyAs <ApiResponse <IssueDto> >(); response.CheckValidationException(4); request = new IssueDto() { ProjectId = ProjectsDbSet.Get().First().Id, MilestoneId = Guid.NewGuid(), Description = "description", Status = Status.Closed, Title = "title" }; httpResponse = await PostAsync(IssueControllerRoutes.CreateIssue, request); response = await httpResponse.BodyAs <ApiResponse <IssueDto> >(); response.CheckValidationException(1); request = new IssueDto() { ProjectId = ProjectsDbSet.Get().First().Id, MilestoneId = Guid.NewGuid(), Status = Status.Closed }; httpResponse = await PostAsync(IssueControllerRoutes.CreateIssue, request); response = await httpResponse.BodyAs <ApiResponse <IssueDto> >(); response.CheckValidationException(5); }
public async Task ProjectRepository_GetAllPagedAsync_ReturnsAllResultsPaged() { var page = 1; var size = 2; await using var context = new TimeTrackingDbContext(_dbOptions); var projectRepository = new ProjectRepository(context); var expected = ProjectsDbSet.Get().Take(size).ToList(); var actual = await projectRepository.GetAllPagedAsync(page, size); actual.EnsurePagedResult(ProjectsDbSet.Get().ToList().Count, size, page); var actualItems = actual.Items.ToList(); actualItems.Should().BeEquivalentTo(expected, options => options .Excluding(e => e.Milestones) .Excluding(e => e.Issues) .Excluding(e => e.Teams)); }
public async Task GetAllProjectPagedAsync_WhenRequestedWithPage_ReturnsAllProjectsPaged() { var pagedRequest = new PagedRequest() { Page = 1, PageSize = 2 }; var calls = 0; var pagedItems = ProjectsDbSet.ProjectBuilder().CreateMany <Project>(); var pagedItemsAfterMap = Fixture.CreateMany <ProjectDetailsDto>().ToList(); var pagedProjects = PagedResult <Project> .Paginate(pagedItems, 1, 2, 4, 8); MockFor <IProjectRepository>().Setup(e => e.GetAllPagedAsync(pagedRequest.Page, pagedRequest.PageSize)) .ReturnsAsync(pagedProjects); MockFor <IModelMapper <Project, ProjectDetailsDto> >().Setup(e => e.MapToModel(It.IsAny <Project>())) .Returns(() => pagedItemsAfterMap[calls]) .Callback(() => calls++); var response = await ClassUnderTest.GetAllProjectPagedAsync(pagedRequest); response.VerifyCorrectPagination(pagedProjects, pagedItemsAfterMap); }
public async Task CreateMilestone_WhenModelValid_CreatesMilestoneInDbAndReturnsSuccessResponse() { var milestonesCount = MilestonesDbSet.Get().Count(); var request = new MilestoneDto() { State = State.Closed, Description = "description", DueDate = DateTimeOffset.UtcNow.AddDays(-3050), ProjectId = ProjectsDbSet.Get().First().Id, Title = "tittle" }; var httpResponse = await PostAsync(MilestoneControllerRoutes.CreateMilestone, request); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <MilestoneDto> >(); await CheckMilestoneIsAddedToDatabase(response.Data, milestonesCount + 1); response.VerifySuccessResponse(); await ReSeedDatabase(); }
public async Task CreateIssue_WhenMilestoneNotFound_ReturnsMilestoneNotFound() { var claims = new Claim("roles", "ProjectManager"); var token = GetJwtToken(new[] { claims }); AddAuth(token); var request = new IssueDto() { ProjectId = ProjectsDbSet.Get().First().Id, MilestoneId = Guid.NewGuid(), Description = "description", Status = Status.Closed, AssignedToUserId = UsersDbSet.Get().First().Id, ReportedByUserId = UsersDbSet.Get().First().Id, Title = "title" }; var httpResponse = await PostAsync(IssueControllerRoutes.CreateIssue, request); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <IssueDto> >(); response.VerifyNotSuccessResponseWithErrorCodeAndMessage(ErrorCode.MileStoneNotFound); }
public async Task CreateTeamAsync_WhenModelValid_CreatesTeamInDbAndReturnsSuccessResponse() { var claims = new Claim("roles", "TeamLead"); var token = GetJwtToken(new[] { claims }); AddAuth(token); var projectCount = TeamsDbSet.Get().Count(); var request = new TeamDto() { ProjectId = ProjectsDbSet.Get().First().Id, MembersCount = 2, Name = "name" }; var httpResponse = await PostAsync(TeamControllerRoutes.CreateTeam, request); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.BodyAs <ApiResponse <TeamDto> >(); await CheckTeamAddedToDatabase(response.Data, projectCount + 1); response.VerifySuccessResponse(); await ReSeedDatabase(); }