DefaultControllerTests.cs 12.7 KB
Newer Older
1
using Coscine.Api.Project.Controllers;
Marcel Nellesen's avatar
Marcel Nellesen committed
2
using Coscine.Database.Models;
3
using Coscine.ApiCommons.Utils;
Marcel Nellesen's avatar
Marcel Nellesen committed
4
using Coscine.Database.DataModel;
5
6
7
8
9
10
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;
Marcel Nellesen's avatar
Marcel Nellesen committed
17
18
19
using LinqToDB.Data;
using Coscine.Database.Settings;
using Coscine.Configuration;
20
21
22
23
24
25
26

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

Marcel Nellesen's avatar
Marcel Nellesen committed
27
        protected readonly List<Coscine.Database.DataModel.Project> Projects = new List<Coscine.Database.DataModel.Project>();
L. Ellenbeck's avatar
L. Ellenbeck committed
28
29
30
31
        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>();
32
33
34
35
36
37
38
        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; }
39

L. Ellenbeck's avatar
L. Ellenbeck committed
40
        protected int Previous;
41

L. Ellenbeck's avatar
L. Ellenbeck committed
42
        protected DefaultControllerTests(T controller)
43
44
45
46
47
48
49
        {
            Controller = controller;
        }

        [OneTimeSetUp]
        public void Setup()
        {
Marcel Nellesen's avatar
Marcel Nellesen committed
50
            DataConnection.DefaultSettings = new CoscineSettings(new ConsulConfiguration());
51
52
53
            DisciplineModel disciplineModel = new DisciplineModel();
            Discipline = new Discipline()
            {
54
55
                DisplayNameDe = "TestDiscipline",
                DisplayNameEn = "TestDiscipline",
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                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);

78
            ProjectModel projectModel = new ProjectModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
79
            Previous = projectModel.GetAll().ToArray().Length;
80
81
82

            UserModel userModel = new UserModel();

83
84
85
86
87
            var user = new User()
            {
                DisplayName = "TestUser",
                EmailAddress = "testUser@test.com",
            };
88
89

            userModel.Insert(user);
L. Ellenbeck's avatar
L. Ellenbeck committed
90
            Users.Add(user);
91
92
93

            FakeControllerContext(user);

Marcel Nellesen's avatar
Marcel Nellesen committed
94
            var project = new Coscine.Database.DataModel.Project()
95
96
            {
                Description = "Description",
97
                ProjectName = "TestProject",
98
99
100
                DisplayName = "TestProject",
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddYears(1),
101
102
103
104
105
106
                Keywords = "Test1;Test2",
                
                GrantId = "testid",
                PrincipleInvestigators = "TestInvestigator",

                VisibilityId = Visibility.Id
107
108
109
110
            };

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

113
114
115
116
117
118
119
120
121
122
123
124
125
126
            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
127
            Projects.Add(projectModel.GetById(project.Id));
128

Marcel Nellesen's avatar
Marcel Nellesen committed
129
            var project2 = new Coscine.Database.DataModel.Project()
130
131
            {
                Description = "Description2",
132
                ProjectName = "TestProject",
133
134
135
                DisplayName = "TestProject2",
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddYears(1),
136
137
138
139
140
141
                Keywords = "Test1;Test2",

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

                VisibilityId = Visibility.Id
142
143
            };
            projectModel.Insert(project2);
L. Ellenbeck's avatar
L. Ellenbeck committed
144
            Projects.Add(projectModel.GetById(project2.Id));
145
146

            ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
147
148
149
150
            var resourceType = resourceTypeModel.GetWhere((dbResourceType) => dbResourceType.DisplayName == "rds");

            RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
            RdsResourceType = new RDSResourceType()
151
            {
152
                BucketName = "c",
153
                Size = 25,
154
            };
155
            rdsResourceTypeModel.Insert(RdsResourceType);
156
157
158
159

            ResourceModel resourceModel = new ResourceModel();
            var resource = new Resource()
            {
160
                DisplayName = "ResourceTest1",
161
162
163
                ResourceName = "ResourceTest1",
                Keywords = "ResourceTest1",
                UsageRights = "ResourceTest1",
164
                TypeId = resourceType.Id,
165
166
167
168
169
170
171
                Type = resourceType,

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

                ResourceTypeOptionId = RdsResourceType.Id
172
173
174
            };
            resourceModel.Insert(resource);
            projectModel.AddResource(project, resource);
L. Ellenbeck's avatar
L. Ellenbeck committed
175
            Resources.Add(resource);
176

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

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

            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);
210
211
212
213
214
215
216
217
218
219
220
221
222
        }


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

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
250
251
252
253
            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;
            }

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

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

265
266
267
268
269
            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);

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

            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);
334
335
336
        }
    }
}