aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-04 15:02:58 +0800
committer杨宇千 <crupest@outlook.com>2019-08-04 15:02:58 +0800
commita17aa8770b0b4861849c6e01812b2ff686497f02 (patch)
tree6d1afc3dbe3b91122a1c95ace2aafee76967e230
parentc1586e28e44835b3636fb63e31f613de4a30bc1e (diff)
downloadtimeline-a17aa8770b0b4861849c6e01812b2ff686497f02.tar.gz
timeline-a17aa8770b0b4861849c6e01812b2ff686497f02.tar.bz2
timeline-a17aa8770b0b4861849c6e01812b2ff686497f02.zip
Add unit tests for token.
-rw-r--r--Timeline.Tests/Helpers/ResponseExtensions.cs14
-rw-r--r--Timeline.Tests/TokenUnitTest.cs (renamed from Timeline.Tests/JwtTokenUnitTest.cs)44
-rw-r--r--Timeline/Controllers/TokenController.cs2
3 files changed, 51 insertions, 9 deletions
diff --git a/Timeline.Tests/Helpers/ResponseExtensions.cs b/Timeline.Tests/Helpers/ResponseExtensions.cs
new file mode 100644
index 00000000..86ac1c88
--- /dev/null
+++ b/Timeline.Tests/Helpers/ResponseExtensions.cs
@@ -0,0 +1,14 @@
+using Newtonsoft.Json;
+using System.Net.Http;
+using System.Threading.Tasks;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class ResponseExtensions
+ {
+ public static async Task<T> ReadBodyAsJson<T>(this HttpResponseMessage response)
+ {
+ return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
+ }
+ }
+}
diff --git a/Timeline.Tests/JwtTokenUnitTest.cs b/Timeline.Tests/TokenUnitTest.cs
index 6c0d4213..27c2ed32 100644
--- a/Timeline.Tests/JwtTokenUnitTest.cs
+++ b/Timeline.Tests/TokenUnitTest.cs
@@ -1,7 +1,9 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
+using System.Linq;
using System.Net;
using System.Net.Http;
+using Timeline.Controllers;
using Timeline.Entities.Http;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
@@ -10,38 +12,64 @@ using Xunit.Abstractions;
namespace Timeline.Tests
{
- public class JwtTokenUnitTest : IClassFixture<WebApplicationFactory<Startup>>
+ public class TokenUnitTest : IClassFixture<WebApplicationFactory<Startup>>
{
private const string CreateTokenUrl = "token/create";
private const string VerifyTokenUrl = "token/verify";
private readonly WebApplicationFactory<Startup> _factory;
- public JwtTokenUnitTest(WebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
+ public TokenUnitTest(WebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
{
_factory = factory.WithTestConfig(outputHelper);
}
[Fact]
- public async void CreateTokenTest_BadCredential()
+ public async void CreateTokenTest_UserNotExist()
{
using (var client = _factory.CreateDefaultClient())
{
- var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "???", Password = "???" });
+ var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "usernotexist", Password = "???" });
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ var body = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(TokenController.ErrorCodes.Create_UserNotExist, body.Code);
}
}
[Fact]
- public async void CreateTokenTest_GoodCredential()
+ public async void CreateTokenTest_BadPassword()
+ {
+ using (var client = _factory.CreateDefaultClient())
+ {
+ var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "user", Password = "???" });
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ var body = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(TokenController.ErrorCodes.Create_BadPassword, body.Code);
+ }
+ }
+
+ [Fact]
+ public async void CreateTokenTest_BadExpireOffset()
+ {
+ using (var client = _factory.CreateDefaultClient())
+ {
+ var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "???", Password = "???", ExpireOffset = -1000 });
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ var body = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(TokenController.ErrorCodes.Create_BadExpireOffset, body.Code);
+ }
+ }
+
+ [Fact]
+ public async void CreateTokenTest_Success()
{
using (var client = _factory.CreateDefaultClient())
{
var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "user", Password = "user" });
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
- var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
- Assert.NotNull(result.Token);
- Assert.NotNull(result.User);
+ var body = await response.ReadBodyAsJson<CreateTokenResponse>();
+ Assert.NotEmpty(body.Token);
+ Assert.Equal(TestMockUsers.MockUserInfos.Where(u => u.Username == "user").Single(), body.User, UserInfoComparers.EqualityComparer);
}
}
diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs
index f9dcfd76..66cf3dad 100644
--- a/Timeline/Controllers/TokenController.cs
+++ b/Timeline/Controllers/TokenController.cs
@@ -20,7 +20,7 @@ namespace Timeline.Controllers
public const int VerifyFailed = 2001;
}
- private static class ErrorCodes
+ public static class ErrorCodes
{
public const int Create_UserNotExist = -1001;
public const int Create_BadPassword = -1002;