def test_updates_issue_status_based_on_pr_created_event_dependencies(
            self, verify_mock, requests_mock, dependency_state_mock,
            get_sha_mock):
        verify_mock.return_value = True, {}
        dependency_state_mock.return_value = 'closed'
        get_sha_mock.return_value = "dummy-sha"

        payload = PR_CREATED.replace("This is the PR body",
                                     "This is the PR body. Depends on #2.")

        sha = "dummy-sha"

        response = self.client.post("/webhook",
                                    headers=self.GITHUB_HEADERS,
                                    data=payload,
                                    content_type='application/json')
        self.assertEqual(status.HTTP_201_CREATED, response.status_code)

        expected_url = "{}repos/{}/{}/statuses/{}".format(
            pierre.BASE_GITHUB_URL, "alvarocavalcanti", "pierre-decheck", sha)

        headers = {'Authorization': 'Token '}

        expected_data = {
            "state": "success",
            "target_url": pierre.TARGET_URL.format('localhost', '2:closed'),
            "description": "All dependencies are met: (2: closed)",
            "context": pierre.CONTEXT
        }

        requests_mock.assert_any_call('POST',
                                      expected_url,
                                      headers=headers,
                                      data=json.dumps(expected_data))
    def test_checks_dependencies_upon_receiving_pr_created_event(
            self, requests_mock):
        payload = json.loads(
            PR_CREATED.replace("This is the PR body",
                               "This is the PR body. Depends on #2."))

        response = check(payload, headers=GITHUB_HEADERS, host=HOST)

        assert 201 == response.get("statusCode")

        expected_url = "{}repos/{}/{}/issues/{}".format(
            BASE_GITHUB_URL, "alvarocavalcanti", "pierre-decheck", "2")

        requests_mock.assert_any_call('GET', expected_url, headers=HEADERS)
    def test_checks_external_dependencies_upon_receiving_pr_created_event(
            self, requests_mock):
        payload = PR_CREATED.replace(
            "This is the PR body",
            "This is the PR body. Depends on foo-owner/foo-repo#2.")

        response = self.client.post("/webhook",
                                    headers=self.GITHUB_HEADERS,
                                    data=payload,
                                    content_type='application/json')
        self.assertEqual(status.HTTP_201_CREATED, response.status_code)

        expected_url = "{}repos/{}/{}/issues/{}".format(
            pierre.BASE_GITHUB_URL, "foo-owner", "foo-repo", "2")

        requests_mock.assert_any_call('GET',
                                      expected_url,
                                      headers=request_headers)
    def failing_test_checks_dependencies_upon_receiving_pr_created_event_for_more_than_one_dependency(
            self, requests_mock):
        payload = PR_CREATED.replace(
            "This is the PR body",
            "This is the PR body. Depends on #2. Depends on #3.")

        response = self.client.post("/webhook",
                                    headers=self.GITHUB_HEADERS,
                                    data=payload,
                                    content_type='application/json')
        self.assertEqual(status.HTTP_201_CREATED, response.status_code)

        expected_url_dep_2 = "{}repos/{}/{}/issues/{}".format(
            pierre.BASE_GITHUB_URL, "alvarocavalcanti", "pierre-decheck", "2")

        expected_url_dep_3 = "{}repos/{}/{}/issues/{}".format(
            pierre.BASE_GITHUB_URL, "alvarocavalcanti", "pierre-decheck", "3")

        requests_mock.assert_any_call('GET',
                                      expected_url_dep_2,
                                      headers=request_headers)
        requests_mock.assert_any_call('GET',
                                      expected_url_dep_3,
                                      headers=request_headers)