using Coscine.Api.Project.Controllers; using Coscine.Database.Models; using Coscine.ApiCommons.Utils; using Coscine.Database.DataModel; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Routing; using Moq; using NUnit.Framework; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Management; using System.Security.Claims; using LinqToDB.Data; using Coscine.Database.Settings; using Coscine.Configuration; namespace Coscine.Api.Project.Tests { public abstract class DefaultControllerTests where T : Controller { protected T Controller { get; } protected readonly List Projects = new List(); protected readonly List Users = new List(); protected readonly List ProjectRoles = new List(); protected readonly List ResourceTypes = new List(); protected readonly List Resources = new List(); protected readonly List ResourceDisciplines = new List(); protected Discipline Discipline { get; set; } protected Institute Institute { get; set; } protected Visibility Visibility { get; set; } protected License License { get; set; } protected RDSResourceType RdsResourceType { get; set; } protected int Previous; protected DefaultControllerTests(T controller) { Controller = controller; } [OneTimeSetUp] public void Setup() { DataConnection.DefaultSettings = new CoscineSettings(new ConsulConfiguration()); DisciplineModel disciplineModel = new DisciplineModel(); Discipline = new Discipline() { DisplayNameDe = "TestDiscipline", DisplayNameEn = "TestDiscipline", Url = "http://rwth-aachen.de" }; disciplineModel.Insert(Discipline); InstituteModel instituteModel = new InstituteModel(); Institute = new Institute() { DisplayName = "TestInstitute", IKZ = "022000" }; instituteModel.Insert(Institute); VisibilityModel visibilityModel = new VisibilityModel(); Visibility = visibilityModel.GetWhere((visibility) => visibility.DisplayName == "Public"); LicenseModel licenseModel = new LicenseModel(); License = new License() { DisplayName = "MIT" }; licenseModel.Insert(License); ProjectModel projectModel = new ProjectModel(); Previous = projectModel.GetAll().ToArray().Length; UserModel userModel = new UserModel(); var user = new User() { DisplayName = "TestUser", EmailAddress = "testUser@test.com", }; userModel.Insert(user); Users.Add(user); FakeControllerContext(user); var project = new Coscine.Database.DataModel.Project() { Description = "Description", ProjectName = "TestProject", DisplayName = "TestProject", StartDate = DateTime.Now, EndDate = DateTime.Now.AddYears(1), Keywords = "Test1;Test2", GrantId = "testid", PrincipleInvestigators = "TestInvestigator", VisibilityId = Visibility.Id }; projectModel.Insert(project); var projectRole = projectModel.SetOwner(project, user); ProjectRoles.Add(projectRole); ProjectDisciplineModel projectDisciplineModel = new ProjectDisciplineModel(); projectDisciplineModel.Insert(new ProjectDiscipline() { DisciplineId = Discipline.Id, ProjectId = project.Id }); ProjectInstituteModel projectInstituteModel = new ProjectInstituteModel(); projectInstituteModel.Insert(new ProjectInstitute() { InstituteId = Institute.Id, ProjectId = project.Id }); Projects.Add(projectModel.GetById(project.Id)); var project2 = new Coscine.Database.DataModel.Project() { Description = "Description2", ProjectName = "TestProject", DisplayName = "TestProject2", StartDate = DateTime.Now, EndDate = DateTime.Now.AddYears(1), Keywords = "Test1;Test2", GrantId = "testid", PrincipleInvestigators = "TestInvestigator", VisibilityId = Visibility.Id }; projectModel.Insert(project2); Projects.Add(projectModel.GetById(project2.Id)); ResourceTypeModel resourceTypeModel = new ResourceTypeModel(); var resourceType = resourceTypeModel.GetWhere((dbResourceType) => dbResourceType.DisplayName == "rds"); RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel(); RdsResourceType = new RDSResourceType() { BucketName = "c", Size = 25, }; rdsResourceTypeModel.Insert(RdsResourceType); ResourceModel resourceModel = new ResourceModel(); var resource = new Resource() { DisplayName = "ResourceTest1", ResourceName = "ResourceTest1", Keywords = "ResourceTest1", UsageRights = "ResourceTest1", TypeId = resourceType.Id, Type = resourceType, Visibility = Visibility, VisibilityId = Visibility.Id, LicenseId = License.Id, ResourceTypeOptionId = RdsResourceType.Id }; resourceModel.Insert(resource); projectModel.AddResource(project, resource); Resources.Add(resource); resourceType = resourceTypeModel.GetWhere((dbResourceType) => dbResourceType.DisplayName == "gitlab"); var resource2 = new Resource() { DisplayName = "ResourceTest2", ResourceName = "ResourceTest2", Keywords = "ResourceTest2", UsageRights = "ResourceTest2", TypeId = resourceType.Id, Type = resourceType, Visibility = Visibility, VisibilityId = Visibility.Id, LicenseId = License.Id }; resourceModel.Insert(resource2); projectModel.AddResource(project2, resource2); Resources.Add(resource2); ResourceDisciplineModel resourceDisciplineModel = new ResourceDisciplineModel(); ResourceDiscipline resourceDiscipline = new ResourceDiscipline() { DisciplineId = Discipline.Id, ResourceId = resource.Id }; resourceDisciplineModel.Insert(resourceDiscipline); ResourceDisciplines.Add(resourceDiscipline); resourceDiscipline = new ResourceDiscipline() { DisciplineId = Discipline.Id, ResourceId = resource2.Id }; resourceDisciplineModel.Insert(resourceDiscipline); ResourceDisciplines.Add(resourceDiscipline); } protected void FakeControllerContext(User user, Stream stream = null) { var request = new Mock(); JWTHandler jwtHandler = new JWTHandler(Program.Configuration); Dictionary values = new Dictionary { { "UserId", user.Id.ToString() } }; var additionalProtocol = Program.Configuration.GetStringAndWait("coscine/local/sharepoint/additional/protocol"); var additionalPort = Program.Configuration.GetStringAndWait("coscine/local/sharepoint/additional/port"); var additionalSite = Program.Configuration.GetStringAndWait("coscine/local/sharepoint/additional/site"); var sharePointSite = "https://"; if (additionalProtocol != null) { sharePointSite = additionalProtocol + "://"; } //initialize the select query with command text ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT Name FROM Win32_ComputerSystem"); //execute the query foreach (ManagementObject process in searcher.Get()) { //print system info process.Get(); sharePointSite = sharePointSite + process["Name"] + "." + process["Domain"]; } if (additionalPort != null) { sharePointSite += ":" + additionalPort; } if (additionalSite != null) { sharePointSite += "/" + additionalSite; } request.SetupGet(x => x.Headers).Returns( new HeaderDictionary { {"X-Requested-With", "XMLHttpRequest"}, {"Authorization", "Bearer " + jwtHandler.GenerateJwtToken(values)}, {"Referer", sharePointSite } } ); var context = new Mock(); context.SetupGet(x => x.Request).Returns(request.Object); var claimsPrincipal = new Mock(); Claim claim = new Claim("UserID", user.Id.ToString()); context.SetupGet(x => x.User).Returns(claimsPrincipal.Object); context.Setup(x => x.User.FindFirst("UserID")).Returns(claim); if (stream != null) { context.SetupGet(x => x.Request.Method).Returns("POST"); context.SetupGet(x => x.Request.HttpContext).Returns(context.Object); context.SetupGet(x => x.Request.Body).Returns(stream); context.SetupGet(x => x.Request.ContentLength).Returns(stream.Length); context.SetupGet(x => x.Request.ContentType).Returns("application/json"); } var actionDescriptor = new Mock(); Controller.ControllerContext = new ControllerContext(new ActionContext(context.Object, new RouteData(), actionDescriptor.Object)); } [OneTimeTearDown] public void End() { ProjectRoleModel projectRoleModel = new ProjectRoleModel(); foreach (var projectRole in ProjectRoles) { projectRoleModel.Delete(projectRole); } ProjectResourceModel projectResourceModel = new ProjectResourceModel(); foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == Resources[0].Id || projectResource.ResourceId == Resources[1].Id)) { projectResourceModel.Delete(projectResource); } ProjectController projectController = new ProjectController(); foreach (var project in Projects) { projectController.DeleteProject(project, true, false); } UserModel userModel = new UserModel(); foreach (var user in Users) { userModel.Delete(user); } ResourceDisciplineModel resourceDisciplineModel = new ResourceDisciplineModel(); foreach (var resourceDiscipline in ResourceDisciplines) { resourceDisciplineModel.Delete(resourceDiscipline); } ResourceModel resourceModel = new ResourceModel(); foreach (var resource in Resources) { resourceModel.DeleteResource(resource); } ResourceTypeModel resourceTypeModel = new ResourceTypeModel(); foreach (var resourceType in ResourceTypes) { resourceTypeModel.Delete(resourceType); } DisciplineModel disciplineModel = new DisciplineModel(); disciplineModel.Delete(Discipline); InstituteModel instituteModel = new InstituteModel(); instituteModel.Delete(Institute); LicenseModel licenseModel = new LicenseModel(); licenseModel.Delete(License); RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel(); rdsResourceTypeModel.Delete(RdsResourceType); } } }