Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobritodev committed Nov 4, 2019
1 parent d479501 commit 40e12c1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using Bogus;
using FluentAssertions;
using IdentityModel.Client;
using IdentityServer4.Contrib.AspNetCore.Testing.Configuration;
using IdentityServer4.Models;
Expand All @@ -7,24 +8,30 @@
using JPProject.Api.Management.Tests.Fakers.ClientFakers;
using JPProject.Domain.Core.ViewModels;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace JPProject.Api.Management.Tests.Controller
{
public class ClientControllerTests : IClassFixture<CustomWebApplicationFactory>
{
private readonly ITestOutputHelper _output;
public CustomWebApplicationFactory Factory { get; }
private readonly HttpClient _client;
private TokenResponse _token;
private Faker _faker;

public ClientControllerTests(CustomWebApplicationFactory factory)
public ClientControllerTests(CustomWebApplicationFactory factory, ITestOutputHelper output)
{
_faker = new Faker();
_output = output;
Factory = factory;
_client = Factory.CreateClient();
}
Expand All @@ -45,7 +52,7 @@ public async Task ShouldGetAllClients()
await Login();
var httpResponse = await _client.GetAsync("/clients");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
}

[Fact]
Expand All @@ -57,7 +64,7 @@ public async Task ShouldSaveClient()

var httpResponse = await _client.PostAsync("/clients", new StringContent(client.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; }
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/clients");
}
Expand All @@ -70,13 +77,13 @@ public async Task ShouldGetClient()

var httpResponse = await _client.PostAsync("/clients", new StringContent(client.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/clients");

httpResponse = await _client.GetAsync($"/clients/{client.ClientId}");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Deserialize and examine results.
var stringResponse = await httpResponse.Content.ReadAsStringAsync();
Expand All @@ -93,13 +100,13 @@ public async Task ShouldDeleteClient()

var httpResponse = await _client.PostAsync("/clients", new StringContent(client.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/clients");

httpResponse = await _client.DeleteAsync($"/clients/{client.ClientId}");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
}

[Fact]
Expand All @@ -111,7 +118,7 @@ public async Task ShouldUpdateClientId()
// Create one
var httpResponse = await _client.PostAsync("/clients", new StringContent(newClient.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/clients");

Expand All @@ -120,13 +127,14 @@ public async Task ShouldUpdateClientId()

// Update it
var oldClientId = client.ClientId;
client.AllowedGrantTypes = new[] { GrantType.ClientCredentials };
client.ClientId = "newclient";
httpResponse = await _client.PutAsync($"/clients/{oldClientId}", new StringContent(client.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));
httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Get updated
httpResponse = await _client.GetAsync($"/clients/{client.ClientId}");
httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Deserialize and examine results.
stringResponse = await httpResponse.Content.ReadAsStringAsync();
Expand All @@ -148,7 +156,7 @@ public async Task ShouldAddNewClientSecret()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/secrets", new StringContent(newSecret.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/secrets");

Expand All @@ -167,13 +175,13 @@ public async Task ShouldListClientSecret()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/secrets", new StringContent(newSecret.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/secrets");

httpResponse = await _client.GetAsync($"/clients/{newClient.ClientId}/secrets");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Deserialize and examine results.
var stringResponse = await httpResponse.Content.ReadAsStringAsync();
Expand All @@ -195,21 +203,21 @@ public async Task ShouldDeleteClientSecret()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/secrets", new StringContent(newSecret.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/secrets");

httpResponse = await _client.GetAsync($"/clients/{newClient.ClientId}/secrets");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Deserialize and examine results.
var stringResponse = await httpResponse.Content.ReadAsStringAsync();
var secrets = stringResponse.FromJson<IEnumerable<SecretViewModel>>();


httpResponse = await _client.DeleteAsync($"/clients/{newClient.ClientId}/secrets/{secrets.First().Id}");
httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
}


Expand All @@ -225,7 +233,7 @@ public async Task ShouldAddNewClientProperty()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/properties", new StringContent(newProperty.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/properties");

Expand All @@ -244,13 +252,13 @@ public async Task ShouldListClientProperties()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/properties", new StringContent(newProperty.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/properties");

httpResponse = await _client.GetAsync($"/clients/{newClient.ClientId}/properties");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Deserialize and examine results.
var stringResponse = await httpResponse.Content.ReadAsStringAsync();
Expand All @@ -272,20 +280,20 @@ public async Task ShouldDeleteClientProperties()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/properties", new StringContent(newProperty.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/properties");

httpResponse = await _client.GetAsync($"/clients/{newClient.ClientId}/properties");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Deserialize and examine results.
var stringResponse = await httpResponse.Content.ReadAsStringAsync();
var properties = stringResponse.FromJson<IEnumerable<ClientPropertyViewModel>>();

httpResponse = await _client.DeleteAsync($"/clients/{newClient.ClientId}/properties/{properties.First().Id}");
httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
}


Expand All @@ -303,7 +311,7 @@ public async Task ShouldAddNewClientClaim()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/claims", new StringContent(claim.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/claims");

Expand All @@ -322,13 +330,13 @@ public async Task ShouldListClientClaims()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/claims", new StringContent(claim.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/claims");

httpResponse = await _client.GetAsync($"/clients/{newClient.ClientId}/claims");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Deserialize and examine results.
var stringResponse = await httpResponse.Content.ReadAsStringAsync();
Expand All @@ -350,13 +358,13 @@ public async Task ShouldDeleteClientClaims()
// Create one
var httpResponse = await _client.PostAsync($"/clients/{newClient.ClientId}/claims", new StringContent(claim.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/claims");

httpResponse = await _client.GetAsync($"/clients/{newClient.ClientId}/claims");

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };

// Deserialize and examine results.
var stringResponse = await httpResponse.Content.ReadAsStringAsync();
Expand All @@ -365,7 +373,7 @@ public async Task ShouldDeleteClientClaims()
claims.Should().HaveCountGreaterThan(0);

httpResponse = await _client.DeleteAsync($"/clients/{newClient.ClientId}/claims/{claims.First().Id}");
httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
}


Expand All @@ -376,7 +384,7 @@ private async Task<SaveClientViewModel> AddClient()
// Create one
var httpResponse = await _client.PostAsync("/clients", new StringContent(newClient.ToJson(), Encoding.UTF8, MediaTypeNames.Application.Json));

httpResponse.EnsureSuccessStatusCode();
try { httpResponse.EnsureSuccessStatusCode(); } catch (Exception ex) { _output.WriteLine(await httpResponse.Content.ReadAsStringAsync()); throw; };
httpResponse.Headers.Location.Should().NotBeNull();
httpResponse.Headers.Location.PathAndQuery.Should().Contain("/clients");
return newClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ClientViewModelFaker
public static Faker<SaveClientViewModel> GenerateSaveClient(string clientId = null)
{
return new Faker<SaveClientViewModel>()
.RuleFor(s => s.ClientId, f => clientId ?? f.Lorem.Word())
.RuleFor(s => s.ClientId, f => clientId ?? f.Random.Guid().ToString())
.RuleFor(s => s.ClientName, f => f.Lorem.Word())
.RuleFor(s => s.ClientUri, f => f.Lorem.Word())
.RuleFor(s => s.LogoUri, f => f.Lorem.Word())
Expand Down

0 comments on commit 40e12c1

Please sign in to comment.