public async Task GetRepositories_NoPullDogSettingsFound_ReturnsNotFound()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Any <EnsureUserForIdentityCommand>())
            .Returns(new User());

            var fakeMapper = Substitute.For <IMapper>();
            var fakeAesEncryptionHelper = Substitute.For <IAesEncryptionHelper>();

            var controller = new PullDogApiController(
                fakeMediator,
                fakeMapper,
                fakeAesEncryptionHelper);

            controller.FakeAuthentication("some-identity-name");

            //Act
            var result = await controller.GetRepositories() as NotFoundObjectResult;

            //Assert
            Assert.IsNotNull(result);
        }
        public async Task GetSettings_NoPullDogSettingsFound_IsInstalledIsFalseAndValuesAreNull()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Any <EnsureUserForIdentityCommand>())
            .Returns(new User());

            var fakeMapper = Substitute.For <IMapper>();
            var fakeAesEncryptionHelper = Substitute.For <IAesEncryptionHelper>();

            var controller = new PullDogApiController(
                fakeMediator,
                fakeMapper,
                fakeAesEncryptionHelper);

            controller.FakeAuthentication("some-identity-name");

            //Act
            var result = await controller.GetSettings() as OkObjectResult;

            //Assert
            Assert.IsNotNull(result);

            var response = result.ToObject <SettingsResponse>();

            Assert.IsNull(response.PoolSize);
            Assert.IsNull(response.PlanId);
            Assert.IsNull(response.ApiKey);
            Assert.IsFalse(response.IsInstalled);
        }
        public async Task ChangePlan_PullDogNotInstalledByUser_ReturnsBadRequest()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Is <EnsureUserForIdentityCommand>(args =>
                                                        args.IdentityName == "some-identity-name"))
            .Returns(new User()
            {
                PullDogSettings = null
            });

            var fakeMapper = Substitute.For <IMapper>();
            var fakeAesEncryptionHelper = Substitute.For <IAesEncryptionHelper>();

            var controller = new PullDogApiController(
                fakeMediator,
                fakeMapper,
                fakeAesEncryptionHelper);

            controller.FakeAuthentication("some-identity-name");

            //Act
            var result = await controller.ChangePlan(new ChangePlanRequest()
            {
                PlanId   = "dummy",
                PoolSize = 1337
            }) as BadRequestObjectResult;

            //Assert
            Assert.IsNotNull(result);
        }
        public async Task GetSettings_PullDogSettingsFound_IsInstalledIsTrueAndValuesAreSet()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Any <EnsureUserForIdentityCommand>())
            .Returns(new User()
            {
                PullDogSettings = new PullDogSettings()
                {
                    PoolSize        = 1337,
                    PlanId          = "some-plan-id",
                    EncryptedApiKey = new byte[] { 1 }
                }
            });

            var fakeMapper = Substitute.For <IMapper>();

            var fakeAesEncryptionHelper = Substitute.For <IAesEncryptionHelper>();

            fakeAesEncryptionHelper
            .DecryptAsync(Arg.Is <byte[]>(args => args.Single() == 1))
            .Returns("some-api-key");

            var controller = new PullDogApiController(
                fakeMediator,
                fakeMapper,
                fakeAesEncryptionHelper);

            controller.FakeAuthentication("some-identity-name");

            //Act
            var result = await controller.GetSettings() as OkObjectResult;

            //Assert
            Assert.IsNotNull(result);

            var response = result.ToObject <SettingsResponse>();

            Assert.IsTrue(response.IsInstalled);
            Assert.AreEqual(1337, response.PoolSize);
            Assert.AreEqual("some-plan-id", response.PlanId);
            Assert.AreEqual("some-api-key", response.ApiKey);
        }
        public async Task ChangePlan_ConditionsPassed_ChangesPullDogPlan()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Is <EnsureUserForIdentityCommand>(args =>
                                                        args.IdentityName == "some-identity-name"))
            .Returns(new User()
            {
                PullDogSettings = new PullDogSettings()
            });

            var fakeMapper = Substitute.For <IMapper>();
            var fakeAesEncryptionHelper = Substitute.For <IAesEncryptionHelper>();

            var controller = new PullDogApiController(
                fakeMediator,
                fakeMapper,
                fakeAesEncryptionHelper);

            controller.FakeAuthentication("some-identity-name");

            //Act
            var result = await controller.ChangePlan(new ChangePlanRequest()
            {
                PlanId   = "some-plan-id",
                PoolSize = 1337
            }) as OkResult;

            //Assert
            Assert.IsNotNull(result);

            await fakeMediator
            .Received(1)
            .Send(Arg.Is <ChangePullDogPlanCommand>(args =>
                                                    args.PoolSize == 1337 &&
                                                    args.PlanId == "some-plan-id"));
        }
        public async Task GetRepositories_PullDogSettingsFound_ReturnsMappedRepositories()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Any <EnsureUserForIdentityCommand>())
            .Returns(new User()
            {
                PullDogSettings = new PullDogSettings()
            });

            fakeMediator
            .Send(Arg.Any <GetRepositoriesForUserQuery>())
            .Returns(new [] {
                new UserRepositoryResponse()
            });

            var fakeMapper = Substitute.For <IMapper>();
            var fakeAesEncryptionHelper = Substitute.For <IAesEncryptionHelper>();

            var controller = new PullDogApiController(
                fakeMediator,
                fakeMapper,
                fakeAesEncryptionHelper);

            controller.FakeAuthentication("some-identity-name");

            //Act
            var result = await controller.GetRepositories() as OkObjectResult;

            //Assert
            Assert.IsNotNull(result);

            var response = result.ToObject <RepositoriesResponse>();

            Assert.AreEqual(1, response.Repositories.Length);
        }