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

Feature/JA-80 UiMessagesService #66

Merged
merged 17 commits into from
Jun 4, 2024
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 @@ -68,6 +68,28 @@ public async Task Ads_WhenResponseIsUnsuccessful_ShouldReturnRedirectToAction()
Assert.IsType<RedirectToActionResult>(result);
}

[Fact]
public async Task Ads_WhenResponseIsUnsuccessful_ShouldShowFailureMessage()
{
// Arrange
int pageNumber = 1;
GetBrowseAdsPageResponse response = CreateGetBrowseAdsPageResponse(success: false);

MockBrowseService
.Setup(x => x.GetBrowseAdsPage(It.IsAny<GetBrowseAdsPageRequest>()))
.Returns(Task.FromResult(response));

MockUiMessagesService
.Setup(x => x.ShowFailureMessage(It.IsAny<string>()))
.Verifiable(Times.Once);

// Act
var result = await BrowseController.Ads(pageNumber);

// Assert
MockUiMessagesService.VerifyAll();
}

[Fact]
public async Task Ads_WhenResponseIsSuccessful_ShouldReturnView()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Moq;
using TutorLizard.BusinessLogic.Interfaces.Services;
using TutorLizard.Web.Controllers;
using TutorLizard.Web.Interfaces.Services;

namespace TutorLizard.Web.Tests.Controllers.Browse;

Expand All @@ -10,11 +11,14 @@ public abstract class BrowseControllerTestsBase
protected BrowseController BrowseController;
protected Mock<IBrowseService> MockBrowseService = new();
protected Mock<IUserAuthenticationService> MockUserAuthenticationService = new();
protected Mock<IUiMessagesService> MockUiMessagesService = new();
protected Fixture Fixture = new();

public BrowseControllerTestsBase()
{
BrowseController = new(MockBrowseService.Object, MockUserAuthenticationService.Object);
BrowseController = new(MockBrowseService.Object,
MockUserAuthenticationService.Object,
MockUiMessagesService.Object);
}

protected void SetupMockGetLoggedInUserId(int? userId)
Expand Down
160 changes: 160 additions & 0 deletions Tests/TutorLizard.Web.Tests/Services/UiMessagesServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Moq;
using TutorLizard.Web.Services;
using TutorLizard.Web.Strings;

namespace TutorLizard.Web.Tests.Services;
public class UiMessagesServiceTests
{
private readonly UiMessagesService _uiMessagesService;
private readonly Mock<IHttpContextAccessor> _mockHttpContextAccessor = new();
private readonly Mock<ITempDataDictionaryFactory> _mockTempDataFactory = new();
private readonly Mock<ITempDataDictionary> _mockTempDataDictionary = new();
public UiMessagesServiceTests()
{
_uiMessagesService = new(_mockHttpContextAccessor.Object,
_mockTempDataFactory.Object);
SetupMockHttpContext();
SetupMockTempData();
}

[Fact]
public void ShowMessage_WhenHttpContextIsNull_ShouldNotThrowException()
{
// Arrange
_mockHttpContextAccessor
.Setup(x => x.HttpContext)
.Returns<HttpContext>(null!);

// Act
var act = () => _uiMessagesService.ShowMessage("", "");
var exception = Record.Exception(act);

// Assert
Assert.Null(exception);
}

[Fact]
public void ShowMessage_WhenMessageIsNull_ShouldReplaceItWithEmptyString()
{
// Arrange
var capturedTempData = SetupCaptureOfTempData();
string message = null!;
string expectedMessage = "";
string messageType = "Test";

// Act
_uiMessagesService.ShowMessage(message, messageType);
var actualMessage = capturedTempData[messageType];

// Assert
Assert.NotNull(actualMessage);
Assert.IsType<string>(actualMessage);
Assert.Equal(expectedMessage, actualMessage);
}

[Fact]
public void ShowMessage_WhenMessageTypeIsNull_ShouldReplaceItWithEmptyString()
{
// Arrange
var capturedTempData = SetupCaptureOfTempData();
string message = "Test message";
string messageType = null!;
string expecedMessageType = "";

// Act
_uiMessagesService.ShowMessage(message, messageType);
var actualMessageType = capturedTempData.Keys.Single();

// Assert
Assert.NotNull(actualMessageType);
Assert.Equal(expecedMessageType, actualMessageType);
}

[Fact]
public void ShowMessage_WhenCalled_ShouldCorrectlyAddMessageToTempData()
{
// Arrange
var capturedTempData = SetupCaptureOfTempData();
string message = "Test message";
string messageType = "Test";
string expectedMessage = message;
string expectedMessageType = messageType;

// Act
_uiMessagesService.ShowMessage(message, messageType);
var actualMessage = capturedTempData.Values.Single();
var actualMessageType = capturedTempData.Keys.Single();

// Assert
Assert.IsType<string>(actualMessage);
Assert.Equal(expectedMessage, actualMessage);
Assert.Equal(expectedMessageType, actualMessageType);
}

[Fact]
public void ShowSuccessMessage_WhenCalled_ShouldAddMessageWithCorrectType()
{
// Arrange
var capturedTempData = SetupCaptureOfTempData();
string message = "Test message";
string expectedMessage = message;
string expectedMessageType = MessageType.Success;

// Act
_uiMessagesService.ShowSuccessMessage(message);
var actualMessage = capturedTempData.Values.Single();
var actualMessageType = capturedTempData.Keys.Single();

// Assert
Assert.IsType<string>(actualMessage);
Assert.Equal(expectedMessage, actualMessage);
Assert.Equal(expectedMessageType, actualMessageType);
}

[Fact]
public void ShowFailureMessage_WhenCalled_ShouldAddMessageWithCorrectType()
{
// Arrange
var capturedTempData = SetupCaptureOfTempData();
string message = "Test message";
string expectedMessage = message;
string expectedMessageType = MessageType.Failure;

// Act
_uiMessagesService.ShowFailureMessage(message);
var actualMessage = capturedTempData.Values.Single();
var actualMessageType = capturedTempData.Keys.Single();

// Assert
Assert.IsType<string>(actualMessage);
Assert.Equal(expectedMessage, actualMessage);
Assert.Equal(expectedMessageType, actualMessageType);
}

private void SetupMockHttpContext()
{
_mockHttpContextAccessor
.Setup(x => x.HttpContext)
.Returns(new DefaultHttpContext());
}

private void SetupMockTempData()
{
_mockTempDataFactory
.Setup(x => x.GetTempData(It.IsAny<HttpContext>()))
.Returns(_mockTempDataDictionary.Object);
}

private Dictionary<string, object?> SetupCaptureOfTempData()
{
Dictionary<string, object?> output = [];

_mockTempDataDictionary
.SetupSet(x => x[It.IsAny<string>()] = It.IsAny<object?>())
.Callback<string, object?>((key, value) => output[key] = value);

return output;
}
}
23 changes: 14 additions & 9 deletions TutorLizard.Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
using Microsoft.AspNetCore.Mvc;
using TutorLizard.BusinessLogic.Enums;
using TutorLizard.BusinessLogic.Interfaces.Services;
using TutorLizard.BusinessLogic.Services;
using TutorLizard.Web.Interfaces.Services;
using TutorLizard.Web.Models;

namespace TutorLizard.Web.Controllers;
public class AccountController : Controller
{
private readonly IUserAuthenticationService _userAuthenticationService;
private readonly IUiMessagesService _uiMessagesService;

public AccountController(IUserAuthenticationService userAuthenticationService)
public AccountController(IUserAuthenticationService userAuthenticationService,
IUiMessagesService uiMessagesService)
{
_userAuthenticationService = userAuthenticationService;
_uiMessagesService = uiMessagesService;
}

public IActionResult Index()
Expand All @@ -37,11 +40,13 @@ public async Task<IActionResult> Login(LoginModel model)
TempData["returnUrl"] as string
: null;

TempData["returnUrl"] = "";

try
{
if (ModelState.IsValid && await _userAuthenticationService.LogInAsync(model.UserName, model.Password))
{
TempData["LoginSuccessful"] = "You are logged in.";
_uiMessagesService.ShowSuccessMessage("Jesteś zalogowany/a.");
if (string.IsNullOrEmpty(returnUrl))
{
return RedirectToAction("Index", "Home");
Expand All @@ -51,11 +56,11 @@ public async Task<IActionResult> Login(LoginModel model)
}
catch
{
TempData["LoginUnsuccessful"] = "Could not log in.";
_uiMessagesService.ShowFailureMessage("Logowanie nieudane.");
return LocalRedirect("/Home/Index");
}
TempData["LoginUnsuccessful"] = "Could not log in.";
return View(new { returnUrl = returnUrl });
_uiMessagesService.ShowFailureMessage("Logowanie nieudane.");
return RedirectToAction(nameof(Login), new { returnUrl = returnUrl });
}
[Authorize]
public async Task<IActionResult> Logout()
Expand All @@ -76,16 +81,16 @@ public async Task<IActionResult> Register(RegisterUserModel model)
if (ModelState.IsValid
&& await _userAuthenticationService.RegisterUser(model.UserName, UserType.Regular, model.Email, model.Password))
{
TempData["RegisterSuccessful"] = "Registered Successfully";
_uiMessagesService.ShowSuccessMessage("Użytkownik zarejestrowany.");
return LocalRedirect("/Home/Index");
}
}
catch
{
TempData["RegisterUnsuccessful"] = "Could not register.";
_uiMessagesService.ShowFailureMessage("Wystąpił błąd. Rejestracja nieudana.");
return LocalRedirect("/Home/Index");
}
TempData["RegisterUnsuccessful"] = "Could not register.";
_uiMessagesService.ShowFailureMessage("Wystąpił błąd. Rejestracja nieudana.");
return LocalRedirect("/Home/Index");
}

Expand Down
9 changes: 7 additions & 2 deletions TutorLizard.Web/Controllers/BrowseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
using TutorLizard.BusinessLogic.Interfaces.Services;
using TutorLizard.BusinessLogic.Models.DTOs.Requests;
using TutorLizard.BusinessLogic.Models.DTOs.Responses;
using TutorLizard.Web.Interfaces.Services;

namespace TutorLizard.Web.Controllers;
public class BrowseController : Controller
{
private readonly IBrowseService _browseService;
private readonly IUserAuthenticationService _userAuthenticationService;
private readonly IUiMessagesService _uiMessagesService;
private readonly int _pageSize;

public BrowseController(IBrowseService browseService, IUserAuthenticationService userAuthenticationService)
public BrowseController(IBrowseService browseService,
IUserAuthenticationService userAuthenticationService,
IUiMessagesService uiMessagesService)
{
_browseService = browseService;
_userAuthenticationService = userAuthenticationService;
_uiMessagesService = uiMessagesService;
_pageSize = 10;
}
public IActionResult Index()
Expand All @@ -33,7 +38,7 @@ public async Task<IActionResult> Ads(int id = 1)

if (response.Success == false)
{
// TODO Add failure notification
_uiMessagesService.ShowFailureMessage("Wystąpił błąd. Nie udało się się załadować ogłoszeń.");
return RedirectToAction("Index", "Home");
}

Expand Down
8 changes: 6 additions & 2 deletions TutorLizard.Web/Controllers/ScheduleItemController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
using Microsoft.EntityFrameworkCore;
using TutorLizard.BusinessLogic.Interfaces.Data.Repositories;
using TutorLizard.BusinessLogic.Models;
using TutorLizard.Web.Interfaces.Services;

namespace TutorLizard.Web.Controllers
{
public class ScheduleItemController : Controller
{
private readonly IDbRepository<ScheduleItem> _scheduleItemRepository;
public ScheduleItemController(IDbRepository<ScheduleItem> scheduleItemRepository)
private readonly IUiMessagesService _notificationService;
public ScheduleItemController(IDbRepository<ScheduleItem> scheduleItemRepository,
IUiMessagesService notificationService)
{
_scheduleItemRepository = scheduleItemRepository;
_notificationService = notificationService;
}

// GET: ScheduleItemController
Expand Down Expand Up @@ -58,7 +62,7 @@ public async Task<ActionResult> Create(ScheduleItem model)

await _scheduleItemRepository.Create(model);

TempData["Success"] = "Produkt został dodany";
_notificationService.ShowSuccessMessage("Termin został dodany");
return RedirectToAction(nameof(Index));
}
catch
Expand Down
Loading
Loading