aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Timeline.Tests/IntegratedTests/IntegratedTestBase.cs3
-rw-r--r--Timeline.Tests/IntegratedTests/UserTest.cs6
-rw-r--r--Timeline/Controllers/UserController.cs6
-rw-r--r--Timeline/Models/Http/UserInfo.cs14
-rw-r--r--Timeline/Services/UserService.cs8
5 files changed, 16 insertions, 21 deletions
diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
index 59af5eab..dfde2ea5 100644
--- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
+++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
@@ -12,13 +12,12 @@ using Xunit;
namespace Timeline.Tests.IntegratedTests
{
-
public abstract class IntegratedTestBase : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
{
static IntegratedTestBase()
{
FluentAssertions.AssertionOptions.AssertEquivalencyUsing(options =>
- options.Excluding(m => m.RuntimeType == typeof(UserInfo) && m.SelectedMemberPath == "_links"));
+ options.Excluding(m => m.RuntimeType == typeof(UserInfoLinks)));
}
protected TestApplication TestApp { get; }
diff --git a/Timeline.Tests/IntegratedTests/UserTest.cs b/Timeline.Tests/IntegratedTests/UserTest.cs
index bbeb6ad6..f863eb6c 100644
--- a/Timeline.Tests/IntegratedTests/UserTest.cs
+++ b/Timeline.Tests/IntegratedTests/UserTest.cs
@@ -296,7 +296,11 @@ namespace Timeline.Tests.IntegratedTests
Administrator = true,
Nickname = "ccc"
});
- res.Should().HaveStatusCode(200);
+ var body = res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<UserInfo>().Which;
+ body.Username.Should().Be("aaa");
+ body.Nickname.Should().Be("ccc");
+ body.Administrator.Should().BeTrue();
}
{
var res = await client.GetAsync("users/aaa");
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index 4572296b..26e63f63 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -108,12 +108,12 @@ namespace Timeline.Controllers
}
[HttpPost("userop/createuser"), AdminAuthorize]
- public async Task<ActionResult> CreateUser([FromBody] CreateUserRequest body)
+ public async Task<ActionResult<UserInfo>> CreateUser([FromBody] CreateUserRequest body)
{
try
{
- await _userService.CreateUser(_mapper.Map<User>(body));
- return Ok();
+ var user = await _userService.CreateUser(_mapper.Map<User>(body));
+ return Ok(ConvertToUserInfo(user));
}
catch (ConflictException)
{
diff --git a/Timeline/Models/Http/UserInfo.cs b/Timeline/Models/Http/UserInfo.cs
index fee53ade..0d1d702b 100644
--- a/Timeline/Models/Http/UserInfo.cs
+++ b/Timeline/Models/Http/UserInfo.cs
@@ -25,14 +25,8 @@ namespace Timeline.Models.Http
public class UserInfoLinksValueResolver : IValueResolver<User, UserInfo, UserInfoLinks?>
{
- private readonly IActionContextAccessor? _actionContextAccessor;
- private readonly IUrlHelperFactory? _urlHelperFactory;
-
- public UserInfoLinksValueResolver()
- {
- _actionContextAccessor = null;
- _urlHelperFactory = null;
- }
+ private readonly IActionContextAccessor _actionContextAccessor;
+ private readonly IUrlHelperFactory _urlHelperFactory;
public UserInfoLinksValueResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory)
{
@@ -42,10 +36,8 @@ namespace Timeline.Models.Http
public UserInfoLinks? Resolve(User source, UserInfo destination, UserInfoLinks? destMember, ResolutionContext context)
{
- if (_actionContextAccessor == null || _urlHelperFactory == null)
- {
+ if (_actionContextAccessor.ActionContext == null)
return null;
- }
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
var result = new UserInfoLinks
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs
index d2dc969e..93d92740 100644
--- a/Timeline/Services/UserService.cs
+++ b/Timeline/Services/UserService.cs
@@ -64,7 +64,7 @@ namespace Timeline.Services
/// </summary>
/// <param name="info">The info of new user.</param>
/// <param name="password">The password, can't be null or empty.</param>
- /// <returns>The id of the new user.</returns>
+ /// <returns>The the new user.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="info"/>is null.</exception>
/// <exception cref="ArgumentException">Thrown when some fields in <paramref name="info"/> is bad.</exception>
/// <exception cref="ConflictException">Thrown when a user with given username already exists.</exception>
@@ -75,7 +75,7 @@ namespace Timeline.Services
/// <see cref="User.Nickname"/> must be a valid nickname if set. It is empty by default.
/// Other fields are ignored.
/// </remarks>
- Task<long> CreateUser(User info);
+ Task<User> CreateUser(User info);
/// <summary>
/// Modify a user's info.
@@ -276,7 +276,7 @@ namespace Timeline.Services
return entities.Select(user => CreateUserFromEntity(user)).ToArray();
}
- public async Task<long> CreateUser(User info)
+ public async Task<User> CreateUser(User info)
{
if (info == null)
throw new ArgumentNullException(nameof(info));
@@ -316,7 +316,7 @@ namespace Timeline.Services
_logger.LogInformation(Log.Format(LogDatabaseCreate,
("Id", newEntity.Id), ("Username", username), ("Administrator", administrator)));
- return newEntity.Id;
+ return CreateUserFromEntity(newEntity);
}
private void ValidateModifyUserInfo(User? info)