Use new Auth API

This commit is contained in:
Robert Marshall 2020-04-12 14:25:16 +01:00
parent 38e76d3539
commit 79c17f75cd
16 changed files with 94 additions and 150 deletions

View file

@ -0,0 +1,55 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Website.Data;
using Website.Models;
using Xunit;
namespace Website.Tests.Data {
public class AuthenticationProviderTests {
[Fact]
public async Task Authenticate_WithSuccessfulLoginRequest_ReturnsUser() {
const string json = @"{""username"":""username"",""password"":""password""}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Post)
.WithUrl("/authenticate")
.WithPostBody(@"{""Username"":""username"",""Password"":""password"",""ReturnUrl"":null}")
.WithResponse(json)
.Build();
var request = new LoginRequest {
Username = "username",
Password = "password"
};
var expectedUser = new User {
Username = "username",
Password = "password"
};
var provider = new AuthenticationProvider(httpClient);
(await provider.Authenticate(request)).Should().BeEquivalentTo(expectedUser);
}
[Fact]
public async Task Authenticate_WithFailedLoginRequest_ReturnsNull() {
const string json = @"{""username"":""username"",""password"":""password""}";
var httpClient = new HttpClientBuilder()
.WithMethod(HttpMethod.Post)
.WithUrl("/authenticate")
.WithPostBody(@"{""Username"":""username"",""Password"":""password"",""ReturnUrl"":null}")
.WithErrorStatus(HttpStatusCode.Unauthorized)
.WithResponse(json)
.Build();
var request = new LoginRequest {
Username = "username",
Password = "wrong"
};
var provider = new AuthenticationProvider(httpClient);
(await provider.Authenticate(request)).Should().BeNull();
}
}
}

View file

@ -1,32 +0,0 @@
using Website.Data;
using Xunit;
using FluentAssertions;
using MySql.Data.MySqlClient;
using NSubstitute;
using Microsoft.Extensions.Configuration;
namespace Website.Tests.Data
{
public class MySQLDatabaseProviderTests
{
const string ConnectionString = "Server=host;User ID=username;Password=password;Database=database";
[Fact]
public void NewConnection_WithStringConstructor_ReturnsMySQLConnection() {
var provider = new MySQLDatabaseProvider(ConnectionString);
var connection = provider.NewConnection();
connection.Should().BeOfType<MySqlConnection>();
(connection as MySqlConnection).ConnectionString.Should().Be(ConnectionString);
}
[Fact]
public void NewConnection_WithConfigConstructor_ReturnsMySQLConnection() {
var config = Substitute.For<IConfiguration>();
config.GetConnectionString("database").Returns(ConnectionString);
var provider = new MySQLDatabaseProvider(ConnectionString);
var connection = provider.NewConnection();
connection.Should().BeOfType<MySqlConnection>();
(connection as MySqlConnection).ConnectionString.Should().Be(ConnectionString);
}
}
}