Skip to content

Instantly share code, notes, and snippets.

@gnschenker
Created December 30, 2019 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnschenker/b771d6ed0b1a76612bd388d34847145c to your computer and use it in GitHub Desktop.
Save gnschenker/b771d6ed0b1a76612bd388d34847145c to your computer and use it in GitHub Desktop.
The base class for tests requiring a token
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Xunit;
namespace tests
{
public class ControllerTestsBase : IClassFixture<WebApiTesterFactory>
{
protected readonly WebApiTesterFactory factory;
protected readonly JsonElement result;
protected readonly string token;
protected readonly HttpClient client;
public ControllerTestsBase(WebApiTesterFactory factory)
{
this.factory = factory;
client = factory.CreateClient();
client.BaseAddress = new Uri("https://localhost:5001/api/");
var config = factory.Services.GetService(typeof(IConfiguration)) as IConfiguration;
Assert.NotNull(config);
// NOTE: we are using client secrets to avoid having secret valuse in code!
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var url = config["auth0:url"];
var request = new HttpRequestMessage(HttpMethod.Post, url);
var json = new {
client_id = config["auth0:client_id"],
client_secret = config["auth0:client_secret"],
audience = config["auth0:audience"],
grant_type = "client_credentials"
};
var content = new StringContent(
JsonSerializer.Serialize(json, typeof(object)),
Encoding.UTF8,
"application/json");
request.Content = content;
var response = httpClient.PostAsync(url, content).Result;
result = JsonSerializer.Deserialize<JsonElement>(response.Content.ReadAsStringAsync().Result);
token = result.GetProperty("access_token").GetString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment