DefaultControllerTests.cs 7.63 KB
Newer Older
1
2
3
4
5
6
7
8
using Coscine.Api.Project.Models;
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;
9
using System;
10
11
12
using System.Collections.Generic;
using System.IO;
using System.Linq;
13
using System.Management;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

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

        protected List<Coscine.Database.Model.Project> projects = new List<Coscine.Database.Model.Project>();
        protected List<User> users = new List<User>();
        protected List<ProjectRole> projectRoles = new List<ProjectRole>();
        protected List<ResourceType> resourceTypes = new List<ResourceType>();
        protected List<Resource> resources = new List<Resource>();

        protected int previous;

        public DefaultControllerTests(T controller)
        {
            Controller = controller;
        }

        [OneTimeSetUp]
        public void Setup()
        {
            ProjectModel projectModel = new ProjectModel();
            previous = projectModel.GetAll().ToArray().Length;

            UserModel userModel = new UserModel();

            var user = new User();

            userModel.Insert(user);
            users.Add(user);

            FakeControllerContext(user);

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

            projectModel.Insert(project);
            var projectRole = projectModel.SetOwner(project, user);
            projectRoles.Add(projectRole);

            projects.Add(projectModel.GetById(project.Id));

            var project2 = new Coscine.Database.Model.Project()
            {
                Description = "Description2",
                Organization = "Organization2",
69
70
71
72
                DisplayName = "TestProject2",
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddYears(1),
                Keywords = "Test1;Test2"
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
            };
            projectModel.Insert(project2);
            projects.Add(projectModel.GetById(project2.Id));

            ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
            var resourceType = new ResourceType()
            {
                DisplayName = "TestType"
            };
            resourceTypeModel.Insert(resourceType);
            resourceTypes.Add(resourceType);

            ResourceModel resourceModel = new ResourceModel();
            var resource = new Resource()
            {
                ExternalId = "123",
                Url = "http://rwth-aachen.de/test",
                TypeId = resourceType.Id,
                Type = resourceType
            };
            resourceModel.Insert(resource);
            projectModel.AddResource(project, resource);
            resources.Add(resource);

            var resource2 = new Resource()
            {
                ExternalId = "12345",
                Url = "http://rwth-aachen.de/test2",
                TypeId = resourceType.Id,
                Type = resourceType
            };
            resourceModel.Insert(resource2);
            projectModel.AddResource(project2, resource2);
            resources.Add(resource2);
        }


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

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
            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;
            }

151
152
153
            request.SetupGet(x => x.Headers).Returns(
                new HeaderDictionary {
                    {"X-Requested-With", "XMLHttpRequest"},
154
155
                    {"Authorization", "Bearer " + jwtHandler.GenerateJwtToken(values)},
                    {"Referer", sharePointSite }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
                }
            );

            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();
            foreach (var projectRole in projectRoles)
            {
                projectRoleModel.Delete(projectRole);
            }
            ProjectResourceModel projectResourceModel = new ProjectResourceModel();
            foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == resources[0].Id || projectResource.ResourceId == resources[1].Id))
            {
                projectResourceModel.Delete(projectResource);
            }
            ProjectModel projectModel = new ProjectModel();
            foreach (var project in projects)
            {
                projectModel.Delete(project);
            }
            UserModel userModel = new UserModel();
            foreach (var user in users)
            {
                userModel.Delete(user);
            }
            ResourceModel resourceModel = new ResourceModel();
            foreach (var resource in resources)
            {
                resourceModel.Delete(resource);
            }
            ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
            foreach (var resourceType in resourceTypes)
            {
                resourceTypeModel.Delete(resourceType);
            }
        }
    }
}