-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BlogApp.Api.Controllers.Models | ||
{ | ||
public class UpdateBlogRequest | ||
{ | ||
public string Url { get; set; } = null!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.5" /> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
<PackageReference Include="Moq" Version="4.14.1" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" /> | ||
<PackageReference Include="FluentAssertions" Version="5.10.3" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\BlogApp.Api\BlogApp.Api.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using BlogApp.Data; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System.Linq; | ||
|
||
namespace BlogApp.Api.Tests | ||
{ | ||
public class BlogWebApplicationFactory : WebApplicationFactory<Startup> | ||
{ | ||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
builder.UseEnvironment("Test"); | ||
builder.ConfigureServices(services => | ||
{ | ||
// Remove the app's ApplicationDbContext registration. | ||
var descriptor = services.SingleOrDefault( | ||
d => d.ServiceType == | ||
typeof(DbContextOptions<BlogDbContext>)); | ||
if (descriptor != null) | ||
services.Remove(descriptor); | ||
// Add ApplicationDbContext using an in-memory database for testing. | ||
services.AddDbContext<BlogDbContext>(options => | ||
{ | ||
options.UseInMemoryDatabase("InMemoryDbForTesting"); | ||
// print EF debug logs during tests | ||
var fac = LoggerFactory.Create(builder => { builder.AddDebug(); }); | ||
options.UseLoggerFactory(fac); | ||
}); | ||
using (var scope = services.BuildServiceProvider().CreateScope()) | ||
{ | ||
var scopedServices = scope.ServiceProvider; | ||
var db = scopedServices.GetRequiredService<BlogDbContext>(); | ||
// Ensure the database is created. | ||
db.Database.EnsureCreated(); | ||
} | ||
}) | ||
.ConfigureAppConfiguration((context, config) => | ||
{ | ||
//config.AddInMemoryCollection(new[] | ||
//{ | ||
// // TODO: Add later the docker connection string | ||
//}); | ||
}) | ||
.ConfigureLogging(builder => | ||
{ | ||
builder.AddDebug(); | ||
builder.SetMinimumLevel(LogLevel.Information); | ||
}); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
tests/BlogApp.Api.Tests/Controllers/BlogsControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using BlogApp.Api.Controllers.Models; | ||
using BlogApp.Data.Entities; | ||
using FluentAssertions; | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace BlogApp.Api.Tests.Controllers | ||
{ | ||
public sealed class BlogsControllerTests : IClassFixture<BlogWebApplicationFactory> | ||
{ | ||
private readonly BlogWebApplicationFactory _factory; | ||
|
||
public BlogsControllerTests(BlogWebApplicationFactory factory) | ||
{ | ||
_factory = factory; | ||
} | ||
|
||
[Fact] | ||
public async Task Create_ShouldCreateBlog() | ||
{ | ||
// Arrange | ||
var createRequest = new CreateBlogRequest | ||
{ | ||
Url = "https://aspnet-core-is-cool.net" | ||
}; | ||
|
||
var client = _factory.CreateClient(); | ||
|
||
// Act | ||
var postResponse = await client.PostAsync("/v1/blogs", new JsonContent<CreateBlogRequest>(createRequest)); | ||
postResponse.EnsureSuccessStatusCode(); | ||
var blogCreateResponse = await postResponse.Content.ReadAsJsonAsync<Blog>(); | ||
|
||
// Assert - by calling the Get/id and comparing the results | ||
var getResponse = await client.GetAsync($"/v1/blogs/{blogCreateResponse.Id}"); | ||
var blogGetResponse = await getResponse.Content.ReadAsJsonAsync<Blog>(); | ||
|
||
blogGetResponse.Should().BeEquivalentTo(blogCreateResponse); | ||
} | ||
|
||
[Fact] | ||
public async Task Update_ShouldUpdateBlogUrl() | ||
{ | ||
// Arrange | ||
var client = _factory.CreateClient(); | ||
|
||
var initialBlog = await CreateRandomBlog(client); | ||
|
||
var updateRequest = new UpdateBlogRequest | ||
{ | ||
// not a real url but just something random.. we don't have any validation for now | ||
Url = Guid.NewGuid().ToString() | ||
}; | ||
|
||
// Act | ||
var response = await client.PutAsync($"/v1/blogs/{initialBlog.Id}", new JsonContent<UpdateBlogRequest>(updateRequest)); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
// Assert | ||
var getResponse = await client.GetAsync($"/v1/blogs/{initialBlog.Id}"); | ||
var actual = await getResponse.Content.ReadAsJsonAsync<Blog>(); | ||
|
||
Assert.Equal(updateRequest.Url, actual.Url); | ||
} | ||
|
||
private async ValueTask<Blog> CreateRandomBlog(HttpClient client) | ||
{ | ||
// Arrange | ||
var createRequest = new CreateBlogRequest | ||
{ | ||
Url = "https://aspnet-core-is-cool.net" | ||
}; | ||
|
||
// Act | ||
var postResponse = await client.PostAsync("/v1/blogs", new JsonContent<CreateBlogRequest>(createRequest)); | ||
postResponse.EnsureSuccessStatusCode(); | ||
return await postResponse.Content.ReadAsJsonAsync<Blog>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "https://localhost:62732/", | ||
"sslPort": 44313 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"BlogApp.Api.Tests": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "https://localhost:5001;https://localhost:5000" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace BlogApp.Api.Tests | ||
{ | ||
/// <summary> | ||
/// Extension methods around <see cref="HttpContent"/> | ||
/// </summary> | ||
public static class HttpContentExtensions | ||
{ | ||
/// <summary> | ||
/// Reads as <see cref="HttpContent"/> as JSON string and deserializes to <typeparam name="TResult"></typeparam> | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of the result.</typeparam> | ||
/// <param name="content">The content.</param> | ||
public static async Task<TResult> ReadAsJsonAsync<TResult>(this HttpContent content) | ||
{ | ||
try | ||
{ | ||
var responseString = await content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<TResult>(responseString); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var rawJson = await content.ReadAsStringAsync().ConfigureAwait(false); | ||
throw new Exception($"Failed to deserialize response:{ rawJson }", ex); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Newtonsoft.Json; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
|
||
namespace BlogApp.Api.Tests | ||
{ | ||
/// <summary> | ||
/// Helper class to convert a <see cref="StringContent"/> into a JsonContent | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class JsonContent<T> : StringContent | ||
{ | ||
public JsonContent(T entity) | ||
: base(JsonConvert.SerializeObject(entity)) | ||
{ | ||
Headers.ContentType = new MediaTypeHeaderValue("application/json"); | ||
} | ||
} | ||
} |