From 6829ad0903aa3310ad29b74fc435611442569d5c Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 23 Jan 2020 21:21:32 +0800 Subject: Add some unit tests for token manager. --- Timeline.Tests/Services/UserTokenManagerTest.cs | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Timeline.Tests/Services/UserTokenManagerTest.cs (limited to 'Timeline.Tests/Services/UserTokenManagerTest.cs') diff --git a/Timeline.Tests/Services/UserTokenManagerTest.cs b/Timeline.Tests/Services/UserTokenManagerTest.cs new file mode 100644 index 00000000..86ad84b3 --- /dev/null +++ b/Timeline.Tests/Services/UserTokenManagerTest.cs @@ -0,0 +1,52 @@ +using FluentAssertions; +using Microsoft.Extensions.Logging.Abstractions; +using Moq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Timeline.Services; +using Timeline.Tests.Helpers; +using Xunit; + +namespace Timeline.Tests.Services +{ + public class UserTokenManagerTest + { + private readonly UserTokenManager _service; + + private readonly Mock _mockUserService; + private readonly Mock _mockUserTokenService; + private readonly TestClock _mockClock; + + public UserTokenManagerTest() + { + _mockUserService = new Mock(); + _mockUserTokenService = new Mock(); + _mockClock = new TestClock(); + + _service = new UserTokenManager(NullLogger.Instance, _mockUserService.Object, _mockUserTokenService.Object, _mockClock); + } + + [Theory] + [InlineData(null, "aaa", "username")] + [InlineData("aaa", null, "password")] + public void CreateToken_NullArgument(string username, string password, string paramName) + { + _service.Invoking(s => s.CreateToken(username, password)).Should().Throw() + .Which.ParamName.Should().Be(paramName); + } + + [Theory] + [InlineData(typeof(UsernameBadFormatException))] + [InlineData(typeof(UserNotExistException))] + [InlineData(typeof(BadPasswordException))] + public async Task CreateToken_VerifyCredential_Throw(Type exceptionType) + { + const string username = "uuu"; + const string password = "ppp"; + _mockUserService.Setup(s => s.VerifyCredential(username, password)).ThrowsAsync((Exception)Activator.CreateInstance(exceptionType)); + await _service.Awaiting(s => s.CreateToken(username, password)).Should().ThrowAsync(exceptionType); + } + } +} -- cgit v1.2.3