diff options
Diffstat (limited to 'BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs')
-rw-r--r-- | BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs | 535 |
1 files changed, 268 insertions, 267 deletions
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index 17c85f22..c5ff507f 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -1,44 +1,48 @@ using FluentAssertions;
-using Microsoft.EntityFrameworkCore;
-using Microsoft.Extensions.DependencyInjection;
-using SixLabors.ImageSharp;
-using SixLabors.ImageSharp.Formats.Png;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
+using System.Text;
using System.Threading.Tasks;
-using Timeline.Entities;
using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Tests.Helpers;
using Xunit;
+using Xunit.Abstractions;
+using SixLabors.ImageSharp.Formats.Gif;
+using SixLabors.ImageSharp.Formats.Png;
+using SixLabors.ImageSharp.Formats.Jpeg;
+using System.Net;
namespace Timeline.Tests.IntegratedTests
{
- public static class TimelineHelper
+ public class TimelinePostTest : BaseTimelineTest
{
- public static HttpTimelinePostContent TextPostContent(string text)
- {
- return new HttpTimelinePostContent("text", text, null, null);
- }
-
- public static HttpTimelinePostCreateRequest TextPostCreateRequest(string text, DateTime? time = null)
+ private static HttpTimelinePostCreateRequest CreateTextPostRequest(string text, DateTime? time = null, string? color = null)
{
- return new HttpTimelinePostCreateRequest
+ return new HttpTimelinePostCreateRequest()
{
- Content = new HttpTimelinePostCreateRequestContent
+ Time = time,
+ Color = color,
+ DataList = new List<HttpTimelinePostCreateRequestData>()
{
- Type = "text",
- Text = text
- },
- Time = time
+ new HttpTimelinePostCreateRequestData()
+ {
+ ContentType = MimeTypes.TextPlain,
+ Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(text))
+ }
+ }
};
}
- }
- public class TimelinePostTest : BaseTimelineTest
- {
+ private readonly ITestOutputHelper _outputHelper;
+
+ public TimelinePostTest(ITestOutputHelper outputHelper)
+ {
+ _outputHelper = outputHelper;
+ }
+
[Theory]
[MemberData(nameof(TimelineNameGeneratorTestData))]
public async Task GetPostsAndVisibility_Should_Work(TimelineNameGenerator generator)
@@ -102,15 +106,13 @@ namespace Timeline.Tests.IntegratedTests {
using var client = await CreateClientAsUser();
- var postContentList = new List<string> { "a", "b", "c", "d" };
var posts = new List<HttpTimelinePost>();
- foreach (var content in postContentList)
+ for (int i = 0; i < 4; i++)
{
- var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts",
- new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostContentTypes.Text } });
+ var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", CreateTextPostRequest("a"));
posts.Add(post);
- await Task.Delay(1000);
+ await Task.Delay(TimeSpan.FromSeconds(1));
}
await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}");
@@ -118,64 +120,57 @@ namespace Timeline.Tests.IntegratedTests {
var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture) }");
body.Should().HaveCount(2)
- .And.Subject.Select(p => p.Content!.Text).Should().Equal("b", "d");
+ .And.Subject.Select(p => p.Id).Should().Equal(posts[1].Id, posts[3].Id);
}
}
[Theory]
[MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineNameGenerator generator)
+ public async Task PostList_IncludeDeleted(TimelineNameGenerator generator)
{
using var client = await CreateClientAsUser();
- var postContentList = new List<string> { "a", "b", "c", "d" };
var posts = new List<HttpTimelinePost>();
- foreach (var (content, index) in postContentList.Select((v, i) => (v, i)))
+ for (int i = 0; i < 4; i++)
{
- var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts",
- new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostContentTypes.Text } });
- posts.Add(post);
- await Task.Delay(1000);
+ var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", CreateTextPostRequest("a"));
+ posts.Add(body);
}
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}");
-
+ foreach (var id in new long[] { posts[0].Id, posts[2].Id })
{
+ await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}");
+ }
- posts = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture)}&includeDeleted=true");
- posts.Should().HaveCount(3);
- posts.Select(p => p.Deleted).Should().Equal(false, true, false);
- posts.Select(p => p.Content == null).Should().Equal(false, true, false);
+ {
+ posts = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts?includeDeleted=true");
+ posts.Should().HaveCount(4);
+ posts.Select(p => p.Deleted).Should().Equal(true, false, true, false);
}
}
[Theory]
[MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task PostList_IncludeDeleted(TimelineNameGenerator generator)
+ public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineNameGenerator generator)
{
using var client = await CreateClientAsUser();
- var postContentList = new List<string> { "a", "b", "c", "d" };
var posts = new List<HttpTimelinePost>();
- foreach (var content in postContentList)
+ for (int i = 0; i < 4; i++)
{
- var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts",
- new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Text = content, Type = TimelinePostContentTypes.Text } });
- posts.Add(body);
+ var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", CreateTextPostRequest("a"));
+ posts.Add(post);
+ await Task.Delay(TimeSpan.FromSeconds(1));
}
- foreach (var id in new long[] { posts[0].Id, posts[2].Id })
- {
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}");
- }
+ await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}");
{
- posts = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts?includeDeleted=true");
- posts.Should().HaveCount(4);
- posts.Select(p => p.Deleted).Should().Equal(true, false, true, false);
- posts.Select(p => p.Content == null).Should().Equal(true, false, true, false);
+ posts = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture)}&includeDeleted=true");
+ posts.Should().HaveCount(3);
+ posts.Select(p => p.Deleted).Should().Equal(false, true, false);
}
}
@@ -190,25 +185,25 @@ namespace Timeline.Tests.IntegratedTests using (var client = await CreateDefaultClient())
{ // no auth should get 401
- await client.TestPostAssertUnauthorizedAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa"));
+ await client.TestPostAssertUnauthorizedAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa"));
}
using (var client = await CreateClientAsUser())
{
// post self's
- await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa"));
+ await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa"));
// post other not as a member should get 403
- await client.TestPostAssertForbiddenAsync($"timelines/{generator(0)}/posts", TimelineHelper.TextPostCreateRequest("aaa"));
+ await client.TestPostAssertForbiddenAsync($"timelines/{generator(0)}/posts", CreateTextPostRequest("aaa"));
}
using (var client = await CreateClientAsAdministrator())
{ // post as admin
- await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa"));
+ await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa"));
}
using (var client = await CreateClientAs(2))
{ // post as member
- await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa"));
+ await client.TestPostAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa"));
}
}
@@ -219,7 +214,7 @@ namespace Timeline.Tests.IntegratedTests async Task<long> CreatePost(int userNumber)
{
using var client = await CreateClientAs(userNumber);
- var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa"));
+ var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa"));
return body.Id;
}
@@ -267,64 +262,13 @@ namespace Timeline.Tests.IntegratedTests [Theory]
[MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task TextPost_Should_Work(TimelineNameGenerator generator)
- {
- using var client = await CreateClientAsUser();
-
- {
- var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts");
- body.Should().BeEmpty();
- }
-
- const string mockContent = "aaa";
- HttpTimelinePost createRes;
- {
- var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest(mockContent));
- body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent));
- body.Author.Should().BeEquivalentTo(await client.GetUserAsync("user1"));
- body.Deleted.Should().BeFalse();
- createRes = body;
- }
- {
- var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts");
- body.Should().BeEquivalentTo(createRes);
- }
- const string mockContent2 = "bbb";
- var mockTime2 = DateTime.UtcNow.AddDays(-1);
- HttpTimelinePost createRes2;
- {
- var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest(mockContent2, mockTime2));
- body.Should().NotBeNull();
- body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent2));
- body.Author.Should().BeEquivalentTo(await client.GetUserAsync("user1"));
- body.Time.Should().BeCloseTo(mockTime2, 1000);
- body.Deleted.Should().BeFalse();
- createRes2 = body;
- }
- {
- var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts");
- body.Should().BeEquivalentTo(createRes, createRes2);
- }
- {
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{createRes.Id}");
- await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/{createRes.Id}");
- await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/30000");
- }
- {
- var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts");
- body.Should().BeEquivalentTo(createRes2);
- }
- }
-
- [Theory]
- [MemberData(nameof(TimelineNameGeneratorTestData))]
public async Task GetPost_Should_Ordered(TimelineNameGenerator generator)
{
using var client = await CreateClientAsUser();
async Task<long> CreatePost(DateTime time)
{
- var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa", time));
+ var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", CreateTextPostRequest("aaa", time));
return body.Id;
}
@@ -341,229 +285,286 @@ namespace Timeline.Tests.IntegratedTests [Theory]
[MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task CreatePost_InvalidModel(TimelineNameGenerator generator)
+ public async Task Color(TimelineNameGenerator generator)
{
using var client = await CreateClientAsUser();
- var postUrl = $"timelines/{generator(1)}/posts";
- await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = null! });
- await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = null! } });
- await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "hahaha" } });
- await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "text", Text = null } });
- await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = null } });
- // image not base64
- await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = "!!!" } });
- // image base64 not image
- await client.TestPostAssertInvalidModelAsync(postUrl, new HttpTimelinePostCreateRequest { Content = new HttpTimelinePostCreateRequestContent { Type = "image", Data = Convert.ToBase64String(new byte[] { 0x01, 0x02, 0x03 }) } });
- }
- [Theory]
- [MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task ImagePost_ShouldWork(TimelineNameGenerator generator)
- {
- var imageData = ImageHelper.CreatePngWithSize(100, 200);
+ await client.TestPostAssertInvalidModelAsync($"timelines/{generator(1)}/posts", CreateTextPostRequest("a", color: "aa"));
- long postId;
- string postImageUrl;
+ long id;
- void AssertPostContent(HttpTimelinePostContent content)
{
- content.Type.Should().Be(TimelinePostContentTypes.Image);
- content.Url.Should().EndWith($"timelines/{generator(1)}/posts/{postId}/data");
- content.Text.Should().Be(null);
+ var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts",
+ CreateTextPostRequest("a", color: "#aabbcc"));
+ post.Color.Should().Be("#aabbcc");
+ id = post.Id;
}
- using var client = await CreateClientAsUser();
-
{
- var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts",
- new HttpTimelinePostCreateRequest
- {
- Content = new HttpTimelinePostCreateRequestContent
- {
- Type = TimelinePostContentTypes.Image,
- Data = Convert.ToBase64String(imageData)
- }
- });
- postId = body.Id;
- postImageUrl = body.Content!.Url!;
- AssertPostContent(body.Content);
+ var post = await client.TestGetAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts/{id}");
+ post.Color.Should().Be("#aabbcc");
}
+ }
- {
- var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts");
- body.Should().HaveCount(1);
- var post = body[0];
- post.Id.Should().Be(postId);
- AssertPostContent(post.Content!);
- }
+ [Theory]
+ [MemberData(nameof(TimelineNameGeneratorTestData))]
+ public async Task GetPost(TimelineNameGenerator generator)
+ {
+ using var client = await CreateClientAsUser();
- {
- var res = await client.GetAsync($"timelines/{generator(1)}/posts/{postId}/data");
- res.Content.Headers.ContentType!.MediaType.Should().Be("image/png");
- var data = await res.Content.ReadAsByteArrayAsync();
- var image = Image.Load(data, out var format);
- image.Width.Should().Be(100);
- image.Height.Should().Be(200);
- format.Name.Should().Be(PngFormat.Instance.Name);
- }
- await CacheTestHelper.TestCache(client, $"timelines/{generator(1)}/posts/{postId}/data");
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}");
- await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/{postId}");
+ await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/1");
- {
- var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts");
- body.Should().BeEmpty();
- }
+ var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", CreateTextPostRequest("a"));
- {
- using var scope = TestApp.Host.Services.CreateScope();
- var database = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
- var count = await database.Data.CountAsync();
- count.Should().Be(0);
- }
+ var post2 = await client.TestGetAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts/{post.Id}");
+ post2.Should().BeEquivalentTo(post);
+
+ await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{post.Id}");
+
+ await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/{post.Id}");
}
[Theory]
[MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task ImagePost_400(TimelineNameGenerator generator)
+ public async Task PatchPost(TimelineNameGenerator generator)
{
using var client = await CreateClientAsUser();
- await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/11234/data", errorCode: ErrorCodes.TimelineController.PostNotExist);
+ var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts",
+ CreateTextPostRequest("a"));
+
+ var date = new DateTime(2000, 10, 1);
- long postId;
+ var post2 = await client.TestPatchAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts/{post.Id}", new HttpTimelinePostPatchRequest
{
- var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa"));
- postId = body.Id;
- }
+ Time = date,
+ Color = "#aabbcc"
+ });
+ post2.Time.Should().Be(date);
+ post2.Color.Should().Be("#aabbcc");
- await client.TestGetAssertErrorAsync($"timelines/{generator(1)}/posts/{postId}/data", errorCode: ErrorCodes.TimelineController.PostNoData);
+ var post3 = await client.TestGetAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts/{post.Id}");
+ post3.Time.Should().Be(date);
+ post3.Color.Should().Be("#aabbcc");
}
- [Theory]
- [MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task PostDataETag(TimelineNameGenerator generator)
+ public static IEnumerable<object?[]> CreatePost_InvalidModelTest_TestData()
{
- using var client = await CreateClientAsUser();
-
- long id;
- string etag;
+ var testDataList = new List<List<HttpTimelinePostCreateRequestData>?>()
+ {
+ null,
+ new List<HttpTimelinePostCreateRequestData>(),
+ Enumerable.Repeat<HttpTimelinePostCreateRequestData>(new HttpTimelinePostCreateRequestData
+ {
+ ContentType = "text/plain",
+ Data = Convert.ToBase64String(Encoding.UTF8.GetBytes("a"))
+ }, 200).ToList(),
+ };
+ var testData = new List<HttpTimelinePostCreateRequestData?>()
{
- var body = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest
+ null,
+ new HttpTimelinePostCreateRequestData
{
- Content = new HttpTimelinePostCreateRequestContent
- {
- Type = TimelinePostContentTypes.Image,
- Data = Convert.ToBase64String(ImageHelper.CreatePngWithSize(100, 50))
- }
- });
- body.Content!.ETag.Should().NotBeNullOrEmpty();
+ ContentType = null!,
+ Data = Convert.ToBase64String(Encoding.UTF8.GetBytes("a"))
+ },
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = "text/plain",
+ Data = null!
+ },
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = "text/xxxxxxx",
+ Data = Convert.ToBase64String(Encoding.UTF8.GetBytes("a"))
+ },
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = "text/plain",
+ Data = "aaa"
+ },
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = "text/plain",
+ Data = Convert.ToBase64String(new byte[] {0xE4, 0x1, 0xA0})
+ },
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = "image/jpeg",
+ Data = Convert.ToBase64String(ImageHelper.CreatePngWithSize(100, 100))
+ },
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = "image/jpeg",
+ Data = Convert.ToBase64String(new byte[] { 100, 200 })
+ }
- id = body.Id;
- etag = body.Content.ETag!;
- }
+ };
- {
- var res = await client.GetAsync($"timelines/{generator(1)}/posts/{id}/data");
- res.StatusCode.Should().Be(200);
- res.Headers.ETag.Should().NotBeNull();
- res.Headers.ETag!.ToString().Should().Be(etag);
- }
+ testDataList.AddRange(testData.Select(d => new List<HttpTimelinePostCreateRequestData>() { d! }));
+
+ return TimelineNameGeneratorTestData().AppendTestData(testDataList);
}
[Theory]
- [MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task Color(TimelineNameGenerator generator)
+ [MemberData(nameof(CreatePost_InvalidModelTest_TestData))]
+ public async Task CreatePost_InvalidModel(TimelineNameGenerator generator, List<HttpTimelinePostCreateRequestData> dataList)
{
using var client = await CreateClientAsUser();
- HttpTimelinePostCreateRequestContent CreateRequestContent() => new()
+ await client.TestPostAssertInvalidModelAsync(
+ $"timelines/{generator(1)}/posts",
+ new HttpTimelinePostCreateRequest
+ {
+ DataList = dataList
+ }
+ );
+ }
+
+ public static IEnumerable<object?[]> CreatePost_ShouldWork_TestData()
+ {
+ var testByteDatas = new List<ByteData>()
{
- Type = "text",
- Text = "aaa"
+ new ByteData(Encoding.UTF8.GetBytes("aaa"), MimeTypes.TextPlain),
+ new ByteData(Encoding.UTF8.GetBytes("aaa"), MimeTypes.TextMarkdown),
+ new ByteData(ImageHelper.CreateImageWithSize(100, 50, PngFormat.Instance), MimeTypes.ImagePng),
+ new ByteData(ImageHelper.CreateImageWithSize(100, 50, JpegFormat.Instance), MimeTypes.ImageJpeg),
+ new ByteData(ImageHelper.CreateImageWithSize(100, 50, GifFormat.Instance), MimeTypes.ImageGif),
};
- await client.TestPostAssertInvalidModelAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest
- {
- Content = CreateRequestContent(),
- Color = "#1"
- });
+ return TimelineNameGeneratorTestData().AppendTestData(testByteDatas);
+ }
- long id;
+ [Theory]
+ [MemberData(nameof(CreatePost_ShouldWork_TestData))]
+ public async Task CreatePost_ShouldWork(TimelineNameGenerator generator, ByteData data)
+ {
+ using var client = await CreateClientAsUser();
- {
- var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest
+ var post = await client.TestPostAsync<HttpTimelinePost>(
+ $"timelines/{generator(1)}/posts",
+ new HttpTimelinePostCreateRequest
{
- Content = CreateRequestContent(),
- Color = "#aabbcc"
- });
- post.Color.Should().Be("#aabbcc");
- id = post.Id;
+ DataList = new List<HttpTimelinePostCreateRequestData>
+ {
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = data.ContentType,
+ Data = Convert.ToBase64String(data.Data)
+ }
+ }
+ }
+ );
+
+ post.DataList.Should().NotBeNull().And.HaveCount(1);
+ var postData = post.DataList[0];
+ postData.Should().NotBeNull();
+ var postDataEtag = postData.ETag;
+ postDataEtag.Should().NotBeNullOrEmpty();
+
+ {
+ var response = await client.GetAsync($"timelines/{generator(1)}/posts/{post.Id}/data");
+ response.StatusCode.Should().Be(HttpStatusCode.OK);
+ response.Headers.ETag.Should().NotBeNull();
+ response.Headers.ETag!.Tag.Should().Be(postDataEtag);
+ response.Content.Headers.ContentType.Should().NotBeNull();
+ response.Content.Headers.ContentType!.MediaType.Should().Be(data.ContentType);
+
+ var body = await response.Content.ReadAsByteArrayAsync();
+ body.Should().Equal(data.Data);
}
{
- var post = await client.TestGetAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts/{id}");
- post.Color.Should().Be("#aabbcc");
+ var response = await client.GetAsync($"timelines/{generator(1)}/posts/{post.Id}/data/0");
+ response.StatusCode.Should().Be(HttpStatusCode.OK);
+ response.Headers.ETag.Should().NotBeNull();
+ response.Headers.ETag!.Tag.Should().Be(postDataEtag);
+ response.Content.Headers.ContentType.Should().NotBeNull();
+ response.Content.Headers.ContentType!.MediaType.Should().Be(data.ContentType);
+
+ var body = await response.Content.ReadAsByteArrayAsync();
+ body.Should().Equal(data.Data);
}
}
[Theory]
[MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task GetPost(TimelineNameGenerator generator)
+ public async Task CreatePost_MultipleData_ShouldWork(TimelineNameGenerator generator)
{
using var client = await CreateClientAsUser();
- HttpTimelinePostCreateRequestContent CreateRequestContent() => new()
- {
- Type = "text",
- Text = "aaa"
- };
+ var textData = Encoding.UTF8.GetBytes("aaa");
+ var imageData = ImageHelper.CreatePngWithSize(100, 50);
- await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/1");
+ var post = await client.TestPostAsync<HttpTimelinePost>(
+ $"timelines/{generator(1)}/posts",
+ new HttpTimelinePostCreateRequest
+ {
+ DataList = new List<HttpTimelinePostCreateRequestData>
+ {
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = MimeTypes.TextMarkdown,
+ Data = Convert.ToBase64String(textData)
+ },
+ new HttpTimelinePostCreateRequestData
+ {
+ ContentType = MimeTypes.ImagePng,
+ Data = Convert.ToBase64String(imageData)
+ }
+ }
+ }
+ );
- var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest
- {
- Content = CreateRequestContent(),
- });
+ post.DataList.Should().NotBeNull().And.HaveCount(2);
- var post2 = await client.TestGetAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts/{post.Id}");
- post2.Should().BeEquivalentTo(post);
+ var postData0 = post.DataList[0];
+ postData0.Should().NotBeNull();
+ var postDataEtag0 = postData0.ETag;
+ postDataEtag0.Should().NotBeNullOrEmpty();
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{post.Id}");
+ var postData1 = post.DataList[1];
+ postData1.Should().NotBeNull();
+ var postDataEtag1 = postData1.ETag;
+ postDataEtag1.Should().NotBeNullOrEmpty();
- await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/{post.Id}");
- }
+ {
+ var response = await client.GetAsync($"timelines/{generator(1)}/posts/{post.Id}/data");
+ response.StatusCode.Should().Be(HttpStatusCode.OK);
+ response.Headers.ETag.Should().NotBeNull();
+ response.Headers.ETag!.Tag.Should().Be(postDataEtag0);
+ response.Content.Headers.ContentType.Should().NotBeNull();
+ response.Content.Headers.ContentType!.MediaType.Should().Be(MimeTypes.TextMarkdown);
- [Theory]
- [MemberData(nameof(TimelineNameGeneratorTestData))]
- public async Task PatchPost(TimelineNameGenerator generator)
- {
- using var client = await CreateClientAsUser();
+ var body = await response.Content.ReadAsByteArrayAsync();
+ body.Should().Equal(textData);
+ }
- var post = await client.TestPostAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest
{
- Content = new()
- {
- Type = "text",
- Text = "aaa"
- }
- });
+ var response = await client.GetAsync($"timelines/{generator(1)}/posts/{post.Id}/data/0");
+ response.StatusCode.Should().Be(HttpStatusCode.OK);
+ response.Headers.ETag.Should().NotBeNull();
+ response.Headers.ETag!.Tag.Should().Be(postDataEtag0);
+ response.Content.Headers.ContentType.Should().NotBeNull();
+ response.Content.Headers.ContentType!.MediaType.Should().Be(MimeTypes.TextMarkdown);
- var date = new DateTime(2000, 10, 1);
+ var body = await response.Content.ReadAsByteArrayAsync();
+ body.Should().Equal(textData);
+ }
- var post2 = await client.TestPatchAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts/{post.Id}", new HttpTimelinePostPatchRequest
{
- Time = date,
- Color = "#aabbcc"
- });
- post2.Time.Should().Be(date);
- post2.Color.Should().Be("#aabbcc");
+ var response = await client.GetAsync($"timelines/{generator(1)}/posts/{post.Id}/data/1");
+ response.StatusCode.Should().Be(HttpStatusCode.OK);
+ response.Headers.ETag.Should().NotBeNull();
+ response.Headers.ETag!.Tag.Should().Be(postDataEtag1);
+ response.Content.Headers.ContentType.Should().NotBeNull();
+ response.Content.Headers.ContentType!.MediaType.Should().Be(MimeTypes.ImagePng);
- var post3 = await client.TestGetAsync<HttpTimelinePost>($"timelines/{generator(1)}/posts/{post.Id}");
- post3.Time.Should().Be(date);
- post3.Color.Should().Be("#aabbcc");
+ var body = await response.Content.ReadAsByteArrayAsync();
+ body.Should().Equal(imageData);
+ }
}
}
}
|