Merge branch 'test' of https://github.com/rjNemo/ticket_manager into backend

This commit is contained in:
Ruidy Nemausat 2020-02-26 14:54:07 +01:00
commit bc400ae21a
14 changed files with 304 additions and 62 deletions

12
.gitignore vendored
View file

@ -1,14 +1,16 @@
bin/
obj/
.vs/ .vs/
.vscode/ .vscode/
Migrations/
app.db*
.DS_Store .DS_Store
app.db bin/
obj/
app.db*
Data/Interfaces Data/Interfaces
Data/UnitOfWork.cs Data/UnitOfWork.cs
Data/*Repository.cs Data/*Repository.cs
Migrations/
Properties/
Tests/TicketManager.Tests/UnitTests/ControllersTests/ControllerTests.cs
Tests/TicketManager.Tests/UnitTests/ControllersTests/SeedDb.cs
# client # client
client/src/pages/TestPage.tsx client/src/pages/TestPage.tsx

View file

@ -37,7 +37,7 @@ namespace TicketManager.Controllers
/// <response code="200">Returns a list of projects</response> /// <response code="200">Returns a list of projects</response>
[HttpGet] [HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IEnumerable<ProjectDTO>> GetProjects() public async Task<List<ProjectDTO>> GetProjects()
{ {
return await _context.Projects return await _context.Projects
.Include(p => p.Assignments) .Include(p => p.Assignments)
@ -60,7 +60,8 @@ namespace TicketManager.Controllers
/// GET: api/v1/Projects/2 /// GET: api/v1/Projects/2
/// ///
/// </remarks> /// </remarks>
/// <response code="200">Returns a project object</response> /// <param name="id">Identifier of the ressource</param>
/// <response code="200">Returns a specific project</response>
/// <response code="404">If the required project is null</response> /// <response code="404">If the required project is null</response>
[HttpGet("{id}")] [HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
@ -242,7 +243,7 @@ namespace TicketManager.Controllers
/// <response code="404">Not Found</response> /// <response code="404">Not Found</response>
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpPut("{id}/members")] [HttpPatch("{id}/members")]
public async Task<ActionResult<Project>> SetProjectMembers(int id, List<AppUser> projectMembers) public async Task<ActionResult<Project>> SetProjectMembers(int id, List<AppUser> projectMembers)
{ {
Project project = await _context.Projects Project project = await _context.Projects

View file

@ -48,6 +48,5 @@ namespace TicketManager.DTO
public List<Project> Projects { get; set; } = new List<Project>(); public List<Project> Projects { get; set; } = new List<Project>();
public List<Ticket> Tickets { get; set; } = new List<Ticket>(); public List<Ticket> Tickets { get; set; } = new List<Ticket>();
} }
} }

View file

@ -36,6 +36,7 @@ namespace TicketManager.DTO
public string Status { get; set; } public string Status { get; set; }
public AppUser Manager { get; set; } public AppUser Manager { get; set; }
public List<AppUser> Users { get; set; } = new List<AppUser>(); public List<AppUser> Users { get; set; } = new List<AppUser>();
public List<Ticket> Tickets { get; set; } = new List<Ticket>(); public List<Ticket> Tickets { get; set; } = new List<Ticket>();

View file

@ -38,14 +38,23 @@ namespace TicketManager.DTO
public DateTime PlannedEnding { get; set; } public DateTime PlannedEnding { get; set; }
public string Status { get; set; } public string Status { get; set; }
public string Impact { get; set; } public string Impact { get; set; }
public string Difficulty { get; set; } public string Difficulty { get; set; }
public string Category { get; set; } public string Category { get; set; }
public Guid CreatorId { get; set; } public Guid CreatorId { get; set; }
public Project Project { get; set; } public Project Project { get; set; }
public List<Note> Notes { get; set; } = new List<Note>(); public List<Note> Notes { get; set; } = new List<Note>();
public List<Activity> Activities { get; set; } = new List<Activity>(); public List<Activity> Activities { get; set; } = new List<Activity>();
public List<File> Files { get; set; } = new List<File>(); public List<File> Files { get; set; } = new List<File>();
public List<AppUser> Users { get; set; } = new List<AppUser>(); public List<AppUser> Users { get; set; } = new List<AppUser>();
} }
} }

View file

@ -95,6 +95,10 @@ namespace TicketManager.Models
public void RemoveMembers(List<AppUser> membersToRemove) public void RemoveMembers(List<AppUser> membersToRemove)
{ {
this.Assignments.RemoveAll(a => membersToRemove.Contains(a.User)); this.Assignments.RemoveAll(a => membersToRemove.Contains(a.User));
// membersToRemove.ForEach(
// m => m.Assignments.RemoveAll(a => (a.Project == this))
// );
} }
public void SetMembers(List<AppUser> projectMembers) public void SetMembers(List<AppUser> projectMembers)

View file

@ -20,7 +20,7 @@
"TicketManager": { "TicketManager": {
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "weatherforecast", "launchUrl": "/",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
}, },

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Reflection;
using System.Linq; using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.HttpsPolicy;
@ -13,13 +12,10 @@ using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer; using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using System.Reflection;
using System.IO;
using TicketManager.Data;
using TicketManager.Models;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson; using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Newtonsoft.Json;
using TicketManager.Data;
[assembly: ApiController] [assembly: ApiController]
namespace TicketManager namespace TicketManager
@ -36,10 +32,14 @@ namespace TicketManager
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddDbContext<AppDbContext>(options => services.AddDbContext<AppDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("Sqlite"))); {
services.AddScoped<IProjectRepository, ProjectRepository>(); options.UseSqlite(Configuration.GetConnectionString("Sqlite"));
services.AddScoped<IAppUserRepository, AppUserRepository>(); options.EnableSensitiveDataLogging(true); //Remove in production.
services.AddScoped<ITicketRepository, TicketRepository>(); }
);
// services.AddScoped<IProjectRepository, ProjectRepository>();
// services.AddScoped<IAppUserRepository, AppUserRepository>();
// services.AddScoped<ITicketRepository, TicketRepository>();
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {

View file

@ -0,0 +1,253 @@
using System;
using Xunit;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;
using TicketManager.Controllers;
using TicketManager.Data;
using TicketManager.Models;
using TicketManager.DTO;
namespace TicketManager.Tests
{
public class ProjectsControllerTests
{
[Fact]
public async Task Get_ReturnsListWith2Projects()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
try
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(connection)
.Options;
// creates DB schema
using (var context = new AppDbContext(options))
{
context.Database.EnsureCreated();
}
// Seed DB usng one context instance
SeedDb(options);
using (var context = new AppDbContext(options))
{
var controller = new ProjectsController(context);
var result = await controller.GetProjects();
Assert.IsAssignableFrom<IEnumerable<ProjectDTO>>(result);
Assert.Equal(2, result.Count);
}
}
finally
{
connection.Close();
}
}
[Fact]
public async Task Get1_Returns1Project()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
try
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(connection)
.Options;
// creates DB schema
using (var context = new AppDbContext(options))
{
context.Database.EnsureCreated();
}
// Seed DB usng one context instance
SeedDb(options);
using (var context = new AppDbContext(options))
{
var controller = new ProjectsController(context);
// Should Return 1 Project
var result = await controller.GetProject(1);
Assert.IsAssignableFrom<ProjectDTO>(result);
// Should Return NotFound
result = await controller.GetProject(3);
Assert.IsType<NotFoundResult>(result);
}
}
finally
{
connection.Close();
}
}
[Fact]
public async Task Put1_Updates1Project()
{
// ControllersTests.Wrapper(Test_PutProject, SeedDb);
// }
// private static async Task Test_PutProject()
// {
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
try
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(connection)
.Options;
// creates DB schema
using (var context = new AppDbContext(options))
{
context.Database.EnsureCreated();
}
// Seed DB usng one context instance
SeedDb(options);
using (var context = new AppDbContext(options))
{
var controller = new ProjectsController(context);
var result = await controller.PutProject(1,
new Project()
{
Id = 1,
Title = "Top Secret Project",
Description = "Shht Don't Ask don't tell",
PlannedEnding = new DateTime(2020, 7, 21)
}
);
// Should Update
Assert.Equal("Top Secret Project", context.Projects.Find(1).Title);
Assert.Equal(new DateTime(2020, 7, 21), context.Projects.Find(1).PlannedEnding);
Assert.IsType<NoContentResult>(result);
result = await controller.PutProject(2,
new Project()
{
Id = 1,
Title = "Top Secret Project",
Description = "Shht Don't Ask don't tell",
PlannedEnding = new DateTime(2020, 7, 21)
}
);
// Should Return BadRequest
Assert.NotEqual("Top Secret Project", context.Projects.Find(2).Title);
Assert.NotEqual(new DateTime(2020, 7, 21), context.Projects.Find(2).CreatedAt);
Assert.IsType<BadRequestResult>(result);
// Delete updated project
context.Projects.RemoveRange(context.Projects.Find(1));
await context.SaveChangesAsync();
result = await controller.PutProject(1,
new Project()
{
Id = 1,
Title = "Top Secret Project",
Description = "Shht Don't Ask don't tell",
PlannedEnding = new DateTime(2020, 7, 21)
}
);
// Should Throw
Assert.IsType<NotFoundResult>(result);
Assert.Equal("Top Secret Project", context.Projects.Find(1).Title);
Assert.Equal(new DateTime(2020, 7, 21), context.Projects.Find(1).PlannedEnding);
}
}
finally
{
connection.Close();
}
}
[Fact]
public async Task Post_CreatesProject()
{
// Create inMemory Test Database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
try
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(connection)
.Options;
// creates DB schema
using (var context = new AppDbContext(options))
{
context.Database.EnsureCreated();
}
// Seed DB usng one context instance
SeedDb(options);
// use another context instance to run the test
using (var context = new AppDbContext(options))
{
var proj = new Project()
{
Title = "The Third",
Description = "Thrice in a row",
PlannedEnding = DateTime.Now
};
var controller = new ProjectsController(context);
var result = await controller.PostProject(proj);
Assert.IsAssignableFrom<ProjectDTO>(result);
Assert.Equal(3, await context.Projects.CountAsync());
}
}
finally
{
connection.Close();
}
}
private static void SeedDb(DbContextOptions<AppDbContext> options)
// Seed DB usng one context instance
{
using (var context = new AppDbContext(options))
{
context.Projects.AddRange(
new Project()
{
Id = 1,
Title = "Secret Project",
Description = "Shht Don't Ask don't tell",
PlannedEnding = new DateTime(2021, 7, 21)
},
new Project()
{
Id = 2,
Title = "Public Project",
Description = "It's quite obvious, isn't it?!",
PlannedEnding = new DateTime(2036, 6, 16)
});
context.SaveChanges();
}
}
}
}

View file

@ -50,6 +50,13 @@ namespace TicketManager.Tests
Ticket t5 = new Ticket(); Ticket t5 = new Ticket();
Ticket t6 = new Ticket(); Ticket t6 = new Ticket();
p1.Tickets.Add(t1);
p2.Tickets.Add(t2);
p2.Tickets.Add(t3);
p3.Tickets.Add(t4);
p3.Tickets.Add(t5);
p3.Tickets.Add(t6);
Assignment a1 = new Assignment() Assignment a1 = new Assignment()
{ {
User = user, User = user,
@ -69,13 +76,6 @@ namespace TicketManager.Tests
}; };
user.Assignments.Add(a3); user.Assignments.Add(a3);
p1.Tickets.Add(t1);
p2.Tickets.Add(t2);
p2.Tickets.Add(t3);
p3.Tickets.Add(t4);
p3.Tickets.Add(t5);
p3.Tickets.Add(t6);
var res = user.GetTickets().Count; var res = user.GetTickets().Count;
Assert.Equal(6, res); Assert.Equal(6, res);
} }

View file

@ -1,32 +0,0 @@
using System;
using Xunit;
using System.Collections.Generic;
using TicketManager.Controllers;
using TicketManager.Data;
using TicketManager.Models;
namespace TicketManager.Tests
{
public class ProjectsControllerTests
{
public ProjectsControllerTests()
{
// _context = context;
}
// [Fact]
// public void Get_ReturnsProjectList()
// {
// // Arange
// // var controller = new ProjectsController();
// // Act
// // var result = controller.GetProjects();
// // Assert
// // Assert.IsType<IEnumerable<Project>>(result);
// }
}
}

View file

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<PropertyGroup > <PropertyGroup>
<LangVersion>8.0</LangVersion> <LangVersion>8.0</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
@ -26,9 +26,14 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
<PackageReference Include="Microsoft.OpenApi" Version="1.1.4" /> <PackageReference Include="Microsoft.OpenApi" Version="1.1.4" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.0.0" /> <PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.0.0" />
<PackageReference Include="Xunit" Version="2.4.1" /> <PackageReference Include="Xunit" Version="2.4.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="client\" /> <Folder Include="client\" />