diff options
Diffstat (limited to 'Timeline.Tests')
-rw-r--r-- | Timeline.Tests/Helpers/TestClock.cs | 25 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs | 6 | ||||
-rw-r--r-- | Timeline.Tests/TokenUnitTest.cs | 58 |
3 files changed, 82 insertions, 7 deletions
diff --git a/Timeline.Tests/Helpers/TestClock.cs b/Timeline.Tests/Helpers/TestClock.cs new file mode 100644 index 00000000..fc200be9 --- /dev/null +++ b/Timeline.Tests/Helpers/TestClock.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; +using System; +using Timeline.Services; + +namespace Timeline.Tests.Helpers +{ + public class TestClock : IClock + { + DateTime? MockCurrentTime { get; set; } = null; + + public DateTime GetCurrentTime() + { + return MockCurrentTime.GetValueOrDefault(DateTime.Now); + } + } + + public static class TestClockWebApplicationFactoryExtensions + { + public static TestClock GetTestClock<T>(this WebApplicationFactory<T> factory) where T : class + { + return factory.Server.Host.Services.GetRequiredService<IClock>() as TestClock; + } + } +} diff --git a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs index a7616b41..aa005ba3 100644 --- a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs +++ b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs @@ -1,9 +1,11 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Timeline.Models; +using Timeline.Services; using Xunit.Abstractions; namespace Timeline.Tests.Helpers @@ -46,6 +48,10 @@ namespace Timeline.Tests.Helpers db.Users.AddRange(TestMockUsers.MockUsers); db.SaveChanges(); } + }) + .ConfigureTestServices(services => + { + services.AddSingleton<IClock, TestClock>(); }); }); } diff --git a/Timeline.Tests/TokenUnitTest.cs b/Timeline.Tests/TokenUnitTest.cs index 27c2ed32..d7df8797 100644 --- a/Timeline.Tests/TokenUnitTest.cs +++ b/Timeline.Tests/TokenUnitTest.cs @@ -1,10 +1,14 @@ using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using System.Linq; using System.Net; using System.Net.Http; using Timeline.Controllers; +using Timeline.Entities; using Timeline.Entities.Http; +using Timeline.Models; +using Timeline.Services; using Timeline.Tests.Helpers; using Timeline.Tests.Helpers.Authentication; using Xunit; @@ -80,23 +84,63 @@ namespace Timeline.Tests { var response = await client.PostAsJsonAsync(VerifyTokenUrl, new VerifyTokenRequest { Token = "bad token hahaha" }); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + var body = await response.ReadBodyAsJson<CommonResponse>(); + Assert.Equal(TokenController.ErrorCodes.Verify_BadToken, body.Code); } } [Fact] - public async void VerifyTokenTest_GoodToken() + public async void VerifyTokenTest_BadVersion_AND_UserNotExist() { using (var client = _factory.CreateDefaultClient()) { - var createTokenResult = await client.CreateUserTokenAsync("admin", "admin"); + using (var scope = _factory.Server.Host.Services.CreateScope()) // UserService is scoped. + { + // create a user for test + var userService = scope.ServiceProvider.GetRequiredService<IUserService>(); + + const string username = "verifytokentest0"; + const string password = "12345678"; + + await userService.PutUser(username, password, false); + + // create a token + var token = (await client.CreateUserTokenAsync(username, password)).Token; + // increase version + await userService.PatchUser(username, null, null); + + // test against bad version + var response = await client.PostAsJsonAsync(VerifyTokenUrl, new VerifyTokenRequest { Token = token }); + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + var body = await response.ReadBodyAsJson<CommonResponse>(); + Assert.Equal(TokenController.ErrorCodes.Verify_BadVersion, body.Code); + + // create another token + var token2 = (await client.CreateUserTokenAsync(username, password)).Token; + + // delete user + await userService.DeleteUser(username); + + // test against user not exist + var response2 = await client.PostAsJsonAsync(VerifyTokenUrl, new VerifyTokenRequest { Token = token }); + Assert.Equal(HttpStatusCode.BadRequest, response2.StatusCode); + var body2 = await response2.ReadBodyAsJson<CommonResponse>(); + Assert.Equal(TokenController.ErrorCodes.Verify_UserNotExist, body2.Code); + } + } + } + + [Fact] + public async void VerifyTokenTest_Success() + { + using (var client = _factory.CreateDefaultClient()) + { + var createTokenResult = await client.CreateUserTokenAsync("admin", "admin"); var response = await client.PostAsJsonAsync(VerifyTokenUrl, new VerifyTokenRequest { Token = createTokenResult.Token }); Assert.Equal(HttpStatusCode.OK, response.StatusCode); - - var result = JsonConvert.DeserializeObject<VerifyTokenResponse>(await response.Content.ReadAsStringAsync()); - Assert.NotNull(result.User); - Assert.Equal(createTokenResult.User.Username, result.User.Username); - Assert.Equal(createTokenResult.User.Administrator, result.User.Administrator); + var body = JsonConvert.DeserializeObject<VerifyTokenResponse>(await response.Content.ReadAsStringAsync()); + Assert.Equal(TestMockUsers.MockUserInfos.Where(u => u.Username == "user").Single(), body.User, UserInfoComparers.EqualityComparer); } } } |