Example #1
0
        public async Task ActivateAsync_NotActivated_ShouldCreateServiceHooksAndConfiguration()
        {
            var collectionId     = Guid.NewGuid().ToString();
            var projectId        = Guid.NewGuid().ToString();
            var repositoryId     = Guid.NewGuid().ToString();
            var statusPolicyName = "test-status-policy";

            var configuration = new AccountConfiguration
            {
                BaseUrl      = "https://fabrikam.visualstudio.com/DefaultCollection",
                CollectionId = collectionId
            };

            var serviceHookId1 = Guid.NewGuid();
            var serviceHookId2 = Guid.NewGuid();

            var serviceHookClientMock = new Mock <ServiceHooksPublisherHttpClient>(new object[]
            {
                new Uri("https://fabrikam.visualstudio.com/DefaultCollection"),
                (VssCredentials) new VssBasicCredential()
            });

            serviceHookClientMock
            .SetupSequence(m => m.CreateSubscriptionAsync(It.IsAny <Subscription>(), It.IsAny <object>()))
            .ReturnsAsync(new Subscription {
                Id = serviceHookId1
            })
            .ReturnsAsync(new Subscription {
                Id = serviceHookId2
            });

            var configurationRepositoryMock = new Mock <IConfigurationRepository>();

            configurationRepositoryMock
            .Setup(m => m.GetAsync(collectionId))
            .ReturnsAsync(configuration);

            var connectionFactoryMock = new Mock <IVssConnectionFactory>();

            connectionFactoryMock
            .Setup(m => m.CreateFactory(It.IsAny <Uri>(), It.IsAny <VssCredentials>()))
            .Returns(connectionFactoryMock.Object);

            connectionFactoryMock
            .Setup(m => m.GetClientAsync <ServiceHooksPublisherHttpClient>())
            .ReturnsAsync(serviceHookClientMock.Object);

            var statusPoliciesMock = new Mock <IStatusPoliciesService>();

            var pullRequestService = new PullRequestService(
                new Uri("https://aitpullrequests.azurewebsites.net/"),
                connectionFactoryMock.Object,
                statusPoliciesMock.Object,
                configurationRepositoryMock.Object);

            await pullRequestService.ActivateStatusPolicyAsync(collectionId, projectId, repositoryId, statusPolicyName);

            configurationRepositoryMock
            .Verify(m => m.GetAsync(collectionId), Times.Once);

            serviceHookClientMock
            .Verify(m => m.CreateSubscriptionAsync(It.IsAny <Subscription>(), It.IsAny <object>()), Times.Exactly(2));

            configurationRepositoryMock
            .Verify(m => m.UpdateAsync(
                        It.Is <AccountConfiguration>(c =>
                                                     c.GetServiceHookIds(projectId).First() == serviceHookId1.ToString() &&
                                                     c.GetServiceHookIds(projectId).Last() == serviceHookId2.ToString())), Times.Once);
        }