Skip to content

Commit

Permalink
feat: add placeholders for endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
raczu committed Jun 7, 2024
1 parent 077d9e2 commit c56618b
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,27 @@ public async Task HandleException_WhenVerificationException_ShouldReturnProblemD
Assert.AreEqual(exception, problemDetailsContext.Exception);
Assert.AreEqual(exception.Message, problemDetailsContext.ProblemDetails.Detail);
}

[TestMethod]
public async Task HandleException_WhenUnauthorizedAccessException_ShouldReturnProblemDetails()
{
var httpContext = new DefaultHttpContext();
var exception = new UnauthorizedAccessException("Unauthorized access");

ProblemDetailsContext? problemDetailsContext = null;
_mockProblemDetailsService.Setup(x =>
x.TryWriteAsync(It.IsAny<ProblemDetailsContext>()))
.Callback<ProblemDetailsContext>(context => problemDetailsContext = context)
.ReturnsAsync(true);

await _handler.TryHandleAsync(httpContext, exception, CancellationToken.None);

Assert.AreEqual((int)HttpStatusCode.Unauthorized, httpContext.Response.StatusCode);
Assert.IsNotNull(problemDetailsContext);
Assert.AreEqual("https://datatracker.ietf.org/doc/html/rfc7235#section-3.1",
problemDetailsContext.ProblemDetails.Type);
Assert.AreEqual("Unauthorized", problemDetailsContext.ProblemDetails.Title);
Assert.AreEqual(exception, problemDetailsContext.Exception);
Assert.AreEqual(exception.Message, problemDetailsContext.ProblemDetails.Detail);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void Register_WhenUserWithPhoneAlreadyExists_ShouldThrowBadRequestExcepti
Username = "jstark",
Phone = "+123 456789"
};

Assert.ThrowsException<BadRequestException>(() => _service.Register(request));
}

Expand Down
2 changes: 1 addition & 1 deletion Server/ReasnAPI/ReasnAPI/Common/IAssemblyMarker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ReasnAPI.Validators;
namespace ReasnAPI.Common;

public interface IAssemblyMarker
{
Expand Down
139 changes: 139 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ReasnAPI.Models.Database;

namespace ReasnAPI.Controllers;

[ApiController]
[Route("[controller]")]
public class EventsController : ControllerBase
{
private readonly ReasnContext _context;

Check warning on line 11 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Remove this unread private field '_context' or refactor the code to use its value. (https://rules.sonarsource.com/csharp/RSPEC-4487)

Check warning on line 11 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Remove this unread private field '_context' or refactor the code to use its value. (https://rules.sonarsource.com/csharp/RSPEC-4487)

Check warning on line 11 in Server/ReasnAPI/ReasnAPI/Controllers/EventsController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Remove this unread private field '_context' or refactor the code to use its value. (https://rules.sonarsource.com/csharp/RSPEC-4487)

public EventsController(ReasnContext context)
{
_context = context;
}

[HttpGet]
public IActionResult GetEvents()
{
throw new NotImplementedException();
}

[HttpPost]
[Authorize(Roles = "Admin, Organizer")]
public IActionResult CreateEvent()
{
throw new NotImplementedException();
}

[HttpGet]
[Route("{slug}")]
public IActionResult GetEventBySlug(string slug)
{
throw new NotImplementedException();
}

[HttpPut]
[Authorize(Roles = "Admin, Organizer")]
[Route("{slug}")]
public IActionResult UpdateEvent(string slug)
{
throw new NotImplementedException();
}

[HttpGet]
[Authorize(Roles = "Admin")]
[Route("requests")]
public IActionResult GetEventsRequests(string slug)
{
throw new NotImplementedException();
}

[HttpPost]
[Authorize(Roles = "Admin")]
[Route("{slug}/approve")]
public IActionResult ApproveEventRequest(string slug)
{
throw new NotImplementedException();
}

[HttpPost]
[Authorize(Roles = "Admin, Organizer")]
[Route("{slug}/images")]
public IActionResult AddEventImage(string slug)
{
throw new NotImplementedException();
}

[HttpPut]
[Authorize(Roles = "Admin, Organizer")]
[Route("{slug}/images/{imageId:int}")]
public IActionResult UpdateEventImage(string slug, int imageId)
{
throw new NotImplementedException();
}

[HttpDelete]
[Authorize(Roles = "Admin, Organizer")]
[Route("{slug}/images/{imageId:int}")]
public IActionResult DeleteEventImage(string slug, int imageId)
{
throw new NotImplementedException();
}

[HttpGet]
[Route("{slug}/users")]
public IActionResult GetEventUsers(string slug)
{
throw new NotImplementedException();
}

[HttpGet]
[Route("{slug}/comments")]
public IActionResult GetEventComments(string slug)
{
throw new NotImplementedException();
}

[HttpPost]
[Authorize]
[Route("{slug}/comments")]
public IActionResult AddEventComment(string slug)
{
throw new NotImplementedException();
}

[HttpPost]
[Authorize(Roles = "Admin, Organizer")]
[Route("{slug}/parameters")]
public IActionResult AddEventParameter(string slug)
{
throw new NotImplementedException();
}

[HttpDelete]
[Authorize(Roles = "Admin, Organizer")]
[Route("{slug}/parameters/{parameterId:int}")]
public IActionResult DeleteEventParameter(string slug, int parameterId)
{
throw new NotImplementedException();
}

[HttpGet]
[Authorize(Roles = "Admin, Organizer")]
[Route("parameters")]
public IActionResult GetEventsParameters(string slug)
{
throw new NotImplementedException();
}

[HttpDelete]
[Authorize(Roles = "Admin")]
[Route("parameters/{parameterId:int}")]
public IActionResult DeleteEventsParameter(int parameterId)
{
throw new NotImplementedException();
}
}
104 changes: 104 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Controllers/MeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ReasnAPI.Mappers;
using ReasnAPI.Models.DTOs;
using ReasnAPI.Services;

namespace ReasnAPI.Controllers;

[ApiController]
[Authorize]
[Route("[controller]")]
public class MeController : ControllerBase
{
private readonly UserService _userService;

public MeController(UserService userService)
{
_userService = userService;
}

[HttpGet]
[ProducesResponseType<UserDto>(StatusCodes.Status200OK)]
public IActionResult GetCurrentUser()
{
var user = _userService.GetCurrentUser();
return Ok(user.ToDto());
}

[HttpPut]
public IActionResult UpdateCurrentUser()
{
throw new NotImplementedException();
}

[HttpPost]
[Route("image")]
public IActionResult AddCurrentUserImage()
{
throw new NotImplementedException();
}

[HttpPut]
[Route("image")]
public IActionResult UpdateCurrentUserImage()
{
throw new NotImplementedException();
}

[HttpDelete]
[Route("image")]
public IActionResult DeleteCurrentUserImage()
{
throw new NotImplementedException();
}

[HttpGet]
[Route("interests")]
public IActionResult GetCurrentUserInterests()
{
throw new NotImplementedException();
}

[HttpPost]
[Route("interests")]
public IActionResult AddCurrentUserInterest()
{
throw new NotImplementedException();
}

[HttpDelete]
[Route("interests/{interestId:int}")]
public IActionResult DeleteCurrentUserInterest(int interestId)
{
throw new NotImplementedException();
}

[HttpGet]
[Route("events")]
public IActionResult GetCurrentUserEvents()
{
throw new NotImplementedException();
}

[HttpPost]
[Route("events/{slug}/enroll")]
public IActionResult EnrollCurrentUserInEvent(string slug)
{
throw new NotImplementedException();
}

[HttpPost]
[Route("events/{slug}/confirm")]
public IActionResult ConfirmCurrentUserEventAttendance(string slug)
{
throw new NotImplementedException();
}

[HttpGet]
[Route("events/recommendations")]
public IActionResult GetCurrentUserEventRecommendations()
{
throw new NotImplementedException();
}
}
32 changes: 32 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ReasnAPI.Controllers;
Expand All @@ -6,10 +7,41 @@ namespace ReasnAPI.Controllers;
[Route("[controller]")]
public class UsersController : ControllerBase
{
[HttpGet]
[Authorize(Roles = "Admin")]
public IActionResult GetUsers()
{
throw new NotImplementedException();
}

[HttpGet]
[Route("{username}")]
public IActionResult GetUserByUsername(string username)
{
throw new NotImplementedException();
}

[HttpPut]
[Authorize]
[Route("{username}")]
public IActionResult UpdateUser(string username)
{
throw new NotImplementedException();
}

[HttpGet]
[Authorize]
[Route("interests")]
public IActionResult GetUsersInterests(string username)
{
throw new NotImplementedException();
}

[HttpDelete]
[Authorize(Roles = "Admin")]
[Route("interests/{interestId:int}")]
public IActionResult DeleteUserInterest(int interestId)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public async ValueTask<bool> TryHandleAsync(
problemDetails.Title = "A verification error occurred";
break;

case UnauthorizedAccessException:
httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
problemDetails.Type = "https://datatracker.ietf.org/doc/html/rfc7235#section-3.1";
problemDetails.Title = "Unauthorized";
break;

default:
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion Server/ReasnAPI/ReasnAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
using FluentValidation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using ReasnAPI.Common;
using ReasnAPI.Exceptions;
using ReasnAPI.Middlewares;
using ReasnAPI.Models.Database;
using ReasnAPI.Services;
using ReasnAPI.Services.Authentication;
using ReasnAPI.Validators;

var builder = WebApplication.CreateSlimBuilder(args);
var config = builder.Configuration;
Expand Down Expand Up @@ -50,9 +51,11 @@
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddHttpContextAccessor();

builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<TokenService>();
builder.Services.AddScoped<UserService>();
builder.Services.AddValidatorsFromAssemblyContaining<IAssemblyMarker>();

var dataSourceBuilder = new NpgsqlDataSourceBuilder(config.GetConnectionString("DefaultValue"));
Expand Down
Loading

0 comments on commit c56618b

Please sign in to comment.