Add integration tests for TokensController and fix bugs.

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-15 23:08:15 +03:00
parent c2a4c0e147
commit 097381a75a
7 changed files with 119 additions and 10 deletions

View file

@ -22,6 +22,7 @@ using Xunit;
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
{
[Collection("IntegrationTests")]
public class TestFeedbackReceiver : IClassFixture<WebApiTestingFactory>
{
private readonly MongoDbFixture _mongoDb;
@ -38,6 +39,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
[Fact]
public async Task Test_Create_NoContent()
{
await _mongoDb.DropAsync();
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
new StringContent("[]", Encoding.UTF8, "application/json"));
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
@ -47,6 +49,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
public async Task Test_Create_NoName()
{
// Arrange
await _mongoDb.DropAsync();
var fixture = new Fixture();
var item = fixture.Create<FeedbackReceiverInDto>();
item.Name = null;
@ -65,6 +68,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
public async Task Test_Create_NoDescription()
{
// Arrange
await _mongoDb.DropAsync();
var fixture = new Fixture();
var item = fixture.Create<FeedbackReceiverInDto>();
item.Description = null;
@ -117,18 +121,19 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
public async Task Test_Delete_OK(IEnumerable<FeedbackReceiver> items)
{
// Arrange
await _mongoDb.DropAsync();
var guids = new List<string>();
await _mongoDb.DropAsync();
byte index = 0;
var cleanItems = items.Select(i =>
var feedbackReceivers = items as FeedbackReceiver[] ?? items.ToArray();
foreach (var i in feedbackReceivers)
{
i.Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, index, 4, 5, 6, 7, 8, 9, index, 11, 14}))
.ToString();
index += 1;
guids.Add(i.Id);
return i;
});
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(cleanItems);
}
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers);
// Test

View file

@ -0,0 +1,101 @@
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Retroactiune.Database;
using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures;
using Retroactiune.Models;
using Xunit;
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
{
[Collection("IntegrationTests")]
public class TestTokens : IClassFixture<WebApiTestingFactory>
{
private readonly MongoDbFixture _mongoDb;
private readonly HttpClient _client;
public TestTokens(WebApiTestingFactory factory)
{
_client = factory.CreateClient();
var dbSettings = factory.Services.GetService<IOptions<DatabaseSettings>>();
_mongoDb = new MongoDbFixture(dbSettings);
}
[Fact]
public async Task Test_GenerateTokens_Ok()
{
// Arrange
await _mongoDb.DropAsync();
await _mongoDb.FeedbackReceiverCollection.InsertOneAsync(new FeedbackReceiver
{
Id = "123456789012345678901234"
});
var expiryTime = DateTime.Today.AddDays(1);
// Test
var httpResponse = await _client.PostAsync("/api/v1/Tokens/",
new StringContent(
$"{{\"numberOfTokens\": 2, \"feedbackReceiverId\": \"123456789012345678901234\", \"expiryTime\": \"{expiryTime.ToUniversalTime():O}\" }}",
Encoding.UTF8,
"application/json"));
// Assert
Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
var tokens = (await _mongoDb.TokensCollection.FindAsync(FilterDefinition<Token>.Empty)).ToList();
Assert.Equal(2, tokens.Count);
foreach (var t in tokens)
{
Assert.Equal("123456789012345678901234", t.FeedbackReceiverId);
Assert.NotNull(t.ExpiryTime);
}
}
[Fact]
public async Task Test_GenerateTokens_ExpiryInThePast()
{
// Arrange
await _mongoDb.DropAsync();
await _mongoDb.FeedbackReceiverCollection.InsertOneAsync(new FeedbackReceiver
{
Id = "123456789012345678901234"
});
var expiryTime = DateTime.Today.AddDays(-1);
// Test
var httpResponse = await _client.PostAsync("/api/v1/Tokens/",
new StringContent(
$"{{\"numberOfTokens\": 2, \"feedbackReceiverId\": \"123456789012345678901234\", \"expiryTime\": \"{expiryTime.ToUniversalTime():O}\" }}",
Encoding.UTF8,
"application/json"));
// Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Fact]
public async Task Test_GenerateTokens_NonExistingFeedbackReceiver()
{
var httpResponse = await _client.PostAsync("/api/v1/Tokens/",
new StringContent("{\"numberOfTokens\": 1, \"feedbackReceiverId\": \"someid\"}", Encoding.UTF8,
"application/json"));
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Fact]
public async Task Test_GenerateTokens_NoBody()
{
var httpResponse = await _client.PostAsync("/api/v1/Tokens/",
new StringContent("{}", Encoding.UTF8, "application/json"));
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
}
}

View file

@ -11,11 +11,14 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures
public class MongoDbFixture : IAsyncDisposable
{
private readonly IDatabaseSettings _settings;
public IMongoDatabase Database { get; }
private IMongoDatabase Database { get; }
public IMongoCollection<FeedbackReceiver> FeedbackReceiverCollection =>
Database.GetCollection<FeedbackReceiver>(_settings.FeedbackReceiverCollectionName);
public IMongoCollection<Token> TokensCollection =>
Database.GetCollection<Token>(_settings.TokensCollectionName);
public MongoDbFixture(IOptions<DatabaseSettings> options)
{
_settings = options.Value;

View file

@ -70,7 +70,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
var item = new Token
{
Id = null,
ExpiryTime = null,
ExpiryTime = expiryTime,
TimeUsed = null,
FeedbackReceiverId = "Hello",
CreatedAt = expiryTime

View file

@ -22,8 +22,7 @@ namespace Retroactiune.Controllers
_feedbackReceiverService = feedbackReceiverService;
_tokensService = tokensService;
}
// TODO: Test integration.
/// <summary>
/// Creates a new batch of tokens, the tokens are tied to a FeedbackReceiver and are used by the client
/// when leaving Feedback.

View file

@ -32,7 +32,7 @@ namespace Retroactiune.Services
{
CreatedAt = DateTime.UtcNow,
FeedbackReceiverId = feedbackReceiverGuid,
ExpiryTime = null,
ExpiryTime = expiryTime,
TimeUsed = null
});
}

View file

@ -11,7 +11,7 @@ namespace Retroactiune
{
public class TestingStartup
{
public TestingStartup(IConfiguration configuration)
public TestingStartup()
{
var configurationBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
@ -34,6 +34,7 @@ namespace Retroactiune
// Services
services.AddSingleton<IFeedbackReceiverService, FeedbackReceiverService>();
services.AddSingleton<ITokensService, TokensService>();
services.AddSingleton<IMongoClient, MongoClient>(i =>
{
var settings = i.GetService<IOptions<DatabaseSettings>>();