DefaultControllerTests.cs 12.5 KB
Newer Older
1 2
using Coscine.Api.Project.Controllers;
using Coscine.Api.Project.Models;
3
using Coscine.ApiCommons.Utils;
4 5 6 7 8 9 10
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;
11
using System;
12 13 14
using System.Collections.Generic;
using System.IO;
using System.Linq;
15
using System.Management;
16
using System.Security.Claims;
17 18 19 20 21 22 23

namespace Coscine.Api.Project.Tests
{
    public abstract class DefaultControllerTests<T> where T : Controller
    {
        protected T Controller { get; }

L. Ellenbeck's avatar
L. Ellenbeck committed
24 25 26 27 28
        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>();
29 30 31 32 33 34 35
        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; }
36

L. Ellenbeck's avatar
L. Ellenbeck committed
37
        protected int Previous;
38

L. Ellenbeck's avatar
L. Ellenbeck committed
39
        protected DefaultControllerTests(T controller)
40 41 42 43 44 45 46
        {
            Controller = controller;
        }

        [OneTimeSetUp]
        public void Setup()
        {
47 48 49
            DisciplineModel disciplineModel = new DisciplineModel();
            Discipline = new Discipline()
            {
50 51
                DisplayNameDe = "TestDiscipline",
                DisplayNameEn = "TestDiscipline",
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
                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);

74
            ProjectModel projectModel = new ProjectModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
75
            Previous = projectModel.GetAll().ToArray().Length;
76 77 78

            UserModel userModel = new UserModel();

79 80 81 82 83
            var user = new User()
            {
                DisplayName = "TestUser",
                EmailAddress = "testUser@test.com",
            };
84 85

            userModel.Insert(user);
L. Ellenbeck's avatar
L. Ellenbeck committed
86
            Users.Add(user);
87 88 89 90 91 92

            FakeControllerContext(user);

            var project = new Coscine.Database.Model.Project()
            {
                Description = "Description",
93
                ProjectName = "TestProject",
94 95 96
                DisplayName = "TestProject",
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddYears(1),
97 98 99 100 101 102
                Keywords = "Test1;Test2",
                
                GrantId = "testid",
                PrincipleInvestigators = "TestInvestigator",

                VisibilityId = Visibility.Id
103 104 105 106
            };

            projectModel.Insert(project);
            var projectRole = projectModel.SetOwner(project, user);
L. Ellenbeck's avatar
L. Ellenbeck committed
107
            ProjectRoles.Add(projectRole);
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122
            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
            });

L. Ellenbeck's avatar
L. Ellenbeck committed
123
            Projects.Add(projectModel.GetById(project.Id));
124 125 126 127

            var project2 = new Coscine.Database.Model.Project()
            {
                Description = "Description2",
128
                ProjectName = "TestProject",
129 130 131
                DisplayName = "TestProject2",
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddYears(1),
132 133 134 135 136 137
                Keywords = "Test1;Test2",

                GrantId = "testid",
                PrincipleInvestigators = "TestInvestigator",

                VisibilityId = Visibility.Id
138 139
            };
            projectModel.Insert(project2);
L. Ellenbeck's avatar
L. Ellenbeck committed
140
            Projects.Add(projectModel.GetById(project2.Id));
141 142

            ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
143 144 145 146
            var resourceType = resourceTypeModel.GetWhere((dbResourceType) => dbResourceType.DisplayName == "rds");

            RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
            RdsResourceType = new RDSResourceType()
147
            {
148
                BucketName = "c",
149
                Size = 25,
150
            };
151
            rdsResourceTypeModel.Insert(RdsResourceType);
152 153 154 155

            ResourceModel resourceModel = new ResourceModel();
            var resource = new Resource()
            {
156
                DisplayName = "ResourceTest1",
157 158 159
                ResourceName = "ResourceTest1",
                Keywords = "ResourceTest1",
                UsageRights = "ResourceTest1",
160
                TypeId = resourceType.Id,
161 162 163 164 165 166 167
                Type = resourceType,

                Visibility = Visibility,
                VisibilityId = Visibility.Id,
                LicenseId = License.Id,

                ResourceTypeOptionId = RdsResourceType.Id
168 169 170
            };
            resourceModel.Insert(resource);
            projectModel.AddResource(project, resource);
L. Ellenbeck's avatar
L. Ellenbeck committed
171
            Resources.Add(resource);
172

173
            resourceType = resourceTypeModel.GetWhere((dbResourceType) => dbResourceType.DisplayName == "gitlab");
174 175
            var resource2 = new Resource()
            {
176
                DisplayName = "ResourceTest2",
177 178 179
                ResourceName = "ResourceTest2",
                Keywords = "ResourceTest2",
                UsageRights = "ResourceTest2",
180
                TypeId = resourceType.Id,
181 182 183 184 185
                Type = resourceType,

                Visibility = Visibility,
                VisibilityId = Visibility.Id,
                LicenseId = License.Id
186 187 188
            };
            resourceModel.Insert(resource2);
            projectModel.AddResource(project2, resource2);
L. Ellenbeck's avatar
L. Ellenbeck committed
189
            Resources.Add(resource2);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

            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);
206 207 208 209 210 211 212 213 214 215 216 217 218
        }


        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() }
            };

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            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;
            }

250 251 252
            request.SetupGet(x => x.Headers).Returns(
                new HeaderDictionary {
                    {"X-Requested-With", "XMLHttpRequest"},
253 254
                    {"Authorization", "Bearer " + jwtHandler.GenerateJwtToken(values)},
                    {"Referer", sharePointSite }
255 256 257 258 259 260
                }
            );

            var context = new Mock<HttpContext>();
            context.SetupGet(x => x.Request).Returns(request.Object);

261 262 263 264 265
            var claimsPrincipal = new Mock<ClaimsPrincipal>();
            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);

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
            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();
L. Ellenbeck's avatar
L. Ellenbeck committed
284
            foreach (var projectRole in ProjectRoles)
285 286 287 288
            {
                projectRoleModel.Delete(projectRole);
            }
            ProjectResourceModel projectResourceModel = new ProjectResourceModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
289
            foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == Resources[0].Id || projectResource.ResourceId == Resources[1].Id))
290 291 292
            {
                projectResourceModel.Delete(projectResource);
            }
293
            ProjectController projectController = new ProjectController();
L. Ellenbeck's avatar
L. Ellenbeck committed
294
            foreach (var project in Projects)
295
            {
296
                projectController.DeleteProject(project);
297 298
            }
            UserModel userModel = new UserModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
299
            foreach (var user in Users)
300 301 302
            {
                userModel.Delete(user);
            }
303 304 305 306 307
            ResourceDisciplineModel resourceDisciplineModel = new ResourceDisciplineModel();
            foreach (var resourceDiscipline in ResourceDisciplines)
            {
                resourceDisciplineModel.Delete(resourceDiscipline);
            }
308
            ResourceModel resourceModel = new ResourceModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
309
            foreach (var resource in Resources)
310
            {
311
                resourceModel.DeleteResource(resource);
312 313
            }
            ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
314
            foreach (var resourceType in ResourceTypes)
315 316 317
            {
                resourceTypeModel.Delete(resourceType);
            }
318 319 320 321 322 323 324 325 326 327 328 329

            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);
330 331 332
        }
    }
}