aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/JwtTokenUnitTest.cs
blob: 7e881895f59bd4addab2da36e79932201dcbb474 (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
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
using System.Net;
using System.Net.Http;
using Timeline.Entities;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
using Xunit;
using Xunit.Abstractions;

namespace Timeline.Tests
{
    public class JwtTokenUnitTest : IClassFixture<WebApplicationFactory<Startup>>
    {
        private const string CreateTokenUrl = "/api/User/CreateToken";
        private const string ValidateTokenUrl = "/api/User/ValidateToken";

        private readonly WebApplicationFactory<Startup> _factory;

        public JwtTokenUnitTest(WebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
        {
            _factory = factory.WithTestConfig(outputHelper);
        }

        [Fact]
        public async void CreateTokenTest_BadCredential()
        {
            using (var client = _factory.CreateDefaultClient())
            {
                var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "???", Password = "???" });
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
                Assert.False(result.Success);
                Assert.Null(result.Token);
                Assert.Null(result.UserInfo);
            }
        }

        [Fact]
        public async void CreateTokenTest_GoodCredential()
        {
            using (var client = _factory.CreateDefaultClient())
            {
                var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "user", Password = "user" });
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
                Assert.True(result.Success);
                Assert.NotNull(result.Token);
                Assert.NotNull(result.UserInfo);
            }
        }

        [Fact]
        public async void ValidateTokenTest_BadToken()
        {
            using (var client = _factory.CreateDefaultClient())
            {
                var response = await client.PostAsJsonAsync(ValidateTokenUrl, new TokenValidationRequest { Token = "bad token hahaha" });

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var validationInfo = JsonConvert.DeserializeObject<TokenValidationResponse>(await response.Content.ReadAsStringAsync());

                Assert.False(validationInfo.IsValid);
                Assert.Null(validationInfo.UserInfo);
            }
        }

        [Fact]
        public async void ValidateTokenTest_GoodToken()
        {
            using (var client = _factory.CreateDefaultClient())
            {
                var createTokenResult = await client.CreateUserTokenAsync("admin", "admin");

                var response = await client.PostAsJsonAsync(ValidateTokenUrl, new TokenValidationRequest { Token = createTokenResult.Token });
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var result = JsonConvert.DeserializeObject<TokenValidationResponse>(await response.Content.ReadAsStringAsync());

                Assert.True(result.IsValid);
                Assert.NotNull(result.UserInfo);
                Assert.Equal(createTokenResult.UserInfo.Username, result.UserInfo.Username);
                Assert.Equal(createTokenResult.UserInfo.Roles, result.UserInfo.Roles);
            }
        }
    }
}