mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
hot fix
This commit is contained in:
@@ -48,7 +48,7 @@ public class AuthControllerTests
|
||||
var invitation = _fixture.Create<Invitation>();
|
||||
var token = _fixture.Create<string>();
|
||||
|
||||
_invitesServiceMock.Setup(s => s.Validate(request.InviteLink)).Returns(invitation);
|
||||
_invitesServiceMock.Setup(s => s.ValidateAsync(request.InviteLink)).ReturnsAsync(invitation);
|
||||
_accountServiceMock.Setup(s => s.RegistrationAsync(request.Name, request.Password, invitation)).ReturnsAsync(token);
|
||||
|
||||
// Act
|
||||
@@ -80,7 +80,7 @@ public class AuthControllerTests
|
||||
{
|
||||
// Arrange
|
||||
var request = _fixture.Create<RegistrationRequest>();
|
||||
_invitesServiceMock.Setup(s => s.Validate(request.InviteLink)).Throws(new InviteLinkInvalidException(request.InviteLink));
|
||||
_invitesServiceMock.Setup(s => s.ValidateAsync(request.InviteLink)).ThrowsAsync(new InviteLinkInvalidException(request.InviteLink));
|
||||
|
||||
// Act
|
||||
var result = await _controller.Register(request);
|
||||
@@ -97,7 +97,7 @@ public class AuthControllerTests
|
||||
// Arrange
|
||||
var request = _fixture.Create<RegistrationRequest>();
|
||||
var invitation = _fixture.Create<Invitation>();
|
||||
_invitesServiceMock.Setup(s => s.Validate(request.InviteLink)).Returns(invitation);
|
||||
_invitesServiceMock.Setup(s => s.ValidateAsync(request.InviteLink)).ReturnsAsync(invitation);
|
||||
_accountServiceMock.Setup(s => s.RegistrationAsync(request.Name, request.Password, invitation))
|
||||
.ThrowsAsync(new UserAlreadyExistException(request.Name));
|
||||
|
||||
@@ -116,7 +116,7 @@ public class AuthControllerTests
|
||||
// Arrange
|
||||
var request = _fixture.Create<RegistrationRequest>();
|
||||
var invitation = _fixture.Create<Invitation>();
|
||||
_invitesServiceMock.Setup(s => s.Validate(request.InviteLink)).Returns(invitation);
|
||||
_invitesServiceMock.Setup(s => s.ValidateAsync(request.InviteLink)).ReturnsAsync(invitation);
|
||||
_accountServiceMock.Setup(s => s.RegistrationAsync(request.Name, request.Password, invitation))
|
||||
.ThrowsAsync(new System.Exception("Generic error"));
|
||||
|
||||
|
||||
@@ -233,7 +233,6 @@ public class FriendsControllerTests
|
||||
}
|
||||
|
||||
// Tests for AcceptFriend action
|
||||
|
||||
[Test]
|
||||
public async Task AcceptFriend_ValidRequest_ReturnsOkResult()
|
||||
{
|
||||
|
||||
@@ -45,7 +45,7 @@ public class JwtServiceTests
|
||||
// Arrange
|
||||
var user = _fixture.Create<User>();
|
||||
var expectedRole = "User";
|
||||
_invitesServiceMock.Setup(s => s.GetRole(user)).Returns(Task.FromResult(expectedRole));
|
||||
_invitesServiceMock.Setup(s => s.GetRoleAsync(user)).Returns(Task.FromResult(expectedRole));
|
||||
// Act
|
||||
var tokenString = _jwtService.GenerateJwtToken(user);
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Govor.API.Services.AdminsStuff.Interfaces;
|
||||
using Govor.Contracts.Responses.Admins;
|
||||
using Govor.Core.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -25,7 +27,7 @@ public class UsersController : Controller
|
||||
_logger.LogInformation("Getting all users by administrator");
|
||||
var read = await _users.GetAllUsersAsync();
|
||||
|
||||
return Ok(read);
|
||||
return Ok(BuildUserDtos(read));
|
||||
}
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
@@ -33,4 +35,18 @@ public class UsersController : Controller
|
||||
{
|
||||
return Ok(id);
|
||||
}
|
||||
|
||||
private List<UserResponse> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserResponse
|
||||
{
|
||||
Id = user.Id,
|
||||
Username = user.Username,
|
||||
Description = user.Description,
|
||||
WasOnline = user.WasOnline,
|
||||
IconId = user.IconId,
|
||||
PasswordHash = user.PasswordHash,
|
||||
InviteId = user.InviteId,
|
||||
CreatedOn = user.CreatedOn,
|
||||
IsAdmin = user.Invite?.IsAdmin ?? false,
|
||||
}).ToList();
|
||||
|
||||
}
|
||||
@@ -3,11 +3,13 @@ using Govor.Application.Exceptions.AuthService;
|
||||
using Govor.Application.Exceptions.InvitesService;
|
||||
using Govor.Application.Interfaces.Authentication;
|
||||
using Govor.Contracts.Requests;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Govor.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController : Controller
|
||||
{
|
||||
@@ -23,7 +25,7 @@ public class AuthController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("register")]// api/auth/register
|
||||
[RequireHttps]
|
||||
//[RequireHttps]
|
||||
public async Task<IActionResult> Register([FromBody] RegistrationRequest registrationRequest)
|
||||
{
|
||||
try
|
||||
@@ -33,11 +35,11 @@ public class AuthController : Controller
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var invite = _invitesService.Validate(registrationRequest.InviteLink);
|
||||
var invite = await _invitesService.ValidateAsync(registrationRequest.InviteLink);
|
||||
|
||||
var token = await _accountService.RegistrationAsync(registrationRequest.Name, registrationRequest.Password,
|
||||
invite);
|
||||
_logger.LogInformation($"Register request for {registrationRequest.Name}");
|
||||
_logger.LogInformation($"Register request for {registrationRequest.Name} processed successfully");
|
||||
return Ok(new { token });
|
||||
}
|
||||
catch (UserAlreadyExistException ex)
|
||||
@@ -63,7 +65,7 @@ public class AuthController : Controller
|
||||
}
|
||||
|
||||
[HttpPost("login")]// api/auth/login
|
||||
[RequireHttps]
|
||||
//[RequireHttps]
|
||||
public async Task<IActionResult> Login([FromBody] LoginRequest userRequest)
|
||||
{
|
||||
try
|
||||
@@ -74,7 +76,7 @@ public class AuthController : Controller
|
||||
}
|
||||
|
||||
var token = await _accountService.LoginAsync(userRequest.Name, userRequest.Password);
|
||||
_logger.LogInformation($"Login request for {userRequest.Name}");
|
||||
_logger.LogInformation($"Login request for {userRequest.Name} processed successfully");
|
||||
return Ok(new { token });
|
||||
}
|
||||
catch (UserNotRegisteredException ex)
|
||||
|
||||
@@ -26,7 +26,7 @@ public class FriendsController : Controller
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
[HttpGet("search")]
|
||||
[HttpGet("search")] // api/friends/search?query=
|
||||
public async Task<IActionResult> Search(string query)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<Nullable>disable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
|
||||
|
||||
@@ -17,7 +17,7 @@ builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowFrontend", policy =>
|
||||
{
|
||||
policy.WithOrigins("http://localhost:3000", "https://localhost:3000")
|
||||
policy.WithOrigins("http://localhost:5000", "https://localhost:5000")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
@@ -106,7 +106,10 @@ if (app.Environment.IsDevelopment())
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseCors();
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
|
||||
@@ -4,6 +4,6 @@ namespace Govor.API.Services.Authentication.Interfaces;
|
||||
|
||||
public interface IInvitesService
|
||||
{
|
||||
public Task<string> GetRole(User user);
|
||||
public Invitation Validate(string inviteCode);
|
||||
public Task<string> GetRoleAsync(User user);
|
||||
public Task<Invitation> ValidateAsync(string inviteCode);
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class InvitesService : IInvitesService
|
||||
_invitesRepository = invitesRepository;
|
||||
}
|
||||
|
||||
public async Task<string> GetRole(User user)
|
||||
public async Task<string> GetRoleAsync(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -28,20 +28,27 @@ public class InvitesService : IInvitesService
|
||||
}
|
||||
}
|
||||
|
||||
public Invitation Validate(string inviteCode)
|
||||
public async Task<Invitation> ValidateAsync(string inviteCode)
|
||||
{
|
||||
var invite = _invitesRepository.FindByCodeAsync(inviteCode).Result;
|
||||
try
|
||||
{
|
||||
var invite = await _invitesRepository.FindByCodeAsync(inviteCode);
|
||||
|
||||
if (invite.EndDate < DateTime.Now ||
|
||||
invite.MaxParticipants <= invite.Users.Count)
|
||||
if (invite.EndDate < DateTime.Now || invite.MaxParticipants <= invite.Users.Count)
|
||||
{
|
||||
invite.IsActive = false;
|
||||
_invitesRepository.UpdateAsync(invite);
|
||||
await _invitesRepository.UpdateAsync(invite);
|
||||
throw new InviteLinkInvalidException(inviteCode);
|
||||
}
|
||||
|
||||
return invite;
|
||||
}
|
||||
catch (NotFoundByKeyException<string>)
|
||||
{
|
||||
throw new InviteLinkInvalidException(inviteCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string GenerateInvitationLink(Invitation invitation)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ public class JwtService : IJwtService
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim("userID", user.Id.ToString()),
|
||||
new Claim(ClaimTypes.Role, _invitesService.GetRole(user).Result, ClaimValueTypes.String)
|
||||
new Claim(ClaimTypes.Role, _invitesService.GetRoleAsync(user).Result, ClaimValueTypes.String)
|
||||
};
|
||||
|
||||
var singing = new SigningCredentials(
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Govor.Contracts.Responses.Admins;
|
||||
|
||||
public class UserResponse
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string PasswordHash { get; set; }
|
||||
public DateTime WasOnline { get; set; }
|
||||
public DateOnly CreatedOn { get; set; }
|
||||
public Guid IconId {get; set;}
|
||||
public Guid InviteId {get; set;}
|
||||
public bool IsAdmin {get; set;}
|
||||
}
|
||||
Reference in New Issue
Block a user