-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
257 additions
and
17 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
namespace MediatRWebAPI.Tests | ||
{ | ||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FakeItEasy; | ||
using FluentValidation; | ||
using FluentValidation.AspNetCore; | ||
using MediatR; | ||
using MediatRWebAPI.Features.Films.CreateFilm; | ||
using MediatRWebAPI.Features.Films.DeleteFilm; | ||
using MediatRWebAPI.Features.Films.ListFilmById; | ||
using MediatRWebAPI.Features.Films.ListFilms; | ||
using MediatRWebAPI.Features.Films.UpdateFilm; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Models; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
|
||
public class FilmControllerTests | ||
{ | ||
[Fact] | ||
public async Task Should_get_list_of_films() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<ListFilmsMessage>.Ignored)).Returns(new[] { new Film { Name = "Goodfellas" } }); | ||
var client = this.GetClient(fakeMediatR); | ||
|
||
//When | ||
var response = await client.GetAsync("/api/films"); | ||
var contents = await response.Content.ReadAsStringAsync(); | ||
|
||
//Then | ||
Assert.Contains("Goodfellas", contents, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_get_film_by_id() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<ListFilmsByIdMessage>.Ignored)).Returns(new Film { Name = "Blade Runner" }); | ||
var client = this.GetClient(fakeMediatR); | ||
|
||
//When | ||
var response = await client.GetAsync("/api/films/1"); | ||
var contents = await response.Content.ReadAsStringAsync(); | ||
|
||
//Then | ||
Assert.Contains("Blade Runner", contents, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_404_when_no_film_found_via_get_film_by_id() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<ListFilmsByIdMessage>.Ignored)).Returns(null); | ||
|
||
var client = this.GetClient(fakeMediatR); | ||
|
||
//When | ||
var response = await client.GetAsync("/api/films/1"); | ||
|
||
//Then | ||
Assert.Equal(404, (int)response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_400_on_invalid_data_when_creating_film() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
var client = this.GetClient(fakeMediatR); | ||
var film = new Film { Name = "" }; | ||
|
||
//When | ||
var response = await client.PostAsync("/api/films", new StringContent(JsonConvert.SerializeObject(film), Encoding.UTF8, "application/json")); | ||
|
||
//Then | ||
Assert.Equal(400, (int)response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_403_on_invalid_user_when_creating_film() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<CreateFilmMessage>.Ignored)).Throws<InvalidOperationException>(); | ||
|
||
var client = this.GetClient(fakeMediatR); | ||
|
||
var film = new Film { Name = "Shrek" }; | ||
|
||
//When | ||
var response = await client.PostAsync("/api/films", new StringContent(JsonConvert.SerializeObject(film), Encoding.UTF8, "application/json")); | ||
|
||
//Then | ||
Assert.Equal(403, (int)response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_201_when_creating_film() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<CreateFilmMessage>.Ignored)).Returns(new Unit()); | ||
var client = this.GetClient(fakeMediatR); | ||
|
||
var film = new Film { Name = "Shrek" }; | ||
|
||
//When | ||
var response = await client.PostAsync("/api/films", new StringContent(JsonConvert.SerializeObject(film), Encoding.UTF8, "application/json")); | ||
|
||
//Then | ||
Assert.Equal(201, (int)response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_400_on_invalid_data_when_updating_film() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
var client = this.GetClient(fakeMediatR); | ||
var film = new Film { Name = "" }; | ||
|
||
//When | ||
var response = await client.PutAsync("/api/films/1", new StringContent(JsonConvert.SerializeObject(film), Encoding.UTF8, "application/json")); | ||
|
||
//Then | ||
Assert.Equal(400, (int)response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_403_on_invalid_user_when_updating_film() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<UpdateFilmMessage>.Ignored)).Throws<InvalidOperationException>(); | ||
|
||
var client = this.GetClient(fakeMediatR); | ||
|
||
var film = new Film { Name = "Shrek" }; | ||
|
||
//When | ||
var response = await client.PutAsync("/api/films/1", new StringContent(JsonConvert.SerializeObject(film), Encoding.UTF8, "application/json")); | ||
|
||
//Then | ||
Assert.Equal(403, (int)response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_204_when_deleting_film() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<UpdateFilmMessage>.Ignored)).Returns(new Unit()); | ||
var client = this.GetClient(fakeMediatR); | ||
|
||
//When | ||
var response = await client.DeleteAsync("/api/films/1"); | ||
|
||
//Then | ||
Assert.Equal(204, (int)response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_403_on_invalid_user_when_deleting_film() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<DeleteFilmMessage>.Ignored)).Throws<InvalidOperationException>(); | ||
|
||
var client = this.GetClient(fakeMediatR); | ||
|
||
//When | ||
var response = await client.DeleteAsync("/api/films/1"); | ||
|
||
//Then | ||
Assert.Equal(403, (int)response.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_204_when_updating_film() | ||
{ | ||
//Given | ||
var fakeMediatR = A.Fake<IMediator>(); | ||
A.CallTo(() => fakeMediatR.Send(A<DeleteFilmMessage>.Ignored)).Returns(new Unit()); | ||
var client = this.GetClient(fakeMediatR); | ||
|
||
var film = new Film { Name = "Shrek" }; | ||
|
||
//When | ||
var response = await client.PutAsync("/api/films/1", new StringContent(JsonConvert.SerializeObject(film), Encoding.UTF8, "application/json")); | ||
|
||
//Then | ||
Assert.Equal(204, (int)response.StatusCode); | ||
} | ||
|
||
private HttpClient GetClient(IMediator fakeMediatR) | ||
{ | ||
var server = new TestServer(WebHost.CreateDefaultBuilder() | ||
.Configure(app => { app.UseMvc(); }) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddSingleton<IMediator>(fakeMediatR); | ||
|
||
services.AddTransient<IValidator<Film>, FilmValidator>(); | ||
|
||
services.AddMvc().AddFluentValidation(); | ||
}) | ||
); | ||
|
||
return server.CreateClient(); | ||
} | ||
} | ||
} |
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="FakeItEasy" Version="4.3.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> | ||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\MediatRWebAPI\MediatRWebAPI.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
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