Context
I am currently working on a .net Core 3.1 API that has an authentication method that checks if a HTTP request is send from a specific IP addresses. The requesters IP Address should match with ones that are stored in the database or localhost otherwise the client is denied.
Code
I Have the following code:
Controller
public async Task<IActionResult> AuthenticatePlanbord([FromBody] AuthPlanbordRequest request)
{
if (request.AuthType == AuthType.Planbord)
{
// Validate the IP address of the client to check if the request is made from the server of the planbord.
var ip = _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
var AuthResponse = await _authService.AuthenticatePlanbordAsync(ip, request.DatabaseName, request.UserId);
if (AuthResponse == null) return Unauthorized(new ServerResponse(false, "Unauthorized", HttpStatusCode.Unauthorized));
return Ok(new ServerResponse(TokenGenerator.GenerateJsonWebToken(AuthResponse)));
}
return BadRequest(new ServerResponse(false, _localizer["AuthTypeNotSupported"], HttpStatusCode.BadRequest));
}
Authentication service
public async Task<AuthEntityPlanbord> AuthenticatePlanbordAsync(string ip, string databaseName, Guid userId = default)
{
_unitOfWork.Init();
// Check if the request does not originate from localhost
if (ip != "::1")
{
var Ip = await _unitOfWork.Connection.ExecuteScalarAsync<string>("SELECT IpAdres FROM PlanbordAutorisaties WITH(NOLOCK) WHERE IpAdres = @Ip ", new { Ip = ip }, _unitOfWork.Transaction);
if (string.IsNullOrEmpty(Ip)) return null;
}
var userData = await _unitOfWork.AuthRepository.AuthenticatePlanbordAsync(userId);
userData.IPAdress = ip;
userData.DatabaseName = databaseName;
return userData;
}
Problem & Question
To fully test if the logic works I would like to write a integration test that sends a HTTP request from a different IP address than localhost. Would this be possible in .net Core? Or Should I just rely on Unit tests only?