-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJwtAuthTests.cs
More file actions
54 lines (50 loc) · 2.03 KB
/
JwtAuthTests.cs
File metadata and controls
54 lines (50 loc) · 2.03 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Model;
using NUnit.Framework;
namespace Aspose.BarCode.Cloud.Sdk.Tests
{
[TestFixture]
public class JwtAuthTests : TestsBase
{
private async Task<string> FetchToken()
{
Dictionary<string, string> formParams = new Dictionary<string, string>
{
["grant_type"] = "client_credentials",
// ReSharper disable once RedundantToStringCall
["client_id"] = TestConfiguration.ClientId.ToString(),
// ReSharper disable once RedundantToStringCall
["client_secret"] = TestConfiguration.ClientSecret.ToString()
};
FormUrlEncodedContent formContent = new FormUrlEncodedContent(formParams);
HttpResponseMessage response = await new HttpClient().PostAsync(TestConfiguration.TokenUrl, formContent);
response.EnsureSuccessStatusCode();
JsonDocument json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
string accessToken = json.RootElement.GetProperty("access_token").GetString();
return accessToken;
}
[Test]
public async Task CanUseExternalTokenWithAsync()
{
if (TestConfiguration.AuthType != AuthType.JWT)
{
Assert.Ignore($"Unsupported TestConfiguration.AuthType={TestConfiguration.AuthType}");
}
Configuration configWithToken = new Configuration
{
ApiBaseUrl = TestConfiguration.ApiBaseUrl,
TokenUrl = TestConfiguration.TokenUrl,
JwtToken = await FetchToken()
};
GenerateApi api = new GenerateApi(configWithToken);
using Stream generated = await api.GenerateAsync(EncodeBarcodeType.QR, "Test");
Assert.Greater(generated.Length, 0);
}
}
}