-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from jmg1138/end-to-end-tests
End to end tests
- Loading branch information
Showing
8 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -458,3 +458,6 @@ $RECYCLE.BIN/ | |
|
||
# Flyway | ||
flyway*/ | ||
|
||
# Codecov.io | ||
codecov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using System.Text.Json; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using TrappyKeepy.Domain.Models; | ||
using TrappyKeepy.Test.TestObjects; | ||
using Xunit; | ||
|
||
namespace TrappyKeepy.Test.End2End | ||
{ | ||
public class SessionsEnd2EndTests : IClassFixture<WebApplicationFactory<Program>> | ||
{ | ||
private SpawnyDb _db; | ||
private readonly WebApplicationFactory<Program> _webApplicationFactory; | ||
private DtoTestObjects _dto; | ||
private JsonSerializerOptions _jsonOpts; | ||
|
||
public SessionsEnd2EndTests(WebApplicationFactory<Program> webApplicationFactory) | ||
{ | ||
_db = new SpawnyDb(); | ||
_webApplicationFactory = webApplicationFactory; | ||
_dto = new DtoTestObjects(); | ||
_jsonOpts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; | ||
} | ||
|
||
[Fact] | ||
[Trait("TestType", "End2End")] | ||
public async Task PostSessionsWithValidCredentialsShouldCreateNewToken() | ||
{ | ||
// ---------- ARRANGE ---------- | ||
await _db.RecycleDb(); | ||
var user = _dto.TestUserSessionDto; | ||
var json = JsonSerializer.Serialize(user); | ||
var content = new StringContent(json, Encoding.UTF8, "application/json"); | ||
HttpResponseMessage? response; | ||
|
||
// ---------- ACT ---------- | ||
using (var client = _webApplicationFactory.CreateDefaultClient()) | ||
{ | ||
response = await client.PostAsync("/v1/sessions", content); | ||
} | ||
|
||
// ---------- ASSERT ---------- | ||
Assert.NotNull(response); | ||
var responseJson = await response.Content.ReadAsStringAsync(); | ||
Assert.NotNull(responseJson); | ||
var controllerResponse = JsonSerializer.Deserialize<ControllerResponse>(responseJson, _jsonOpts); | ||
Assert.NotNull(controllerResponse); | ||
Assert.NotNull(controllerResponse.Status); | ||
Assert.Equal("success", controllerResponse.Status); | ||
Assert.NotNull(controllerResponse.Data); | ||
var token = controllerResponse.Data.ToString(); | ||
Assert.NotNull(token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Npgsql; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace TrappyKeepy.Test.End2End | ||
{ | ||
public class SpawnyDb | ||
{ | ||
private string _testDbName; | ||
|
||
// Connection to use when creating the temporary testing database. | ||
private NpgsqlConnection _connectionCreateTestDb; | ||
|
||
// Connection to use when running queries in the temporary testing database. | ||
private NpgsqlConnection _connectionUseTestDb; | ||
|
||
public SpawnyDb() | ||
{ | ||
_testDbName = "keepytest"; | ||
|
||
// Connect to the default/maintenance database named "postgres" when we create the temporary testing database. | ||
_connectionCreateTestDb = new NpgsqlConnection("Host=localhost;Database=postgres;Port=15432;Username=dbowner;Password=dbpass;Pooling=false"); | ||
|
||
// Connect to the temporary testing database when we're ready to run queries like seeding test data. | ||
_connectionUseTestDb = new NpgsqlConnection($"Host=localhost;Database={_testDbName};Port=15432;Username=dbowner;Password=dbpass;Pooling=false"); | ||
|
||
// Set the TKDB_CONN_STRING env var that the UnitOfWork class will use to connect to the database. | ||
// This way when the WebApplicationFactory creates the API in memory for the e2e tests, the UnitOfWork | ||
// class will connect to the temporary testing database instead of the development database. | ||
Environment.SetEnvironmentVariable("TKDB_CONN_STRING", $"Host=localhost;Database={_testDbName};Port=15432;Username=dbowner;Password=dbpass;Pooling=false"); | ||
} | ||
|
||
public async Task RecycleDb() | ||
{ | ||
// Drop any old temporary testing database, and create a fresh one. | ||
await _connectionCreateTestDb.OpenAsync(); | ||
await Drop(); | ||
await Create(); | ||
await _connectionCreateTestDb.CloseAsync(); | ||
await _connectionCreateTestDb.DisposeAsync(); | ||
|
||
// Seed test data into the temporary testing database for the next e2e test. | ||
await _connectionUseTestDb.OpenAsync(); | ||
await SeedAdminUser(); | ||
await _connectionUseTestDb.CloseAsync(); | ||
await _connectionUseTestDb.DisposeAsync(); | ||
} | ||
|
||
private async Task Drop() | ||
{ | ||
using (var command = new NpgsqlCommand()) | ||
{ | ||
command.CommandText = $"DROP DATABASE IF EXISTS {_testDbName};"; | ||
command.Connection = _connectionCreateTestDb; | ||
await command.PrepareAsync(); | ||
await command.ExecuteNonQueryAsync(); | ||
} | ||
} | ||
|
||
private async Task Create() | ||
{ | ||
using (var command = new NpgsqlCommand()) | ||
{ | ||
command.CommandText = $"CREATE DATABASE {_testDbName} WITH TEMPLATE keepydb OWNER dbowner;"; | ||
command.Connection = _connectionCreateTestDb; | ||
await command.PrepareAsync(); | ||
await command.ExecuteNonQueryAsync(); | ||
} | ||
} | ||
|
||
private async Task SeedAdminUser() | ||
{ | ||
using (var command = new NpgsqlCommand()) | ||
{ | ||
command.CommandText = "SELECT * FROM tk.users_create('foo', 'passwordfoo', 'foo@trappykeepy.com', 'admin');"; | ||
command.Connection = _connectionUseTestDb; | ||
await command.PrepareAsync(); | ||
await command.ExecuteReaderAsync(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters