ResourceControllerTests.cs 4.64 KB
Newer Older
1 2 3
using Coscine.Api.Project.Controllers;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
4 5
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
6
using Microsoft.AspNetCore.Mvc;
7
using Newtonsoft.Json.Linq;
8 9
using NUnit.Framework;
using System;
10
using System.Collections.Generic;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
using System.IO;

namespace Coscine.Api.Project.Tests
{
    [TestFixture]
    public class ResourceControllerTests : DefaultControllerTests<ResourceController>
    {
        public ResourceControllerTests() : base(new ResourceController())
        {
        }

        [Test]
        public void TestControllerIndex()
        {
            var actionResult = Controller.Index();
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
        }

        [Test]
        public void TestControllerGet()
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
32
            var actionResult = Controller.Get(Resources[0].Id.ToString());
33 34 35 36 37
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));

            OkObjectResult okObjectResult = (OkObjectResult)actionResult;
            Assert.IsTrue(okObjectResult.Value.GetType() == typeof(ResourceObject));

38
            ResourceObject resourceObject = (ResourceObject)okObjectResult.Value;
39

40 41 42 43 44
            Assert.IsTrue(resourceObject.Id == Resources[0].Id);
            Assert.IsTrue(resourceObject.DisplayName == Resources[0].DisplayName);
            Assert.IsTrue(resourceObject.Visibility.DisplayName == Resources[0].Visibility.DisplayName);
            Assert.IsTrue(resourceObject.Type.Id == Resources[0].Type.Id);
            Assert.IsTrue(resourceObject.Type.DisplayName == Resources[0].Type.DisplayName);
45 46 47 48 49 50
        }


        [Test]
        public void TestControllerUpdate()
        {
L. Ellenbeck's avatar
L. Ellenbeck committed
51
            var actionResult = Controller.Get(Resources[0].Id.ToString());
52 53 54
            OkObjectResult okObjectResult = (OkObjectResult)actionResult;
            ResourceObject resourceObject = (ResourceObject)okObjectResult.Value;

55 56
            resourceObject.DisplayName = "OtherName";
            resourceObject.ResourceTypeOption = JObject.FromObject(new RDSResourceTypeObject(Guid.NewGuid(), "PITLABTTEST", "accesskey", "secretkey"));
57 58 59

            Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);

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

L. Ellenbeck's avatar
L. Ellenbeck committed
62
            actionResult = Controller.Update(Resources[0].Id.ToString());
63 64 65 66 67 68 69
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));

            // Cleanup
            stream.Close();

            stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);

L. Ellenbeck's avatar
L. Ellenbeck committed
70
            FakeControllerContext(Users[0], stream);
71 72 73

            try
            {
L. Ellenbeck's avatar
L. Ellenbeck committed
74
                Controller.Update(Resources[1].Id.ToString());
75 76 77 78 79 80 81 82 83 84 85 86 87 88
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e.GetType() == typeof(NotAuthorizedException));
            }

            // Cleanup
            stream.Close();
        }

        [Test]
        public void TestControllerStoreToProject()
        {
89 90 91 92
            ResourceObject resourceObject = new ResourceObject(
                Guid.NewGuid(),
                "Test",
                "testR",
93
                "testD",
94 95 96
                "keys",
                "usageR",
                new ResourceTypeObject(Resources[0].Type.Id, Resources[0].Type.DisplayName),
97
                new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
98 99
                new VisibilityObject(Visibility.Id, Visibility.DisplayName),
                new LicenseObject(License.Id, License.DisplayName),
100 101 102
                JObject.FromObject(new RDSResourceTypeObject(Guid.NewGuid(), "PITLABTTEST", "accesskey", "secretkey")),
                "link",
                JToken.Parse("{}")
103
            );
104 105 106

            Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);

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

L. Ellenbeck's avatar
L. Ellenbeck committed
109
            var actionResult = Controller.StoreToProject(Projects[0].Id.ToString());
110 111 112 113 114 115 116
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
            OkObjectResult okObjectResult = (OkObjectResult)actionResult;
            resourceObject = (ResourceObject)okObjectResult.Value;

            // Cleanup
            stream.Close();

117
            stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
118

119 120 121 122
            FakeControllerContext(Users[0], stream);

            actionResult = Controller.Delete(resourceObject.Id.ToString());
            Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
123

124
            stream.Close();
125 126 127
        }
    }
}