aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/JwtTokenUnitTest.cs
blob: e55bc82c5656cad7b6199274e3bca6c1c20b0264 (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
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Timeline.Controllers;
using Timeline.Services;
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 ValidateTokenUrl = "/api/User/ValidateToken";

        private readonly WebApplicationFactory<Startup> _factory;

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

        [Fact]
        public async void ValidateToken_BadTokenTest()
        {
            using (var client = _factory.CreateDefaultClient())
            {
                var response = await client.PostAsync(ValidateTokenUrl, new StringContent("bad token hahaha", Encoding.UTF8, "text/plain"));

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

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

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

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

                var response = await client.PostAsync(ValidateTokenUrl, new StringContent(createTokenResult.Token, Encoding.UTF8, "text/plain"));

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

                var result = JsonConvert.DeserializeObject<TokenValidationResult>(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);
            }
        }

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

                var response = await client.PostAsJsonAsync(ValidateTokenUrl, new UserController.TokenValidationRequest { Token = createTokenResult.Token });

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

                var result = JsonConvert.DeserializeObject<TokenValidationResult>(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);
            }
        }
    }
}