ProjectControllerTests.cs 4.73 KB
Newer Older
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
1
2
3
4
using NUnit.Framework;
using Coscine.Api.Project.Controllers;
using Microsoft.AspNetCore.Mvc;
using static Coscine.Api.Project.Controllers.ProjectController;
5
6
7
8
9
10
11
using Coscine.Api.Project.Models;
using System.Collections.Generic;
using Coscine.Database.Model;
using System.Linq;
using Moq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
12
13
14
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Controllers;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
15
16
17
18
19

namespace Coscine.Api.Project.Tests
{

    [TestFixture]
20
    public class ProjectControllerTests
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
21
    {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
22
        private readonly ProjectController _projectController = new ProjectController();
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
23

24
25
        private List<Coscine.Database.Model.Project> projects = new List<Coscine.Database.Model.Project>();
        private List<User> users = new List<User>();
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
26
        private List<ProjectRole> projectRoles = new List<ProjectRole>();
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
53
54
55
56
57
58
59
60

        private int previous;

        [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);

            var request = new Mock<HttpRequest>();

            JWTHandler jwtHandler = new JWTHandler(Program.Configuration);
            Dictionary<string, string> values = new Dictionary<string, string>();
            values.Add("UserId", user.Id.ToString());

            // Not working - IsAjaxRequest() is static extension method and cannot be mocked
            // request.Setup(x => x.IsAjaxRequest()).Returns(true /* or false */);
            // use this
            request.SetupGet(x => x.Headers).Returns(
                new HeaderDictionary {
                    {"X-Requested-With", "XMLHttpRequest"},
                    {"bearer", jwtHandler.GenerateJwtToken(values)}
                });

            var context = new Mock<HttpContext>();
            context.SetupGet(x => x.Request).Returns(request.Object);

Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
61
62
63
            var actionDescriptor = new Mock<ControllerActionDescriptor>();

            _projectController.ControllerContext = new ControllerContext(new ActionContext(context.Object, new RouteData(), actionDescriptor.Object));
64
65
66
67
68
69
70
71
72

            var project = new Coscine.Database.Model.Project()
            {
                Description = "Description",
                Organization = "Organization",
                DisplayName = "TestProject"
            };

            projectModel.Insert(project);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
73
74
75
            var projectRole = projectModel.SetOwner(project, user);
            projectRoles.Add(projectRole);

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
            projects.Add(projectModel.GetById(project.Id));

            var project2 = new Coscine.Database.Model.Project()
            {
                Description = "Description2",
                Organization = "Organization2",
                DisplayName = "TestProject2"
            };
            projectModel.Insert(project2);
            projects.Add(projectModel.GetById(project2.Id));
        }

        [OneTimeTearDown]
        public void End()
        {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
91
92
93
94
95
            ProjectRoleModel projectRoleModel = new ProjectRoleModel();
            foreach (var projectRole in projectRoles)
            {
                projectRoleModel.Delete(projectRole);
            }
96
97
98
99
100
101
102
103
104
105
106
107
            ProjectModel projectModel = new ProjectModel();
            foreach (var project in projects)
            {
                projectModel.Delete(project);
            }
            UserModel userModel = new UserModel();
            foreach (var user in users)
            {
                userModel.Delete(user);
            }
        }

Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
108
        [Test]
109
        public void OwnsTest()
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
110
        {
111
112
113
            ProjectModel projectModel = new ProjectModel();
            Assert.IsTrue(projectModel.OwnsProject(users[0], projects[0]));
            Assert.IsFalse(projectModel.OwnsProject(users[0], projects[1]));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
114

115
116
117
118
119
120
121
122
            var all = projectModel.GetAllWhere((project) =>
                (from projectRole in project.ProjectRolesProjectIdIds
                 where projectRole.User == users[0]
                     && projectRole.Role.DisplayName == "Owner"
                 select projectRole).Any()
            );
            Assert.IsTrue(all.ToArray().Length == 1);
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
123

124
125
126
127
        [Test]
        public void TestController()
        {
            var actionResult = _projectController.Index();
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
128
129
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
130

131
132
133
134
135
        [Test]
        public void CountTest()
        {
            ProjectModel projectModel = new ProjectModel();
            Assert.IsTrue(previous == projectModel.GetAll().ToArray().Length - projects.Count);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
136
137
138
        }
    }
}