aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-23 21:21:32 +0800
committercrupest <crupest@outlook.com>2020-01-23 21:21:32 +0800
commit6829ad0903aa3310ad29b74fc435611442569d5c (patch)
treea739de322dcd3bb43bbbdfed6d1e54f15918e06d
parentc72996ff9854aededc4ecfab92ef5d42167edd86 (diff)
downloadtimeline-6829ad0903aa3310ad29b74fc435611442569d5c.tar.gz
timeline-6829ad0903aa3310ad29b74fc435611442569d5c.tar.bz2
timeline-6829ad0903aa3310ad29b74fc435611442569d5c.zip
Add some unit tests for token manager.
-rw-r--r--Timeline.Tests/Helpers/ParameterInfoAssertions.cs3
-rw-r--r--Timeline.Tests/Services/UserTokenManagerTest.cs52
2 files changed, 52 insertions, 3 deletions
diff --git a/Timeline.Tests/Helpers/ParameterInfoAssertions.cs b/Timeline.Tests/Helpers/ParameterInfoAssertions.cs
index e3becee1..d3e5a41e 100644
--- a/Timeline.Tests/Helpers/ParameterInfoAssertions.cs
+++ b/Timeline.Tests/Helpers/ParameterInfoAssertions.cs
@@ -3,10 +3,7 @@ using FluentAssertions.Execution;
using FluentAssertions.Formatting;
using FluentAssertions.Primitives;
using System;
-using System.Collections.Generic;
-using System.Linq;
using System.Reflection;
-using System.Threading.Tasks;
namespace Timeline.Tests.Helpers
{
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<IUserService> _mockUserService;
+ private readonly Mock<IUserTokenService> _mockUserTokenService;
+ private readonly TestClock _mockClock;
+
+ public UserTokenManagerTest()
+ {
+ _mockUserService = new Mock<IUserService>();
+ _mockUserTokenService = new Mock<IUserTokenService>();
+ _mockClock = new TestClock();
+
+ _service = new UserTokenManager(NullLogger<UserTokenManager>.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<ArgumentNullException>()
+ .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);
+ }
+ }
+}