Add integration tests for TokensController and fix bugs.
This commit is contained in:
parent
c2a4c0e147
commit
097381a75a
7 changed files with 119 additions and 10 deletions
|
@ -22,6 +22,7 @@ using Xunit;
|
||||||
|
|
||||||
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
|
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
|
||||||
{
|
{
|
||||||
|
[Collection("IntegrationTests")]
|
||||||
public class TestFeedbackReceiver : IClassFixture<WebApiTestingFactory>
|
public class TestFeedbackReceiver : IClassFixture<WebApiTestingFactory>
|
||||||
{
|
{
|
||||||
private readonly MongoDbFixture _mongoDb;
|
private readonly MongoDbFixture _mongoDb;
|
||||||
|
@ -38,6 +39,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Test_Create_NoContent()
|
public async Task Test_Create_NoContent()
|
||||||
{
|
{
|
||||||
|
await _mongoDb.DropAsync();
|
||||||
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
|
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
|
||||||
new StringContent("[]", Encoding.UTF8, "application/json"));
|
new StringContent("[]", Encoding.UTF8, "application/json"));
|
||||||
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
|
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
|
||||||
|
@ -47,6 +49,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
|
||||||
public async Task Test_Create_NoName()
|
public async Task Test_Create_NoName()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
|
await _mongoDb.DropAsync();
|
||||||
var fixture = new Fixture();
|
var fixture = new Fixture();
|
||||||
var item = fixture.Create<FeedbackReceiverInDto>();
|
var item = fixture.Create<FeedbackReceiverInDto>();
|
||||||
item.Name = null;
|
item.Name = null;
|
||||||
|
@ -65,6 +68,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
|
||||||
public async Task Test_Create_NoDescription()
|
public async Task Test_Create_NoDescription()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
|
await _mongoDb.DropAsync();
|
||||||
var fixture = new Fixture();
|
var fixture = new Fixture();
|
||||||
var item = fixture.Create<FeedbackReceiverInDto>();
|
var item = fixture.Create<FeedbackReceiverInDto>();
|
||||||
item.Description = null;
|
item.Description = null;
|
||||||
|
@ -117,18 +121,19 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
|
||||||
public async Task Test_Delete_OK(IEnumerable<FeedbackReceiver> items)
|
public async Task Test_Delete_OK(IEnumerable<FeedbackReceiver> items)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
|
await _mongoDb.DropAsync();
|
||||||
var guids = new List<string>();
|
var guids = new List<string>();
|
||||||
await _mongoDb.DropAsync();
|
await _mongoDb.DropAsync();
|
||||||
byte index = 0;
|
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}))
|
i.Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, index, 4, 5, 6, 7, 8, 9, index, 11, 14}))
|
||||||
.ToString();
|
.ToString();
|
||||||
index += 1;
|
index += 1;
|
||||||
guids.Add(i.Id);
|
guids.Add(i.Id);
|
||||||
return i;
|
}
|
||||||
});
|
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers);
|
||||||
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(cleanItems);
|
|
||||||
|
|
||||||
|
|
||||||
// Test
|
// Test
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,11 +11,14 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures
|
||||||
public class MongoDbFixture : IAsyncDisposable
|
public class MongoDbFixture : IAsyncDisposable
|
||||||
{
|
{
|
||||||
private readonly IDatabaseSettings _settings;
|
private readonly IDatabaseSettings _settings;
|
||||||
public IMongoDatabase Database { get; }
|
private IMongoDatabase Database { get; }
|
||||||
|
|
||||||
public IMongoCollection<FeedbackReceiver> FeedbackReceiverCollection =>
|
public IMongoCollection<FeedbackReceiver> FeedbackReceiverCollection =>
|
||||||
Database.GetCollection<FeedbackReceiver>(_settings.FeedbackReceiverCollectionName);
|
Database.GetCollection<FeedbackReceiver>(_settings.FeedbackReceiverCollectionName);
|
||||||
|
|
||||||
|
public IMongoCollection<Token> TokensCollection =>
|
||||||
|
Database.GetCollection<Token>(_settings.TokensCollectionName);
|
||||||
|
|
||||||
public MongoDbFixture(IOptions<DatabaseSettings> options)
|
public MongoDbFixture(IOptions<DatabaseSettings> options)
|
||||||
{
|
{
|
||||||
_settings = options.Value;
|
_settings = options.Value;
|
||||||
|
|
|
@ -70,7 +70,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
|
||||||
var item = new Token
|
var item = new Token
|
||||||
{
|
{
|
||||||
Id = null,
|
Id = null,
|
||||||
ExpiryTime = null,
|
ExpiryTime = expiryTime,
|
||||||
TimeUsed = null,
|
TimeUsed = null,
|
||||||
FeedbackReceiverId = "Hello",
|
FeedbackReceiverId = "Hello",
|
||||||
CreatedAt = expiryTime
|
CreatedAt = expiryTime
|
||||||
|
|
|
@ -22,8 +22,7 @@ namespace Retroactiune.Controllers
|
||||||
_feedbackReceiverService = feedbackReceiverService;
|
_feedbackReceiverService = feedbackReceiverService;
|
||||||
_tokensService = tokensService;
|
_tokensService = tokensService;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Test integration.
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new batch of tokens, the tokens are tied to a FeedbackReceiver and are used by the client
|
/// Creates a new batch of tokens, the tokens are tied to a FeedbackReceiver and are used by the client
|
||||||
/// when leaving Feedback.
|
/// when leaving Feedback.
|
||||||
|
|
|
@ -32,7 +32,7 @@ namespace Retroactiune.Services
|
||||||
{
|
{
|
||||||
CreatedAt = DateTime.UtcNow,
|
CreatedAt = DateTime.UtcNow,
|
||||||
FeedbackReceiverId = feedbackReceiverGuid,
|
FeedbackReceiverId = feedbackReceiverGuid,
|
||||||
ExpiryTime = null,
|
ExpiryTime = expiryTime,
|
||||||
TimeUsed = null
|
TimeUsed = null
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace Retroactiune
|
||||||
{
|
{
|
||||||
public class TestingStartup
|
public class TestingStartup
|
||||||
{
|
{
|
||||||
public TestingStartup(IConfiguration configuration)
|
public TestingStartup()
|
||||||
{
|
{
|
||||||
var configurationBuilder = new ConfigurationBuilder()
|
var configurationBuilder = new ConfigurationBuilder()
|
||||||
.AddJsonFile("appsettings.json")
|
.AddJsonFile("appsettings.json")
|
||||||
|
@ -34,6 +34,7 @@ namespace Retroactiune
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
services.AddSingleton<IFeedbackReceiverService, FeedbackReceiverService>();
|
services.AddSingleton<IFeedbackReceiverService, FeedbackReceiverService>();
|
||||||
|
services.AddSingleton<ITokensService, TokensService>();
|
||||||
services.AddSingleton<IMongoClient, MongoClient>(i =>
|
services.AddSingleton<IMongoClient, MongoClient>(i =>
|
||||||
{
|
{
|
||||||
var settings = i.GetService<IOptions<DatabaseSettings>>();
|
var settings = i.GetService<IOptions<DatabaseSettings>>();
|
||||||
|
|
Loading…
Reference in a new issue