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