aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Timeline.Tests/Helpers/HttpClientExtensions.cs9
-rw-r--r--Timeline.Tests/Helpers/ImageHelper.cs21
-rw-r--r--Timeline.Tests/IntegratedTests/UserAvatarTests.cs153
-rw-r--r--Timeline.Tests/UserAvatarServiceTest.cs48
4 files changed, 190 insertions, 41 deletions
diff --git a/Timeline.Tests/Helpers/HttpClientExtensions.cs b/Timeline.Tests/Helpers/HttpClientExtensions.cs
index cd40d91e..b9204fcc 100644
--- a/Timeline.Tests/Helpers/HttpClientExtensions.cs
+++ b/Timeline.Tests/Helpers/HttpClientExtensions.cs
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using System.Net.Http;
+using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
@@ -11,5 +12,13 @@ namespace Timeline.Tests.Helpers
{
return client.PatchAsync(url, new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"));
}
+
+ public static Task<HttpResponseMessage> PutByteArrayAsync(this HttpClient client, string url, byte[] body, string mimeType)
+ {
+ var content = new ByteArrayContent(body);
+ content.Headers.ContentLength = body.Length;
+ content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
+ return client.PutAsync(url, content);
+ }
}
}
diff --git a/Timeline.Tests/Helpers/ImageHelper.cs b/Timeline.Tests/Helpers/ImageHelper.cs
new file mode 100644
index 00000000..c5a9cf17
--- /dev/null
+++ b/Timeline.Tests/Helpers/ImageHelper.cs
@@ -0,0 +1,21 @@
+using SixLabors.ImageSharp;
+using SixLabors.ImageSharp.PixelFormats;
+using System.IO;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class ImageHelper
+ {
+ public static byte[] CreatePngWithSize(int width, int height)
+ {
+ using (var image = new Image<Rgba32>(width, height))
+ {
+ using (var stream = new MemoryStream())
+ {
+ image.SaveAsPng(stream);
+ return stream.ToArray();
+ }
+ }
+ }
+ }
+}
diff --git a/Timeline.Tests/IntegratedTests/UserAvatarTests.cs b/Timeline.Tests/IntegratedTests/UserAvatarTests.cs
index 14781474..e282da1b 100644
--- a/Timeline.Tests/IntegratedTests/UserAvatarTests.cs
+++ b/Timeline.Tests/IntegratedTests/UserAvatarTests.cs
@@ -1,11 +1,154 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
+using FluentAssertions;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.Extensions.DependencyInjection;
+using SixLabors.ImageSharp.Formats.Png;
+using System;
+using System.IO;
+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;
+using Xunit;
+using Xunit.Abstractions;
namespace Timeline.Tests.IntegratedTests
{
- public class UserAvatarTests
+ public class UserAvatarUnitTest : IClassFixture<MyWebApplicationFactory<Startup>>, IDisposable
{
+ private readonly WebApplicationFactory<Startup> _factory;
+ private readonly Action _disposeAction;
+
+ public UserAvatarUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
+ {
+ _factory = factory.WithTestConfig(outputHelper, out _disposeAction);
+ }
+
+ public void Dispose()
+ {
+ _disposeAction();
+ }
+
+ [Fact]
+ public async Task Test()
+ {
+ Avatar mockAvatar = new Avatar
+ {
+ Data = ImageHelper.CreatePngWithSize(100, 100),
+ Type = PngFormat.Instance.DefaultMimeType
+ };
+
+ using (var client = await _factory.CreateClientAsUser())
+ {
+ {
+ var res = await client.GetAsync("users/usernotexist/avatar");
+ res.Should().HaveStatusCodeNotFound()
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserAvatarController.ErrorCodes.Get_UserNotExist);
+ }
+
+ var env = _factory.Server.Host.Services.GetRequiredService<IHostingEnvironment>();
+ var defaultAvatarData = await File.ReadAllBytesAsync(Path.Combine(env.ContentRootPath, "default-avatar.png"));
+
+ async Task GetReturnDefault(string username = "user")
+ {
+ var res = await client.GetAsync($"users/{username}/avatar");
+ res.Should().HaveStatusCodeOk();
+ res.Content.Headers.ContentType.MediaType.Should().Be("image/png");
+ var body = await res.Content.ReadAsByteArrayAsync();
+ body.Should().Equal(defaultAvatarData);
+ }
+
+ await GetReturnDefault();
+ await GetReturnDefault("admin");
+
+ {
+ var res = await client.PutByteArrayAsync("users/user/avatar", new[] { (byte)0x00 }, "image/notaccept");
+ res.Should().HaveStatusCode(HttpStatusCode.UnsupportedMediaType);
+ }
+
+ {
+ var res = await client.PutByteArrayAsync("users/user/avatar", new[] { (byte)0x00 }, "image/notaccept");
+ res.Should().HaveStatusCode(HttpStatusCode.UnsupportedMediaType);
+ }
+
+ {
+ var res = await client.PutByteArrayAsync("users/user/avatar", new[] { (byte)0x00 }, "image/png");
+ res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserAvatarController.ErrorCodes.Put_BadFormat_CantDecode);
+ }
+
+ {
+ var res = await client.PutByteArrayAsync("users/user/avatar", mockAvatar.Data, "image/jpeg");
+ res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserAvatarController.ErrorCodes.Put_BadFormat_UnmatchedFormat);
+ }
+
+ {
+ var res = await client.PutByteArrayAsync("users/user/avatar", ImageHelper.CreatePngWithSize(100, 200), "image/png");
+ res.Should().HaveStatusCode(HttpStatusCode.BadRequest)
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserAvatarController.ErrorCodes.Put_BadFormat_BadSize);
+ }
+
+ {
+ var res = await client.PutByteArrayAsync("users/user/avatar", mockAvatar.Data, mockAvatar.Type);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+
+ var res2 = await client.GetAsync("users/user/avatar");
+ res2.Should().HaveStatusCodeOk();
+ res2.Content.Headers.ContentType.MediaType.Should().Be(mockAvatar.Type);
+ var body = await res2.Content.ReadAsByteArrayAsync();
+ body.Should().Equal(mockAvatar.Data);
+ }
+
+ {
+ var res = await client.PutByteArrayAsync("users/admin/avatar", new[] { (byte)0x00 }, "image/png");
+ res.Should().HaveStatusCode(HttpStatusCode.Forbidden)
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserAvatarController.ErrorCodes.Put_Forbid);
+ }
+
+ {
+ var res = await client.DeleteAsync("users/admin/avatar");
+ res.Should().HaveStatusCode(HttpStatusCode.Forbidden)
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserAvatarController.ErrorCodes.Delete_Forbid);
+ }
+
+ for (int i = 0; i < 2; i++) // double delete should work.
+ {
+ var res = await client.DeleteAsync("users/user/avatar");
+ res.Should().HaveStatusCodeOk();
+ await GetReturnDefault();
+ }
+ }
+
+ // Authorization check.
+ using (var client = await _factory.CreateClientAsAdmin())
+ {
+ {
+ var res = await client.PutByteArrayAsync("users/user/avatar", mockAvatar.Data, mockAvatar.Type);
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+
+ {
+ var res = await client.DeleteAsync("users/user/avatar");
+ res.Should().HaveStatusCode(HttpStatusCode.OK);
+ }
+
+ {
+ var res = await client.PutByteArrayAsync("users/usernotexist/avatar", new[] { (byte)0x00 }, "image/png");
+ res.Should().HaveStatusCodeBadRequest()
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserAvatarController.ErrorCodes.Put_UserNotExist);
+ }
+
+ {
+ var res = await client.DeleteAsync("users/usernotexist/avatar");
+ res.Should().HaveStatusCodeBadRequest()
+ .And.Should().HaveBodyAsCommonResponseWithCode(UserAvatarController.ErrorCodes.Delete_UserNotExist);
+ }
+ }
+ }
}
-}
+} \ No newline at end of file
diff --git a/Timeline.Tests/UserAvatarServiceTest.cs b/Timeline.Tests/UserAvatarServiceTest.cs
index 04f52c71..69d6b61c 100644
--- a/Timeline.Tests/UserAvatarServiceTest.cs
+++ b/Timeline.Tests/UserAvatarServiceTest.cs
@@ -61,19 +61,11 @@ namespace Timeline.Tests
[Fact]
public void UnmatchedFormat()
{
- Avatar avatar;
- using (var image = new Image<Rgba32>(100, 100))
+ Avatar avatar = new Avatar
{
- using (var stream = new MemoryStream())
- {
- image.SaveAsPng(stream);
- avatar = new Avatar
- {
- Data = stream.ToArray(),
- Type = "image/jpeg"
- };
- }
- }
+ Data = ImageHelper.CreatePngWithSize(100, 100),
+ Type = "image/jpeg"
+ };
_validator.Awaiting(v => v.Validate(avatar))
.Should().Throw<AvatarDataException>()
.Where(e => e.Avatar == avatar && e.Error == AvatarDataException.ErrorReason.UnmatchedFormat);
@@ -82,19 +74,11 @@ namespace Timeline.Tests
[Fact]
public void BadSize()
{
- Avatar avatar;
- using (var image = new Image<Rgba32>(100, 200))
+ Avatar avatar = new Avatar
{
- using (var stream = new MemoryStream())
- {
- image.SaveAsPng(stream);
- avatar = new Avatar
- {
- Data = stream.ToArray(),
- Type = PngFormat.Instance.DefaultMimeType
- };
- }
- }
+ Data = ImageHelper.CreatePngWithSize(100, 200),
+ Type = PngFormat.Instance.DefaultMimeType
+ };
_validator.Awaiting(v => v.Validate(avatar))
.Should().Throw<AvatarDataException>()
.Where(e => e.Avatar == avatar && e.Error == AvatarDataException.ErrorReason.BadSize);
@@ -103,19 +87,11 @@ namespace Timeline.Tests
[Fact]
public void Success()
{
- Avatar avatar;
- using (var image = new Image<Rgba32>(100, 100))
+ Avatar avatar = new Avatar
{
- using (var stream = new MemoryStream())
- {
- image.SaveAsPng(stream);
- avatar = new Avatar
- {
- Data = stream.ToArray(),
- Type = PngFormat.Instance.DefaultMimeType
- };
- }
- }
+ Data = ImageHelper.CreatePngWithSize(100, 100),
+ Type = PngFormat.Instance.DefaultMimeType
+ };
_validator.Awaiting(v => v.Validate(avatar))
.Should().NotThrow();
}