2021-04-07 15:29:15 -04:00
|
|
|
using System;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Http.Headers;
|
2021-12-24 12:28:27 -05:00
|
|
|
using System.Net.Http.Json;
|
2021-04-07 15:29:15 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Jellyfin.Api.Models.StartupDtos;
|
|
|
|
using Jellyfin.Api.Models.UserDtos;
|
2021-06-19 12:02:33 -04:00
|
|
|
using Jellyfin.Extensions.Json;
|
2023-02-04 11:56:12 -05:00
|
|
|
using MediaBrowser.Model.Dto;
|
2021-04-07 15:29:15 -04:00
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Integration.Tests
|
|
|
|
{
|
|
|
|
public static class AuthHelper
|
|
|
|
{
|
|
|
|
public const string AuthHeaderName = "X-Emby-Authorization";
|
|
|
|
public const string DummyAuthHeader = "MediaBrowser Client=\"Jellyfin.Server Integration Tests\", DeviceId=\"69420\", Device=\"Apple II\", Version=\"10.8.0\"";
|
|
|
|
|
|
|
|
public static async Task<string> CompleteStartupAsync(HttpClient client)
|
|
|
|
{
|
|
|
|
var jsonOptions = JsonDefaults.Options;
|
2023-09-18 11:55:52 -04:00
|
|
|
var userResponse = await client.GetByteArrayAsync("/Startup/User");
|
2021-04-07 15:29:15 -04:00
|
|
|
var user = JsonSerializer.Deserialize<StartupUserDto>(userResponse, jsonOptions);
|
|
|
|
|
2023-09-18 11:55:52 -04:00
|
|
|
using var completeResponse = await client.PostAsync("/Startup/Complete", new ByteArrayContent(Array.Empty<byte>()));
|
2021-04-07 15:29:15 -04:00
|
|
|
Assert.Equal(HttpStatusCode.NoContent, completeResponse.StatusCode);
|
|
|
|
|
2021-11-30 18:13:02 -05:00
|
|
|
using var content = JsonContent.Create(
|
2021-04-07 15:29:15 -04:00
|
|
|
new AuthenticateUserByName()
|
|
|
|
{
|
|
|
|
Username = user!.Name,
|
|
|
|
Pw = user.Password,
|
|
|
|
},
|
2021-11-30 18:13:02 -05:00
|
|
|
options: jsonOptions);
|
2021-04-07 15:29:15 -04:00
|
|
|
content.Headers.Add("X-Emby-Authorization", DummyAuthHeader);
|
|
|
|
|
2023-09-18 11:55:52 -04:00
|
|
|
using var authResponse = await client.PostAsync("/Users/AuthenticateByName", content);
|
2021-04-07 15:29:15 -04:00
|
|
|
var auth = await JsonSerializer.DeserializeAsync<AuthenticationResultDto>(
|
2023-09-18 11:55:52 -04:00
|
|
|
await authResponse.Content.ReadAsStreamAsync(),
|
|
|
|
jsonOptions);
|
2021-04-07 15:29:15 -04:00
|
|
|
|
|
|
|
return auth!.AccessToken;
|
|
|
|
}
|
|
|
|
|
2023-02-04 11:56:12 -05:00
|
|
|
public static async Task<UserDto> GetUserDtoAsync(HttpClient client)
|
|
|
|
{
|
2023-09-18 11:55:52 -04:00
|
|
|
using var response = await client.GetAsync("Users/Me");
|
2023-02-04 11:56:12 -05:00
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var userDto = await JsonSerializer.DeserializeAsync<UserDto>(
|
2023-09-18 11:55:52 -04:00
|
|
|
await response.Content.ReadAsStreamAsync(), JsonDefaults.Options);
|
2023-02-04 11:56:12 -05:00
|
|
|
Assert.NotNull(userDto);
|
|
|
|
return userDto;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<BaseItemDto> GetRootFolderDtoAsync(HttpClient client, Guid userId = default)
|
|
|
|
{
|
|
|
|
if (userId.Equals(default))
|
|
|
|
{
|
2023-09-18 11:55:52 -04:00
|
|
|
var userDto = await GetUserDtoAsync(client);
|
2023-02-04 11:56:12 -05:00
|
|
|
userId = userDto.Id;
|
|
|
|
}
|
|
|
|
|
2023-09-18 11:55:52 -04:00
|
|
|
var response = await client.GetAsync($"Users/{userId}/Items/Root");
|
2023-02-04 11:56:12 -05:00
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var rootDto = await JsonSerializer.DeserializeAsync<BaseItemDto>(
|
2023-09-18 11:55:52 -04:00
|
|
|
await response.Content.ReadAsStreamAsync(),
|
|
|
|
JsonDefaults.Options);
|
2023-02-04 11:56:12 -05:00
|
|
|
Assert.NotNull(rootDto);
|
|
|
|
return rootDto;
|
|
|
|
}
|
|
|
|
|
2021-04-08 19:04:49 -04:00
|
|
|
public static void AddAuthHeader(this HttpHeaders headers, string accessToken)
|
2021-04-07 15:29:15 -04:00
|
|
|
{
|
|
|
|
headers.Add(AuthHeaderName, DummyAuthHeader + $", Token={accessToken}");
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:03:03 -05:00
|
|
|
private sealed class AuthenticationResultDto
|
2021-04-07 15:29:15 -04:00
|
|
|
{
|
|
|
|
public string AccessToken { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
public string ServerId { get; set; } = string.Empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|