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

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

L. Ellenbeck's avatar
L. Ellenbeck committed
22
23
24
25
26
        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>();
27

L. Ellenbeck's avatar
L. Ellenbeck committed
28
        protected int Previous;
29

L. Ellenbeck's avatar
L. Ellenbeck committed
30
        protected DefaultControllerTests(T controller)
31
32
33
34
35
36
37
38
        {
            Controller = controller;
        }

        [OneTimeSetUp]
        public void Setup()
        {
            ProjectModel projectModel = new ProjectModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
39
            Previous = projectModel.GetAll().ToArray().Length;
40
41
42

            UserModel userModel = new UserModel();

43
44
45
46
47
            var user = new User()
            {
                DisplayName = "TestUser",
                EmailAddress = "testUser@test.com",
            };
48
49

            userModel.Insert(user);
L. Ellenbeck's avatar
L. Ellenbeck committed
50
            Users.Add(user);
51
52
53
54
55
56
57

            FakeControllerContext(user);

            var project = new Coscine.Database.Model.Project()
            {
                Description = "Description",
                Organization = "Organization",
58
59
60
61
                DisplayName = "TestProject",
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddYears(1),
                Keywords = "Test1;Test2"
62
63
64
65
            };

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

L. Ellenbeck's avatar
L. Ellenbeck committed
68
            Projects.Add(projectModel.GetById(project.Id));
69
70
71
72
73

            var project2 = new Coscine.Database.Model.Project()
            {
                Description = "Description2",
                Organization = "Organization2",
74
75
76
77
                DisplayName = "TestProject2",
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddYears(1),
                Keywords = "Test1;Test2"
78
79
            };
            projectModel.Insert(project2);
L. Ellenbeck's avatar
L. Ellenbeck committed
80
            Projects.Add(projectModel.GetById(project2.Id));
81
82
83
84
85
86
87

            ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
            var resourceType = new ResourceType()
            {
                DisplayName = "TestType"
            };
            resourceTypeModel.Insert(resourceType);
L. Ellenbeck's avatar
L. Ellenbeck committed
88
            ResourceTypes.Add(resourceType);
89
90
91
92
93
94

            ResourceModel resourceModel = new ResourceModel();
            var resource = new Resource()
            {
                ExternalId = "123",
                Url = "http://rwth-aachen.de/test",
95
                DisplayName = "ResourceTest1",
96
97
98
99
100
                TypeId = resourceType.Id,
                Type = resourceType
            };
            resourceModel.Insert(resource);
            projectModel.AddResource(project, resource);
L. Ellenbeck's avatar
L. Ellenbeck committed
101
            Resources.Add(resource);
102
103
104
105
106

            var resource2 = new Resource()
            {
                ExternalId = "12345",
                Url = "http://rwth-aachen.de/test2",
107
                DisplayName = "ResourceTest2",
108
109
110
111
112
                TypeId = resourceType.Id,
                Type = resourceType
            };
            resourceModel.Insert(resource2);
            projectModel.AddResource(project2, resource2);
L. Ellenbeck's avatar
L. Ellenbeck committed
113
            Resources.Add(resource2);
114
115
116
117
118
119
120
121
122
123
124
125
126
        }


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

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
            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;
            }

158
159
160
            request.SetupGet(x => x.Headers).Returns(
                new HeaderDictionary {
                    {"X-Requested-With", "XMLHttpRequest"},
161
162
                    {"Authorization", "Bearer " + jwtHandler.GenerateJwtToken(values)},
                    {"Referer", sharePointSite }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
                }
            );

            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
187
            foreach (var projectRole in ProjectRoles)
188
189
190
191
            {
                projectRoleModel.Delete(projectRole);
            }
            ProjectResourceModel projectResourceModel = new ProjectResourceModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
192
            foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == Resources[0].Id || projectResource.ResourceId == Resources[1].Id))
193
194
195
196
            {
                projectResourceModel.Delete(projectResource);
            }
            ProjectModel projectModel = new ProjectModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
197
            foreach (var project in Projects)
198
199
200
201
            {
                projectModel.Delete(project);
            }
            UserModel userModel = new UserModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
202
            foreach (var user in Users)
203
204
205
206
            {
                userModel.Delete(user);
            }
            ResourceModel resourceModel = new ResourceModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
207
            foreach (var resource in Resources)
208
209
210
211
            {
                resourceModel.Delete(resource);
            }
            ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
212
            foreach (var resourceType in ResourceTypes)
213
214
215
216
217
218
            {
                resourceTypeModel.Delete(resourceType);
            }
        }
    }
}