Ejemplo n.º 1
0
        public async Task TestInteractor_NotAuthenticated()
        {
            // ARRANGE
            var authServiceMock = new Mock <IAuthService>();

            authServiceMock.Setup(d => d.IsAuthenticated()).Returns(false);
            var repositoryMock = new Mock <IMotorcycleRepository>();

            (PostMotorcycleRequest request, _) = PostMotorcycleRequest.NewPostMotorcycleRequest("Honda", "Shadow", 2006, "01234567890123456");

            (PostMotorcycleInteractor interactor, _) = PostMotorcycleInteractor.NewPostMotorcycleInteractor(repositoryMock.Object, authServiceMock.Object);

            // ACT
            (_, IError error) = await interactor.HandleAsync(request);

            // ASSERT
            Assert.NotNull(error);
        }
Ejemplo n.º 2
0
        public async Task TestInteractor_VinAlreadyExists()
        {
            // 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 tcsExists = new TaskCompletionSource <(bool, OperationStatus, IError)>();

            tcsExists.SetResult((false, OperationStatus.NotFound, null));
            repositoryMock.Setup(r => r.ExistsByIdAsync(It.IsAny <long>())).Returns(tcsExists.Task);

            var tcsInsert = new TaskCompletionSource <(Motorcycle, OperationStatus, IError)>();

            tcsInsert.SetResult((motorcycle, OperationStatus.Ok, null));
            repositoryMock.Setup(r => r.InsertAsync(It.IsAny <Motorcycle>(), It.IsAny <Func <Motorcycle, Task <(bool exists, OperationStatus status, IError error)> > >())).Returns(tcsInsert.Task);

            // Create the request to add a new motorcycle to the repository.
            var(request, _) = PostMotorcycleRequest.NewPostMotorcycleRequest("Honda", "Shadow", 2006, "01234567890123456");

            // Create the interactor that will coordinate adding a new motorcycle to the repository.
            (PostMotorcycleInteractor interactor, _) = PostMotorcycleInteractor.NewPostMotorcycleInteractor(repositoryMock.Object, authServiceMock.Object);

            // ACT
            (PostMotorcycleResponse response, _) = await interactor.HandleAsync(request);

            // ASSERT
            Assert.True(response.Id > Domain.Constants.InvalidEntityId);
            Assert.Null(response.Error);
        }
Ejemplo n.º 3
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <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 <(PostMotorcycleResponse response, IError error)> HandleAsync(PostMotorcycleRequest request)
        {
            // Verify that the user has been properly authenticated.
            if (!_authService.IsAuthenticated())
            {
                return(PostMotorcycleResponse.NewPostMotorcycleResponse(Domain.Constants.InvalidEntityId,
                                                                        OperationStatus.NotAuthenticated,
                                                                        new Error("Post operation failed due to not being authenticated.")));
            }

            // Verify that the user has the necessary authorizations.
            if (!_authService.IsAuthorized(AuthorizationRole.Admin))
            {
                return(PostMotorcycleResponse.NewPostMotorcycleResponse(Domain.Constants.InvalidEntityId,
                                                                        OperationStatus.NotAuthorized,
                                                                        new
                                                                        Error("Post operation failed due to not being authorized, so please contact your system administrator.")));
            }

            // Create the new motorcycle instance.
            (Motorcycle motorcycle, IError error) = Motorcycle.NewMotorcycle(request.Make, request.Model, request.Year, request.Vin);
            if (error != null)
            {
                return(PostMotorcycleResponse.NewPostMotorcycleResponse(Domain.Constants.InvalidEntityId, OperationStatus.InternalError, error));
            }

            OperationStatus status = OperationStatus.InternalError;

            // Post the motorcycle with Id from the repository.
            (motorcycle, status, error) = await _motorcycleRepository.InsertAsync(motorcycle,
                                                                                  (moto) =>
            {
                return(_motorcycleRepository.ExistsByIdAsync(moto.Id));
            });

            if (error != null)
            {
                return(PostMotorcycleResponse.NewPostMotorcycleResponse(Domain.Constants.InvalidEntityId, status, error));
            }

            // Save the changes.
            (status, error) = await _motorcycleRepository.SaveAsync();

            if (error != null)
            {
                return(PostMotorcycleResponse.NewPostMotorcycleResponse(Domain.Constants.InvalidEntityId, status, error));
            }

            // Return the successful response message.
            return(PostMotorcycleResponse.NewPostMotorcycleResponse(motorcycle.Id, OperationStatus.Ok, null));
        }