Add unit tests for TokensService.cs
This commit is contained in:
parent
348e69cd88
commit
3da48bb65f
4 changed files with 109 additions and 13 deletions
|
@ -0,0 +1,84 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using Moq;
|
||||||
|
using Retroactiune.Models;
|
||||||
|
using Retroactiune.Services;
|
||||||
|
using Retroactiune.Settings;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
|
||||||
|
{
|
||||||
|
public class TestTokensService
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Test_GenerateTokensAsync_InvalidNumberOfTokens()
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
||||||
|
var mongoClientMock = new Mock<IMongoClient>();
|
||||||
|
var mongoSettingsMock = new Mock<IMongoDbSettings>();
|
||||||
|
var mongoCollectionMock = new Mock<IMongoCollection<Token>>();
|
||||||
|
|
||||||
|
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
|
||||||
|
mongoSettingsMock.SetupGet(i => i.TokensCollectionName).Returns("tokens");
|
||||||
|
|
||||||
|
mongoClientMock
|
||||||
|
.Setup(stub => stub.GetDatabase(It.IsAny<string>(),
|
||||||
|
It.IsAny<MongoDatabaseSettings>()))
|
||||||
|
.Returns(mongoDatabaseMock.Object);
|
||||||
|
|
||||||
|
mongoDatabaseMock
|
||||||
|
.Setup(i => i.GetCollection<Token>(It.IsAny<string>(),
|
||||||
|
It.IsAny<MongoCollectionSettings>()))
|
||||||
|
.Returns(mongoCollectionMock.Object);
|
||||||
|
|
||||||
|
// Test & Assert
|
||||||
|
var service = new TokensService(mongoClientMock.Object, mongoSettingsMock.Object);
|
||||||
|
await Assert.ThrowsAsync<ArgumentException>(async () => { await service.GenerateTokensAsync(-1, ""); });
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Test_GenerateTokensAsync_Success()
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
||||||
|
var mongoClientMock = new Mock<IMongoClient>();
|
||||||
|
var mongoSettingsMock = new Mock<IMongoDbSettings>();
|
||||||
|
var mongoCollectionMock = new Mock<IMongoCollection<Token>>();
|
||||||
|
|
||||||
|
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
|
||||||
|
mongoSettingsMock.SetupGet(i => i.TokensCollectionName).Returns("tokens");
|
||||||
|
|
||||||
|
mongoClientMock
|
||||||
|
.Setup(stub => stub.GetDatabase(It.IsAny<string>(),
|
||||||
|
It.IsAny<MongoDatabaseSettings>()))
|
||||||
|
.Returns(mongoDatabaseMock.Object);
|
||||||
|
|
||||||
|
mongoDatabaseMock
|
||||||
|
.Setup(i => i.GetCollection<Token>(It.IsAny<string>(),
|
||||||
|
It.IsAny<MongoCollectionSettings>()))
|
||||||
|
.Returns(mongoCollectionMock.Object);
|
||||||
|
|
||||||
|
// Test
|
||||||
|
var expiryTime = DateTime.UtcNow;
|
||||||
|
var service = new TokensService(mongoClientMock.Object, mongoSettingsMock.Object);
|
||||||
|
await service.GenerateTokensAsync(3, "Hello", expiryTime);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var item = new Token
|
||||||
|
{
|
||||||
|
Id = null,
|
||||||
|
ExpiryTime = null,
|
||||||
|
TimeUsed = null,
|
||||||
|
FeedbackReceiverId = "Hello",
|
||||||
|
CreatedAt = expiryTime
|
||||||
|
};
|
||||||
|
mongoCollectionMock.Verify(
|
||||||
|
i => i.InsertManyAsync(new[] {item, item, item},
|
||||||
|
It.IsAny<InsertManyOptions>(),
|
||||||
|
It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,7 +27,7 @@ namespace Retroactiune.Models
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Id.Equals(convertedObj.Id) && Name.Equals(convertedObj.Name) &&
|
return string.Equals(Id, convertedObj.Id) && string.Equals(Name, convertedObj.Name) &&
|
||||||
Description.Equals(convertedObj.Description) && CreatedAt.Equals(convertedObj.CreatedAt);
|
Description.Equals(convertedObj.Description) && CreatedAt.Equals(convertedObj.CreatedAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,17 +14,30 @@ namespace Retroactiune.Models
|
||||||
[BsonId, JsonPropertyName("id")]
|
[BsonId, JsonPropertyName("id")]
|
||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
[BsonRepresentation(BsonType.ObjectId), JsonPropertyName("feedback_receiver_id")]
|
[BsonRepresentation(BsonType.ObjectId), JsonPropertyName("feedback_receiver_id")]
|
||||||
public string FeedbackReceiverId { get; set; }
|
public string FeedbackReceiverId { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("time_used")]
|
[JsonPropertyName("time_used")] public DateTime? TimeUsed { get; set; }
|
||||||
public DateTime? TimeUsed { get; set; }
|
|
||||||
|
[JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; }
|
||||||
[JsonPropertyName("created_at")]
|
|
||||||
public DateTime CreatedAt { get; set; }
|
[JsonPropertyName("expiry_time")] public DateTime? ExpiryTime { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("expiry_time")]
|
public override bool Equals(object obj)
|
||||||
public DateTime? ExpiryTime { get; set; }
|
{
|
||||||
|
if (!(obj is Token convertedObj))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return string.Equals(Id, convertedObj.Id) && string.Equals(FeedbackReceiverId, convertedObj.FeedbackReceiverId) &&
|
||||||
|
TimeUsed == convertedObj.TimeUsed && ExpiryTime == convertedObj.ExpiryTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return base.GetHashCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -20,12 +20,11 @@ namespace Retroactiune.Services
|
||||||
public async Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid,
|
public async Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid,
|
||||||
DateTime? expiryTime = null)
|
DateTime? expiryTime = null)
|
||||||
{
|
{
|
||||||
// TODO: Test unit
|
|
||||||
if (numberOfTokens <= 0)
|
if (numberOfTokens <= 0)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("numberOfTokens must be positive");
|
throw new ArgumentException("numberOfTokens must be positive");
|
||||||
}
|
}
|
||||||
|
|
||||||
var token = new List<Token>();
|
var token = new List<Token>();
|
||||||
for (var i = 0; i < numberOfTokens; i++)
|
for (var i = 0; i < numberOfTokens; i++)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue