af1152b001
Refactored all C# test files to use explicit namespace declarations and moved all using directives inside the namespace block for consistency. Updated assembly info and cache files to reflect the new build. Adjusted MvcTestingAppManifest.json to use fully qualified assembly names. No functional changes; all updates are related to code style, organization, and project metadata.
173 lines
6.0 KiB
C#
173 lines
6.0 KiB
C#
// <copyright file="UserControllerTests.cs" company="PlaceholderCompany">
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
// </copyright>
|
|
|
|
namespace Jellyfin.Server.Integration.Tests.Controllers
|
|
{
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Jellyfin.Api.Models.UserDtos;
|
|
using Jellyfin.Extensions.Json;
|
|
using MediaBrowser.Model.Dto;
|
|
using Xunit;
|
|
using Xunit.Priority;
|
|
|
|
[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
|
|
public sealed class UserControllerTests : IClassFixture<JellyfinApplicationFactory>
|
|
{
|
|
private const string TestUsername = "testUser01";
|
|
|
|
private readonly JellyfinApplicationFactory _factory;
|
|
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
|
private static string? _accessToken;
|
|
private static Guid _testUserId = Guid.Empty;
|
|
|
|
public UserControllerTests(JellyfinApplicationFactory factory)
|
|
{
|
|
_factory = factory;
|
|
}
|
|
|
|
private Task<HttpResponseMessage> CreateUserByName(HttpClient httpClient, CreateUserByName request)
|
|
=> httpClient.PostAsJsonAsync("Users/New", request, _jsonOptions);
|
|
|
|
private Task<HttpResponseMessage> UpdateUserPassword(HttpClient httpClient, Guid userId, UpdateUserPassword request)
|
|
=> httpClient.PostAsJsonAsync("Users/" + userId.ToString("N", CultureInfo.InvariantCulture) + "/Password", request, _jsonOptions);
|
|
|
|
[Fact]
|
|
[Priority(-1)]
|
|
public async Task GetPublicUsers_Valid_Success()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
|
|
using var response = await client.GetAsync("Users/Public");
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
var users = await response.Content.ReadFromJsonAsync<UserDto[]>(_jsonOptions);
|
|
// User are hidden by default
|
|
Assert.NotNull(users);
|
|
Assert.Empty(users);
|
|
}
|
|
|
|
[Fact]
|
|
[Priority(-1)]
|
|
public async Task GetUsers_Valid_Success()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
|
|
|
|
using var response = await client.GetAsync("Users");
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
var users = await response.Content.ReadFromJsonAsync<UserDto[]>(_jsonOptions);
|
|
Assert.NotNull(users);
|
|
Assert.Single(users);
|
|
}
|
|
|
|
[Fact]
|
|
[Priority(-1)]
|
|
public async Task Me_Valid_Success()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
|
|
|
|
_ = await AuthHelper.GetUserDtoAsync(client);
|
|
}
|
|
|
|
[Fact]
|
|
[Priority(0)]
|
|
public async Task New_Valid_Success()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
|
|
// access token can't be null here as the previous test populated it
|
|
client.DefaultRequestHeaders.AddAuthHeader(_accessToken!);
|
|
|
|
var createRequest = new CreateUserByName()
|
|
{
|
|
Name = TestUsername
|
|
};
|
|
|
|
using var response = await CreateUserByName(client, createRequest);
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
var user = await response.Content.ReadFromJsonAsync<UserDto>(_jsonOptions);
|
|
Assert.Equal(TestUsername, user!.Name);
|
|
|
|
_testUserId = user.Id;
|
|
|
|
Console.WriteLine(user.Id.ToString("N", CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
[InlineData("‼️")]
|
|
[Priority(0)]
|
|
public async Task New_Invalid_Fail(string? username)
|
|
{
|
|
var client = _factory.CreateClient();
|
|
|
|
// access token can't be null here as the previous test populated it
|
|
client.DefaultRequestHeaders.AddAuthHeader(_accessToken!);
|
|
|
|
var createRequest = new CreateUserByName()
|
|
{
|
|
Name = username!
|
|
};
|
|
|
|
using var response = await CreateUserByName(client, createRequest);
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
[Priority(0)]
|
|
public async Task Delete_DoesntExist_NotFound()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
|
|
// access token can't be null here as the previous test populated it
|
|
client.DefaultRequestHeaders.AddAuthHeader(_accessToken!);
|
|
|
|
using var response = await client.DeleteAsync($"User/{Guid.NewGuid()}");
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
[Priority(1)]
|
|
public async Task UpdateUserPassword_Valid_Success()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.AddAuthHeader(_accessToken!);
|
|
|
|
var createRequest = new UpdateUserPassword()
|
|
{
|
|
NewPw = "4randomPa$$word"
|
|
};
|
|
|
|
using var response = await UpdateUserPassword(client, _testUserId, createRequest);
|
|
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
[Priority(2)]
|
|
public async Task UpdateUserPassword_Empty_RemoveSetPassword()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
|
|
client.DefaultRequestHeaders.AddAuthHeader(_accessToken!);
|
|
|
|
var createRequest = new UpdateUserPassword()
|
|
{
|
|
CurrentPw = "4randomPa$$word",
|
|
};
|
|
|
|
using var response = await UpdateUserPassword(client, _testUserId, createRequest);
|
|
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
|
}
|
|
}
|
|
}
|