コード例 #1
0
    def test_event_not_allowed(
        self,
        patched_compare_signatures,
        rf: RequestFactory,
        settings,
        config: dict,
        expected_status_code: int,
        expected_response: dict,
    ):
        settings.DJANGO_GITHUB_WEBHOOKS = config
        patched_compare_signatures.return_value = True
        headers = {
            constants.Headers.SIGNATURE:
            "{digest}=12345".format(digest=constants.SHA1_DIGEST),
            constants.Headers.EVENT:
            constants.Events.ISSUES,
        }

        # Test signal send & payload type (should convert binary to dict)
        if expected_status_code == 200:

            def handle_issues_event(payload: dict, **kwargs) -> None:
                assert payload == {"test": "ok"}

            signals.issues.connect(handle_issues_event)

        # Create request and save view response
        request = rf.post("/fake-url/", **headers)
        request._body = '{"test": "ok"}'.encode()
        view = views.GitHubWebhookView(request=request)
        response = view.post(request)

        # Test response
        assert response.status_code == expected_status_code
        assert json.loads(response.content.decode()) == expected_response
コード例 #2
0
 def test_event_is_allowed_method(self, rf: RequestFactory, settings,
                                  config: dict, expected_is_allowed: bool):
     settings.DJANGO_GITHUB_WEBHOOKS = config
     headers = {constants.Headers.EVENT: constants.Events.ISSUES}
     request = rf.post("/fake-url/", **headers)
     view = views.GitHubWebhookView(request=request)
     assert view.event_is_allowed() == (expected_is_allowed,
                                        constants.Events.ISSUES)
コード例 #3
0
    def test_signature_header_is_missing(self, rf: RequestFactory):
        request = rf.post("/fake-url/")
        response: HttpResponse = views.GitHubWebhookView().post(request)

        assert response.status_code == 400
        assert json.loads(response.content.decode()) == {
            "detail": constants.X_HUB_SIGNATURE_HEADER_IS_MISSING
        }
コード例 #4
0
    def test_signature_header_is_invalid_split(self, rf: RequestFactory):
        headers = {
            constants.Headers.SIGNATURE: "invalid_header",
            constants.Headers.EVENT: "test",
        }
        request = rf.post("/fake-url/", **headers)
        response: HttpResponse = views.GitHubWebhookView().post(request)

        assert response.status_code == 400
        assert json.loads(response.content.decode()) == {
            "detail": constants.X_HUB_SIGNATURE_HEADER_IS_INVALID
        }
コード例 #5
0
    def test_event_header_is_missing(self, rf: RequestFactory):
        headers = {
            constants.Headers.SIGNATURE:
            "{digest}=12345".format(digest=constants.SHA1_DIGEST)
        }
        request = rf.post("/fake-url/", **headers)
        response: HttpResponse = views.GitHubWebhookView().post(request)

        assert response.status_code == 400
        assert json.loads(response.content.decode()) == {
            "detail": constants.X_GITHUB_EVENT_HEADER_IS_MISSING
        }
コード例 #6
0
    def test_digest_is_not_supported(self, rf: RequestFactory):
        digest = "not_supported_digest"
        headers = {
            constants.Headers.SIGNATURE:
            "{digest}=12345".format(digest=digest),
            constants.Headers.EVENT: "test",
        }
        request = rf.post("/fake-url/", **headers)
        response: HttpResponse = views.GitHubWebhookView().post(request)

        assert response.status_code == 400
        assert json.loads(response.content.decode()) == {
            "detail": constants.DIGEST_IS_NOT_SUPPORTED.format(digest=digest)
        }
コード例 #7
0
    def test_signature_value_is_invalid(self, rf: RequestFactory, settings):
        settings.DJANGO_GITHUB_WEBHOOKS = {"SECRET": "6789"}
        headers = {
            constants.Headers.SIGNATURE:
            "{digest}=12345".format(digest=constants.SHA1_DIGEST),
            constants.Headers.EVENT:
            "test",
        }
        request = rf.post("/fake-url/", **headers)
        response: HttpResponse = views.GitHubWebhookView().post(request)

        assert response.status_code == 400
        assert json.loads(response.content.decode()) == {
            "detail": constants.X_HUB_SIGNATURE_HEADER_IS_INVALID
        }
コード例 #8
0
 def test_get_secret_improperly_configured(self, settings):
     settings.DJANGO_GITHUB_WEBHOOKS = {}
     with pytest.raises(ImproperlyConfigured):
         views.GitHubWebhookView().get_secret()