diff --git a/Retroactiune.IntegrationTests/Retroactiune.IntegrationTests.csproj b/Retroactiune.IntegrationTests/Retroactiune.IntegrationTests.csproj
index 0738422..a52df2b 100644
--- a/Retroactiune.IntegrationTests/Retroactiune.IntegrationTests.csproj
+++ b/Retroactiune.IntegrationTests/Retroactiune.IntegrationTests.csproj
@@ -11,6 +11,7 @@
+
diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs
index fd9ff3c..d6cfda8 100644
--- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs
+++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs
@@ -124,7 +124,6 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
// Arrange
await _mongoDb.DropAsync();
var guids = new List();
- await _mongoDb.DropAsync();
byte index = 0;
var feedbackReceivers = items as FeedbackReceiver[] ?? items.ToArray();
foreach (var i in feedbackReceivers)
@@ -134,6 +133,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
index += 1;
guids.Add(i.Id);
}
+
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers);
@@ -150,14 +150,13 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
.Empty);
Assert.Equal(0L, docs);
}
-
+
[Theory, AutoData]
public async Task Test_DeleteMany_OK(IEnumerable items)
{
// Arrange
await _mongoDb.DropAsync();
var guids = new List();
- await _mongoDb.DropAsync();
byte index = 0;
var feedbackReceivers = items as FeedbackReceiver[] ?? items.ToArray();
foreach (var i in feedbackReceivers)
@@ -167,19 +166,22 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
index += 1;
guids.Add(i.Id);
}
+
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers);
// Test
- var request = new HttpRequestMessage {
+ var request = new HttpRequestMessage
+ {
Method = HttpMethod.Delete,
RequestUri = new Uri($"{_client.BaseAddress.AbsoluteUri}api/v1/FeedbackReceivers"),
+ // ReSharper disable once MethodHasAsyncOverload
Content = new StringContent(JsonConvert.SerializeObject(guids), Encoding.UTF8, "application/json")
};
var httpResponse = await _client.SendAsync(request);
Assert.Equal(HttpStatusCode.NoContent, httpResponse.StatusCode);
- // Assert
+ // Assert
var docs = await _mongoDb.FeedbackReceiverCollection.CountDocumentsAsync(FilterDefinition
.Empty);
Assert.Equal(0L, docs);
diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestTokens.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestTokens.cs
index d60885d..dae2f5a 100644
--- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestTokens.cs
+++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestTokens.cs
@@ -1,11 +1,17 @@
using System;
+using System.Collections.Generic;
+using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
+using AutoFixture.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
+using MongoDB.Bson;
using MongoDB.Driver;
+using Newtonsoft.Json;
using Retroactiune.Database;
using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures;
using Retroactiune.Models;
@@ -78,12 +84,12 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
// 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,
+ new StringContent("{\"numberOfTokens\": 1, \"feedbackReceiverId\": \"some_id\"}", Encoding.UTF8,
"application/json"));
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
@@ -97,5 +103,109 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
+
+ // Delete nok, DeleteMany nok
+
+ [Theory, AutoData]
+ public async Task Test_Delete_Ok(IEnumerable tokens)
+ {
+ // Setup
+ await _mongoDb.DropAsync();
+ var guids = new List();
+ var tokensArr = tokens as Token[] ?? tokens.ToArray();
+ byte index = 0;
+ foreach (var i in tokensArr)
+ {
+ i.Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, index, 4, 5, 6, 7, 8, 9, index, 11, 14}))
+ .ToString();
+ i.FeedbackReceiverId = i.Id;
+ index += 1;
+ guids.Add(i.Id);
+ }
+
+ await _mongoDb.TokensCollection.InsertManyAsync(tokensArr);
+ Assert.Equal(tokensArr.Length,
+ await _mongoDb.TokensCollection.CountDocumentsAsync(FilterDefinition.Empty));
+
+ // Test
+ foreach (var guid in guids)
+ {
+ var response = await _client.DeleteAsync($"/api/v1/Tokens/{guid}", CancellationToken.None);
+ Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
+ }
+
+ // Assert
+ Assert.Equal(0, await _mongoDb.TokensCollection.CountDocumentsAsync(FilterDefinition.Empty));
+ }
+
+ [Fact]
+ public async Task Test_Delete_BadGuid()
+ {
+ // Setup
+ await _mongoDb.DropAsync();
+
+ // Test
+ var response = await _client.DeleteAsync("/api/v1/Tokens/batman", CancellationToken.None);
+
+ // Assert
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ }
+
+ [Theory, AutoData]
+ public async Task Test_DeleteMany_Ok(IEnumerable tokens)
+ {
+ // Setup
+ await _mongoDb.DropAsync();
+ var guids = new List();
+ var tokensArr = tokens as Token[] ?? tokens.ToArray();
+ byte index = 0;
+ foreach (var i in tokensArr)
+ {
+ i.Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, index, 4, 5, 6, 7, 8, 9, index, 11, 14}))
+ .ToString();
+ i.FeedbackReceiverId = i.Id;
+ index += 1;
+ guids.Add(i.Id);
+ }
+
+ await _mongoDb.TokensCollection.InsertManyAsync(tokensArr);
+ Assert.Equal(tokensArr.Length,
+ await _mongoDb.TokensCollection.CountDocumentsAsync(FilterDefinition.Empty));
+
+ // Test
+ var request = new HttpRequestMessage
+ {
+ Method = HttpMethod.Delete,
+ RequestUri = new Uri($"{_client.BaseAddress.AbsoluteUri}api/v1/Tokens"),
+ // ReSharper disable once MethodHasAsyncOverload
+ Content = new StringContent(JsonConvert.SerializeObject(guids), Encoding.UTF8, "application/json")
+ };
+ var response = await _client.SendAsync(request);
+ Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
+
+ // Assert
+ Assert.Equal(0, await _mongoDb.TokensCollection.CountDocumentsAsync(FilterDefinition.Empty));
+ }
+
+ [Fact]
+ public async Task Test_DeleteMany_BadRequest()
+ {
+ // Setup
+ await _mongoDb.DropAsync();
+
+ // Test
+ var request = new HttpRequestMessage
+ {
+ Method = HttpMethod.Delete,
+ RequestUri = new Uri($"{_client.BaseAddress.AbsoluteUri}api/v1/Tokens"),
+ // ReSharper disable once MethodHasAsyncOverload
+ Content = new StringContent(JsonConvert.SerializeObject(new[] {"bad", "badder"}), Encoding.UTF8,
+ "application/json")
+ };
+ var response = await _client.SendAsync(request);
+
+ // Assert
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ }
}
}
\ No newline at end of file
diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestTokensController.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestTokensController.cs
index 21744f9..0f066a1 100644
--- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestTokensController.cs
+++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestTokensController.cs
@@ -52,5 +52,73 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
Assert.IsType(result);
tokens.Verify(i => i.GenerateTokensAsync(2, "froid", null), Times.Once);
}
+
+ [Fact]
+ public async Task Test_Delete_Ok()
+ {
+ // Arrange
+ var feedbackService = new Mock();
+ var tokens = new Mock();
+
+ // Test
+ var controller = new TokensController(feedbackService.Object, tokens.Object);
+ var result = await controller.DeleteToken("my_guid");
+
+ // Assert
+ Assert.IsType(result);
+ tokens.Verify(i => i.DeleteTokens(new[] {"my_guid"}), Times.Once);
+ }
+
+ [Fact]
+ public async Task Test_Delete_BadRequest()
+ {
+ // Arrange
+ var feedbackService = new Mock();
+ var tokens = new Mock();
+ tokens.Setup(i => i.DeleteTokens(It.IsAny>()))
+ .Throws(new GenericServiceException("op fail"));
+
+ // Test
+ var controller = new TokensController(feedbackService.Object, tokens.Object);
+ var result = await controller.DeleteToken("my_guid");
+
+ // Assert
+ Assert.IsType(result);
+ tokens.Verify(i => i.DeleteTokens(new[] {"my_guid"}), Times.Once);
+ }
+
+ [Fact]
+ public async Task Test_DeleteMany_Ok()
+ {
+ // Arrange
+ var feedbackService = new Mock();
+ var tokens = new Mock();
+
+ // Test
+ var controller = new TokensController(feedbackService.Object, tokens.Object);
+ var result = await controller.DeleteTokens(new[] {"my_guid", "b"});
+
+ // Assert
+ Assert.IsType(result);
+ tokens.Verify(i => i.DeleteTokens(new[] {"my_guid", "b"}), Times.Once);
+ }
+
+ [Fact]
+ public async Task Test_DeleteMany_BadRequest()
+ {
+ // Arrange
+ var feedbackService = new Mock();
+ var tokens = new Mock();
+ tokens.Setup(i => i.DeleteTokens(It.IsAny>()))
+ .Throws(new GenericServiceException("op fail"));
+
+ // Test
+ var controller = new TokensController(feedbackService.Object, tokens.Object);
+ var result = await controller.DeleteTokens(new[] {"my_guid", "b"});
+
+ // Assert
+ Assert.IsType(result);
+ tokens.Verify(i => i.DeleteTokens(new[] {"my_guid", "b"}), Times.Once);
+ }
}
}
\ No newline at end of file
diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestTokensService.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestTokensService.cs
index b26e8f0..3c95536 100644
--- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestTokensService.cs
+++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestTokensService.cs
@@ -80,5 +80,39 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
It.IsAny(),
It.IsAny()), Times.Once);
}
+
+ [Fact]
+ public async Task Test_DeleteTokens_Ok()
+ {
+ // Setup
+ var mongoDatabaseMock = new Mock();
+ var mongoClientMock = new Mock();
+ var mongoSettingsMock = new Mock();
+ var mongoCollectionMock = new Mock>();
+
+ mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
+ mongoSettingsMock.SetupGet(i => i.TokensCollectionName).Returns("tokens");
+
+ mongoClientMock
+ .Setup(stub => stub.GetDatabase(It.IsAny(),
+ It.IsAny()))
+ .Returns(mongoDatabaseMock.Object);
+
+ mongoDatabaseMock
+ .Setup(i => i.GetCollection(It.IsAny(),
+ It.IsAny()))
+ .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>(),
+ It.IsAny()), Times.Once);
+ }
}
}
\ No newline at end of file
diff --git a/Retroactiune.WebAPI/Controllers/TokensController.cs b/Retroactiune.WebAPI/Controllers/TokensController.cs
index 05a803f..8d2fe57 100644
--- a/Retroactiune.WebAPI/Controllers/TokensController.cs
+++ b/Retroactiune.WebAPI/Controllers/TokensController.cs
@@ -68,7 +68,6 @@ namespace Retroactiune.Controllers
[ProducesResponseType(typeof(BasicResponse),StatusCodes.Status400BadRequest)]
public async Task DeleteTokens([Required] IEnumerable tokenIds)
{
- // TODO: Unit test, integration test.
try
{
await _tokensService.DeleteTokens(tokenIds);
@@ -93,12 +92,21 @@ namespace Retroactiune.Controllers
[HttpDelete("{guid}")]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
- public async Task DeleteToken(
+ public async Task DeleteToken(
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] string guid)
{
- // TODO: Unit test, integration test.
- await _tokensService.DeleteTokens(new []{ guid });
- return NoContent();
+ try
+ {
+ await _tokensService.DeleteTokens(new []{ guid });
+ return NoContent();
+ }
+ catch (GenericServiceException e)
+ {
+ return BadRequest(new BasicResponse
+ {
+ Message = e.Message
+ });
+ }
}
}
}
\ No newline at end of file
diff --git a/Retroactiune.WebAPI/Services/TokensService.cs b/Retroactiune.WebAPI/Services/TokensService.cs
index 2cc8a39..36fa88a 100644
--- a/Retroactiune.WebAPI/Services/TokensService.cs
+++ b/Retroactiune.WebAPI/Services/TokensService.cs
@@ -42,7 +42,6 @@ namespace Retroactiune.Services
public async Task DeleteTokens(IEnumerable tokenIds)
{
- // TODO: Unit test.
try
{
var filter = new FilterDefinitionBuilder();