74 lines
2.9 KiB
C#
74 lines
2.9 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using FluentAssertions;
|
|
using Xunit;
|
|
|
|
namespace backend.tests;
|
|
|
|
public class PairingEndpointsTests
|
|
{
|
|
private static async Task<HttpClient> SignedInClient(ApiFactory factory, string email = "owner@example.com")
|
|
{
|
|
var client = factory.CreateClient();
|
|
await client.PostAsJsonAsync("/api/auth/register",
|
|
new { email, password = "Passw0rd!" });
|
|
await client.PostAsJsonAsync("/api/auth/login",
|
|
new { email, password = "Passw0rd!" });
|
|
return client;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Anonymous_user_cannot_request_a_pairing_code()
|
|
{
|
|
using var factory = new ApiFactory();
|
|
var client = factory.CreateClient();
|
|
var res = await client.PostAsJsonAsync("/api/pair-code", new { name = "kitchen" });
|
|
res.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Pair_code_then_pair_returns_token_and_device_id()
|
|
{
|
|
using var factory = new ApiFactory();
|
|
var client = await SignedInClient(factory);
|
|
var codeRes = await client.PostAsJsonAsync("/api/pair-code", new { name = "kitchen" });
|
|
codeRes.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
var codeBody = await codeRes.Content.ReadFromJsonAsync<Dictionary<string, object>>();
|
|
var code = codeBody!["code"].ToString()!;
|
|
|
|
var pairRes = await factory.CreateClient().PostAsJsonAsync("/api/pair",
|
|
new { code, hostname = "rpi", client_version = "0.1" });
|
|
pairRes.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
var body = await pairRes.Content.ReadAsStringAsync();
|
|
body.Should().Contain("device_id");
|
|
body.Should().Contain("device_token");
|
|
body.Should().Contain("backend_ws");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Pair_with_invalid_code_returns_404()
|
|
{
|
|
using var factory = new ApiFactory();
|
|
var res = await factory.CreateClient().PostAsJsonAsync("/api/pair",
|
|
new { code = "ZZZZZZZZ", hostname = "rpi", client_version = "0.1" });
|
|
res.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Pair_with_consumed_code_returns_404()
|
|
{
|
|
using var factory = new ApiFactory();
|
|
var client = await SignedInClient(factory);
|
|
var codeRes = await client.PostAsJsonAsync("/api/pair-code", new { name = "kitchen" });
|
|
var code = (await codeRes.Content.ReadFromJsonAsync<Dictionary<string, object>>())!["code"].ToString()!;
|
|
|
|
var first = await factory.CreateClient().PostAsJsonAsync("/api/pair",
|
|
new { code, hostname = "rpi", client_version = "0.1" });
|
|
first.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
|
|
var second = await factory.CreateClient().PostAsJsonAsync("/api/pair",
|
|
new { code, hostname = "rpi", client_version = "0.1" });
|
|
second.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
|
}
|
|
}
|