aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/IntegratedTests/UserDetailTest.cs
blob: 571f200f29d6f78a21b7d0292b07599d0f71191b (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
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Timeline.Controllers;
using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
using Timeline.Tests.Mock.Data;
using Xunit;
using Xunit.Abstractions;

namespace Timeline.Tests.IntegratedTests
{
    public class UserDetailTest : IClassFixture<MyWebApplicationFactory<Startup>>, IDisposable
    {
        private readonly WebApplicationFactory<Startup> _factory;
        private readonly Action _disposeAction;

        public UserDetailTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
        {
            _factory = factory.WithTestConfig(outputHelper, out _disposeAction);
        }

        public void Dispose()
        {
            _disposeAction();
        }

        [Fact]
        public async Task TestAsUser()
        {
            using (var client = await _factory.CreateClientAsUser())
            {
                {
                    var res = await client.GetAsync($"users/usernotexist/details");
                    res.Should().HaveStatusCodeNotFound()
                        .And.Should().HaveBodyAsCommonResponseWithCode(UserDetailController.ErrorCodes.Get_UserNotExist);
                }

                async Task GetAndTest(UserDetail d)
                {
                    var res = await client.GetAsync($"users/{MockUsers.UserUsername}/details");
                    res.Should().HaveStatusCodeOk()
                        .And.Should().HaveBodyAsJson<UserDetail>()
                        .Which.Should().BeEquivalentTo(d);
                }

                await GetAndTest(new UserDetail());

                {
                    var res = await client.PatchAsJsonAsync($"users/{MockUsers.AdminUsername}/details", new UserDetail());
                    res.Should().HaveStatusCode(HttpStatusCode.Forbidden)
                        .And.Should().HaveBodyAsCommonResponseWithCode(UserDetailController.ErrorCodes.Patch_Forbid);
                }

                {
                    var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", new UserDetail
                    {
                        QQ = "aaaaaaa",
                        EMail = "aaaaaa"
                    });
                    res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
                        .And.Should().HaveBodyAsCommonResponseWithCode(CommonResponse.ErrorCodes.InvalidModel);
                }

                var detail = new UserDetail
                {
                    QQ = "1234567",
                    EMail = "aaaa@aaa.net",
                    Description = "aaaaaaaaa"
                };

                {
                    var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", detail);
                    res.Should().HaveStatusCodeOk();
                    await GetAndTest(detail);
                }

                var detail2 = new UserDetail
                {
                    QQ = "",
                    PhoneNumber = "12345678910",
                    Description = "bbbbbbbb"
                };

                {
                    var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", detail2);
                    res.Should().HaveStatusCodeOk();
                    await GetAndTest(new UserDetail
                    {
                        QQ = null,
                        EMail = detail.EMail,
                        PhoneNumber = detail2.PhoneNumber,
                        Description = detail2.Description
                    });
                }
            }
        }

        [Fact]
        public async Task TestAsAdmin()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                {
                    var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", new UserDetail());
                    res.Should().HaveStatusCodeOk();
                }

                {
                    var res = await client.PatchAsJsonAsync($"users/usernotexist/details", new UserDetail());
                    res.Should().HaveStatusCodeNotFound()
                        .And.Should().HaveBodyAsCommonResponseWithCode(UserDetailController.ErrorCodes.Patch_UserNotExist);
                }
            }
        }
    }
}