DefaultControllerTests.cs 12.2 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
17
18
19
20
21
22

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

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

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

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

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

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

            UserModel userModel = new UserModel();

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

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

            FakeControllerContext(user);

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

                VisibilityId = Visibility.Id
101
102
103
104
            };

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

107
108
109
110
111
112
113
114
115
116
117
118
119
120
            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
121
            Projects.Add(projectModel.GetById(project.Id));
122
123
124
125

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

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

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

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

            RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
            RdsResourceType = new RDSResourceType()
145
            {
146
147
148
                AccessKey = "a",
                SecretKey = "b",
                BucketName = "c",
149
            };
150
            rdsResourceTypeModel.Insert(RdsResourceType);
151
152
153
154

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

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

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

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

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

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


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

218
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
            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;
            }

249
250
251
            request.SetupGet(x => x.Headers).Returns(
                new HeaderDictionary {
                    {"X-Requested-With", "XMLHttpRequest"},
252
253
                    {"Authorization", "Bearer " + jwtHandler.GenerateJwtToken(values)},
                    {"Referer", sharePointSite }
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
                }
            );

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

            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);
324
325
326
        }
    }
}