Last active
July 23, 2019 07:59
-
-
Save ntakouris/95efa7313f0912f31fa11e60fe0c341e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class IntegrationTest | |
{ | |
protected readonly HttpClient TestClient; | |
private readonly IServiceProvider _serviceProvider; | |
protected IntegrationTest() | |
{ | |
var appFactory = new WebApplicationFactory<Startup>() | |
.WithWebHostBuilder(builder => | |
{ | |
builder.ConfigureServices(services => | |
{ | |
services.RemoveAll(typeof(DataContext)); | |
services.AddDbContext<DataContext>(options => { options.UseInMemoryDatabase("TestDb"); }); | |
}); | |
}); | |
_serviceProvider = appFactory.Services; | |
TestClient = appFactory.CreateClient(); | |
} | |
protected async Task AuthenticateAsync() | |
{ | |
TestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", await GetJwtAsync()); | |
} | |
protected async Task<PostResponse> CreatePostAsync(CreatePostRequest request) | |
{ | |
var response = await TestClient.PostAsJsonAsync(ApiRoutes.Posts.Create, request); | |
return await response.Content.ReadAsAsync<PostResponse>(); | |
} | |
private async Task<string> GetJwtAsync() | |
{ | |
var response = await TestClient.PostAsJsonAsync(ApiRoutes.Identity.Register, new UserRegistrationRequest | |
{ | |
Email = "[email protected]", | |
Password = "SomePass1234!" | |
}); | |
var registrationResponse = await response.Content.ReadAsAsync<AuthSuccessResponse>(); | |
return registrationResponse.Token; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment