aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/AuthorizationUnitTest.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-02-14 23:05:04 +0800
committercrupest <crupest@outlook.com>2019-02-14 23:05:04 +0800
commit7024bc9666e62cc33f651e7a060235d17bd51f25 (patch)
treeb9762a15e83391d8b7b4d76aa76f125557bb0e51 /Timeline.Tests/AuthorizationUnitTest.cs
parent44645b381ffe399e444444cc0011287d4e09fd7e (diff)
downloadtimeline-7024bc9666e62cc33f651e7a060235d17bd51f25.tar.gz
timeline-7024bc9666e62cc33f651e7a060235d17bd51f25.tar.bz2
timeline-7024bc9666e62cc33f651e7a060235d17bd51f25.zip
Develop user token interface.
Diffstat (limited to 'Timeline.Tests/AuthorizationUnitTest.cs')
-rw-r--r--Timeline.Tests/AuthorizationUnitTest.cs47
1 files changed, 14 insertions, 33 deletions
diff --git a/Timeline.Tests/AuthorizationUnitTest.cs b/Timeline.Tests/AuthorizationUnitTest.cs
index e9e86c8e..1566f2ac 100644
--- a/Timeline.Tests/AuthorizationUnitTest.cs
+++ b/Timeline.Tests/AuthorizationUnitTest.cs
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
using System;
-using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Timeline.Controllers;
using Timeline.Tests.Helpers;
+using Timeline.Tests.Helpers.Authentication;
using Xunit;
using Xunit.Abstractions;
@@ -14,6 +14,10 @@ namespace Timeline.Tests
{
public class AuthorizationUnitTest : IClassFixture<WebApplicationFactory<Startup>>
{
+ private const string NeedAuthorizeUrl = "api/test/User/NeedAuthorize";
+ private const string BothUserAndAdminUrl = "api/test/User/BothUserAndAdmin";
+ private const string OnlyAdminUrl = "api/test/User/OnlyAdmin";
+
private readonly WebApplicationFactory<Startup> _factory;
public AuthorizationUnitTest(WebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
@@ -26,41 +30,18 @@ namespace Timeline.Tests
{
using (var client = _factory.CreateDefaultClient())
{
- var response = await client.GetAsync("/api/Test/Action1");
+ var response = await client.GetAsync(NeedAuthorizeUrl);
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
}
}
- private static async Task<string> Login(HttpClient client, string username, string password)
- {
- var response = await client.PostAsJsonAsync("/api/User/LogIn", new UserController.UserCredentials { Username = username, Password = password });
-
- Assert.Equal(HttpStatusCode.OK, response.StatusCode);
-
- var loginInfo = JsonConvert.DeserializeObject<UserController.LoginInfo>(await response.Content.ReadAsStringAsync());
-
- return loginInfo.Token;
- }
-
- private static async Task<HttpResponseMessage> GetWithAuthentication(HttpClient client, string path, string token)
- {
- var request = new HttpRequestMessage
- {
- RequestUri = new Uri(client.BaseAddress, path),
- Method = HttpMethod.Get
- };
- request.Headers.Add("Authorization", "Bearer " + token);
-
- return await client.SendAsync(request);
- }
-
[Fact]
public async Task AuthenticationTest()
{
using (var client = _factory.CreateDefaultClient())
{
- var token = await Login(client, "user", "user");
- var response = await GetWithAuthentication(client, "/api/Test/Action1", token);
+ var token = (await client.CreateUserTokenAsync("user", "user")).Token;
+ var response = await client.SendWithAuthenticationAsync(token, NeedAuthorizeUrl);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
@@ -70,10 +51,10 @@ namespace Timeline.Tests
{
using (var client = _factory.CreateDefaultClient())
{
- var token = await Login(client, "user", "user");
- var response1 = await GetWithAuthentication(client, "/api/Test/Action2", token);
+ var token = (await client.CreateUserTokenAsync("user", "user")).Token;
+ var response1 = await client.SendWithAuthenticationAsync(token, BothUserAndAdminUrl);
Assert.Equal(HttpStatusCode.OK, response1.StatusCode);
- var response2 = await GetWithAuthentication(client, "/api/Test/Action3", token);
+ var response2 = await client.SendWithAuthenticationAsync(token, OnlyAdminUrl);
Assert.Equal(HttpStatusCode.Forbidden, response2.StatusCode);
}
}
@@ -83,10 +64,10 @@ namespace Timeline.Tests
{
using (var client = _factory.CreateDefaultClient())
{
- var token = await Login(client, "admin", "admin");
- var response1 = await GetWithAuthentication(client, "/api/Test/Action2", token);
+ var token = (await client.CreateUserTokenAsync("admin", "admin")).Token;
+ var response1 = await client.SendWithAuthenticationAsync(token, BothUserAndAdminUrl);
Assert.Equal(HttpStatusCode.OK, response1.StatusCode);
- var response2 = await GetWithAuthentication(client, "/api/Test/Action3", token);
+ var response2 = await client.SendWithAuthenticationAsync(token, OnlyAdminUrl);
Assert.Equal(HttpStatusCode.OK, response2.StatusCode);
}
}