public void WhenAddingExistingApplicationThenExceptionIsThrown()
        {
            // Arrange
            IApplicationRepository fakeApplicationRepository = A.Fake <IApplicationRepository>();

            A.CallTo(() => fakeApplicationRepository.TryGetByDisplayNameAsync(A <string> .Ignored)).Returns(Task.FromResult(new ApplicationEntity()));

            IUnitOfWork fakeUnitOfWork = A.Fake <IUnitOfWork>();

            IPasswordWithSaltHasher fakePasswordHasher = A.Fake <IPasswordWithSaltHasher>();

            IApplicationService applicationService = new ApplicationService(fakeApplicationRepository, fakeUnitOfWork, fakePasswordHasher);

            ApplicationCreateDto applicationCreateDto = new ApplicationCreateDto()
            {
                DisplayName = "TestApplicationDisplayName",
                Password    = "******"
            };

            // Act
            ArgumentException ex = Assert.CatchAsync <ArgumentException>(async() => await applicationService.AddApplicationAsync(applicationCreateDto));

            // Assert
            StringAssert.Contains("Application with DisplayName TestApplicationDisplayName already exists.", ex.Message);
        }
        public async Task WhenAddingApplicationThenUnitOfWorkIsCommitted()
        {
            // Arrange
            IApplicationRepository fakeApplicationRepository = A.Fake <IApplicationRepository>();

            A.CallTo(() => fakeApplicationRepository.TryGetByDisplayNameAsync(A <string> .Ignored)).Returns(Task.FromResult <ApplicationEntity>(null));

            IUnitOfWork fakeUnitOfWork = A.Fake <IUnitOfWork>();

            IPasswordWithSaltHasher fakePasswordHasher = A.Fake <IPasswordWithSaltHasher>();

            A.CallTo(() => fakePasswordHasher.HashPassword(A <string> .Ignored)).Returns(new HashWithSaltResult());

            IApplicationService applicationService = new ApplicationService(fakeApplicationRepository, fakeUnitOfWork, fakePasswordHasher);

            ApplicationCreateDto applicationCreateDto = new ApplicationCreateDto()
            {
                DisplayName = "TestApplicationDisplayName",
                Password    = "******"
            };

            // Act
            await applicationService.AddApplicationAsync(applicationCreateDto);

            // Assert
            A.CallTo(() => fakeUnitOfWork.CommitAsync()).MustHaveHappened();
        }
        public async Task WhenFindingExistingApplicationWithWrongPasswordThenNullIsReturned()
        {
            // Arrange
            ApplicationEntity application = new ApplicationEntity()
            {
                Id           = 0,
                DisplayName  = "TestApplicationDisplayName",
                PasswordHash = "TestPasswordHash",
                PasswordSalt = "TestPasswordSalt"
            };

            IApplicationRepository fakeApplicationRepository = A.Fake <IApplicationRepository>();

            A.CallTo(() => fakeApplicationRepository.TryGetByDisplayNameAsync("TestApplicationDisplayName")).Returns(Task.FromResult(application));

            IUnitOfWork fakeUnitOfWork = A.Fake <IUnitOfWork>();

            IPasswordWithSaltHasher fakePasswordHasher = A.Fake <IPasswordWithSaltHasher>();

            A.CallTo(() => fakePasswordHasher.CheckPassword(A <string> .Ignored, A <string> .Ignored, A <string> .Ignored)).Returns(false);

            IApplicationService applicationService = new ApplicationService(fakeApplicationRepository, fakeUnitOfWork, fakePasswordHasher);

            // Act
            ApplicationDto applicationDto = await applicationService.FindApplicationAsync("TestApplicationDisplayName", "TestPassword");

            // Assert
            Assert.IsNull(applicationDto);
        }
 public ApplicationService(IApplicationRepository applicationRepository,
                           IUnitOfWork unitOfWork,
                           IPasswordWithSaltHasher passwordWithSaltHasher
                           )
 {
     _applicationRepository  = applicationRepository;
     _unitOfWork             = unitOfWork;
     _passwordWithSaltHasher = passwordWithSaltHasher;
 }
        public void WhenAddingNullApplicationThenExceptionIsThrown()
        {
            // Arrange
            IApplicationRepository  fakeApplicationRepository = A.Fake <IApplicationRepository>();
            IUnitOfWork             fakeUnitOfWork            = A.Fake <IUnitOfWork>();
            IPasswordWithSaltHasher fakePasswordHasher        = A.Fake <IPasswordWithSaltHasher>();
            IApplicationService     applicationService        = new ApplicationService(fakeApplicationRepository, fakeUnitOfWork, fakePasswordHasher);

            // Act
            ArgumentException ex = Assert.CatchAsync <ArgumentException>(async() => await applicationService.AddApplicationAsync(null));

            // Assert
            StringAssert.Contains("Application name and password must be provided.", ex.Message);
        }
        public async Task WhenFindingAbsentApplicationThenNullIsReturned()
        {
            // Arrange
            IApplicationRepository fakeApplicationRepository = A.Fake <IApplicationRepository>();

            A.CallTo(() => fakeApplicationRepository.TryGetByDisplayNameAsync("TestApplicationDisplayName")).Returns(Task.FromResult <ApplicationEntity>(null));

            IUnitOfWork fakeUnitOfWork = A.Fake <IUnitOfWork>();

            IPasswordWithSaltHasher fakePasswordHasher = A.Fake <IPasswordWithSaltHasher>();

            IApplicationService applicationService = new ApplicationService(fakeApplicationRepository, fakeUnitOfWork, fakePasswordHasher);

            // Act
            ApplicationDto applicationDto = await applicationService.FindApplicationAsync("TestApplicationId", "TestApplicationSecret");

            // Assert
            Assert.IsNull(applicationDto);
        }
        public void WhenAddingApplicationWithEmptyPasswordThenExceptionIsThrown()
        {
            // Arrange
            IApplicationRepository  fakeApplicationRepository = A.Fake <IApplicationRepository>();
            IUnitOfWork             fakeUnitOfWork            = A.Fake <IUnitOfWork>();
            IPasswordWithSaltHasher fakePasswordHasher        = A.Fake <IPasswordWithSaltHasher>();
            IApplicationService     applicationService        = new ApplicationService(fakeApplicationRepository, fakeUnitOfWork, fakePasswordHasher);

            ApplicationCreateDto applicationCreateDto = new ApplicationCreateDto()
            {
                DisplayName = "TestApplicationDisplayName",
                Password    = ""
            };

            // Act
            ArgumentException ex = Assert.CatchAsync <ArgumentException>(async() => await applicationService.AddApplicationAsync(applicationCreateDto));

            // Assert
            StringAssert.Contains("Application name and password must be provided.", ex.Message);
        }