aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-18 18:07:50 +0800
committer杨宇千 <crupest@outlook.com>2019-08-18 18:07:50 +0800
commit36a5a7c81569bbc4fa76b77e9823767d951944b4 (patch)
tree21b4c77e48f34e1a72858209e4d4d6d7978b663a
parent0630fd020ec11e343b787a18d70f1f13fdb350b3 (diff)
downloadtimeline-36a5a7c81569bbc4fa76b77e9823767d951944b4.tar.gz
timeline-36a5a7c81569bbc4fa76b77e9823767d951944b4.tar.bz2
timeline-36a5a7c81569bbc4fa76b77e9823767d951944b4.zip
Add avatar service.
-rw-r--r--Timeline.Tests/Helpers/MyTestLoggerFactory.cs14
-rw-r--r--Timeline.Tests/Helpers/MyWebApplicationFactory.cs5
-rw-r--r--Timeline.Tests/Mock/Data/TestDatabase.cs46
-rw-r--r--Timeline.Tests/UserAvatarServiceTest.cs159
-rw-r--r--Timeline/Entities/DatabaseContext.cs3
-rw-r--r--Timeline/Entities/UserAvatar.cs18
-rw-r--r--Timeline/Services/UserAvatarService.cs181
-rw-r--r--Timeline/Timeline.csproj6
-rw-r--r--Timeline/appsettings.Development.json2
-rw-r--r--Timeline/default-avatar.pngbin0 -> 26442 bytes
-rw-r--r--art-src/user.svg4
11 files changed, 431 insertions, 7 deletions
diff --git a/Timeline.Tests/Helpers/MyTestLoggerFactory.cs b/Timeline.Tests/Helpers/MyTestLoggerFactory.cs
new file mode 100644
index 00000000..40c6a77e
--- /dev/null
+++ b/Timeline.Tests/Helpers/MyTestLoggerFactory.cs
@@ -0,0 +1,14 @@
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Logging.Testing;
+using Xunit.Abstractions;
+
+namespace Timeline.Tests.Helpers
+{
+ public static class MyTestLoggerFactory
+ {
+ public static LoggerFactory Create(ITestOutputHelper outputHelper)
+ {
+ return new LoggerFactory(new[] { new XunitLoggerProvider(outputHelper) });
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
index dfadd1ae..e96d11fe 100644
--- a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
+++ b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
@@ -37,6 +38,10 @@ namespace Timeline.Tests.Helpers
{
var options = new DbContextOptionsBuilder<DatabaseContext>()
.UseSqlite(_databaseConnection)
+ .ConfigureWarnings(builder =>
+ {
+ builder.Throw(RelationalEventId.QueryClientEvaluationWarning);
+ })
.Options;
using (var context = new DatabaseContext(options))
diff --git a/Timeline.Tests/Mock/Data/TestDatabase.cs b/Timeline.Tests/Mock/Data/TestDatabase.cs
new file mode 100644
index 00000000..09c77dce
--- /dev/null
+++ b/Timeline.Tests/Mock/Data/TestDatabase.cs
@@ -0,0 +1,46 @@
+using Microsoft.Data.Sqlite;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Diagnostics;
+using System;
+using Timeline.Entities;
+
+namespace Timeline.Tests.Mock.Data
+{
+ public class TestDatabase : IDisposable
+ {
+ private readonly SqliteConnection _databaseConnection;
+ private readonly DatabaseContext _databaseContext;
+
+ public TestDatabase()
+ {
+ _databaseConnection = new SqliteConnection("Data Source=:memory:;");
+ _databaseConnection.Open();
+
+ var options = new DbContextOptionsBuilder<DatabaseContext>()
+ .UseSqlite(_databaseConnection)
+ .ConfigureWarnings(builder =>
+ {
+ builder.Throw(RelationalEventId.QueryClientEvaluationWarning);
+ })
+ .Options;
+
+ _databaseContext = new DatabaseContext(options);
+
+ // init with mock data
+ _databaseContext.Database.EnsureCreated();
+ _databaseContext.Users.AddRange(MockUsers.Users);
+ _databaseContext.SaveChanges();
+ }
+
+ public void Dispose()
+ {
+ _databaseContext.Dispose();
+
+ _databaseConnection.Close();
+ _databaseConnection.Dispose();
+ }
+
+ public SqliteConnection DatabaseConnection => _databaseConnection;
+ public DatabaseContext DatabaseContext => _databaseContext;
+ }
+}
diff --git a/Timeline.Tests/UserAvatarServiceTest.cs b/Timeline.Tests/UserAvatarServiceTest.cs
new file mode 100644
index 00000000..a8e0562b
--- /dev/null
+++ b/Timeline.Tests/UserAvatarServiceTest.cs
@@ -0,0 +1,159 @@
+using FluentAssertions;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Services;
+using Timeline.Tests.Helpers;
+using Timeline.Tests.Mock.Data;
+using Xunit;
+using Xunit.Abstractions;
+
+namespace Timeline.Tests
+{
+ public class MockDefaultUserAvatarProvider : IDefaultUserAvatarProvider
+ {
+ public static Avatar Avatar { get; } = new Avatar { Type = "image/test", Data = Encoding.ASCII.GetBytes("test") };
+
+ public Task<Avatar> GetDefaultAvatar()
+ {
+ return Task.FromResult(Avatar);
+ }
+ }
+
+ public class UserAvatarServiceTest : IDisposable, IClassFixture<MockDefaultUserAvatarProvider>
+ {
+ private static Avatar MockAvatar { get; } = new Avatar
+ {
+ Type = "image/testaaa",
+ Data = Encoding.ASCII.GetBytes("amock")
+ };
+
+ private static Avatar MockAvatar2 { get; } = new Avatar
+ {
+ Type = "image/testbbb",
+ Data = Encoding.ASCII.GetBytes("bmock")
+ };
+
+ private readonly MockDefaultUserAvatarProvider _mockDefaultUserAvatarProvider;
+
+ private readonly LoggerFactory _loggerFactory;
+ private readonly TestDatabase _database;
+
+ private readonly UserAvatarService _service;
+
+ public UserAvatarServiceTest(ITestOutputHelper outputHelper, MockDefaultUserAvatarProvider mockDefaultUserAvatarProvider)
+ {
+ _mockDefaultUserAvatarProvider = mockDefaultUserAvatarProvider;
+
+ _loggerFactory = MyTestLoggerFactory.Create(outputHelper);
+ _database = new TestDatabase();
+
+ _service = new UserAvatarService(_loggerFactory.CreateLogger<UserAvatarService>(), _database.DatabaseContext, _mockDefaultUserAvatarProvider);
+ }
+
+ public void Dispose()
+ {
+ _loggerFactory.Dispose();
+ _database.Dispose();
+ }
+
+ [Fact]
+ public void GetAvatar_ShouldThrow_ArgumentException()
+ {
+ // no need to await because arguments are checked syncronizedly.
+ _service.Invoking(s => s.GetAvatar(null)).Should().Throw<ArgumentException>()
+ .Where(e => e.ParamName == "username" && e.Message.Contains("null", StringComparison.OrdinalIgnoreCase));
+ _service.Invoking(s => s.GetAvatar("")).Should().Throw<ArgumentException>()
+ .Where(e => e.ParamName == "username" && e.Message.Contains("empty", StringComparison.OrdinalIgnoreCase));
+ }
+
+ [Fact]
+ public void GetAvatar_ShouldThrow_UserNotExistException()
+ {
+ const string username = "usernotexist";
+ _service.Awaiting(s => s.GetAvatar(username)).Should().Throw<UserNotExistException>()
+ .Where(e => e.Username == username);
+ }
+
+ [Fact]
+ public async Task GetAvatar_ShouldReturn_Default()
+ {
+ const string username = MockUsers.UserUsername;
+ (await _service.GetAvatar(username)).Should().BeEquivalentTo(await _mockDefaultUserAvatarProvider.GetDefaultAvatar());
+ }
+
+ [Fact]
+ public async Task GetAvatar_ShouldReturn_Data()
+ {
+ const string username = MockUsers.UserUsername;
+
+ {
+ // create mock data
+ var context = _database.DatabaseContext;
+ var user = await context.Users.Where(u => u.Name == username).Include(u => u.Avatar).SingleAsync();
+ user.Avatar = new UserAvatar
+ {
+ Type = MockAvatar.Type,
+ Data = MockAvatar.Data
+ };
+ await context.SaveChangesAsync();
+ }
+
+ (await _service.GetAvatar(username)).Should().BeEquivalentTo(MockAvatar);
+ }
+
+ [Fact]
+ public void SetAvatar_ShouldThrow_ArgumentException()
+ {
+ // no need to await because arguments are checked syncronizedly.
+ _service.Invoking(s => s.SetAvatar(null, MockAvatar)).Should().Throw<ArgumentException>()
+ .Where(e => e.ParamName == "username" && e.Message.Contains("null", StringComparison.OrdinalIgnoreCase));
+ _service.Invoking(s => s.SetAvatar("", MockAvatar)).Should().Throw<ArgumentException>()
+ .Where(e => e.ParamName == "username" && e.Message.Contains("empty", StringComparison.OrdinalIgnoreCase));
+
+ _service.Invoking(s => s.SetAvatar("aaa", new Avatar { Type = null, Data = new[] { (byte)0x00 } })).Should().Throw<ArgumentException>()
+ .Where(e => e.ParamName == "avatar" && e.Message.Contains("null", StringComparison.OrdinalIgnoreCase));
+ _service.Invoking(s => s.SetAvatar("aaa", new Avatar { Type = "", Data = new[] { (byte)0x00 } })).Should().Throw<ArgumentException>()
+ .Where(e => e.ParamName == "avatar" && e.Message.Contains("empty", StringComparison.OrdinalIgnoreCase));
+
+ _service.Invoking(s => s.SetAvatar("aaa", new Avatar { Type = "aaa", Data = null })).Should().Throw<ArgumentException>()
+ .Where(e => e.ParamName == "avatar" && e.Message.Contains("null", StringComparison.OrdinalIgnoreCase));
+ }
+
+ [Fact]
+ public void SetAvatar_ShouldThrow_UserNotExistException()
+ {
+ const string username = "usernotexist";
+ _service.Awaiting(s => s.SetAvatar(username, MockAvatar)).Should().Throw<UserNotExistException>()
+ .Where(e => e.Username == username);
+ }
+
+ [Fact]
+ public async Task SetAvatar_Should_Work()
+ {
+ const string username = MockUsers.UserUsername;
+
+ var user = await _database.DatabaseContext.Users.Where(u => u.Name == username).Include(u => u.Avatar).SingleAsync();
+
+ // create
+ await _service.SetAvatar(username, MockAvatar);
+ user.Avatar.Should().NotBeNull();
+ user.Avatar.Type.Should().Be(MockAvatar.Type);
+ user.Avatar.Data.Should().Equal(MockAvatar.Data);
+
+ // modify
+ await _service.SetAvatar(username, MockAvatar2);
+ user.Avatar.Should().NotBeNull();
+ user.Avatar.Type.Should().Be(MockAvatar2.Type);
+ user.Avatar.Data.Should().Equal(MockAvatar2.Data);
+
+ // delete
+ await _service.SetAvatar(username, null);
+ user.Avatar.Should().BeNull();
+ }
+ }
+}
diff --git a/Timeline/Entities/DatabaseContext.cs b/Timeline/Entities/DatabaseContext.cs
index 3629e821..f32e5992 100644
--- a/Timeline/Entities/DatabaseContext.cs
+++ b/Timeline/Entities/DatabaseContext.cs
@@ -27,6 +27,8 @@ namespace Timeline.Entities
[Column("version"), Required]
public long Version { get; set; }
+
+ public UserAvatar Avatar { get; set; }
}
public class DatabaseContext : DbContext
@@ -44,5 +46,6 @@ namespace Timeline.Entities
}
public DbSet<User> Users { get; set; }
+ public DbSet<UserAvatar> UserAvatars { get; set; }
}
}
diff --git a/Timeline/Entities/UserAvatar.cs b/Timeline/Entities/UserAvatar.cs
new file mode 100644
index 00000000..d7c24403
--- /dev/null
+++ b/Timeline/Entities/UserAvatar.cs
@@ -0,0 +1,18 @@
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Timeline.Entities
+{
+ [Table("user_avatars")]
+ public class UserAvatar
+ {
+ [Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
+ public long Id { get; set; }
+
+ [Column("data"), Required]
+ public byte[] Data { get; set; }
+
+ [Column("type"), Required]
+ public string Type { get; set; }
+ }
+}
diff --git a/Timeline/Services/UserAvatarService.cs b/Timeline/Services/UserAvatarService.cs
new file mode 100644
index 00000000..21153575
--- /dev/null
+++ b/Timeline/Services/UserAvatarService.cs
@@ -0,0 +1,181 @@
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging;
+using System;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+
+namespace Timeline.Services
+{
+ public class Avatar
+ {
+ public string Type { get; set; }
+ public byte[] Data { get; set; }
+ }
+
+ /// <summary>
+ /// Thrown when avatar is of bad format.
+ /// </summary>
+ [Serializable]
+ public class AvatarDataException : Exception
+ {
+ public AvatarDataException(Avatar avatar, string message) : base(message) { Avatar = avatar; }
+ public AvatarDataException(Avatar avatar, string message, Exception inner) : base(message, inner) { Avatar = avatar; }
+ protected AvatarDataException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
+
+ public Avatar Avatar { get; set; }
+ }
+
+ /// <summary>
+ /// Provider for default user avatar.
+ /// </summary>
+ /// <remarks>
+ /// Mainly for unit tests.
+ /// </remarks>
+ public interface IDefaultUserAvatarProvider
+ {
+ /// <summary>
+ /// Get the default avatar.
+ /// </summary>
+ Task<Avatar> GetDefaultAvatar();
+ }
+
+ public interface IUserAvatarService
+ {
+ /// <summary>
+ /// Get avatar of a user. If the user has no avatar, a default one is returned.
+ /// </summary>
+ /// <param name="username">The username of the user to get avatar of.</param>
+ /// <returns>The avatar.</returns>
+ /// <exception cref="ArgumentException">Thrown if <paramref name="username"/> is null or empty.</exception>
+ /// <exception cref="UserNotExistException">Thrown if the user does not exist.</exception>
+ Task<Avatar> GetAvatar(string username);
+
+ /// <summary>
+ /// Set avatar for a user.
+ /// </summary>
+ /// <param name="username">The username of the user to set avatar for.</param>
+ /// <param name="avatar">The avatar. Can be null to delete the saved avatar.</param>
+ /// <exception cref="ArgumentException">Throw if <paramref name="username"/> is null or empty.
+ /// Or thrown if <paramref name="avatar"/> is not null but <see cref="Avatar.Type"/> is null or empty or <see cref="Avatar.Data"/> is null.</exception>
+ /// <exception cref="UserNotExistException">Thrown if the user does not exist.</exception>
+ /// <exception cref="AvatarDataException">Thrown if avatar is of bad format.</exception>
+ Task SetAvatar(string username, Avatar avatar);
+ }
+
+ public class DefaultUserAvatarProvider : IDefaultUserAvatarProvider
+ {
+ private readonly IHostingEnvironment _environment;
+
+ public DefaultUserAvatarProvider(IHostingEnvironment environment)
+ {
+ _environment = environment;
+ }
+
+ public async Task<Avatar> GetDefaultAvatar()
+ {
+ return new Avatar
+ {
+ Type = "image/png",
+ Data = await File.ReadAllBytesAsync(Path.Combine(_environment.ContentRootPath, "default-avatar.png"))
+ };
+ }
+ }
+
+ public class UserAvatarService : IUserAvatarService
+ {
+
+ private readonly ILogger<UserAvatarService> _logger;
+
+ private readonly DatabaseContext _database;
+
+ private readonly IDefaultUserAvatarProvider _defaultUserAvatarProvider;
+
+ public UserAvatarService(ILogger<UserAvatarService> logger, DatabaseContext database, IDefaultUserAvatarProvider defaultUserAvatarProvider)
+ {
+ _logger = logger;
+ _database = database;
+ _defaultUserAvatarProvider = defaultUserAvatarProvider;
+ }
+
+ public async Task<Avatar> GetAvatar(string username)
+ {
+ if (string.IsNullOrEmpty(username))
+ throw new ArgumentException("Username is null or empty.", nameof(username));
+
+ var user = await _database.Users.Where(u => u.Name == username).SingleOrDefaultAsync();
+ if (user == null)
+ throw new UserNotExistException(username);
+
+ await _database.Entry(user).Reference(u => u.Avatar).LoadAsync();
+ var avatar = user.Avatar;
+
+ if (avatar == null)
+ {
+ return await _defaultUserAvatarProvider.GetDefaultAvatar();
+ }
+ else
+ {
+ return new Avatar
+ {
+ Type = avatar.Type,
+ Data = avatar.Data
+ };
+ }
+ }
+
+ public async Task SetAvatar(string username, Avatar avatar)
+ {
+ if (string.IsNullOrEmpty(username))
+ throw new ArgumentException("Username is null or empty.", nameof(username));
+
+ if (avatar != null)
+ {
+ if (string.IsNullOrEmpty(avatar.Type))
+ throw new ArgumentException("Type of avatar is null or empty.", nameof(avatar));
+ if (avatar.Data == null)
+ throw new ArgumentException("Data of avatar is null.", nameof(avatar));
+ }
+
+ var user = await _database.Users.Where(u => u.Name == username).SingleOrDefaultAsync();
+ if (user == null)
+ throw new UserNotExistException(username);
+
+ await _database.Entry(user).Reference(u => u.Avatar).LoadAsync();
+ var avatarEntity = user.Avatar;
+
+ if (avatar == null)
+ {
+ if (avatarEntity == null)
+ return;
+ else
+ {
+ _database.UserAvatars.Remove(avatarEntity);
+ await _database.SaveChangesAsync();
+ }
+ }
+ else
+ {
+ // TODO: Use image library to check the format to prohibit bad data.
+ if (avatarEntity == null)
+ {
+ user.Avatar = new UserAvatar
+ {
+ Type = avatar.Type,
+ Data = avatar.Data
+ };
+ }
+ else
+ {
+ avatarEntity.Type = avatar.Type;
+ avatarEntity.Data = avatar.Data;
+ }
+ await _database.SaveChangesAsync();
+ }
+ }
+ }
+}
diff --git a/Timeline/Timeline.csproj b/Timeline/Timeline.csproj
index 1f70c634..29ff3354 100644
--- a/Timeline/Timeline.csproj
+++ b/Timeline/Timeline.csproj
@@ -5,12 +5,6 @@
<UserSecretsId>1f6fb74d-4277-4bc0-aeea-b1fc5ffb0b43</UserSecretsId>
<Authors>crupest</Authors>
</PropertyGroup>
- <ItemGroup>
- <Compile Remove="Migrations\20190817094126_Enhance.cs" />
- <Compile Remove="Migrations\20190817094126_Enhance.Designer.cs" />
- <Compile Remove="Migrations\20190817094254_Enhance.cs" />
- <Compile Remove="Migrations\20190817094254_Enhance.Designer.cs" />
- </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
diff --git a/Timeline/appsettings.Development.json b/Timeline/appsettings.Development.json
index db4b074a..424b3885 100644
--- a/Timeline/appsettings.Development.json
+++ b/Timeline/appsettings.Development.json
@@ -7,6 +7,6 @@
}
},
"JwtConfig": {
- "SigningKey": "crupest hahahahahahahhahahahahaha"
+ "SigningKey": "this is very very very very very long secret"
}
}
diff --git a/Timeline/default-avatar.png b/Timeline/default-avatar.png
new file mode 100644
index 00000000..4086e1d2
--- /dev/null
+++ b/Timeline/default-avatar.png
Binary files differ
diff --git a/art-src/user.svg b/art-src/user.svg
new file mode 100644
index 00000000..acbd3c8a
--- /dev/null
+++ b/art-src/user.svg
@@ -0,0 +1,4 @@
+<svg viewbox="0 0 100 100" stroke-width="5" stroke="#000000" fill="transparent">
+ <circle cx="50" cy="30" r="20"/>
+ <path d="M 10,90 A 40 40 180 1 1 90,90"/>
+</svg>