aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
blob: 721a25af4f5cfa42fd933c19b0e015d753969d5c (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
using AutoMapper;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Timeline.Models.Http;
using Timeline.Services;
using Timeline.Tests.Helpers;
using Xunit;

namespace Timeline.Tests.IntegratedTests
{

    public abstract class IntegratedTestBase : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
    {
        protected TestApplication TestApp { get; }

        protected WebApplicationFactory<Startup> Factory => TestApp.Factory;

        public IntegratedTestBase(WebApplicationFactory<Startup> factory) : this(factory, 1)
        {

        }

        public IntegratedTestBase(WebApplicationFactory<Startup> factory, int userCount)
        {
            if (userCount < 0)
                throw new ArgumentOutOfRangeException(nameof(userCount), userCount, "User count can't be negative.");

            TestApp = new TestApplication(factory);

            using (var scope = Factory.Services.CreateScope())
            {
                var users = new List<User>()
                {
                    new User
                    {
                        Username = "admin",
                        Password = "adminpw",
                        Administrator = true,
                        Nickname = "administrator"
                    }
                };

                for (int i = 1; i <= userCount; i++)
                {
                    users.Add(new User
                    {
                        Username = $"user{i}",
                        Password = $"user{i}pw",
                        Administrator = false,
                        Nickname = $"imuser{i}"
                    });
                }

                var userInfoList = new List<UserInfo>();
                var userInfoForAdminList = new List<UserInfoForAdmin>();

                var userService = scope.ServiceProvider.GetRequiredService<IUserService>();
                var mapper = scope.ServiceProvider.GetRequiredService<IMapper>();

                foreach (var user in users)
                {
                    userService.CreateUser(user);
                    userInfoList.Add(mapper.Map<UserInfo>(user));
                    userInfoForAdminList.Add(mapper.Map<UserInfoForAdmin>(user));
                }

                UserInfoList = userInfoList;
                UserInfoForAdminList = userInfoForAdminList;
            }
        }

        protected virtual void OnDispose()
        {
        }

        public void Dispose()
        {
            OnDispose();
            TestApp.Dispose();
        }

        public IReadOnlyList<UserInfo> UserInfoList { get; }

        public IReadOnlyList<UserInfoForAdmin> UserInfoForAdminList { get; }

        public Task<HttpClient> CreateDefaultClient()
        {
            return Task.FromResult(Factory.CreateDefaultClient());
        }

        public async Task<HttpClient> CreateClientWithCredential(string username, string password)
        {
            var client = Factory.CreateDefaultClient();
            var response = await client.PostAsJsonAsync("/token/create",
                new CreateTokenRequest { Username = username, Password = password });
            var token = response.Should().HaveStatusCode(200)
                .And.HaveJsonBody<CreateTokenResponse>().Which.Token;
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            return client;
        }

        public Task<HttpClient> CreateClientAs(int userNumber)
        {
            if (userNumber < 0)
                throw new ArgumentOutOfRangeException(nameof(userNumber), "User number can't be negative.");

            if (userNumber == 0)
                return CreateClientWithCredential("admin", "adminpw");
            else
                return CreateClientWithCredential($"user{userNumber}", $"user{userNumber}pw");
        }

        public Task<HttpClient> CreateClientAsAdministrator()
        {
            return CreateClientAs(0);
        }

        public Task<HttpClient> CreateClientAsUser()
        {
            return CreateClientAs(1);
        }
    }
}