diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestTokensService.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestTokensService.cs new file mode 100644 index 0000000..2c736e3 --- /dev/null +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestTokensService.cs @@ -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(); + var mongoClientMock = new Mock(); + var mongoSettingsMock = new Mock(); + var mongoCollectionMock = new Mock>(); + + mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB"); + mongoSettingsMock.SetupGet(i => i.TokensCollectionName).Returns("tokens"); + + mongoClientMock + .Setup(stub => stub.GetDatabase(It.IsAny(), + It.IsAny())) + .Returns(mongoDatabaseMock.Object); + + mongoDatabaseMock + .Setup(i => i.GetCollection(It.IsAny(), + It.IsAny())) + .Returns(mongoCollectionMock.Object); + + // Test & Assert + var service = new TokensService(mongoClientMock.Object, mongoSettingsMock.Object); + await Assert.ThrowsAsync(async () => { await service.GenerateTokensAsync(-1, ""); }); + } + + [Fact] + public async Task Test_GenerateTokensAsync_Success() + { + // Setup + var mongoDatabaseMock = new Mock(); + var mongoClientMock = new Mock(); + var mongoSettingsMock = new Mock(); + var mongoCollectionMock = new Mock>(); + + mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB"); + mongoSettingsMock.SetupGet(i => i.TokensCollectionName).Returns("tokens"); + + mongoClientMock + .Setup(stub => stub.GetDatabase(It.IsAny(), + It.IsAny())) + .Returns(mongoDatabaseMock.Object); + + mongoDatabaseMock + .Setup(i => i.GetCollection(It.IsAny(), + It.IsAny())) + .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(), + It.IsAny()), Times.Once); + } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Models/FeedbackReceiver.cs b/Retroactiune.WebAPI/Models/FeedbackReceiver.cs index 80d9b82..4c5738d 100644 --- a/Retroactiune.WebAPI/Models/FeedbackReceiver.cs +++ b/Retroactiune.WebAPI/Models/FeedbackReceiver.cs @@ -27,7 +27,7 @@ namespace Retroactiune.Models 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); } diff --git a/Retroactiune.WebAPI/Models/Token.cs b/Retroactiune.WebAPI/Models/Token.cs index 014d296..db929ab 100644 --- a/Retroactiune.WebAPI/Models/Token.cs +++ b/Retroactiune.WebAPI/Models/Token.cs @@ -14,17 +14,30 @@ namespace Retroactiune.Models [BsonId, JsonPropertyName("id")] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } - + [BsonRepresentation(BsonType.ObjectId), JsonPropertyName("feedback_receiver_id")] public string FeedbackReceiverId { get; set; } - - [JsonPropertyName("time_used")] - public DateTime? TimeUsed { get; set; } - - [JsonPropertyName("created_at")] - public DateTime CreatedAt { get; set; } - - [JsonPropertyName("expiry_time")] - public DateTime? ExpiryTime { get; set; } + + [JsonPropertyName("time_used")] public DateTime? TimeUsed { get; set; } + + [JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; } + + [JsonPropertyName("expiry_time")] public DateTime? ExpiryTime { get; set; } + + public override bool Equals(object obj) + { + 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(); + } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/TokensService.cs b/Retroactiune.WebAPI/Services/TokensService.cs index a96fa54..853ff7e 100644 --- a/Retroactiune.WebAPI/Services/TokensService.cs +++ b/Retroactiune.WebAPI/Services/TokensService.cs @@ -20,12 +20,11 @@ namespace Retroactiune.Services public async Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid, DateTime? expiryTime = null) { - // TODO: Test unit if (numberOfTokens <= 0) { throw new ArgumentException("numberOfTokens must be positive"); } - + var token = new List(); for (var i = 0; i < numberOfTokens; i++) {