ProjectControllerTests.cs 12.7 KB
Newer Older
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
1 2 3
using NUnit.Framework;
using Coscine.Api.Project.Controllers;
using Microsoft.AspNetCore.Mvc;
4 5
using Coscine.Api.Project.Models;
using System.Linq;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
6
using Coscine.Api.Project.ReturnObjects;
7 8
using System.IO;
using System;
9 10
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
11
using System.Collections.Generic;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
12

L. Ellenbeck's avatar
L. Ellenbeck committed
13
#region DupFinder Exclusion
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
14 15 16
namespace Coscine.Api.Project.Tests
{
    [TestFixture]
17
    public class ProjectControllerTests : DefaultControllerTests<ProjectController>
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
18
    {
19
        public ProjectControllerTests() : base(new ProjectController())
20 21 22 23
        {

        }

Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
24
        [Test]
25
        public void OwnsTest()
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
26
        {
27
            ProjectModel projectModel = new ProjectModel();
28 29
            Assert.IsTrue(projectModel.HasAccess(Users[0], Projects[0], UserRoles.Owner));
            Assert.IsFalse(projectModel.HasAccess(Users[0], Projects[1], UserRoles.Owner));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
30

31 32
            var all = projectModel.GetAllWhere((project) =>
                (from projectRole in project.ProjectRolesProjectIdIds
L. Ellenbeck's avatar
L. Ellenbeck committed
33
                 where projectRole.User == Users[0]
34 35 36 37 38
                     && projectRole.Role.DisplayName == "Owner"
                 select projectRole).Any()
            );
            Assert.IsTrue(all.ToArray().Length == 1);
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
39

40
        [Test]
41
        public void TestControllerIndex()
42
        {
43
            var actionResult = Controller.Index();
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
44
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
45
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
46

47 48 49
        [Test]
        public void TestControllerGet()
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
50
            var actionResult = Controller.Get(Projects[0].Id.ToString());
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
51 52
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));

53
            OkObjectResult okObjectResult = (OkObjectResult)actionResult;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
54 55
            Assert.IsTrue(okObjectResult.Value.GetType() == typeof(ProjectObject));

56
            ProjectObject projectObject = (ProjectObject)okObjectResult.Value;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
57

L. Ellenbeck's avatar
L. Ellenbeck committed
58 59 60 61 62 63
            Assert.IsTrue(projectObject.Id == Projects[0].Id);
            Assert.IsTrue(projectObject.Description == Projects[0].Description);
            Assert.IsTrue(projectObject.DisplayName == Projects[0].DisplayName);
            Assert.IsTrue(projectObject.StartDate == Projects[0].StartDate);
            Assert.IsTrue(projectObject.EndDate == Projects[0].EndDate);
            Assert.IsTrue(projectObject.Keywords == Projects[0].Keywords);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
64
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
65

66

67

68 69 70
        [Test]
        public void TestControllerUpdate()
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
71 72
            var actionResult = Controller.Get(Projects[0].Id.ToString());
            ProjectObject projectObject = (ProjectObject)((OkObjectResult)actionResult).Value;
73 74 75 76 77
            
            projectObject.Description = "ChangedDescription";

            Stream stream = ObjectFactory<ProjectObject>.SerializeToStream(projectObject);

L. Ellenbeck's avatar
L. Ellenbeck committed
78
            FakeControllerContext(Users[0], stream);
79

L. Ellenbeck's avatar
L. Ellenbeck committed
80
            actionResult = Controller.Update(Projects[0].Id.ToString());
81 82 83 84 85 86 87
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));

            // Cleanup
            stream.Close();

            stream = ObjectFactory<ProjectObject>.SerializeToStream(projectObject);

L. Ellenbeck's avatar
L. Ellenbeck committed
88
            FakeControllerContext(Users[0], stream);
89

90 91
            actionResult = Controller.Update(Projects[1].Id.ToString());
            Assert.IsTrue(actionResult.GetType() == typeof(UnauthorizedObjectResult));
92 93 94 95 96 97 98

            // Cleanup
            stream.Close();
        }

        [Test]
        public void TestControllerStore()
99 100
        {
            ProjectObject newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId", 
101
                                                                new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
102 103
                                                                new List<InstituteObject>() { new InstituteObject(Institute.Id, Institute.IKZ, Institute.DisplayName) },
                                                                new VisibilityObject(Visibility.Id, Visibility.DisplayName));
104 105 106

            var stream = ObjectFactory<ProjectObject>.SerializeToStream(newProjectObject);

L. Ellenbeck's avatar
L. Ellenbeck committed
107
            FakeControllerContext(Users[0], stream);
108 109

            var actionResult = Controller.Store();
110
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
111

112 113
            JsonResult jsonResult = (JsonResult)actionResult;
            Assert.IsTrue(jsonResult.Value.GetType() == typeof(ProjectObject));
114

115
            ProjectObject createdProjectObject = (ProjectObject)jsonResult.Value;
116 117 118

            Assert.IsTrue(createdProjectObject.Description == newProjectObject.Description);
            Assert.IsTrue(createdProjectObject.DisplayName == newProjectObject.DisplayName);
119 120 121
            Assert.IsTrue(createdProjectObject.StartDate == newProjectObject.StartDate);
            Assert.IsTrue(createdProjectObject.EndDate == newProjectObject.EndDate);
            Assert.IsTrue(createdProjectObject.Keywords == newProjectObject.Keywords);
122 123 124 125 126 127

            // Cleanup
            stream.Close();

            ProjectModel projectModel = new ProjectModel();
            var project = projectModel.GetById(createdProjectObject.Id);
128 129 130 131 132 133 134 135
            Controller.DeleteProject(project);
        }


        [Test]
        public void TestControllerDelete()
        {
            ProjectObject newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId",
136
                                                                new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
137 138 139 140 141 142 143 144
                                                                new List<InstituteObject>() { new InstituteObject(Institute.Id, Institute.IKZ, Institute.DisplayName) },
                                                                new VisibilityObject(Visibility.Id, Visibility.DisplayName));

            var stream = ObjectFactory<ProjectObject>.SerializeToStream(newProjectObject);

            FakeControllerContext(Users[0], stream);

            var actionResult = Controller.Store();
145
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
146

147 148
            JsonResult result = (JsonResult)actionResult;
            Assert.IsTrue(result.Value.GetType() == typeof(ProjectObject));
149

150
            ProjectObject createdProjectObject = (ProjectObject)result.Value;
151 152 153 154 155 156 157
            stream.Close();

            stream = ObjectFactory<ProjectObject>.SerializeToStream(createdProjectObject);

            FakeControllerContext(Users[0], stream);

            actionResult = Controller.Delete(createdProjectObject.Id.ToString());
158
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
159

160 161
            result = (JsonResult)actionResult;
            Assert.IsTrue(result.Value.GetType() == typeof(ProjectObject));
162 163 164 165 166 167 168 169

            stream.Close();
        }
        
        [Test]
        public void TestControllerDeleteWithSubProjects()
        {
            ProjectObject newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId",
170
                                                                new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
171 172 173 174 175 176 177 178 179
                                                                new List<InstituteObject>() { new InstituteObject(Institute.Id, Institute.IKZ, Institute.DisplayName) },
                                                                new VisibilityObject(Visibility.Id, Visibility.DisplayName));

            var stream = ObjectFactory<ProjectObject>.SerializeToStream(newProjectObject);

            FakeControllerContext(Users[0], stream);

            var actionResult = Controller.Store();

180 181
            JsonResult result = (JsonResult)actionResult;
            ProjectObject createdProjectObject = (ProjectObject)result.Value;
182 183 184
            stream.Close();

            newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId",
185
                                                                new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                                                                new List<InstituteObject>() { new InstituteObject(Institute.Id, Institute.IKZ, Institute.DisplayName) },
                                                                new VisibilityObject(Visibility.Id, Visibility.DisplayName),
                                                                createdProjectObject.Id);

            stream = ObjectFactory<ProjectObject>.SerializeToStream(newProjectObject);

            FakeControllerContext(Users[0], stream);

            actionResult = Controller.Store();

            stream = ObjectFactory<ProjectObject>.SerializeToStream(createdProjectObject);

            FakeControllerContext(Users[0], stream);

            actionResult = Controller.Delete(createdProjectObject.Id.ToString());
201
            Assert.IsTrue(actionResult.GetType() == typeof(JsonResult));
202

203 204
            result = (JsonResult)actionResult;
            Assert.IsTrue(result.Value.GetType() == typeof(ProjectObject));
205 206

            stream.Close();
207 208
        }

209 210 211
        [Test]
        public void TestControllerStoreWithSubProject()
        {
212
            ProjectObject newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId",
213
                                                                    new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
214 215
                                                                    new List<InstituteObject>() { new InstituteObject(Institute.Id, Institute.IKZ, Institute.DisplayName) },
                                                                    new VisibilityObject(Visibility.Id, Visibility.DisplayName));
216 217 218 219 220

            var stream = ObjectFactory<ProjectObject>.SerializeToStream(newProjectObject);
            FakeControllerContext(Users[0], stream);
            var actionResult = Controller.Store();

221 222
            JsonResult result = (JsonResult)actionResult;
            ProjectObject createdProjectObject = (ProjectObject)result.Value;
223

224
            ProjectObject newSubProjectObject = new ProjectObject(Guid.NewGuid(), "NewSubProject", "NewDisplayNameSub", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId",
225
                                                                        new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
226 227 228
                                                                        new List<InstituteObject>() { new InstituteObject(Institute.Id, Institute.IKZ, Institute.DisplayName) },
                                                                        new VisibilityObject(Visibility.Id, Visibility.DisplayName), 
                                                                        createdProjectObject.Id);
229 230 231 232 233

            var subStream = ObjectFactory<ProjectObject>.SerializeToStream(newSubProjectObject);
            FakeControllerContext(Users[0], subStream);
            var subActionResult = Controller.Store();

234 235
            JsonResult resultSubProject = (JsonResult)subActionResult;
            ProjectObject createdSubProjectObject = (ProjectObject)resultSubProject.Value;
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

            SubProjectModel subProjectModel = new SubProjectModel();
            var subProjects = subProjectModel.GetAllWhere((x) => x.ProjectId == createdProjectObject.Id);
            foreach(var subProject in subProjects)
            {
                Assert.IsTrue(subProject.SubProjectId == createdSubProjectObject.Id);
                Assert.IsTrue(subProject.ProjectId == createdProjectObject.Id);
            }

            // Cleanup
            stream.Close();
            subStream.Close();

            ProjectModel projectModel = new ProjectModel();
            var project = projectModel.GetById(createdProjectObject.Id);
251
            Controller.DeleteProject(project);
252 253
        }

254 255 256 257
        [Test]
        public void CountTest()
        {
            ProjectModel projectModel = new ProjectModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
258
            Assert.IsTrue(Previous == projectModel.GetAll().ToArray().Length - Projects.Count);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
259 260 261
        }
    }
}
L. Ellenbeck's avatar
L. Ellenbeck committed
262
#endregion