From 53ea25f9fdbc2dbbde4792bec47eaaae2b00309a Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Thu, 24 Jul 2025 14:45:01 +0700 Subject: [PATCH] small fix --- .../Controllers/FriendshipControllerTests.cs | 16 +--------------- .../Friends/FriendsRequestQueryController.cs | 12 +++++++++++- .../Controllers/Friends/FriendshipController.cs | 9 +++++++-- .../Services/Friends/FriendshipServiceTests.cs | 9 ++++++--- .../Services/Friends/FriendshipService.cs | 3 +-- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Govor.API.Tests/IntegrationTests/Controllers/FriendshipControllerTests.cs b/Govor.API.Tests/IntegrationTests/Controllers/FriendshipControllerTests.cs index 85bda4b..6d712fb 100644 --- a/Govor.API.Tests/IntegrationTests/Controllers/FriendshipControllerTests.cs +++ b/Govor.API.Tests/IntegrationTests/Controllers/FriendshipControllerTests.cs @@ -82,21 +82,7 @@ public class FriendshipControllerTests var badRequestResult = result as BadRequestObjectResult; Assert.That(badRequestResult.Value, Is.EqualTo("Query cannot be empty")); } - - - [Test] - public async Task Search_NotFound_IfThrowsSearchUsersException() - { - // Arrange - _friendsServiceMock.Setup(f => f.SearchUsersAsync(It.IsAny(), It.IsAny())) - .ThrowsAsync(new SearchUsersException(_fixture.Create())); - - // Act - var result = await _controller.Search(_fixture.Create()); - // Assert - Assert.That(result, Is.InstanceOf()); - } - + [Test] public async Task Search_StatusCode500_IfThrowsSomeException() { diff --git a/Govor.API/Controllers/Friends/FriendsRequestQueryController.cs b/Govor.API/Controllers/Friends/FriendsRequestQueryController.cs index 251765e..9609238 100644 --- a/Govor.API/Controllers/Friends/FriendsRequestQueryController.cs +++ b/Govor.API/Controllers/Friends/FriendsRequestQueryController.cs @@ -36,7 +36,7 @@ public class FriendsRequestQueryController : Controller { var result = await _friendsService.GetIncomingAsync(_currentUserService.GetCurrentUserId()); var response = _mapper.Map>(result); - + return Ok(response); } catch (InvalidOperationException ex) @@ -44,6 +44,11 @@ public class FriendsRequestQueryController : Controller _logger.LogWarning(ex, ex.Message); return Ok(new List()); } + catch (UnauthorizedAccessException ex) + { + _logger.LogWarning(ex, ex.Message); + return Forbid(ex.Message); + } catch (Exception ex) { _logger.LogError(ex, ex.Message); @@ -70,6 +75,11 @@ public class FriendsRequestQueryController : Controller _logger.LogWarning(ex, ex.Message); return Ok(new List()); } + catch (UnauthorizedAccessException ex) + { + _logger.LogWarning(ex, ex.Message); + return Forbid(ex.Message); + } catch (Exception ex) { _logger.LogError(ex, ex.Message); diff --git a/Govor.API/Controllers/Friends/FriendshipController.cs b/Govor.API/Controllers/Friends/FriendshipController.cs index fa6cd10..61bb81a 100644 --- a/Govor.API/Controllers/Friends/FriendshipController.cs +++ b/Govor.API/Controllers/Friends/FriendshipController.cs @@ -41,10 +41,10 @@ public class FriendshipController : Controller return Ok(response); } - catch (SearchUsersException ex) + catch (UnauthorizedAccessException ex) { _logger.LogWarning(ex, ex.Message); - return NotFound(new { error = ex.Message }); + return Forbid(ex.Message); } catch (Exception ex) { @@ -69,6 +69,11 @@ public class FriendshipController : Controller _logger.LogError(ex, ex.Message); return Ok(Array.Empty()); } + catch (UnauthorizedAccessException ex) + { + _logger.LogWarning(ex, ex.Message); + return Forbid(ex.Message); + } catch (Exception ex) { _logger.LogError(ex, ex.Message); diff --git a/Govor.Application.Tests/Services/Friends/FriendshipServiceTests.cs b/Govor.Application.Tests/Services/Friends/FriendshipServiceTests.cs index df8d52d..672dc86 100644 --- a/Govor.Application.Tests/Services/Friends/FriendshipServiceTests.cs +++ b/Govor.Application.Tests/Services/Friends/FriendshipServiceTests.cs @@ -61,15 +61,18 @@ public class FriendshipServiceTests } [Test] - public void SearchUsersAsync_Throws_SearchUsersException_IfThrowsNotFoundByKeyException_String_Guid() + public async Task SearchUsersAsync_Returns_EmptyList_IfThrowsNotFoundByKeyException_String_Guid() { // Arrange _usersRepositoryMock .Setup(u => u.SearchPotentialFriendsAsync(It.IsAny(), It.IsAny())) .ThrowsAsync(new NotFoundByKeyException<(string,Guid)>(("test",Guid.NewGuid()),"test")); - // Axt & Assert - Assert.ThrowsAsync(async () => await _service.SearchUsersAsync("test", Guid.NewGuid())); + // Act + var users = await _service.SearchUsersAsync("test", Guid.NewGuid()); + + // & Assert + Assert.That(users, Is.Empty); } [Test] diff --git a/Govor.Application/Services/Friends/FriendshipService.cs b/Govor.Application/Services/Friends/FriendshipService.cs index a6bfa9f..20e3a4a 100644 --- a/Govor.Application/Services/Friends/FriendshipService.cs +++ b/Govor.Application/Services/Friends/FriendshipService.cs @@ -33,8 +33,7 @@ public class FriendshipService : IFriendshipService } catch (NotFoundByKeyException<(string, Guid)> ex) { - throw new SearchUsersException( - $"Users with given query: \"{query}\" for user with id {currentId} was not found", ex); + return []; } catch (NotFoundByKeyException ex) {