51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Net.Http.Json;
|
|
using System.Security.Claims;
|
|
using System.Text.Json;
|
|
|
|
using BrokerApiCore;
|
|
|
|
using BrokerApiServer;
|
|
using BrokerTest.Helper;
|
|
|
|
namespace BrokerTest;
|
|
public class PlanetControllerTests
|
|
{
|
|
private readonly BrokerTestServer m_server = new();
|
|
private readonly JsonSerializerOptions m_json_options = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
[Fact]
|
|
public async Task Auth_ValidRequest_ReturnsOkWithAccessToken()
|
|
{
|
|
// Arrange
|
|
const string planet_id = "new_earth";
|
|
const string planet_secret_key = "A8h$KmP3sWxZqL5vYnR7uTgBdEjHkMlQoT1wXzCv";
|
|
|
|
var client = m_server.getTestClient();
|
|
var request = new PlanetAuthRequest { PlanetId = planet_id, PlanetSecretKey = planet_secret_key };
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/v1/planet/auth", request);
|
|
|
|
// Assert
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var auth_response = await response.Content.ReadFromJsonAsync<PlanetAuthResponse>(m_json_options);
|
|
Assert.NotNull(auth_response);
|
|
var jwt_option = m_server.getRequiredService<JwtOption>();
|
|
Assert.NotNull(jwt_option);
|
|
var jwt_service = new JwtParser(jwt_option);
|
|
var claims = jwt_service.parseToken(auth_response.AccessToken);
|
|
Assert.NotNull(claims);
|
|
var id = claims.FindFirstValue(JwtRegisteredClaimNames.Sid);
|
|
Assert.NotNull(id);
|
|
Assert.Equal(planet_id, id);
|
|
Assert.NotNull(auth_response.AccessToken);
|
|
Assert.NotEqual(auth_response.AccessToken, string.Empty);
|
|
}
|
|
}
|