2
0

AuthHelper.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Net.Http.Headers;
  5. using System.Net.Http.Json;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using Jellyfin.Api.Models.StartupDtos;
  9. using Jellyfin.Api.Models.UserDtos;
  10. using Jellyfin.Extensions.Json;
  11. using MediaBrowser.Model.Dto;
  12. using Xunit;
  13. namespace Jellyfin.Server.Integration.Tests
  14. {
  15. public static class AuthHelper
  16. {
  17. public const string AuthHeaderName = "Authorization";
  18. public const string DummyAuthHeader = "MediaBrowser Client=\"Jellyfin.Server%20Integration%20Tests\", DeviceId=\"69420\", Device=\"Apple%20II\", Version=\"10.8.0\"";
  19. public static async Task<string> CompleteStartupAsync(HttpClient client)
  20. {
  21. var jsonOptions = JsonDefaults.Options;
  22. var userResponse = await client.GetByteArrayAsync("/Startup/User");
  23. var user = JsonSerializer.Deserialize<StartupUserDto>(userResponse, jsonOptions);
  24. using var completeResponse = await client.PostAsync("/Startup/Complete", new ByteArrayContent(Array.Empty<byte>()));
  25. Assert.Equal(HttpStatusCode.NoContent, completeResponse.StatusCode);
  26. using var httpRequest = new HttpRequestMessage(HttpMethod.Post, "/Users/AuthenticateByName");
  27. httpRequest.Headers.TryAddWithoutValidation(AuthHeaderName, DummyAuthHeader);
  28. httpRequest.Content = JsonContent.Create(
  29. new AuthenticateUserByName()
  30. {
  31. Username = user!.Name,
  32. Pw = user.Password,
  33. },
  34. options: jsonOptions);
  35. using var authResponse = await client.SendAsync(httpRequest);
  36. authResponse.EnsureSuccessStatusCode();
  37. var auth = await JsonSerializer.DeserializeAsync<AuthenticationResultDto>(
  38. await authResponse.Content.ReadAsStreamAsync(),
  39. jsonOptions);
  40. return auth!.AccessToken;
  41. }
  42. public static async Task<UserDto> GetUserDtoAsync(HttpClient client)
  43. {
  44. using var response = await client.GetAsync("Users/Me");
  45. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  46. var userDto = await JsonSerializer.DeserializeAsync<UserDto>(
  47. await response.Content.ReadAsStreamAsync(), JsonDefaults.Options);
  48. Assert.NotNull(userDto);
  49. return userDto;
  50. }
  51. public static async Task<BaseItemDto> GetRootFolderDtoAsync(HttpClient client, Guid userId = default)
  52. {
  53. if (userId.Equals(default))
  54. {
  55. var userDto = await GetUserDtoAsync(client);
  56. userId = userDto.Id;
  57. }
  58. var response = await client.GetAsync($"Users/{userId}/Items/Root");
  59. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  60. var rootDto = await JsonSerializer.DeserializeAsync<BaseItemDto>(
  61. await response.Content.ReadAsStreamAsync(),
  62. JsonDefaults.Options);
  63. Assert.NotNull(rootDto);
  64. return rootDto;
  65. }
  66. public static void AddAuthHeader(this HttpHeaders headers, string accessToken)
  67. {
  68. headers.Add(AuthHeaderName, DummyAuthHeader + $", Token={accessToken}");
  69. }
  70. private sealed class AuthenticationResultDto
  71. {
  72. public string AccessToken { get; set; } = string.Empty;
  73. public string ServerId { get; set; } = string.Empty;
  74. }
  75. }
  76. }