feat: добавил отображение аватарки
This commit is contained in:
1 parent
34e450815b
commit
038f0d89a6
22 files changed
+598
-28
No files matched your search
@@ -0,0 +1,59 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NSubstitute;
|
||||
using UniVerse.Api.Controllers;
|
||||
using UniVerse.Application.DTOs.Users;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using UniVerse.Domain.Enums;
|
||||
using Xunit;
|
||||
|
||||
namespace UniVerse.Api.Tests.Auth;
|
||||
|
||||
public class ProfilePhotoControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Get_UsesCurrentUserAndReturnsPhotoWithoutCaching()
|
||||
{
|
||||
var auth = Substitute.For<IAuthService>();
|
||||
auth.GetCurrentUserAsync(7).Returns(new CurrentUserDto(
|
||||
7, "user@sfedu.ru", "Test", null, [UserRole.Student], 0, 0, 1, DateTime.UtcNow));
|
||||
var microsoft = Substitute.For<IMicrosoftAuthClient>();
|
||||
microsoft.GetPhotoAsync(7, Arg.Any<CancellationToken>())
|
||||
.Returns(new MicrosoftPhotoResult([1, 2, 3], "image/jpeg"));
|
||||
var controller = CreateController(auth, microsoft, 7);
|
||||
|
||||
var result = await controller.Get(CancellationToken.None);
|
||||
|
||||
var file = Assert.IsType<FileContentResult>(result);
|
||||
Assert.Equal([1, 2, 3], file.FileContents);
|
||||
Assert.Equal("image/jpeg", file.ContentType);
|
||||
Assert.Equal("private, no-store", controller.Response.Headers.CacheControl);
|
||||
await microsoft.Received(1).GetPhotoAsync(7, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Get_ReturnsNotFoundWhenPhotoIsUnavailable()
|
||||
{
|
||||
var auth = Substitute.For<IAuthService>();
|
||||
auth.GetCurrentUserAsync(7).Returns(new CurrentUserDto(
|
||||
7, "user@sfedu.ru", "Test", null, [UserRole.Student], 0, 0, 1, DateTime.UtcNow));
|
||||
var controller = CreateController(auth, Substitute.For<IMicrosoftAuthClient>(), 7);
|
||||
|
||||
Assert.IsType<NotFoundResult>(await controller.Get(CancellationToken.None));
|
||||
}
|
||||
|
||||
private static ProfilePhotoController CreateController(IAuthService auth, IMicrosoftAuthClient microsoft, int userId)
|
||||
{
|
||||
var controller = new ProfilePhotoController(auth, microsoft);
|
||||
controller.ControllerContext = new ControllerContext
|
||||
{
|
||||
HttpContext = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(
|
||||
[new Claim(ClaimTypes.NameIdentifier, userId.ToString())], "test"))
|
||||
}
|
||||
};
|
||||
return controller;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user