Skip to content

Commit

Permalink
[RSN-60] - Implement required methods in MeController and UserControl…
Browse files Browse the repository at this point in the history
…ler (#58)

* feat: add jwt and exceptions handlers

Simplify the logic on the controllers' side, custom exceptions and
a library for validation were added. The implemented handlers are
allow to catch those exceptions and return the corresponding
statuses and detailed response.

Additionally, mappers have been created to more easily convert
entities into the corresponding DTOs, fixed enum conversion
when it comes to UserRole and export postgres port on the
development environment.

* test: add unit tests for jwt, handlers and validators

* feat: add placeholders for endpoints

* chore: add missing controllers placeholders

* Created controllers

* Created controllers

* Finished controllers and updated services

* Update UserService tests

* Create and update image methods update

* Image controllers update

* Update controller and service

* Added Address to UserDto

* Controller update and service changes

Changed Image and Participant service (and participant tests), controller now use new service methods

* Methods name changes

---------

Co-authored-by: raczu <[email protected]>
  • Loading branch information
mkoper02 and raczu authored Jun 23, 2024
1 parent 551aadb commit f42c647
Show file tree
Hide file tree
Showing 15 changed files with 361 additions and 246 deletions.
153 changes: 65 additions & 88 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/ParticipantServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,6 @@ namespace ReasnAPI.Tests.Services;
[TestClass]
public class ParticipantServiceTests
{
[TestMethod]
public void GetParticipantById_ParticipantExists_ParticipantReturned()
{
var mockContext = new Mock<ReasnContext>();

var event1 = new Event
{
Id = 1,
Name = "Event",
Description = "Description"
};

var user = new User
{
Id = 1,
Username = "User",
Email = "Email",
Password = "Password"
};

var participant = new Participant
{
Id = 1,
EventId = event1.Id,
UserId = user.Id,
Status = ParticipantStatus.Interested
};

var fakeParticipant = new FakeDbSet<Participant> { participant };
var fakeEvent = new FakeDbSet<Event> { event1 };
var fakeUser = new FakeDbSet<User> { user };

mockContext.Setup(c => c.Events).Returns(fakeEvent);
mockContext.Setup(c => c.Users).Returns(fakeUser);
mockContext.Setup(c => c.Participants).Returns(fakeParticipant);

var participantService = new ParticipantService(mockContext.Object);

var result = participantService.GetParticipantById(1);

Assert.IsNotNull(result);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
Assert.AreEqual(ParticipantStatus.Interested, result.Status);
}

[TestMethod]
public void GetParticipantById_ParticipantDoesNotExist_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Participants).ReturnsDbSet([]);

var participantService = new ParticipantService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => participantService.GetParticipantById(1));
}

[TestMethod]
public void GetAllParticipants_ParticipantsExist_ParticipantsReturned()
Expand Down Expand Up @@ -99,16 +43,16 @@ public void GetAllParticipants_ParticipantsExist_ParticipantsReturned()
var participant1 = new Participant
{
Id = 1,
EventId = event1.Id,
UserId = user1.Id,
Event = event1,
User = user1,
Status = ParticipantStatus.Interested
};

var participant2 = new Participant
{
Id = 2,
EventId = event1.Id,
UserId = user2.Id,
Event = event1,
User = user2,
Status = ParticipantStatus.Participating
};

Expand Down Expand Up @@ -169,16 +113,18 @@ public void GetParticipantsByFilter_ParticipantsExist_ParticipantsReturned()
var participant1 = new Participant
{
Id = 1,
Event = event1,
EventId = event1.Id,
UserId = user1.Id,
User = user1,
Status = ParticipantStatus.Interested
};

var participant2 = new Participant
{
Id = 2,
Event = event1,
EventId = event1.Id,
UserId = user2.Id,
User = user2,
Status = ParticipantStatus.Participating
};

Expand Down Expand Up @@ -217,13 +163,14 @@ public void CreateParticipant_ParticipantCreated_ParticipantReturned()
{
Id = 1,
Name = "Event",
Description = "Description"
Description = "Description",
Slug = "Event-Slug"
};

var user = new User
{
Id = 1,
Username = "User",
Username = "Username",
Email = "Email",
Password = "Password"
};
Expand All @@ -236,16 +183,16 @@ public void CreateParticipant_ParticipantCreated_ParticipantReturned()

var participantDto = new ParticipantDto
{
EventId = 1,
UserId = 1,
EventSlug = "Event-Slug",
Username = "Username",
Status = ParticipantStatus.Interested
};

var result = participantService.CreateParticipant(participantDto);
var result = participantService.CreateOrUpdateParticipant(participantDto);

Assert.IsNotNull(result);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
Assert.AreEqual("Event-Slug", result.EventSlug);
Assert.AreEqual("Username", result.Username);
Assert.AreEqual(ParticipantStatus.Interested, result.Status);
}

Expand All @@ -257,7 +204,7 @@ public void CreateParticipant_ParticipantDtoIsNull_NullReturned()

var participantService = new ParticipantService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => participantService.CreateParticipant(null));
Assert.ThrowsException<ArgumentNullException>(() => participantService.CreateOrUpdateParticipant(null));
}

[TestMethod]
Expand All @@ -269,22 +216,23 @@ public void UpdateParticipant_ParticipantExists_ParticipantReturned()
{
Id = 1,
Name = "Event",
Description = "Description"
Description = "Description",
Slug = "Event-Slug"
};

var user = new User
{
Id = 1,
Username = "User",
Username = "Username",
Email = "Email",
Password = "Password"
};

var participant = new Participant
{
Id = 1,
EventId = event1.Id,
UserId = user.Id,
Event = event1,
User = user,
Status = ParticipantStatus.Interested
};

Expand All @@ -294,31 +242,33 @@ public void UpdateParticipant_ParticipantExists_ParticipantReturned()

var participantService = new ParticipantService(mockContext.Object);

var result = participantService.UpdateParticipant(1, new ParticipantDto
var result = participantService.CreateOrUpdateParticipant(new ParticipantDto
{
EventId = 2,
UserId = 2,
EventSlug = "Event-Slug",
Username = "Username",
Status = ParticipantStatus.Participating
});

Assert.IsNotNull(result);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
Assert.AreEqual("Event-Slug", result.EventSlug);
Assert.AreEqual("Username", result.Username);
Assert.AreEqual(ParticipantStatus.Participating, result.Status);
}

[TestMethod]
public void UpdateParticipant_ParticipantDoesNotExist_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Events).ReturnsDbSet([]);
mockContext.Setup(c => c.Users).ReturnsDbSet([]);
mockContext.Setup(c => c.Participants).ReturnsDbSet([]);

var participantService = new ParticipantService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => participantService.UpdateParticipant(1, new ParticipantDto
Assert.ThrowsException<NotFoundException>(() => participantService.CreateOrUpdateParticipant(new ParticipantDto
{
EventId = 2,
UserId = 2,
EventSlug = "Event-Slug",
Username = "Username",
Status = ParticipantStatus.Participating
}));
}
Expand All @@ -333,20 +283,45 @@ public void UpdateParticipant_ParticipantDtoIsNull_NullReturned()

var participantService = new ParticipantService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => participantService.UpdateParticipant(1, null));
Assert.ThrowsException<ArgumentNullException>(() => participantService.CreateOrUpdateParticipant(null));
}

[TestMethod]
public void DeleteParticipant_ParticipantExists_ParticipantDeleted()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Participants).ReturnsDbSet([
new() { Id = 1, EventId = 1, UserId = 1, Status = ParticipantStatus.Participating }
]);

var event1 = new Event
{
Id = 1,
Name = "Event",
Description = "Description",
Slug = "Event-Slug"
};

var user = new User
{
Id = 1,
Username = "Username",
Email = "Email",
Password = "Password"
};

var participant = new Participant
{
Id = 1,
Event = event1,
User = user,
Status = ParticipantStatus.Participating
};

mockContext.Setup(c => c.Participants).ReturnsDbSet([participant]);
mockContext.Setup(c => c.Events).ReturnsDbSet([event1]);
mockContext.Setup(c => c.Users).ReturnsDbSet([user]);

var participantService = new ParticipantService(mockContext.Object);

participantService.DeleteParticipant(1);
participantService.DeleteParticipant(user.Id, event1.Slug);

mockContext.Verify(c => c.SaveChanges(), Times.Once);
}
Expand All @@ -356,10 +331,12 @@ public void DeleteParticipant_ParticipantDoesNotExist_NothingDeleted()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Participants).ReturnsDbSet([]);
mockContext.Setup(c => c.Events).ReturnsDbSet([]);
mockContext.Setup(c => c.Users).ReturnsDbSet([]);

var participantService = new ParticipantService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => participantService.DeleteParticipant(1));
Assert.ThrowsException<NotFoundException>(() => participantService.DeleteParticipant(1, "Event-Slug"));
mockContext.Verify(c => c.SaveChanges(), Times.Never);
}
}
34 changes: 29 additions & 5 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/UserServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,28 @@ public void GetUserById_UserExists_UserReturned()
{
var mockContext = new Mock<ReasnContext>();

var address = new Address
{
Id = 1,
City = "City",
Country = "Country",
Street = "Street",
State = "State",
ZipCode = "ZipCode"
};

var user = new User
{
Id = 1,
Name = "John",
Surname = "Doe",
Username = "Username",
Email = "Email"
Email = "Email",
Address = address,
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
mockContext.Setup(c => c.Addresses).ReturnsDbSet([address]);

var userService = new UserService(mockContext.Object);

Expand Down Expand Up @@ -65,13 +77,24 @@ public void GetUserByUsername_UserExists_UserReturned()
{
var mockContext = new Mock<ReasnContext>();

var address = new Address
{
Id = 1,
City = "City",
Country = "Country",
Street = "Street",
State = "State",
ZipCode = "ZipCode"
};

var user = new User
{
Id = 1,
Name = "John",
Surname = "Doe",
Username = "Username",
Email = "Email"
Email = "Email",
Address = address,
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
Expand Down Expand Up @@ -143,6 +166,7 @@ public void UpdateUser_UserUpdated_UserReturned()

mockContext.Setup(c => c.Addresses).ReturnsDbSet([address]);
mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
mockContext.Setup(c => c.UserInterests).ReturnsDbSet([]);

var userService = new UserService(mockContext.Object);

Expand All @@ -157,7 +181,7 @@ public void UpdateUser_UserUpdated_UserReturned()
Role = UserRole.User
};

var result = userService.UpdateUser(1, userDto);
var result = userService.UpdateUser("Username", userDto);

Assert.IsNotNull(result);
Assert.AreEqual("Jane", result.Name);
Expand Down Expand Up @@ -187,7 +211,7 @@ public void UpdateUser_UserDtoIsNull_NullReturned()

var userService = new UserService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => userService.UpdateUser(1, null));
Assert.ThrowsException<ArgumentNullException>(() => userService.UpdateUser("Username", null));
}

[TestMethod]
Expand All @@ -209,6 +233,6 @@ public void UpdateUser_UserDoesNotExist_NullReturned()
AddressId = 1
};

Assert.ThrowsException<NotFoundException>(() => userService.UpdateUser(1, userDto));
Assert.ThrowsException<NotFoundException>(() => userService.UpdateUser("Username", userDto));
}
}
Loading

0 comments on commit f42c647

Please sign in to comment.