55 lines
No EOL
1.6 KiB
C#
55 lines
No EOL
1.6 KiB
C#
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();
|
|
}
|
|
}
|
|
} |