Skip to content
Snippets Groups Projects
Select Git revision
  • f50bd8f735eb32d41be1ed57b2aeaf6ea48810ea
  • master default protected
  • gitkeep
  • dev protected
  • Issue/2449-GuidPidSlugToProjectSettings
  • Issue/2309-docs
  • Issue/2355-topLevelOrg
  • Issue/2328-noFailOnLog
  • Hotfix/2371-fixGitLabinRCV
  • Issue/2287-guestRole
  • Fix/xxxx-activateGitlab
  • Test/xxxx-enablingGitLab
  • Issue/2349-gitlabHttps
  • Issue/2259-updatePids
  • Issue/2101-gitLabResTypeUi
  • Hotfix/2202-fixNaNQuota
  • Issue/2246-quotaResoval
  • Issue/2221-projectDateCreated
  • Hotfix/2224-quotaSizeAnalytics
  • Fix/xxxx-resourceVisibility
  • Issue/2000-gitlabResourcesAPI
  • v4.4.3
  • v4.4.2
  • v4.4.1
  • v4.4.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
  • v4.3.0
  • v4.2.8
  • v4.2.7
  • v4.2.6
  • v4.2.5
  • v4.2.4
  • v4.2.3
  • v4.2.2
  • v4.2.1
  • v4.2.0
  • v4.1.1
  • v4.1.0
41 results

DefaultControllerTests.cs

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    DefaultControllerTests.cs 12.23 KiB
    using Coscine.Api.Project.Controllers;
    using Coscine.Api.Project.Models;
    using Coscine.ApiCommons.Utils;
    using Coscine.Database.Model;
    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;
    
    namespace Coscine.Api.Project.Tests
    {
        public abstract class DefaultControllerTests<T> where T : Controller
        {
            protected T Controller { get; }
    
            protected readonly List<Coscine.Database.Model.Project> Projects = new List<Coscine.Database.Model.Project>();
            protected readonly List<User> Users = new List<User>();
            protected readonly List<ProjectRole> ProjectRoles = new List<ProjectRole>();
            protected readonly List<ResourceType> ResourceTypes = new List<ResourceType>();
            protected readonly List<Resource> Resources = new List<Resource>();
            protected readonly List<ResourceDiscipline> ResourceDisciplines = new List<ResourceDiscipline>();
    
            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()
            {
                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.Model.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.Model.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<HttpRequest>();
    
                JWTHandler jwtHandler = new JWTHandler(Program.Configuration);
                Dictionary<string, string> values = new Dictionary<string, string>
                {
                    { "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<HttpContext>();
                context.SetupGet(x => x.Request).Returns(request.Object);
    
                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<ControllerActionDescriptor>();
    
                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);
                }
                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);
            }
        }
    }