aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-05-30 16:23:25 +0800
committercrupest <crupest@outlook.com>2020-05-30 16:23:25 +0800
commit68ca8b0976efe90c0c40bcae69f0825671b98bad (patch)
tree9333baf56fd4e20e1e98edcc6afbbde9d82dcd3f /Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
parent7e393559d2883a37b1be0c82cccc06bc97c3d102 (diff)
downloadtimeline-68ca8b0976efe90c0c40bcae69f0825671b98bad.tar.gz
timeline-68ca8b0976efe90c0c40bcae69f0825671b98bad.tar.bz2
timeline-68ca8b0976efe90c0c40bcae69f0825671b98bad.zip
Merge front end to this repo. But I need to wait for aspnet core support for custom port and package manager for dev server.
Diffstat (limited to 'Timeline.Tests/IntegratedTests/IntegratedTestBase.cs')
-rw-r--r--Timeline.Tests/IntegratedTests/IntegratedTestBase.cs125
1 files changed, 64 insertions, 61 deletions
diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
index a4a7638c..6c349ffb 100644
--- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
+++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
@@ -1,43 +1,43 @@
-using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
-using System;
+using System;
using System.Collections.Generic;
-using System.Net.Http;
+using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
-using System.Threading.Tasks;
+using System.Threading.Tasks;
using Timeline.Models;
using Timeline.Models.Converters;
-using Timeline.Models.Http;
+using Timeline.Models.Http;
using Timeline.Services;
-using Timeline.Tests.Helpers;
-using Xunit;
-
-namespace Timeline.Tests.IntegratedTests
-{
- public abstract class IntegratedTestBase : IClassFixture<WebApplicationFactory<Startup>>, IAsyncLifetime
- {
- protected TestApplication TestApp { get; }
-
- protected WebApplicationFactory<Startup> Factory => TestApp.Factory;
-
- public IReadOnlyList<UserInfo> UserInfos { get; private set; }
-
- private readonly int _userCount;
-
- public IntegratedTestBase(WebApplicationFactory<Startup> factory) : this(factory, 1)
- {
-
+using Timeline.Tests.Helpers;
+using Xunit;
+
+namespace Timeline.Tests.IntegratedTests
+{
+ public abstract class IntegratedTestBase : IClassFixture<WebApplicationFactory<Startup>>, IAsyncLifetime
+ {
+ protected TestApplication TestApp { get; }
+
+ protected WebApplicationFactory<Startup> Factory => TestApp.Factory;
+
+ public IReadOnlyList<UserInfo> UserInfos { get; private set; }
+
+ private readonly int _userCount;
+
+ public IntegratedTestBase(WebApplicationFactory<Startup> factory) : this(factory, 1)
+ {
+
}
- public IntegratedTestBase(WebApplicationFactory<Startup> factory, int userCount)
- {
- if (userCount < 0)
- throw new ArgumentOutOfRangeException(nameof(userCount), userCount, "User count can't be negative.");
-
- _userCount = userCount;
-
- TestApp = new TestApplication(factory);
+ public IntegratedTestBase(WebApplicationFactory<Startup> factory, int userCount)
+ {
+ if (userCount < 0)
+ throw new ArgumentOutOfRangeException(nameof(userCount), userCount, "User count can't be negative.");
+
+ _userCount = userCount;
+
+ TestApp = new TestApplication(factory);
}
protected virtual Task OnInitializeAsync()
@@ -115,34 +115,37 @@ namespace Timeline.Tests.IntegratedTests
await OnDisposeAsync();
OnDispose();
await TestApp.DisposeAsync();
- }
-
- public Task<HttpClient> CreateDefaultClient()
- {
- return Task.FromResult(Factory.CreateDefaultClient());
- }
-
- public async Task<HttpClient> CreateClientWithCredential(string username, string password)
- {
- var client = Factory.CreateDefaultClient();
- var response = await client.PostAsJsonAsync("/token/create",
- new CreateTokenRequest { Username = username, Password = password });
- var token = response.Should().HaveStatusCode(200)
- .And.HaveJsonBody<CreateTokenResponse>().Which.Token;
- client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
- return client;
- }
-
- public Task<HttpClient> CreateClientAs(int userNumber)
- {
- if (userNumber < 0)
- return CreateDefaultClient();
- if (userNumber == 0)
- return CreateClientWithCredential("admin", "adminpw");
- else
- return CreateClientWithCredential($"user{userNumber}", $"user{userNumber}pw");
- }
-
+ }
+
+ public Task<HttpClient> CreateDefaultClient()
+ {
+ var client = Factory.CreateDefaultClient();
+ client.BaseAddress = new Uri(client.BaseAddress, "api");
+ return Task.FromResult(client);
+ }
+
+ public async Task<HttpClient> CreateClientWithCredential(string username, string password)
+ {
+ var client = Factory.CreateDefaultClient();
+ client.BaseAddress = new Uri(client.BaseAddress, "api");
+ var response = await client.PostAsJsonAsync("/token/create",
+ new CreateTokenRequest { Username = username, Password = password });
+ var token = response.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<CreateTokenResponse>().Which.Token;
+ client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
+ return client;
+ }
+
+ public Task<HttpClient> CreateClientAs(int userNumber)
+ {
+ if (userNumber < 0)
+ return CreateDefaultClient();
+ if (userNumber == 0)
+ return CreateClientWithCredential("admin", "adminpw");
+ else
+ return CreateClientWithCredential($"user{userNumber}", $"user{userNumber}pw");
+ }
+
public Task<HttpClient> CreateClientAsAdministrator()
{
return CreateClientAs(0);
@@ -152,5 +155,5 @@ namespace Timeline.Tests.IntegratedTests
{
return CreateClientAs(1);
}
- }
-}
+ }
+}