DefaultControllerTests.cs 12.3 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
            DisciplineModel disciplineModel = new DisciplineModel();
            Discipline = new Discipline()
            {
49
50
                DisplayNameDe = "TestDiscipline",
                DisplayNameEn = "TestDiscipline",
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                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);

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

            UserModel userModel = new UserModel();

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

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

            FakeControllerContext(user);

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

                VisibilityId = Visibility.Id
102
103
104
105
            };

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

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

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

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

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

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

            RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
            RdsResourceType = new RDSResourceType()
146
            {
147
148
149
                AccessKey = "a",
                SecretKey = "b",
                BucketName = "c",
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
                }
            );

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

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