aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/IntegratedTests
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline.Tests/IntegratedTests')
-rw-r--r--Timeline.Tests/IntegratedTests/TokenTest.cs12
-rw-r--r--Timeline.Tests/IntegratedTests/UserAvatarTest.cs35
-rw-r--r--Timeline.Tests/IntegratedTests/UserDetailTest.cs164
-rw-r--r--Timeline.Tests/IntegratedTests/UserTest.cs16
4 files changed, 195 insertions, 32 deletions
diff --git a/Timeline.Tests/IntegratedTests/TokenTest.cs b/Timeline.Tests/IntegratedTests/TokenTest.cs
index e9b6e1e9..111e8d8e 100644
--- a/Timeline.Tests/IntegratedTests/TokenTest.cs
+++ b/Timeline.Tests/IntegratedTests/TokenTest.cs
@@ -69,7 +69,7 @@ namespace Timeline.Tests.IntegratedTests
var response = await client.PostAsJsonAsync(CreateTokenUrl,
new CreateTokenRequest { Username = username, Password = password });
response.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Create.BadCredential);
}
@@ -80,7 +80,7 @@ namespace Timeline.Tests.IntegratedTests
var response = await client.PostAsJsonAsync(CreateTokenUrl,
new CreateTokenRequest { Username = MockUser.User.Username, Password = MockUser.User.Password });
var body = response.Should().HaveStatusCode(200)
- .And.Should().HaveJsonBody<CreateTokenResponse>().Which;
+ .And.HaveJsonBody<CreateTokenResponse>().Which;
body.Token.Should().NotBeNullOrWhiteSpace();
body.User.Should().BeEquivalentTo(MockUser.User.Info);
}
@@ -100,7 +100,7 @@ namespace Timeline.Tests.IntegratedTests
var response = await client.PostAsJsonAsync(VerifyTokenUrl,
new VerifyTokenRequest { Token = "bad token hahaha" });
response.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Verify.BadFormat);
}
@@ -120,7 +120,7 @@ namespace Timeline.Tests.IntegratedTests
(await client.PostAsJsonAsync(VerifyTokenUrl,
new VerifyTokenRequest { Token = token }))
.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Verify.OldVersion);
}
@@ -139,7 +139,7 @@ namespace Timeline.Tests.IntegratedTests
(await client.PostAsJsonAsync(VerifyTokenUrl,
new VerifyTokenRequest { Token = token }))
.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Verify.UserNotExist);
}
@@ -169,7 +169,7 @@ namespace Timeline.Tests.IntegratedTests
var response = await client.PostAsJsonAsync(VerifyTokenUrl,
new VerifyTokenRequest { Token = createTokenResult.Token });
response.Should().HaveStatusCode(200)
- .And.Should().HaveJsonBody<VerifyTokenResponse>()
+ .And.HaveJsonBody<VerifyTokenResponse>()
.Which.User.Should().BeEquivalentTo(MockUser.User.Info);
}
}
diff --git a/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/Timeline.Tests/IntegratedTests/UserAvatarTest.cs
index ce389046..2310fc66 100644
--- a/Timeline.Tests/IntegratedTests/UserAvatarTest.cs
+++ b/Timeline.Tests/IntegratedTests/UserAvatarTest.cs
@@ -13,7 +13,6 @@ using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
-using Timeline.Controllers;
using Timeline.Services;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
@@ -23,12 +22,12 @@ using static Timeline.ErrorCodes.Http.UserAvatar;
namespace Timeline.Tests.IntegratedTests
{
- public class UserAvatarUnitTest : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
+ public class UserAvatarTest : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
{
private readonly TestApplication _testApp;
private readonly WebApplicationFactory<Startup> _factory;
- public UserAvatarUnitTest(WebApplicationFactory<Startup> factory)
+ public UserAvatarTest(WebApplicationFactory<Startup> factory)
{
_testApp = new TestApplication(factory);
_factory = _testApp.Factory;
@@ -54,7 +53,7 @@ namespace Timeline.Tests.IntegratedTests
{
var res = await client.GetAsync("users/usernotexist/avatar");
res.Should().HaveStatusCode(404)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Get.UserNotExist);
}
@@ -95,7 +94,7 @@ namespace Timeline.Tests.IntegratedTests
request.Headers.TryAddWithoutValidation("If-None-Match", "\"dsdfd");
var res = await client.SendAsync(request);
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Common.Header.BadFormat_IfNonMatch);
+ .And.HaveCommonBody().Which.Code.Should().Be(Header.IfNonMatch.BadFormat);
}
{
@@ -125,7 +124,7 @@ namespace Timeline.Tests.IntegratedTests
content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
var res = await client.PutAsync("users/user/avatar", content);
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Common.Header.Missing_ContentLength);
+ .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Filter.Header.ContentLength.Missing); ;
}
{
@@ -133,7 +132,7 @@ namespace Timeline.Tests.IntegratedTests
content.Headers.ContentLength = 1;
var res = await client.PutAsync("users/user/avatar", content);
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Common.Header.Missing_ContentType);
+ .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Filter.Header.ContentType.Missing);
}
{
@@ -142,7 +141,7 @@ namespace Timeline.Tests.IntegratedTests
content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
var res = await client.PutAsync("users/user/avatar", content);
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Common.Header.Zero_ContentLength);
+ .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Http.Filter.Header.ContentLength.Zero);
}
{
@@ -156,7 +155,7 @@ namespace Timeline.Tests.IntegratedTests
content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
var res = await client.PutAsync("users/user/avatar", content);
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Content.TooBig);
+ .And.HaveCommonBody().Which.Code.Should().Be(Content.TooBig);
}
{
@@ -165,7 +164,7 @@ namespace Timeline.Tests.IntegratedTests
content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
var res = await client.PutAsync("users/user/avatar", content);
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Content.UnmatchedLength_Smaller);
+ .And.HaveCommonBody().Which.Code.Should().Be(Content.UnmatchedLength_Smaller);
}
{
@@ -174,25 +173,25 @@ namespace Timeline.Tests.IntegratedTests
content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
var res = await client.PutAsync("users/user/avatar", content);
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Content.UnmatchedLength_Bigger);
+ .And.HaveCommonBody().Which.Code.Should().Be(Content.UnmatchedLength_Bigger);
}
{
var res = await client.PutByteArrayAsync("users/user/avatar", new[] { (byte)0x00 }, "image/png");
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Put.BadFormat_CantDecode);
+ .And.HaveCommonBody().Which.Code.Should().Be(Put.BadFormat_CantDecode);
}
{
var res = await client.PutByteArrayAsync("users/user/avatar", mockAvatar.Data, "image/jpeg");
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Put.BadFormat_UnmatchedFormat);
+ .And.HaveCommonBody().Which.Code.Should().Be(Put.BadFormat_UnmatchedFormat);
}
{
var res = await client.PutByteArrayAsync("users/user/avatar", ImageHelper.CreatePngWithSize(100, 200), "image/png");
res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Put.BadFormat_BadSize);
+ .And.HaveCommonBody().Which.Code.Should().Be(Put.BadFormat_BadSize);
}
{
@@ -222,13 +221,13 @@ namespace Timeline.Tests.IntegratedTests
{
var res = await client.PutByteArrayAsync("users/admin/avatar", new[] { (byte)0x00 }, "image/png");
res.Should().HaveStatusCode(HttpStatusCode.Forbidden)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Put.Forbid);
+ .And.HaveCommonBody().Which.Code.Should().Be(Put.Forbid);
}
{
var res = await client.DeleteAsync("users/admin/avatar");
res.Should().HaveStatusCode(HttpStatusCode.Forbidden)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Delete.Forbid);
+ .And.HaveCommonBody().Which.Code.Should().Be(Delete.Forbid);
}
for (int i = 0; i < 2; i++) // double delete should work.
@@ -255,14 +254,14 @@ namespace Timeline.Tests.IntegratedTests
{
var res = await client.PutByteArrayAsync("users/usernotexist/avatar", new[] { (byte)0x00 }, "image/png");
res.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Put.UserNotExist);
}
{
var res = await client.DeleteAsync("users/usernotexist/avatar");
res.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody().Which.Code.Should().Be(Delete.UserNotExist);
+ .And.HaveCommonBody().Which.Code.Should().Be(Delete.UserNotExist);
}
}
diff --git a/Timeline.Tests/IntegratedTests/UserDetailTest.cs b/Timeline.Tests/IntegratedTests/UserDetailTest.cs
new file mode 100644
index 00000000..8f2b6925
--- /dev/null
+++ b/Timeline.Tests/IntegratedTests/UserDetailTest.cs
@@ -0,0 +1,164 @@
+using FluentAssertions;
+using Microsoft.AspNetCore.Mvc.Testing;
+using System;
+using System.Net;
+using System.Net.Http.Headers;
+using System.Net.Mime;
+using System.Threading.Tasks;
+using Timeline.Tests.Helpers;
+using Timeline.Tests.Helpers.Authentication;
+using Timeline.Tests.Mock.Data;
+using Xunit;
+
+namespace Timeline.Tests.IntegratedTests
+{
+ public class UserDetailTest : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
+ {
+ private readonly TestApplication _testApp;
+ private readonly WebApplicationFactory<Startup> _factory;
+
+ public UserDetailTest(WebApplicationFactory<Startup> factory)
+ {
+ _testApp = new TestApplication(factory);
+ _factory = _testApp.Factory;
+ }
+
+ public void Dispose()
+ {
+ _testApp.Dispose();
+ }
+
+ [Fact]
+ public async Task PermissionTest()
+ {
+ { // unauthorize
+ using var client = _factory.CreateDefaultClient();
+ { // GET
+ var res = await client.GetAsync($"users/{MockUser.User.Username}/nickname");
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+ { // PUT
+ var res = await client.PutStringAsync($"users/{MockUser.User.Username}/nickname", "aaa");
+ res.Should().HaveStatusCode(HttpStatusCode.Unauthorized);
+ }
+ { // DELETE
+ var res = await client.DeleteAsync($"users/{MockUser.User.Username}/nickname");
+ res.Should().HaveStatusCode(HttpStatusCode.Unauthorized);
+ }
+ }
+ { // user
+ using var client = await _factory.CreateClientAsUser();
+ { // GET
+ var res = await client.GetAsync($"users/{MockUser.User.Username}/nickname");
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+ { // PUT self
+ var res = await client.PutStringAsync($"users/{MockUser.User.Username}/nickname", "aaa");
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+ { // PUT other
+ var res = await client.PutStringAsync($"users/{MockUser.Admin.Username}/nickname", "aaa");
+ res.Should().HaveStatusCode(HttpStatusCode.Forbidden);
+ }
+ { // DELETE self
+ var res = await client.DeleteAsync($"users/{MockUser.User.Username}/nickname");
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+ { // DELETE other
+ var res = await client.DeleteAsync($"users/{MockUser.Admin.Username}/nickname");
+ res.Should().HaveStatusCode(HttpStatusCode.Forbidden);
+ }
+ }
+ { // user
+ using var client = await _factory.CreateClientAsAdmin();
+ { // PUT other
+ var res = await client.PutStringAsync($"users/{MockUser.User.Username}/nickname", "aaa");
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+ { // DELETE other
+ var res = await client.DeleteAsync($"users/{MockUser.User.Username}/nickname");
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+ }
+ }
+
+ [Fact]
+ public async Task FunctionTest()
+ {
+ var url = $"users/{MockUser.User.Username}/nickname";
+ var userNotExistUrl = "users/usernotexist/nickname";
+ {
+ using var client = await _factory.CreateClientAsUser();
+ {
+ var res = await client.GetAsync(userNotExistUrl);
+ res.Should().HaveStatusCode(HttpStatusCode.NotFound)
+ .And.HaveCommonBody()
+ .Which.Code.Should().Be(ErrorCodes.Http.Filter.User.NotExist);
+
+ }
+ {
+ var res = await client.GetAsync(url);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ res.Content.Headers.ContentType.Should().Be(new MediaTypeHeaderValue(MediaTypeNames.Text.Plain) { CharSet = "utf-8" });
+ var body = await res.Content.ReadAsStringAsync();
+ body.Should().Be(MockUser.User.Username);
+ }
+ {
+ var res = await client.PutStringAsync(url, "");
+ res.Should().BeInvalidModel();
+ }
+ {
+ var res = await client.PutStringAsync(url, new string('a', 11));
+ res.Should().BeInvalidModel();
+ }
+ var nickname1 = "nnn";
+ var nickname2 = "nn2";
+ {
+ var res = await client.PutStringAsync(url, nickname1);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ (await client.GetStringAsync(url)).Should().Be(nickname1);
+ }
+ {
+ var res = await client.PutStringAsync(url, nickname2);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ (await client.GetStringAsync(url)).Should().Be(nickname2);
+ }
+ {
+ var res = await client.DeleteAsync(url);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ (await client.GetStringAsync(url)).Should().Be(MockUser.User.Username);
+ }
+ {
+ var res = await client.DeleteAsync(url);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+ }
+ {
+ using var client = await _factory.CreateClientAsAdmin();
+ {
+ var res = await client.PutStringAsync(userNotExistUrl, "aaa");
+ res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
+ .And.HaveCommonBody()
+ .Which.Code.Should().Be(ErrorCodes.Http.Filter.User.NotExist);
+ }
+ {
+ var res = await client.DeleteAsync(userNotExistUrl);
+ res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
+ .And.HaveCommonBody()
+ .Which.Code.Should().Be(ErrorCodes.Http.Filter.User.NotExist);
+ }
+ var nickname = "nnn";
+ {
+ var res = await client.PutStringAsync(url, nickname);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ (await client.GetStringAsync(url)).Should().Be(nickname);
+ }
+ {
+ var res = await client.DeleteAsync(url);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ (await client.GetStringAsync(url)).Should().Be(MockUser.User.Username);
+ }
+ }
+ }
+ }
+}
diff --git a/Timeline.Tests/IntegratedTests/UserTest.cs b/Timeline.Tests/IntegratedTests/UserTest.cs
index ec70b7e8..7e99ddba 100644
--- a/Timeline.Tests/IntegratedTests/UserTest.cs
+++ b/Timeline.Tests/IntegratedTests/UserTest.cs
@@ -36,7 +36,7 @@ namespace Timeline.Tests.IntegratedTests
using var client = await _factory.CreateClientAsAdmin();
var res = await client.GetAsync("users");
res.Should().HaveStatusCode(200)
- .And.Should().HaveJsonBody<UserInfo[]>()
+ .And.HaveJsonBody<UserInfo[]>()
.Which.Should().BeEquivalentTo(MockUser.UserInfoList);
}
@@ -46,7 +46,7 @@ namespace Timeline.Tests.IntegratedTests
using var client = await _factory.CreateClientAsAdmin();
var res = await client.GetAsync("users/" + MockUser.User.Username);
res.Should().HaveStatusCode(200)
- .And.Should().HaveJsonBody<UserInfo>()
+ .And.HaveJsonBody<UserInfo>()
.Which.Should().BeEquivalentTo(MockUser.User.Info);
}
@@ -64,7 +64,7 @@ namespace Timeline.Tests.IntegratedTests
using var client = await _factory.CreateClientAsAdmin();
var res = await client.GetAsync("users/usernotexist");
res.Should().HaveStatusCode(404)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Get.NotExist);
}
@@ -89,7 +89,7 @@ namespace Timeline.Tests.IntegratedTests
{
var res = await client.GetAsync("users/" + username);
res.Should().HaveStatusCode(200)
- .And.Should().HaveJsonBody<UserInfo>()
+ .And.HaveJsonBody<UserInfo>()
.Which.Administrator.Should().Be(administrator);
}
@@ -128,7 +128,7 @@ namespace Timeline.Tests.IntegratedTests
using var client = await _factory.CreateClientAsAdmin();
var res = await client.PatchAsJsonAsync("users/usernotexist", new UserPatchRequest { });
res.Should().HaveStatusCode(404)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Patch.NotExist);
}
@@ -208,7 +208,7 @@ namespace Timeline.Tests.IntegratedTests
var res = await client.PostAsJsonAsync(changeUsernameUrl,
new ChangeUsernameRequest { OldUsername = "usernotexist", NewUsername = "newUsername" });
res.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Op.ChangeUsername.NotExist);
}
@@ -219,7 +219,7 @@ namespace Timeline.Tests.IntegratedTests
var res = await client.PostAsJsonAsync(changeUsernameUrl,
new ChangeUsernameRequest { OldUsername = MockUser.User.Username, NewUsername = MockUser.Admin.Username });
res.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Op.ChangeUsername.AlreadyExist);
}
@@ -258,7 +258,7 @@ namespace Timeline.Tests.IntegratedTests
using var client = await _factory.CreateClientAsUser();
var res = await client.PostAsJsonAsync(changePasswordUrl, new ChangePasswordRequest { OldPassword = "???", NewPassword = "???" });
res.Should().HaveStatusCode(400)
- .And.Should().HaveCommonBody()
+ .And.HaveCommonBody()
.Which.Code.Should().Be(Op.ChangePassword.BadOldPassword);
}