From d30c011d70453029a7a5c27b00f7f33357c3a9c2 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 11 Feb 2021 23:22:50 +0800 Subject: test: Add create post invalid model test. --- .../IntegratedTests/TimelinePostTest.cs | 142 +++++++++++++++++++++ 1 file changed, 142 insertions(+) (limited to 'BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index bd79ae18..08f88dfc 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -7,6 +7,7 @@ using System.Text; using System.Threading.Tasks; using Timeline.Models; using Timeline.Models.Http; +using Timeline.Tests.Helpers; using Xunit; using Xunit.Abstractions; @@ -343,5 +344,146 @@ namespace Timeline.Tests.IntegratedTests post3.Time.Should().Be(date); post3.Color.Should().Be("#aabbcc"); } + + [Theory] + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task CreatePost_InvalidModel(TimelineNameGenerator generator) + { + using var client = await CreateClientAsUser(); + + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest { DataList = null! } + ); + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest { DataList = new List() } + ); + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = Enumerable.Repeat(new HttpTimelinePostCreateRequestData + { + ContentType = "text/plain", + Data = Convert.ToBase64String(Encoding.UTF8.GetBytes("a")) + }, 200).ToList() + } + ); + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = new List() + { + new HttpTimelinePostCreateRequestData + { + ContentType = null!, + Data = Convert.ToBase64String(Encoding.UTF8.GetBytes("a")) + } + } + } + ); + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = new List() + { + new HttpTimelinePostCreateRequestData + { + ContentType = "text/plain", + Data = null! + } + } + } + ); + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = new List() + { + new HttpTimelinePostCreateRequestData + { + ContentType = "text/xxxxxxx", + Data = Convert.ToBase64String(Encoding.UTF8.GetBytes("a")) + } + } + } + ); + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = new List() + { + new HttpTimelinePostCreateRequestData + { + ContentType = "text/plain", + Data = "aaa" + } + } + } + ); + } + + [Theory] + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task CreatePost_InvalidModel_NonUtf8(TimelineNameGenerator generator) + { + using var client = await CreateClientAsUser(); + + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = new List() + { + new HttpTimelinePostCreateRequestData + { + ContentType = "text/plain", + Data = Convert.ToBase64String(new byte[] {0xE4, 0x1, 0xA0}) + } + } + } + ); + } + + [Theory] + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task CreatePost_InvalidModel_Image(TimelineNameGenerator generator) + { + using var client = await CreateClientAsUser(); + + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = new List() + { + new HttpTimelinePostCreateRequestData + { + ContentType = "image/jpeg", + Data = Convert.ToBase64String(ImageHelper.CreatePngWithSize(100, 100)) + } + } + } + ); + await client.TestPostAssertInvalidModelAsync( + $"timelines/{generator(1)}/posts", + new HttpTimelinePostCreateRequest + { + DataList = new List() + { + new HttpTimelinePostCreateRequestData + { + ContentType = "image/jpeg", + Data = Convert.ToBase64String(new byte[] { 100, 200 }) + } + } + } + ); + } } } -- cgit v1.2.3