aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-05 18:58:56 +0800
committer杨宇千 <crupest@outlook.com>2019-08-05 18:58:56 +0800
commit57151c879376374425adb04fb68dad2cf7930df8 (patch)
treeb5f26eabcfa547e76dd7e70ffe0763f3fadf0ad5 /Timeline.Tests
parent0c0e0c963458aae3ba9589622fc688388833fa9c (diff)
downloadtimeline-57151c879376374425adb04fb68dad2cf7930df8.tar.gz
timeline-57151c879376374425adb04fb68dad2cf7930df8.tar.bz2
timeline-57151c879376374425adb04fb68dad2cf7930df8.zip
3 things.
1. Exchange Models and Entities namespace. 2. Fix the bug that input with missing field leads to 500. 3. Write unit tests.
Diffstat (limited to 'Timeline.Tests')
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs4
-rw-r--r--Timeline.Tests/Helpers/InvalidModelTestHelpers.cs19
-rw-r--r--Timeline.Tests/Helpers/MyWebApplicationFactory.cs2
-rw-r--r--Timeline.Tests/Helpers/ResponseExtensions.cs2
-rw-r--r--Timeline.Tests/Helpers/UserInfoComparers.cs2
-rw-r--r--Timeline.Tests/TokenUnitTest.cs62
-rw-r--r--Timeline.Tests/UserUnitTest.cs2
7 files changed, 70 insertions, 23 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
index 27362ac3..92617588 100644
--- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
+++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
@@ -2,7 +2,7 @@
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
-using Timeline.Entities.Http;
+using Timeline.Models.Http;
namespace Timeline.Tests.Helpers.Authentication
{
@@ -10,7 +10,7 @@ namespace Timeline.Tests.Helpers.Authentication
{
private const string CreateTokenUrl = "/token/create";
- public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, double? expireOffset = null)
+ public static async Task<CreateTokenResponse> CreateUserTokenAsync(this HttpClient client, string username, string password, int? expireOffset = null)
{
var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, ExpireOffset = expireOffset });
var result = JsonConvert.DeserializeObject<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
diff --git a/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs b/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs
new file mode 100644
index 00000000..d35982d4
--- /dev/null
+++ b/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs
@@ -0,0 +1,19 @@
+using System.Net;
+using System.Net.Http;
+using System.Threading.Tasks;
+using Timeline.Models.Http;
+using Xunit;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class InvalidModelTestHelpers
+ {
+ public static async Task TestPostInvalidModel<T>(HttpClient client, string url, T body)
+ {
+ var response = await client.PostAsJsonAsync(url, body);
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ var responseBody = await response.ReadBodyAsJson<CommonResponse>();
+ Assert.Equal(CommonResponse.ErrorCodes.InvalidModel, responseBody.Code);
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
index 903cd670..ef207fd2 100644
--- a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
+++ b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
@@ -5,7 +5,7 @@ using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
-using Timeline.Models;
+using Timeline.Entities;
using Timeline.Services;
using Xunit.Abstractions;
diff --git a/Timeline.Tests/Helpers/ResponseExtensions.cs b/Timeline.Tests/Helpers/ResponseExtensions.cs
index 86ac1c88..9ca583fc 100644
--- a/Timeline.Tests/Helpers/ResponseExtensions.cs
+++ b/Timeline.Tests/Helpers/ResponseExtensions.cs
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
-
+
namespace Timeline.Tests.Helpers
{
public static class ResponseExtensions
diff --git a/Timeline.Tests/Helpers/UserInfoComparers.cs b/Timeline.Tests/Helpers/UserInfoComparers.cs
index fcf37e5c..3068b6dd 100644
--- a/Timeline.Tests/Helpers/UserInfoComparers.cs
+++ b/Timeline.Tests/Helpers/UserInfoComparers.cs
@@ -1,5 +1,5 @@
using System.Collections.Generic;
-using Timeline.Entities;
+using Timeline.Models;
namespace Timeline.Tests.Helpers
{
diff --git a/Timeline.Tests/TokenUnitTest.cs b/Timeline.Tests/TokenUnitTest.cs
index 7b83cd13..5c7496b5 100644
--- a/Timeline.Tests/TokenUnitTest.cs
+++ b/Timeline.Tests/TokenUnitTest.cs
@@ -6,7 +6,7 @@ using System.Linq;
using System.Net;
using System.Net.Http;
using Timeline.Controllers;
-using Timeline.Entities.Http;
+using Timeline.Models.Http;
using Timeline.Services;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
@@ -28,43 +28,61 @@ namespace Timeline.Tests
}
[Fact]
- public async void CreateTokenTest_UserNotExist()
+ public async void CreateToken_MissingUsername()
{
using (var client = _factory.CreateDefaultClient())
{
- 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);
+ await InvalidModelTestHelpers.TestPostInvalidModel(client, CreateTokenUrl,
+ new CreateTokenRequest { Username = null, Password = "user" });
}
}
[Fact]
- public async void CreateTokenTest_BadPassword()
+ public async void CreateToken_InvalidModel_MissingPassword()
{
using (var client = _factory.CreateDefaultClient())
{
- var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "user", Password = "???" });
+ await InvalidModelTestHelpers.TestPostInvalidModel(client, CreateTokenUrl,
+ new CreateTokenRequest { Username = "user", Password = null });
+ }
+ }
+
+ [Fact]
+ public async void CreateToken_InvalidModel_BadExpireOffset()
+ {
+ using (var client = _factory.CreateDefaultClient())
+ {
+ await InvalidModelTestHelpers.TestPostInvalidModel(client, CreateTokenUrl,
+ new CreateTokenRequest { Username = "user", Password = "user", ExpireOffset = -1000 });
+ }
+ }
+
+ [Fact]
+ public async void CreateToken_UserNotExist()
+ {
+ using (var client = _factory.CreateDefaultClient())
+ {
+ 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_BadPassword, body.Code);
+ Assert.Equal(TokenController.ErrorCodes.Create_UserNotExist, body.Code);
}
}
[Fact]
- public async void CreateTokenTest_BadExpireOffset()
+ public async void CreateToken_BadPassword()
{
using (var client = _factory.CreateDefaultClient())
{
- var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = "???", Password = "???", ExpireOffset = -1000 });
+ 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_BadExpireOffset, body.Code);
+ Assert.Equal(TokenController.ErrorCodes.Create_BadPassword, body.Code);
}
}
[Fact]
- public async void CreateTokenTest_Success()
+ public async void CreateToken_Success()
{
using (var client = _factory.CreateDefaultClient())
{
@@ -77,7 +95,17 @@ namespace Timeline.Tests
}
[Fact]
- public async void VerifyTokenTest_BadToken()
+ public async void VerifyToken_InvalidModel_MissingToken()
+ {
+ using (var client = _factory.CreateDefaultClient())
+ {
+ await InvalidModelTestHelpers.TestPostInvalidModel(client, VerifyTokenUrl,
+ new VerifyTokenRequest { Token = null });
+ }
+ }
+
+ [Fact]
+ public async void VerifyToken_BadToken()
{
using (var client = _factory.CreateDefaultClient())
{
@@ -89,7 +117,7 @@ namespace Timeline.Tests
}
[Fact]
- public async void VerifyTokenTest_BadVersion_AND_UserNotExist()
+ public async void VerifyToken_BadVersion_AND_UserNotExist()
{
using (var client = _factory.CreateDefaultClient())
{
@@ -131,7 +159,7 @@ namespace Timeline.Tests
}
[Fact]
- public async void VerifyTokenTest_Expired()
+ public async void VerifyToken_Expired()
{
using (var client = _factory.CreateDefaultClient())
{
@@ -148,7 +176,7 @@ namespace Timeline.Tests
}
[Fact]
- public async void VerifyTokenTest_Success()
+ public async void VerifyToken_Success()
{
using (var client = _factory.CreateDefaultClient())
{
diff --git a/Timeline.Tests/UserUnitTest.cs b/Timeline.Tests/UserUnitTest.cs
index b3377f7b..5728879b 100644
--- a/Timeline.Tests/UserUnitTest.cs
+++ b/Timeline.Tests/UserUnitTest.cs
@@ -3,7 +3,7 @@ using Newtonsoft.Json;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
-using Timeline.Entities;
+using Timeline.Models;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
using Xunit;