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

97 lines
3.4 KiB
C#
Raw Normal View History

2021-05-30 17:39:04 +00:00
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.Tasks;
using AutoFixture;
using AutoFixture.Xunit2;
2021-05-30 17:39:04 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
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_CreateFeedbackReceiver_NoContent()
{
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/",
new StringContent("[]", Encoding.UTF8, "application/json"));
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Fact]
public async Task Test_CreateFeedbackReceiver_NoName()
{
// Arrange
var fixture = new Fixture();
var item = fixture.Create<FeedbackReceiverDto>();
item.Name = null;
var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverDto> {item});
// Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/",
new StringContent(jsonContent, Encoding.UTF8, "application/json"));
// Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Fact]
public async Task Test_CreateFeedbackReceiver_NoDescription()
{
// Arrange
var fixture = new Fixture();
var item = fixture.Create<FeedbackReceiverDto>();
item.Description = null;
var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverDto> {item});
// Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/",
new StringContent(jsonContent, Encoding.UTF8, "application/json"));
// Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
[Theory, AutoData]
public async Task Test_CreateFeedbackReceiver_Ok(IEnumerable<FeedbackReceiverDto> items)
{
// Arrange
await _mongoDb.DropAsync();
var feedbackReceiversDto = items.ToList();
var jsonContent = JsonSerializer.Serialize(feedbackReceiversDto);
// Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/",
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);
}
}
}