public async Task TestInteractor_NotEmptyList() { // ARRANGE // Authorization Service Mocking var authServiceMock = new Mock <IAuthService>(); authServiceMock.Setup(d => d.IsAuthenticated()) .Returns(true); authServiceMock.Setup(d => d.IsAuthorized(AuthorizationRole.Admin)) .Returns(true); // Repository Mocking (Motorcycle motorcycle, _) = Motorcycle.NewMotorcycle("Honda", "Shadow", 2006, "01234567890123456"); motorcycle.Id = 1; var repositoryMock = new Mock <IMotorcycleRepository>(); var motorcycles = new List <Motorcycle> { motorcycle }; // Repository List Mocking var tcs = new TaskCompletionSource <(IReadOnlyCollection <Motorcycle>, OperationStatus, IError)>(); tcs.SetResult((motorcycles, OperationStatus.Ok, null)); repositoryMock.Setup(d => d.ListAsync()) .Returns(tcs.Task); var(request, _) = ListMotorcyclesRequest.NewListMotorcyclesRequest(); (ListMotorcyclesInteractor interactor, _) = ListMotorcyclesInteractor.NewListMotorcycleInteractor(repositoryMock.Object, authServiceMock.Object); // ACT (ListMotorcyclesResponse response, _) = await interactor.HandleAsync(request); // ASSERT Assert.True(response.Motorcycles.Count == 1); }
public async Task TestInteractor_NotAuthenticated() { // ARRANGE var authServiceMock = new Mock <IAuthService>(); authServiceMock.Setup(d => d.IsAuthenticated()).Returns(false); var repositoryMock = new Mock <IMotorcycleRepository>(); var(request, _) = ListMotorcyclesRequest.NewListMotorcyclesRequest(); (ListMotorcyclesInteractor interactor, _) = ListMotorcyclesInteractor.NewListMotorcycleInteractor(repositoryMock.Object, authServiceMock.Object); // ACT (_, IError error) = await interactor.HandleAsync(request); // ASSERT Assert.NotNull(error); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Handle processes the request message and generates the response message. It is performing /// the use case. The request message is a dto containing the required data for completing the /// use case. /// </summary> /// /// <param name="request"> The request. </param> /// /// <returns> /// On success, the method returns the (response message, nil), otherwise (nil, error). /// </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public async Task <(ListMotorcyclesResponse response, IError error)> HandleAsync(ListMotorcyclesRequest request) { // Verify that the user has been properly authenticated. if (!_authService.IsAuthenticated()) { return(ListMotorcyclesResponse.NewListMotorcyclesResponse(null, OperationStatus.NotAuthenticated, new Error("List operation failed due to not being authenticated."))); } // Verify that the user has the necessary authorizations. if (!_authService.IsAuthorized(AuthorizationRole.Admin)) { return(ListMotorcyclesResponse.NewListMotorcyclesResponse(null, OperationStatus.NotAuthorized, new Error("List operation failed due to not being authorized, so please contact your system administrator."))); } // List the motorcycle with Id from the repository. (IReadOnlyCollection <Motorcycle> motorcycles, OperationStatus status, IError error) = await _motorcycleRepository.ListAsync(); if (error != null) { return(ListMotorcyclesResponse.NewListMotorcyclesResponse(null, status, error)); } // Save the changes. (status, error) = await _motorcycleRepository.SaveAsync(); if (error != null) { return(ListMotorcyclesResponse.NewListMotorcyclesResponse(null, status, error)); } // Return the successful response message. return(ListMotorcyclesResponse.NewListMotorcyclesResponse(motorcycles, OperationStatus.Ok, null)); }