retroactiune/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs

201 lines
6.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2021-05-30 15:29:30 +00:00
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
2021-05-30 15:29:30 +00:00
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.Xunit2;
2021-05-30 17:39:04 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
2021-05-30 15:29:30 +00:00
using MongoDB.Driver;
using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures;
using Retroactiune.Models;
2021-05-30 17:39:04 +00:00
using Retroactiune.Settings;
2021-05-30 15:29:30 +00:00
using Xunit;
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
{
2021-05-30 17:39:04 +00:00
public class TestFeedbackReceiver : IClassFixture<WebApiTestingFactory>
2021-05-30 15:29:30 +00:00
{
private readonly MongoDbFixture _mongoDb;
private readonly HttpClient _client;
2021-05-30 17:39:04 +00:00
public TestFeedbackReceiver(WebApiTestingFactory factory)
2021-05-30 15:29:30 +00:00
{
_client = factory.CreateClient();
2021-05-30 17:39:04 +00:00
var dbSettings = factory.Services.GetService<IOptions<RetroactiuneDbSettings>>();
_mongoDb = new MongoDbFixture(dbSettings);
2021-05-30 15:29:30 +00:00
}
[Fact]
public async Task Test_Create_NoContent()
2021-05-30 15:29:30 +00:00
{
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
2021-05-30 15:29:30 +00:00
new StringContent("[]", Encoding.UTF8, "application/json"));
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Fact]
public async Task Test_Create_NoName()
2021-05-30 15:29:30 +00:00
{
// Arrange
var fixture = new Fixture();
var item = fixture.Create<FeedbackReceiverInDto>();
2021-05-30 15:29:30 +00:00
item.Name = null;
var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverInDto> {item});
2021-05-30 15:29:30 +00:00
// Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
2021-05-30 15:29:30 +00:00
new StringContent(jsonContent, Encoding.UTF8, "application/json"));
// Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Fact]
public async Task Test_Create_NoDescription()
2021-05-30 15:29:30 +00:00
{
// Arrange
var fixture = new Fixture();
var item = fixture.Create<FeedbackReceiverInDto>();
2021-05-30 15:29:30 +00:00
item.Description = null;
var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverInDto> {item});
2021-05-30 15:29:30 +00:00
// Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
2021-05-30 15:29:30 +00:00
new StringContent(jsonContent, Encoding.UTF8, "application/json"));
// Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Theory, AutoData]
public async Task Test_Create_Ok(IEnumerable<FeedbackReceiverInDto> items)
2021-05-30 15:29:30 +00:00
{
// Arrange
await _mongoDb.DropAsync();
var feedbackReceiversDto = items.ToList();
var jsonContent = JsonSerializer.Serialize(feedbackReceiversDto);
// Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
2021-05-30 15:29:30 +00:00
new StringContent(jsonContent, Encoding.UTF8, "application/json"));
// Assert
httpResponse.EnsureSuccessStatusCode();
var filter = new FilterDefinitionBuilder<FeedbackReceiver>().Empty;
var createdDocs = await _mongoDb.FeedbackReceiverCollection.CountDocumentsAsync(filter);
Assert.Equal(feedbackReceiversDto.Count(), createdDocs);
}
[Fact]
public async Task Test_Delete_ValidationFail()
{
// Arrange
await _mongoDb.DropAsync();
// Test
var httpResponse =
await _client.DeleteAsync("/api/v1/FeedbackReceivers/abc_not_guid", CancellationToken.None);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Theory, AutoData]
public async Task Test_Delete_OK(IEnumerable<FeedbackReceiver> items)
{
// Arrange
var guids = new List<string>();
await _mongoDb.DropAsync();
byte index = 0;
var cleanItems = items.Select(i =>
{
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);
// Test
foreach (var guid in guids)
{
var httpResponse =
await _client.DeleteAsync($"/api/v1/FeedbackReceivers/{guid}", CancellationToken.None);
Assert.Equal(HttpStatusCode.NoContent, httpResponse.StatusCode);
}
// Assert
var docs = await _mongoDb.FeedbackReceiverCollection.CountDocumentsAsync(FilterDefinition<FeedbackReceiver>
.Empty);
Assert.Equal(0L, docs);
}
[Fact]
public async Task Test_Get_Ok()
{
// Arrange
await _mongoDb.DropAsync();
var feedbackReceiver = new FeedbackReceiver()
{
Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3",
Description = "someting",
CreatedAt = DateTime.Parse("2020-02-01")
};
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(new[] {feedbackReceiver});
// Test
var httpResponse =
await _client.GetAsync($"/api/v1/FeedbackReceivers/{feedbackReceiver.Id}", CancellationToken.None);
var item = await httpResponse.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
Assert.Equal(
"{\"id\":\"0102030405060708090a0b0e\",\"name\":\"N4m3\",\"description\":\"someting\",\"createdAt\":\"2020-01-31T22:00:00Z\"}",
item);
}
[Fact]
public async Task Test_Get_NotFound()
{
// Arrange
await _mongoDb.DropAsync();
// Test
var httpResponse = await _client.GetAsync("/api/v1/FeedbackReceivers/0102030405060708090a0b0e",
CancellationToken.None);
// Assert
Assert.Equal(HttpStatusCode.NotFound, httpResponse.StatusCode);
}
[Fact]
public async Task Test_Get_BadRequest()
{
// Arrange
await _mongoDb.DropAsync();
// Test
var httpResponse = await _client.GetAsync("/api/v1/FeedbackReceivers/xxx", CancellationToken.None);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
// TODO test list
2021-05-30 15:29:30 +00:00
}
}