-
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.
traditional tests and more delegate based film routes
- Loading branch information
Showing
7 changed files
with
479 additions
and
9 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
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
27 changes: 27 additions & 0 deletions
27
FunctionalProject/Features/DelegateFilms/UpdateFilm/UpdateFilmRoute.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,27 @@ | ||
namespace FunctionalProject.Features.DelegateFilms.UpdateFilm | ||
{ | ||
using System; | ||
using Models; | ||
|
||
public static class UpdateFilmRoute | ||
{ | ||
public static void Handle(int id, Film film, Func<bool> validUserQuery, Func<int, Film> listFilmById) | ||
{ | ||
if (!validUserQuery()) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
//Do some special MEGA CORP business validation | ||
|
||
var existingFilm = listFilmById(id); | ||
|
||
existingFilm.Name = film.Name; | ||
existingFilm.Budget = film.Budget; | ||
existingFilm.Language = film.Language; | ||
|
||
//Write some SQL to store in db | ||
|
||
} | ||
} | ||
} |
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,212 @@ | ||
namespace TradtionalWebAPI.Tests | ||
{ | ||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FakeItEasy; | ||
using FluentValidation; | ||
using FluentValidation.AspNetCore; | ||
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 TraditionalWebAPI.Services; | ||
using Xunit; | ||
|
||
public class FilmControllerTests | ||
{ | ||
|
||
[Fact] | ||
public async Task Should_get_list_of_films() | ||
{ | ||
//Given | ||
var client = this.GetClient(); | ||
|
||
//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 client = this.GetClient(); | ||
|
||
//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 fakeFilmService = A.Fake<IFilmService>(); | ||
A.CallTo(() => fakeFilmService.ListFilmById(1)).Returns(null); | ||
|
||
var client = this.GetClient(fakeFilmService); | ||
|
||
//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 client = this.GetClient(); | ||
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 fakeFilmService = A.Fake<IFilmService>(); | ||
A.CallTo(() => fakeFilmService.CreateFilm(A<Film>.Ignored)).Throws<InvalidOperationException>(); | ||
|
||
var client = this.GetClient(fakeFilmService); | ||
|
||
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 client = this.GetClient(); | ||
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 client = this.GetClient(); | ||
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 fakeFilmService = A.Fake<IFilmService>(); | ||
A.CallTo(() => fakeFilmService.UpdateFilm(1, A<Film>.Ignored)).Throws<InvalidOperationException>(); | ||
|
||
var client = this.GetClient(fakeFilmService); | ||
|
||
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 client = this.GetClient(); | ||
|
||
//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 fakeFilmService = A.Fake<IFilmService>(); | ||
A.CallTo(() => fakeFilmService.DeleteFilm(1)).Throws<InvalidOperationException>(); | ||
|
||
var client = this.GetClient(fakeFilmService); | ||
|
||
//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 client = this.GetClient(); | ||
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(IFilmService filmService = null) | ||
{ | ||
if (filmService == null) | ||
{ | ||
filmService = A.Fake<IFilmService>(); | ||
A.CallTo(() => filmService.ListFilms()).Returns(new[] { new Film { Name = "Goodfellas" } }); | ||
A.CallTo(() => filmService.ListFilmById(1)).Returns(new Film { Name = "Blade Runner" }); | ||
} | ||
|
||
var server = new TestServer(WebHost.CreateDefaultBuilder() | ||
.Configure(app => { app.UseMvc(); }) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddSingleton<IFilmService>(filmService); | ||
|
||
services.AddTransient<IValidator<Film>, FilmValidator>(); | ||
|
||
services.AddMvc().AddFluentValidation(); | ||
}) | ||
); | ||
|
||
return server.CreateClient(); | ||
} | ||
} | ||
} |
Oops, something went wrong.