From a9f248ad817683e911348cd168c570db3d07757f Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 21 Apr 2019 00:08:59 +0800 Subject: Reorgnize api. Add basic unit test. --- Timeline.Tests/JwtTokenUnitTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Timeline.Tests/JwtTokenUnitTest.cs') diff --git a/Timeline.Tests/JwtTokenUnitTest.cs b/Timeline.Tests/JwtTokenUnitTest.cs index fa9c7628..39ffc928 100644 --- a/Timeline.Tests/JwtTokenUnitTest.cs +++ b/Timeline.Tests/JwtTokenUnitTest.cs @@ -12,8 +12,8 @@ namespace Timeline.Tests { public class JwtTokenUnitTest : IClassFixture> { - private const string CreateTokenUrl = "User/CreateToken"; - private const string VerifyTokenUrl = "User/VerifyToken"; + private const string CreateTokenUrl = "token/create"; + private const string VerifyTokenUrl = "token/verify"; private readonly WebApplicationFactory _factory; -- cgit v1.2.3 From 407f97db0be86aa071802b67bfdeadc7703528c9 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 22 Apr 2019 14:45:52 +0800 Subject: Move http models in to a new namespace. Revert last commit. --- .../Authentication/AuthenticationExtensions.cs | 2 +- Timeline.Tests/JwtTokenUnitTest.cs | 2 +- Timeline/Controllers/TokenController.cs | 2 +- Timeline/Controllers/UserController.cs | 46 ++++++---------------- Timeline/Entities/Common.cs | 12 ------ Timeline/Entities/Http/Common.cs | 29 ++++++++++++++ Timeline/Entities/Http/Token.cs | 26 ++++++++++++ Timeline/Entities/Http/User.cs | 26 ++++++++++++ Timeline/Entities/Token.cs | 26 ------------ Timeline/Entities/User.cs | 30 -------------- Timeline/Services/UserService.cs | 10 ++--- 11 files changed, 102 insertions(+), 109 deletions(-) delete mode 100644 Timeline/Entities/Common.cs create mode 100644 Timeline/Entities/Http/Common.cs create mode 100644 Timeline/Entities/Http/Token.cs create mode 100644 Timeline/Entities/Http/User.cs delete mode 100644 Timeline/Entities/Token.cs delete mode 100644 Timeline/Entities/User.cs (limited to 'Timeline.Tests/JwtTokenUnitTest.cs') diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index 40191009..cda9fe99 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -4,7 +4,7 @@ using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; -using Timeline.Entities; +using Timeline.Entities.Http; using Xunit; namespace Timeline.Tests.Helpers.Authentication diff --git a/Timeline.Tests/JwtTokenUnitTest.cs b/Timeline.Tests/JwtTokenUnitTest.cs index 39ffc928..8a503bd7 100644 --- a/Timeline.Tests/JwtTokenUnitTest.cs +++ b/Timeline.Tests/JwtTokenUnitTest.cs @@ -2,7 +2,7 @@ using Newtonsoft.Json; using System.Net; using System.Net.Http; -using Timeline.Entities; +using Timeline.Entities.Http; using Timeline.Tests.Helpers; using Timeline.Tests.Helpers.Authentication; using Xunit; diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index 463fb83c..0be5fb2f 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System.Threading.Tasks; -using Timeline.Entities; +using Timeline.Entities.Http; using Timeline.Services; namespace Timeline.Controllers diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index d2708eeb..59c7a48c 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc; using System; using System.Threading.Tasks; using Timeline.Entities; +using Timeline.Entities.Http; using Timeline.Services; namespace Timeline.Controllers @@ -48,50 +49,29 @@ namespace Timeline.Controllers } } - [HttpPatch("user/{username}"), Authorize] + [HttpPatch("user/{username}"), Authorize(Roles = "admin")] public async Task Patch([FromBody] UserModifyRequest request, [FromRoute] string username) { - if (User.IsInRole("admin")) - { - var result = await _userService.PatchUser(username, request.Password, request.Roles); - switch (result) - { - case PatchUserResult.Success: - return Ok(); - case PatchUserResult.NotExists: - return NotFound(); - default: - throw new Exception("Unreachable code."); - } - } - else + var result = await _userService.PatchUser(username, request.Password, request.Roles); + switch (result) { - if (User.Identity.Name != username) - return StatusCode(403, new MessageResponse("Can't patch other user when you are not admin.")); - if (request.Roles != null) - return StatusCode(403, new MessageResponse("Can't patch roles when you are not admin.")); - - var result = await _userService.PatchUser(username, request.Password, null); - switch (result) - { - case PatchUserResult.Success: - return Ok(); - case PatchUserResult.NotExists: - return NotFound(new MessageResponse("This username no longer exists. Please update your token.")); - default: - throw new Exception("Unreachable code."); - } + case PatchUserResult.Success: + return Ok(); + case PatchUserResult.NotExists: + return NotFound(); + default: + throw new Exception("Unreachable code."); } } [HttpDelete("user/{username}"), Authorize(Roles = "admin")] - public async Task> Delete([FromRoute] string username) + public async Task Delete([FromRoute] string username) { var result = await _userService.DeleteUser(username); switch (result) { - case DeleteUserResult.Success: - return Ok(UserDeleteResponse.Success); + case DeleteUserResult.Deleted: + return Ok(UserDeleteResponse.Deleted); case DeleteUserResult.NotExists: return Ok(UserDeleteResponse.NotExists); default: diff --git a/Timeline/Entities/Common.cs b/Timeline/Entities/Common.cs deleted file mode 100644 index 235a2a20..00000000 --- a/Timeline/Entities/Common.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Timeline.Entities -{ - public class MessageResponse - { - public MessageResponse(string message) - { - Message = message; - } - - public string Message { get; set; } - } -} diff --git a/Timeline/Entities/Http/Common.cs b/Timeline/Entities/Http/Common.cs new file mode 100644 index 00000000..9575e6fa --- /dev/null +++ b/Timeline/Entities/Http/Common.cs @@ -0,0 +1,29 @@ +namespace Timeline.Entities.Http +{ + public class ReturnCodeMessageResponse + { + public ReturnCodeMessageResponse() + { + + } + + public ReturnCodeMessageResponse(int code) + { + ReturnCode = code; + } + + public ReturnCodeMessageResponse(string message) + { + Message = message; + } + + public ReturnCodeMessageResponse(int code, string message) + { + ReturnCode = code; + Message = message; + } + + public int? ReturnCode { get; set; } = null; + public string Message { get; set; } = null; + } +} diff --git a/Timeline/Entities/Http/Token.cs b/Timeline/Entities/Http/Token.cs new file mode 100644 index 00000000..45ee0fc5 --- /dev/null +++ b/Timeline/Entities/Http/Token.cs @@ -0,0 +1,26 @@ +namespace Timeline.Entities.Http +{ + public class CreateTokenRequest + { + public string Username { get; set; } + public string Password { get; set; } + } + + public class CreateTokenResponse + { + public bool Success { get; set; } + public string Token { get; set; } + public UserInfo UserInfo { get; set; } + } + + public class VerifyTokenRequest + { + public string Token { get; set; } + } + + public class VerifyTokenResponse + { + public bool IsValid { get; set; } + public UserInfo UserInfo { get; set; } + } +} diff --git a/Timeline/Entities/Http/User.cs b/Timeline/Entities/Http/User.cs new file mode 100644 index 00000000..24952ac7 --- /dev/null +++ b/Timeline/Entities/Http/User.cs @@ -0,0 +1,26 @@ +namespace Timeline.Entities.Http +{ + public class UserModifyRequest + { + public string Password { get; set; } + public string[] Roles { get; set; } + } + + public static class UserPutResponse + { + public const int CreatedCode = 0; + public const int ModifiedCode = 1; + + public static ReturnCodeMessageResponse Created { get; } = new ReturnCodeMessageResponse(CreatedCode, "A new user is created."); + public static ReturnCodeMessageResponse Modified { get; } = new ReturnCodeMessageResponse(ModifiedCode, "A existing user is modified."); + } + + public static class UserDeleteResponse + { + public const int DeletedCode = 0; + public const int NotExistsCode = 1; + + public static ReturnCodeMessageResponse Deleted { get; } = new ReturnCodeMessageResponse(DeletedCode, "A existing user is deleted."); + public static ReturnCodeMessageResponse NotExists { get; } = new ReturnCodeMessageResponse(NotExistsCode, "User with given name does not exists."); + } +} diff --git a/Timeline/Entities/Token.cs b/Timeline/Entities/Token.cs deleted file mode 100644 index 1b5a469d..00000000 --- a/Timeline/Entities/Token.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace Timeline.Entities -{ - public class CreateTokenRequest - { - public string Username { get; set; } - public string Password { get; set; } - } - - public class CreateTokenResponse - { - public bool Success { get; set; } - public string Token { get; set; } - public UserInfo UserInfo { get; set; } - } - - public class VerifyTokenRequest - { - public string Token { get; set; } - } - - public class VerifyTokenResponse - { - public bool IsValid { get; set; } - public UserInfo UserInfo { get; set; } - } -} diff --git a/Timeline/Entities/User.cs b/Timeline/Entities/User.cs deleted file mode 100644 index eb126165..00000000 --- a/Timeline/Entities/User.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace Timeline.Entities -{ - public class UserModifyRequest - { - public string Password { get; set; } - public string[] Roles { get; set; } - } - - public class UserPutResponse - { - public const int CreatedCode = 0; - public const int ModifiedCode = 1; - - public static UserPutResponse Created { get; } = new UserPutResponse { ReturnCode = CreatedCode }; - public static UserPutResponse Modified { get; } = new UserPutResponse { ReturnCode = ModifiedCode }; - - public int ReturnCode { get; set; } - } - - public class UserDeleteResponse - { - public const int SuccessCode = 0; - public const int NotExistsCode = 1; - - public static UserDeleteResponse Success { get; } = new UserDeleteResponse { ReturnCode = SuccessCode }; - public static UserDeleteResponse NotExists { get; } = new UserDeleteResponse { ReturnCode = NotExistsCode }; - - public int ReturnCode { get; set; } - } -} diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index a0d358dd..8615d0c5 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -40,9 +40,9 @@ namespace Timeline.Services public enum DeleteUserResult { /// - /// Succeed to delete user. + /// A existing user is deleted. /// - Success, + Deleted, /// /// A user of given username does not exist. /// @@ -105,12 +105,12 @@ namespace Timeline.Services /// /// Delete a user of given username. - /// Return if success to delete. + /// Return if the user is deleted. /// Return if the user of given username /// does not exist. /// /// Username of thet user to delete. - /// if success to delete. + /// if the user is deleted. /// if the user doesn't exist. Task DeleteUser(string username); } @@ -250,7 +250,7 @@ namespace Timeline.Services _databaseContext.Users.Remove(user); await _databaseContext.SaveChangesAsync(); - return DeleteUserResult.Success; + return DeleteUserResult.Deleted; } } } -- cgit v1.2.3