89 lines
3.3 KiB
C#
89 lines
3.3 KiB
C#
namespace BrokerTest.Controllers;
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Security.Claims;
|
|
using System.Text.Json;
|
|
using BrokerCore.ApiModels;
|
|
using BrokerCore.Common;
|
|
using BrokerCore.Services;
|
|
using Helper;
|
|
using Xunit;
|
|
|
|
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);
|
|
}
|
|
|
|
// [Fact]
|
|
// public async Task Auth_InvalidRequest_EmptyPlanetId_ReturnsBadRequest()
|
|
// {
|
|
// // Arrange
|
|
// var client = m_server.getTestClient();
|
|
// var request = new PlanetAuthRequest { PlanetId = "", PlanetSecretKey = "test_secret_key" };
|
|
//
|
|
// // Act
|
|
// var response = await client.PostAsJsonAsync("/api/v1/planet/auth", request);
|
|
//
|
|
// // Assert
|
|
// Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
// var error_response = await response.Content.ReadFromJsonAsync<ApiErrorResponse>(m_json_options);
|
|
// Assert.NotNull(error_response);
|
|
// Assert.Equal((int)ServerErrorCode.InvalidRequest, error_response.Code);
|
|
// Assert.Contains("planet id is required", error_response.Message);
|
|
// }
|
|
//
|
|
// [Fact]
|
|
// public async Task Auth_InvalidRequest_NullPlanetSecretKey_ReturnsBadRequest()
|
|
// {
|
|
// // Arrange
|
|
// var client = m_server.getTestClient();
|
|
// var request = new PlanetAuthRequest { PlanetId = "test_planet_id", PlanetSecretKey = null };
|
|
//
|
|
// // Act
|
|
// var response = await client.PostAsJsonAsync("/api/v1/planet/auth", request);
|
|
//
|
|
// // Assert
|
|
// Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
// var error_response = await response.Content.ReadFromJsonAsync<ApiErrorResponse>(m_json_options);
|
|
// Assert.NotNull(error_response);
|
|
// Assert.Equal((int)ServerErrorCode.InvalidRequest, error_response.Code);
|
|
// Assert.Contains("planet access key is required", error_response.Message);
|
|
// }
|
|
}
|