aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs
blob: 6857a27f6af7cf700815f114be5415def2af18e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using FluentAssertions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Timeline.Controllers;
using Timeline.Filters;
using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
using Xunit;

namespace Timeline.Tests.Controllers
{
    public class PersonalTimelineControllerTest : IDisposable
    {
        private readonly Mock<IPersonalTimelineService> _service;

        private readonly PersonalTimelineController _controller;

        public PersonalTimelineControllerTest()
        {
            _service = new Mock<IPersonalTimelineService>();
            _controller = new PersonalTimelineController(NullLogger<PersonalTimelineController>.Instance, _service.Object);
        }

        public void Dispose()
        {
            _controller.Dispose();
        }

        [Fact]
        public void AttributeTest()
        {
            static void AssertUsernameParameter(MethodInfo m)
            {
                m.GetParameter("username")
                .Should().BeDecoratedWith<FromRouteAttribute>()
                .And.BeDecoratedWith<UsernameAttribute>();
            }

            static void AssertBodyParamter<TBody>(MethodInfo m)
            {
                var p = m.GetParameter("body");
                p.Should().BeDecoratedWith<FromBodyAttribute>();
                p.ParameterType.Should().Be(typeof(TBody));
            }

            var type = typeof(PersonalTimelineController);
            type.Should().BeDecoratedWith<ApiControllerAttribute>();

            {
                var m = type.GetMethod(nameof(PersonalTimelineController.TimelineGet));
                m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
                    .And.BeDecoratedWith<HttpGetAttribute>();
                AssertUsernameParameter(m);
            }

            {
                var m = type.GetMethod(nameof(PersonalTimelineController.PostsGet));
                m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
                    .And.BeDecoratedWith<HttpGetAttribute>();
                AssertUsernameParameter(m);
            }

            {
                var m = type.GetMethod(nameof(PersonalTimelineController.TimelinePost));
                m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
                    .And.BeDecoratedWith<AuthorizeAttribute>()
                    .And.BeDecoratedWith<HttpPostAttribute>();
                AssertUsernameParameter(m);
                AssertBodyParamter<TimelinePostCreateRequest>(m);
            }

            {
                var m = type.GetMethod(nameof(PersonalTimelineController.TimelinePostDelete));
                m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
                    .And.BeDecoratedWith<AuthorizeAttribute>()
                    .And.BeDecoratedWith<HttpPostAttribute>();
                AssertUsernameParameter(m);
                AssertBodyParamter<TimelinePostDeleteRequest>(m);
            }

            {
                var m = type.GetMethod(nameof(PersonalTimelineController.TimelineChangeProperty));
                m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
                    .And.BeDecoratedWith<AuthorizeAttribute>()
                    .And.BeDecoratedWith<SelfOrAdminAttribute>()
                    .And.BeDecoratedWith<HttpPostAttribute>();
                AssertUsernameParameter(m);
                AssertBodyParamter<TimelinePropertyChangeRequest>(m);
            }

            {
                var m = type.GetMethod(nameof(PersonalTimelineController.TimelineChangeMember));
                m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
                    .And.BeDecoratedWith<AuthorizeAttribute>()
                    .And.BeDecoratedWith<SelfOrAdminAttribute>()
                    .And.BeDecoratedWith<HttpPostAttribute>();
                AssertUsernameParameter(m);
                AssertBodyParamter<TimelineMemberChangeRequest>(m);
            }
        }

        const string authUsername = "authuser";
        private void SetUser(bool administrator)
        {
            _controller.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    User = PrincipalHelper.Create(authUsername, administrator)
                }
            };
        }

        [Fact]
        public async Task TimelineGet()
        {
            const string username = "username";
            var timelineInfo = new BaseTimelineInfo();
            _service.Setup(s => s.GetTimeline(username)).ReturnsAsync(timelineInfo);
            (await _controller.TimelineGet(username)).Value.Should().Be(timelineInfo);
            _service.VerifyAll();
        }

        [Fact]
        public async Task PostsGet_Forbid()
        {
            const string username = "username";
            SetUser(false);
            _service.Setup(s => s.HasReadPermission(username, authUsername)).ReturnsAsync(false);
            (await _controller.PostsGet(username)).Result
                .Should().BeAssignableTo<ObjectResult>()
                .Which.Value.Should().BeAssignableTo<CommonResponse>()
                .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostsGetForbid);
            _service.VerifyAll();
        }

        [Fact]
        public async Task PostsGet_Admin_Success()
        {
            const string username = "username";
            SetUser(true);
            _service.Setup(s => s.GetPosts(username)).ReturnsAsync(new List<TimelinePostInfo>());
            (await _controller.PostsGet(username)).Value
                .Should().BeAssignableTo<IList<TimelinePostInfo>>()
                .Which.Should().NotBeNull().And.BeEmpty();
            _service.VerifyAll();
        }

        [Fact]
        public async Task PostsGet_User_Success()
        {
            const string username = "username";
            SetUser(false);
            _service.Setup(s => s.HasReadPermission(username, authUsername)).ReturnsAsync(true);
            _service.Setup(s => s.GetPosts(username)).ReturnsAsync(new List<TimelinePostInfo>());
            (await _controller.PostsGet(username)).Value
                .Should().BeAssignableTo<IList<TimelinePostInfo>>()
                .Which.Should().NotBeNull().And.BeEmpty();
            _service.VerifyAll();
        }

        //TODO! Write all the other tests.
    }
}