2021-06-11 19:49:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using Moq;
|
2021-06-21 14:46:44 +00:00
|
|
|
|
using Retroactiune.Core.Entities;
|
|
|
|
|
using Retroactiune.Core.Interfaces;
|
|
|
|
|
using Retroactiune.Core.Services;
|
2021-06-11 19:49:12 +00:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
2021-06-21 14:46:44 +00:00
|
|
|
|
namespace Retroactiune.Tests.Retroactiune.Core.Services
|
2021-06-11 19:49:12 +00:00
|
|
|
|
{
|
|
|
|
|
public class TestTokensService
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_GenerateTokensAsync_InvalidNumberOfTokens()
|
|
|
|
|
{
|
|
|
|
|
// Setup
|
|
|
|
|
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
|
|
|
|
var mongoClientMock = new Mock<IMongoClient>();
|
2021-06-11 20:17:03 +00:00
|
|
|
|
var mongoSettingsMock = new Mock<IDatabaseSettings>();
|
2021-06-11 19:49:12 +00:00
|
|
|
|
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>();
|
2021-06-11 20:17:03 +00:00
|
|
|
|
var mongoSettingsMock = new Mock<IDatabaseSettings>();
|
2021-06-11 19:49:12 +00:00
|
|
|
|
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,
|
2021-06-15 20:08:15 +00:00
|
|
|
|
ExpiryTime = expiryTime,
|
2021-06-11 19:49:12 +00:00
|
|
|
|
TimeUsed = null,
|
|
|
|
|
FeedbackReceiverId = "Hello",
|
|
|
|
|
CreatedAt = expiryTime
|
|
|
|
|
};
|
|
|
|
|
mongoCollectionMock.Verify(
|
|
|
|
|
i => i.InsertManyAsync(new[] {item, item, item},
|
|
|
|
|
It.IsAny<InsertManyOptions>(),
|
|
|
|
|
It.IsAny<CancellationToken>()), Times.Once);
|
|
|
|
|
}
|
2021-06-20 13:53:43 +00:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_DeleteTokens_Ok()
|
|
|
|
|
{
|
|
|
|
|
// Setup
|
|
|
|
|
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
|
|
|
|
var mongoClientMock = new Mock<IMongoClient>();
|
|
|
|
|
var mongoSettingsMock = new Mock<IDatabaseSettings>();
|
|
|
|
|
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 service = new TokensService(mongoClientMock.Object, mongoSettingsMock.Object);
|
|
|
|
|
await service.DeleteTokens(new[] {"test_id"});
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
mongoCollectionMock.Verify(
|
|
|
|
|
i
|
|
|
|
|
=> i.DeleteManyAsync(
|
|
|
|
|
It.IsAny<FilterDefinition<Token>>(),
|
|
|
|
|
It.IsAny<CancellationToken>()), Times.Once);
|
|
|
|
|
}
|
2021-06-11 19:49:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|