diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-22 15:29:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-22 15:29:03 +0800 |
commit | 3716d2431de08194d3a107ef640febc47c3ee72a (patch) | |
tree | af83f8596b4fa78713733c0db6b4b6d1695d0ff0 /Timeline.Tests/IntegratedTests/UserDetailTest.cs | |
parent | a585c6e35829e9f2b4b0b8ce8c6b395e5ea84f2c (diff) | |
parent | 968f9688dd3ff7cae6f66af0e69bb03392311c88 (diff) | |
download | timeline-3716d2431de08194d3a107ef640febc47c3ee72a.tar.gz timeline-3716d2431de08194d3a107ef640febc47c3ee72a.tar.bz2 timeline-3716d2431de08194d3a107ef640febc47c3ee72a.zip |
Merge pull request #48 from crupest/user-details
Add user details.
Diffstat (limited to 'Timeline.Tests/IntegratedTests/UserDetailTest.cs')
-rw-r--r-- | Timeline.Tests/IntegratedTests/UserDetailTest.cs | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/Timeline.Tests/IntegratedTests/UserDetailTest.cs b/Timeline.Tests/IntegratedTests/UserDetailTest.cs new file mode 100644 index 00000000..4923cd06 --- /dev/null +++ b/Timeline.Tests/IntegratedTests/UserDetailTest.cs @@ -0,0 +1,146 @@ +using FluentAssertions;
+using Microsoft.AspNetCore.Mvc.Testing;
+using System;
+using System.Net;
+using System.Threading.Tasks;
+using Timeline.Controllers;
+using Timeline.Models;
+using Timeline.Models.Http;
+using Timeline.Tests.Helpers;
+using Timeline.Tests.Helpers.Authentication;
+using Timeline.Tests.Mock.Data;
+using Xunit;
+using Xunit.Abstractions;
+
+namespace Timeline.Tests.IntegratedTests
+{
+ public class UserDetailTest : IClassFixture<MyWebApplicationFactory<Startup>>, IDisposable
+ {
+ private readonly WebApplicationFactory<Startup> _factory;
+ private readonly Action _disposeAction;
+
+ public UserDetailTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
+ {
+ _factory = factory.WithTestConfig(outputHelper, out _disposeAction);
+ }
+
+ public void Dispose()
+ {
+ _disposeAction();
+ }
+
+ [Fact]
+ public async Task TestAsUser()
+ {
+ using (var client = await _factory.CreateClientAsUser())
+ {
+ {
+ var res = await client.GetAsync($"users/usernotexist/nickname");
+ res.Should().HaveStatusCodeNotFound()
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserDetailController.ErrorCodes.GetNickname_UserNotExist);
+ }
+
+ {
+ var res = await client.GetAsync($"users/usernotexist/details");
+ res.Should().HaveStatusCodeNotFound()
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserDetailController.ErrorCodes.Get_UserNotExist);
+ }
+
+ async Task GetAndTest(UserDetail d)
+ {
+ var res = await client.GetAsync($"users/{MockUsers.UserUsername}/details");
+ res.Should().HaveStatusCodeOk()
+ .And.Should().HaveBodyAsJson<UserDetail>()
+ .Which.Should().BeEquivalentTo(d);
+ }
+
+ await GetAndTest(new UserDetail());
+
+ {
+ var res = await client.PatchAsJsonAsync($"users/{MockUsers.AdminUsername}/details", new UserDetail());
+ res.Should().HaveStatusCode(HttpStatusCode.Forbidden)
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserDetailController.ErrorCodes.Patch_Forbid);
+ }
+
+ {
+ var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", new UserDetail
+ {
+ Nickname = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ QQ = "aaaaaaa",
+ EMail = "aaaaaa",
+ PhoneNumber = "aaaaaaaa"
+ });
+ var body = res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
+ .And.Should().HaveBodyAsCommonResponse().Which;
+ body.Code.Should().Be(CommonResponse.ErrorCodes.InvalidModel);
+ foreach (var key in new string[] { "nickname", "qq", "email", "phonenumber" })
+ {
+ body.Message.Should().ContainEquivalentOf(key);
+ }
+ }
+
+
+ var detail = new UserDetail
+ {
+ Nickname = "aaa",
+ QQ = "1234567",
+ EMail = "aaaa@aaa.net",
+ Description = "aaaaaaaaa"
+ };
+
+ {
+ var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", detail);
+ res.Should().HaveStatusCodeOk();
+ await GetAndTest(detail);
+ }
+
+ {
+ var res = await client.GetAsync($"users/{MockUsers.UserUsername}/nickname");
+ res.Should().HaveStatusCodeOk().And.Should().HaveBodyAsJson<UserDetail>()
+ .Which.Should().BeEquivalentTo(new UserDetail
+ {
+ Nickname = detail.Nickname
+ });
+ }
+
+ var detail2 = new UserDetail
+ {
+ QQ = "",
+ PhoneNumber = "12345678910",
+ Description = "bbbbbbbb"
+ };
+
+ {
+ var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", detail2);
+ res.Should().HaveStatusCodeOk();
+ await GetAndTest(new UserDetail
+ {
+ Nickname = detail.Nickname,
+ QQ = null,
+ EMail = detail.EMail,
+ PhoneNumber = detail2.PhoneNumber,
+ Description = detail2.Description
+ });
+ }
+ }
+ }
+
+ [Fact]
+ public async Task TestAsAdmin()
+ {
+ using (var client = await _factory.CreateClientAsAdmin())
+ {
+ {
+ var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", new UserDetail());
+ res.Should().HaveStatusCodeOk();
+ }
+
+ {
+ var res = await client.PatchAsJsonAsync($"users/usernotexist/details", new UserDetail());
+ res.Should().HaveStatusCodeNotFound()
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserDetailController.ErrorCodes.Patch_UserNotExist);
+ }
+ }
+ }
+ }
+}
\ No newline at end of file |