aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs
blob: 179048dde7de567dd634d889bdf1d2063096160e (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
using System.Net;
using System.Net.Http.Headers;
using CrupestApi.Commons.Secrets;
using Microsoft.AspNetCore.TestHost;

namespace CrupestApi.Commons.Crud.Tests;

public class CrudIntegratedTest : IAsyncLifetime
{
    private readonly WebApplication _app;
    private HttpClient _httpClient = default!;
    private HttpClient _authorizedHttpClient = default!;
    private string _token = default!;

    public CrudIntegratedTest()
    {
        var builder = WebApplication.CreateBuilder();
        builder.Logging.ClearProviders();
        builder.Services.AddSingleton<IDbConnectionFactory, SqliteMemoryConnectionFactory>();
        builder.Services.AddCrud<TestEntity>();
        builder.WebHost.UseTestServer();
        _app = builder.Build();
        _app.MapCrud<TestEntity>("/test", "test-perm");
    }

    public async Task InitializeAsync()
    {
        await _app.StartAsync();
        _httpClient = _app.GetTestClient();

        using (var scope = _app.Services.CreateScope())
        {
            var secretService = (SecretService)scope.ServiceProvider.GetRequiredService<ISecretService>();
            var key = secretService.Create(new SecretInfo
            {
                Key = "test-perm"
            });
            _token = secretService.GetByKey(key).Secret;
        }

        _authorizedHttpClient = _app.GetTestClient();
        _authorizedHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token);
    }

    public async Task DisposeAsync()
    {
        await _app.StopAsync();
    }


    [Fact]
    public async Task EmptyTest()
    {
        using var response = await _authorizedHttpClient.GetAsync("/test");
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var body = await response.Content.ReadFromJsonAsync<List<TestEntity>>();
        Assert.NotNull(body);
        Assert.Empty(body);
    }

    [Fact]
    public async Task CrudTest()
    {
        {
            using var response = await _authorizedHttpClient.PostAsJsonAsync("/test", new TestEntity
            {
                Name = "test",
                Age = 22
            });
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var body = await response.Content.ReadFromJsonAsync<TestEntity>();
            Assert.NotNull(body);
            Assert.Equal("test", body.Name);
            Assert.Equal(22, body.Age);
            Assert.Null(body.Height);
            Assert.NotEmpty(body.Secret);
        }

        {
            using var response = await _authorizedHttpClient.GetAsync("/test");
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var body = await response.Content.ReadFromJsonAsync<List<TestEntity>>();
            Assert.NotNull(body);
            var entity = Assert.Single(body);
            Assert.Equal("test", entity.Name);
            Assert.Equal(22, entity.Age);
            Assert.Null(entity.Height);
            Assert.NotEmpty(entity.Secret);
        }

        {
            using var response = await _authorizedHttpClient.GetAsync("/test/test");
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var body = await response.Content.ReadFromJsonAsync<TestEntity>();
            Assert.NotNull(body);
            Assert.Equal("test", body.Name);
            Assert.Equal(22, body.Age);
            Assert.Null(body.Height);
            Assert.NotEmpty(body.Secret);
        }

        {
            using var response = await _authorizedHttpClient.PatchAsJsonAsync("/test/test", new TestEntity
            {
                Name = "test-2",
                Age = 23,
                Height = 188.0f
            });
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var body = await response.Content.ReadFromJsonAsync<TestEntity>();
            Assert.NotNull(body);
            Assert.Equal("test-2", body.Name);
            Assert.Equal(23, body.Age);
            Assert.Equal(188.0f, body.Height);
            Assert.NotEmpty(body.Secret);
        }

        {
            using var response = await _authorizedHttpClient.GetAsync("/test/test-2");
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var body = await response.Content.ReadFromJsonAsync<TestEntity>();
            Assert.NotNull(body);
            Assert.Equal("test-2", body.Name);
            Assert.Equal(23, body.Age);
            Assert.Equal(188.0f, body.Height);
            Assert.NotEmpty(body.Secret);
        }

        {
            using var response = await _authorizedHttpClient.DeleteAsync("/test/test-2");
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }

        {
            using var response = await _authorizedHttpClient.GetAsync("/test");
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var body = await response.Content.ReadFromJsonAsync<List<TestEntity>>();
            Assert.NotNull(body);
            Assert.Empty(body);
        }
    }
}