Skip to content

Commit

Permalink
simplify the login helper for auth api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Jul 6, 2023
1 parent 17d74f4 commit 0d1f591
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions backend/Testing/ApiTests/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,33 @@ namespace Testing.ApiTests;
[Trait("Category", "Integration")]
public class AuthTests
{
private string _host = "staging.languagedepot.org";
private HttpClient _httpClient = new HttpClient();

private async Task<string> LoginAs(string user, string password)
private async Task LoginAs(string user, string password)
{
var cookieContainer = new CookieContainer();
var loginResponse = await _httpClient.PostAsJsonAsync(
$"http://{TestingEnvironmentVariables.ServerHostname}/api/login",
await _httpClient.PostAsJsonAsync(
$"http://{_host}/api/login",
new Dictionary<string, object>
{
{ "password", password }, { "emailOrUsername", user }, { "preHashedPassword", false }
});
foreach (var value in loginResponse.Headers.GetValues("Set-Cookie"))
{
cookieContainer.SetCookies(loginResponse.RequestMessage?.RequestUri ??
throw new ArgumentNullException("requestUri"),
value);
}

var lexBoxCookie = cookieContainer.GetAllCookies().FirstOrDefault(c => c.Name == ".LexBoxAuth");
lexBoxCookie.ShouldNotBeNull();
return lexBoxCookie.Value;
}

[Fact]
public async Task TestLoginAndVerifyDifferentUsers()
{
await LoginAs("manager", "pass");
var managerResponse = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get,
$"http://{TestingEnvironmentVariables.ServerHostname}/api/user/currentUser"),
$"http://{_host}/api/user/currentUser"),
HttpCompletionOption.ResponseContentRead);
var manager = await managerResponse.Content.ReadFromJsonAsync<LexAuthUser>();
manager.ShouldNotBeNull();
manager.Email.ShouldBe("[email protected]");

await LoginAs("admin", "pass");
var response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get,
$"http://{TestingEnvironmentVariables.ServerHostname}/api/user/currentUser"),
$"http://{_host}/api/user/currentUser"),
HttpCompletionOption.ResponseContentRead);
var admin = await response.Content.ReadFromJsonAsync<LexAuthUser>();
admin.ShouldNotBeNull();
Expand All @@ -61,7 +51,7 @@ public async Task TestGqlVerifyDifferentUsers()
var query = """{"query":"query testGetMe { me { id email }}"}""";
await LoginAs("manager", "pass");
var managerResponse = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Post,
$"http://{TestingEnvironmentVariables.ServerHostname}/api/graphql")
$"http://{_host}/api/graphql")
{
Content = new StringContent(query, Encoding.UTF8, "application/json")
},
Expand All @@ -72,7 +62,7 @@ public async Task TestGqlVerifyDifferentUsers()

await LoginAs("admin", "pass");
var adminResponse = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Post,
$"http://{TestingEnvironmentVariables.ServerHostname}/api/graphql")
$"http://{_host}/api/graphql")
{
Content = new StringContent(query, Encoding.UTF8, "application/json")
},
Expand Down

0 comments on commit 0d1f591

Please sign in to comment.