diff options
author | 杨宇千 <crupest@outlook.com> | 2019-10-17 20:46:57 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-10-17 20:46:57 +0800 |
commit | def8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4 (patch) | |
tree | dc7688d7d2dd5ab28a7e3c553154ee84676f75d2 /Timeline.Tests/IntegratedTests/AuthorizationUnitTest.cs | |
parent | 297d0c9029360f1d5334ed843b9b299356740ec1 (diff) | |
download | timeline-def8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4.tar.gz timeline-def8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4.tar.bz2 timeline-def8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4.zip |
...
Diffstat (limited to 'Timeline.Tests/IntegratedTests/AuthorizationUnitTest.cs')
-rw-r--r-- | Timeline.Tests/IntegratedTests/AuthorizationUnitTest.cs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Timeline.Tests/IntegratedTests/AuthorizationUnitTest.cs b/Timeline.Tests/IntegratedTests/AuthorizationUnitTest.cs new file mode 100644 index 00000000..a67bffcf --- /dev/null +++ b/Timeline.Tests/IntegratedTests/AuthorizationUnitTest.cs @@ -0,0 +1,68 @@ +using FluentAssertions;
+using Microsoft.AspNetCore.Mvc.Testing;
+using System;
+using System.Net;
+using System.Threading.Tasks;
+using Timeline.Tests.Helpers;
+using Timeline.Tests.Helpers.Authentication;
+using Xunit;
+
+namespace Timeline.Tests.IntegratedTests
+{
+ public class AuthorizationUnitTest : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
+ {
+ private const string AuthorizeUrl = "Test/User/Authorize";
+ private const string UserUrl = "Test/User/User";
+ private const string AdminUrl = "Test/User/Admin";
+
+ private readonly TestApplication _testApp;
+ private readonly WebApplicationFactory<Startup> _factory;
+
+ public AuthorizationUnitTest(WebApplicationFactory<Startup> factory)
+ {
+ _testApp = new TestApplication(factory);
+ _factory = _testApp.Factory;
+ }
+
+ public void Dispose()
+ {
+ _testApp.Dispose();
+ }
+
+ [Fact]
+ public async Task UnauthenticationTest()
+ {
+ using var client = _factory.CreateDefaultClient();
+ var response = await client.GetAsync(AuthorizeUrl);
+ response.Should().HaveStatusCode(HttpStatusCode.Unauthorized);
+ }
+
+ [Fact]
+ public async Task AuthenticationTest()
+ {
+ using var client = await _factory.CreateClientAsUser();
+ var response = await client.GetAsync(AuthorizeUrl);
+ response.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+
+ [Fact]
+ public async Task UserAuthorizationTest()
+ {
+ using var client = await _factory.CreateClientAsUser();
+ var response1 = await client.GetAsync(UserUrl);
+ response1.Should().HaveStatusCode(HttpStatusCode.OK);
+ var response2 = await client.GetAsync(AdminUrl);
+ response2.Should().HaveStatusCode(HttpStatusCode.Forbidden);
+ }
+
+ [Fact]
+ public async Task AdminAuthorizationTest()
+ {
+ using var client = await _factory.CreateClientAsAdmin();
+ var response1 = await client.GetAsync(UserUrl);
+ response1.Should().HaveStatusCode(HttpStatusCode.OK);
+ var response2 = await client.GetAsync(AdminUrl);
+ response2.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+ }
+}
|