diff options
Diffstat (limited to 'Timeline.Tests')
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs | 4 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/InvalidModelTestHelpers.cs | 19 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 2 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/UserInfoComparers.cs | 2 | ||||
-rw-r--r-- | Timeline.Tests/TokenUnitTest.cs | 62 | ||||
-rw-r--r-- | Timeline.Tests/UserUnitTest.cs | 2 |
6 files changed, 69 insertions, 22 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index c8bec266..de88fd35 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Mvc.Testing; 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..51919021 --- /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 d9503526..d8da7168 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/UserInfoComparers.cs b/Timeline.Tests/Helpers/UserInfoComparers.cs index c7c584b7..1a1c652d 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 f942767d..d2a68553 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 de429c7f..82284a82 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;
|