Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVP: Complete User Fields And Interactions #43

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public async Task GetCategory_Ok_CategoryExists()
.Setup(x => x.GetCategory(It.IsAny<long>()))
.Returns(Task.FromResult<Category?>(new Category { Id = 1 }));

ActionResult<Category> response = await _controller.GetCategory((long)1);
ActionResult<Category> response = await _controller.GetCategory(1);
Category? category = Helpers.GetValueFromObjectResult<OkObjectResult, Category>(response.Result);

Assert.NotNull(response.Value);
Assert.AreEqual(1, response.Value?.Id);
Assert.NotNull(category);
Assert.AreEqual(1, category!.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Moq;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LeaderboardBackend.Test.Controllers;
Expand Down Expand Up @@ -69,7 +70,9 @@ public async Task GetLeaderboards_Ok_ListExists()
.Returns(Task.FromResult(mockList));

ActionResult<List<Leaderboard>> response = await _controller.GetLeaderboards(new long[] { 1, 2 });
List<Leaderboard>? leaderboards = Helpers.GetValueFromObjectResult<OkObjectResult, List<Leaderboard>>(response);

Assert.AreEqual(new ulong[] { 1, 2 }, response.Value?.ConvertAll(l => l.Id));
Assert.NotNull(leaderboards);
Assert.AreEqual(new ulong[] { 1, 2 }, leaderboards!.Select(l => l.Id));
}
}
14 changes: 8 additions & 6 deletions LeaderboardBackend.Test/Controllers/UsersControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,26 @@ public void Setup()
}

[Test]
public async Task GetUser_NotFound_UserDoesNotExist()
public async Task GetUserById_NotFound_UserDoesNotExist()
{
_userServiceMock
.Setup(x => x.GetUser(It.IsAny<Guid>()))
.Setup(x => x.GetUserById(It.IsAny<Guid>()))
.Returns(Task.FromResult<User?>(null));

ActionResult<User> response = await _controller.GetUser(defaultUserId);
ActionResult<User> response = await _controller.GetUserById(defaultUserId);

Helpers.AssertResponseNotFound(response);
}

[Test]
public async Task GetUser_Ok_UserExists()
public async Task GetUserById_Ok_UserExists()
{
_userServiceMock
.Setup(x => x.GetUser(defaultUserId))
.Setup(x => x.GetUserById(defaultUserId))
.Returns(Task.FromResult<User?>(defaultUser));

ActionResult<User> response = await _controller.GetUser(defaultUserId);
ActionResult<User> response = await _controller.GetUserById(defaultUserId);

User? user = Helpers.GetValueFromObjectResult<OkObjectResult, User>(response);

Assert.NotNull(user);
Expand Down
4 changes: 2 additions & 2 deletions LeaderboardBackend.Test/Services/UserServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public async Task GetUser_GetsAnExistingUser()
{
await _userService.CreateUser(_user);

User? getUser = await _userService.GetUser(_user.Id);
User? getUser = await _userService.GetUserById(_user.Id);
Assert.NotNull(getUser);
Assert.AreEqual(getUser, _user);
}

[Test]
public async Task GetUser_ReturnsNullForNonExistingID()
{
User? result = await _userService.GetUser(new Guid());
User? result = await _userService.GetUserById(new Guid());
Assert.Null(result);
}

Expand Down
10 changes: 5 additions & 5 deletions LeaderboardBackend/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public UsersController(IUserService userService, IAuthService authService)
_authService = authService;
}

/// <summary>Gets a User.</summary>
/// <summary>Gets a User by ID.</summary>
/// <param name="id">The User's ID. It must be a GUID.</param>
/// <response code="200">The User with the provided ID.</response>
/// <response code="404">If no User is found with the provided ID.</response>
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Get))]
[HttpGet("{id}")]
public async Task<ActionResult<User>> GetUser(Guid id)
[HttpGet("{id:guid}")]
public async Task<ActionResult<User>> GetUserById(Guid id)
{
User? user = await _userService.GetUser(id);
User? user = await _userService.GetUserById(id);
if (user == null)
{
return NotFound();
Expand Down Expand Up @@ -80,7 +80,7 @@ public async Task<ActionResult<User>> Register([FromBody] RegisterRequest body)
};

await _userService.CreateUser(newUser);
return CreatedAtAction(nameof(GetUser), new { id = newUser.Id }, newUser);
return CreatedAtAction(nameof(GetUserById), new { id = newUser.Id }, newUser);
}

/// <summary>Logs a new user in.</summary>
Expand Down
Loading