From 57c9da7f6bc98fb70e26f5332ffccfd55f7eb69b Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 11 Mar 2020 18:38:22 +0800 Subject: ... --- Timeline.ErrorCodes/ErrorCodes.cs | 3 +- Timeline.Tests/IntegratedTests/TimelineTest.cs | 137 +++++++++---------------- Timeline/Controllers/TimelineController.cs | 15 ++- Timeline/Models/Http/ErrorResponse.cs | 10 ++ Timeline/Resources/Messages.Designer.cs | 9 ++ Timeline/Resources/Messages.resx | 3 + 6 files changed, 85 insertions(+), 92 deletions(-) diff --git a/Timeline.ErrorCodes/ErrorCodes.cs b/Timeline.ErrorCodes/ErrorCodes.cs index ef150921..0af36383 100644 --- a/Timeline.ErrorCodes/ErrorCodes.cs +++ b/Timeline.ErrorCodes/ErrorCodes.cs @@ -62,7 +62,8 @@ public const int NotExist = 1_104_02_01; public const int MemberPut_NotExist = 1_104_03_01; public const int QueryRelateNotExist = 1_104_04_01; - public const int PostNoData = 1_104_05_01; + public const int PostNotExist = 1_104_05_01; + public const int PostNoData = 1_104_05_02; } } } diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index 7206c675..8801a3ef 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -77,6 +77,22 @@ namespace Timeline.Tests.IntegratedTests yield return new[] { new Func(GenerateOrdinaryTimelineUrl) }; } + private static string GeneratePersonalTimelineUrlByName(string name, string subpath = null) + { + return $"timelines/@{name}{(subpath == null ? "" : ("/" + subpath))}"; + } + + private static string GenerateOrdinaryTimelineUrlByName(string name, string subpath = null) + { + return $"timelines/{name}{(subpath == null ? "" : ("/" + subpath))}"; + } + + public static IEnumerable TimelineUrlByNameGeneratorData() + { + yield return new[] { new Func(GeneratePersonalTimelineUrlByName) }; + yield return new[] { new Func(GenerateOrdinaryTimelineUrlByName) }; + } + [Fact] public async Task Personal_TimelineGet_Should_Work() { @@ -420,140 +436,83 @@ namespace Timeline.Tests.IntegratedTests } } - [Fact] - public async Task Ordinary_InvalidModel_BadName() + [Theory] + [MemberData(nameof(TimelineUrlByNameGeneratorData))] + public async Task InvalidModel_BadName(Func generator) { using var client = await CreateClientAsAdministrator(); { - var res = await client.GetAsync("timelines/aaa!!!"); - res.Should().BeInvalidModel(); - } - { - var res = await client.PatchAsJsonAsync("timelines/aaa!!!", new TimelinePatchRequest { }); + var res = await client.GetAsync(generator("aaa!!!", null)); res.Should().BeInvalidModel(); } { - var res = await client.PutAsync("timelines/aaa!!!/members/user1", null); + var res = await client.PatchAsJsonAsync(generator("aaa!!!", null), new TimelinePatchRequest { }); res.Should().BeInvalidModel(); } { - var res = await client.DeleteAsync("timelines/aaa!!!/members/user1"); + var res = await client.PutAsync(generator("aaa!!!", "members/user1"), null); res.Should().BeInvalidModel(); } { - var res = await client.GetAsync("timelines/aaa!!!/posts"); - res.Should().BeInvalidModel(); - } - { - var res = await client.PostAsJsonAsync("timelines/aaa!!!/posts", TimelineHelper.TextPostCreateRequest("aaa")); - res.Should().BeInvalidModel(); - } - { - var res = await client.DeleteAsync("timelines/aaa!!!/posts/123"); - res.Should().BeInvalidModel(); - } - } - - [Fact] - public async Task Personal_InvalidModel_BadUsername() - { - using var client = await CreateClientAsAdministrator(); - { - var res = await client.GetAsync("timelines/@user!!!"); + var res = await client.DeleteAsync(generator("aaa!!!/members", "user1")); res.Should().BeInvalidModel(); } { - var res = await client.PatchAsJsonAsync("timelines/@user!!!", new TimelinePatchRequest { }); + var res = await client.GetAsync(generator("aaa!!!", "posts")); res.Should().BeInvalidModel(); } { - var res = await client.PutAsync("timelines/@user!!!/members/user1", null); + var res = await client.PostAsJsonAsync(generator("aaa!!!", "posts"), TimelineHelper.TextPostCreateRequest("aaa")); res.Should().BeInvalidModel(); } { - var res = await client.DeleteAsync("timelines/@user!!!/members/user1"); + var res = await client.DeleteAsync(generator("aaa!!!", "posts/123")); res.Should().BeInvalidModel(); } { - var res = await client.GetAsync("timelines/@user!!!/posts"); - res.Should().BeInvalidModel(); - } - { - var res = await client.PostAsJsonAsync("timelines/@user!!!/posts", TimelineHelper.TextPostCreateRequest("aaa")); - res.Should().BeInvalidModel(); - } - { - var res = await client.DeleteAsync("timelines/@user!!!/posts/123"); + var res = await client.GetAsync(generator("aaa!!!", "posts/123/data")); res.Should().BeInvalidModel(); } } - [Fact] - public async Task Ordinary_NotFound() + [Theory] + [MemberData(nameof(TimelineUrlByNameGeneratorData))] + public async Task Ordinary_NotFound(Func generator) { + var errorCode = generator == GenerateOrdinaryTimelineUrlByName ? ErrorCodes.TimelineController.NotExist : ErrorCodes.UserCommon.NotExist; + using var client = await CreateClientAsAdministrator(); { - var res = await client.GetAsync("timelines/notexist"); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.TimelineController.NotExist); - } - { - var res = await client.PatchAsJsonAsync("timelines/notexist", new TimelinePatchRequest { }); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.TimelineController.NotExist); + var res = await client.GetAsync(generator("notexist", null)); + res.Should().HaveStatusCode(404).And.HaveCommonBody(errorCode); } { - var res = await client.PutAsync("timelines/notexist/members/user1", null); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.TimelineController.NotExist); - } - { - var res = await client.DeleteAsync("timelines/notexist/members/user1"); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.TimelineController.NotExist); - } - { - var res = await client.GetAsync("timelines/notexist/posts"); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.TimelineController.NotExist); - } - { - var res = await client.PostAsJsonAsync("timelines/notexist/posts", TimelineHelper.TextPostCreateRequest("aaa")); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.TimelineController.NotExist); - } - { - var res = await client.DeleteAsync("timelines/notexist/posts/123"); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.TimelineController.NotExist); - } - } - - - [Fact] - public async Task PersonalNotFound() - { - using var client = await CreateClientAsAdministrator(); - { - var res = await client.GetAsync("timelines/@usernotexist"); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.UserCommon.NotExist); + var res = await client.PatchAsJsonAsync(generator("notexist", null), new TimelinePatchRequest { }); + res.Should().HaveStatusCode(404).And.HaveCommonBody(errorCode); } { - var res = await client.PatchAsJsonAsync("timelines/@usernotexist", new TimelinePatchRequest { }); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.UserCommon.NotExist); + var res = await client.PutAsync(generator("notexist", "members/user1"), null); + res.Should().HaveStatusCode(404).And.HaveCommonBody(errorCode); } { - var res = await client.PutAsync("timelines/@usernotexist/members/user1", null); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.UserCommon.NotExist); + var res = await client.DeleteAsync(generator("notexist", "members/user1")); + res.Should().HaveStatusCode(404).And.HaveCommonBody(errorCode); } { - var res = await client.DeleteAsync("timelines/@usernotexist/members/user1"); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.UserCommon.NotExist); + var res = await client.GetAsync(generator("notexist", "posts")); + res.Should().HaveStatusCode(404).And.HaveCommonBody(errorCode); } { - var res = await client.GetAsync("timelines/@usernotexist/posts"); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.UserCommon.NotExist); + var res = await client.PostAsJsonAsync(generator("notexist", "posts"), TimelineHelper.TextPostCreateRequest("aaa")); + res.Should().HaveStatusCode(404).And.HaveCommonBody(errorCode); } { - var res = await client.PostAsJsonAsync("timelines/@usernotexist/posts", TimelineHelper.TextPostCreateRequest("aaa")); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.UserCommon.NotExist); + var res = await client.DeleteAsync(generator("notexist", "posts/123")); + res.Should().HaveStatusCode(404).And.HaveCommonBody(errorCode); } { - var res = await client.DeleteAsync("timelines/@usernotexist/posts/123"); - res.Should().HaveStatusCode(404).And.HaveCommonBody(ErrorCodes.UserCommon.NotExist); + var res = await client.GetAsync(generator("notexist", "posts/123/data")); + res.Should().HaveStatusCode(404).And.HaveCommonBody(errorCode); } } diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index 0628937d..6e5438c4 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -123,8 +123,19 @@ namespace Timeline.Controllers return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } - var data = await _service.GetPostData(name, id); - return File(data.Data, data.Type, data.LastModified, new EntityTagHeaderValue(data.ETag)); + try + { + var data = await _service.GetPostData(name, id); + return File(data.Data, data.Type, data.LastModified, new EntityTagHeaderValue(data.ETag)); + } + catch (TimelinePostNotExistException) + { + return NotFound(ErrorResponse.TimelineController.PostNotExist()); + } + catch (InvalidOperationException) + { + return BadRequest(ErrorResponse.TimelineController.PostNoData()); + } } [HttpPost("timelines/{name}/posts")] diff --git a/Timeline/Models/Http/ErrorResponse.cs b/Timeline/Models/Http/ErrorResponse.cs index 9c5a7cfe..bb9c44df 100644 --- a/Timeline/Models/Http/ErrorResponse.cs +++ b/Timeline/Models/Http/ErrorResponse.cs @@ -285,6 +285,16 @@ namespace Timeline.Models.Http return new CommonResponse(ErrorCodes.TimelineController.QueryRelateNotExist, string.Format(message, formatArgs)); } + public static CommonResponse PostNotExist(params object?[] formatArgs) + { + return new CommonResponse(ErrorCodes.TimelineController.PostNotExist, string.Format(TimelineController_PostNotExist, formatArgs)); + } + + public static CommonResponse CustomMessage_PostNotExist(string message, params object?[] formatArgs) + { + return new CommonResponse(ErrorCodes.TimelineController.PostNotExist, string.Format(message, formatArgs)); + } + public static CommonResponse PostNoData(params object?[] formatArgs) { return new CommonResponse(ErrorCodes.TimelineController.PostNoData, string.Format(TimelineController_PostNoData, formatArgs)); diff --git a/Timeline/Resources/Messages.Designer.cs b/Timeline/Resources/Messages.Designer.cs index cae91093..40c4a1ce 100644 --- a/Timeline/Resources/Messages.Designer.cs +++ b/Timeline/Resources/Messages.Designer.cs @@ -222,6 +222,15 @@ namespace Timeline.Resources { } } + /// + /// Looks up a localized string similar to The post to operate on does not exist.. + /// + internal static string TimelineController_PostNotExist { + get { + return ResourceManager.GetString("TimelineController_PostNotExist", resourceCulture); + } + } + /// /// Looks up a localized string similar to The user specified by query param "relate" does not exist.. /// diff --git a/Timeline/Resources/Messages.resx b/Timeline/Resources/Messages.resx index 631bd749..8d5543fe 100644 --- a/Timeline/Resources/Messages.resx +++ b/Timeline/Resources/Messages.resx @@ -171,6 +171,9 @@ The post of that type has no data. + + The post to operate on does not exist. + The user specified by query param "relate" does not exist. -- cgit v1.2.3