def test_client_installation_HTTP_500(httpserver):
    httpserver.expect_request("/repos/owner/repo/installation").respond_with_data(
        "This is an 5XX error", status=500
    )
    with mock.patch(
        "mergify_engine.config.GITHUB_API_URL",
        httpserver.url_for("/")[:-1],
    ):
        with github.GithubInstallationClient(
            github.get_auth("owner", "repo")
        ) as client:
            with pytest.raises(http.HTTPServerSideError) as exc_info:
                client.get(httpserver.url_for("/"))

    # 5 retries
    assert len(httpserver.log) == 5

    assert exc_info.value.message == "This is an 5XX error"
    assert exc_info.value.status_code == 500
    assert exc_info.value.response.status_code == 500
    assert str(exc_info.value.request.url) == httpserver.url_for(
        "/repos/owner/repo/installation"
    )

    httpserver.check_assertions()
Beispiel #2
0
def test_client_user_token(httpserver: httpserver.HTTPServer) -> None:
    with mock.patch(
            "mergify_engine.config.GITHUB_API_URL",
            httpserver.url_for("/")[:-1],
    ):
        httpserver.expect_request("/users/owner").respond_with_json({
            "login":
            "******",
            "id":
            12345,
        })
        httpserver.expect_request("/",
                                  headers={
                                      "Authorization": "token <user-token>"
                                  }).respond_with_json({"work": True},
                                                       status=200)

        with github.GithubInstallationClient(
                github.get_auth("owner")) as client:
            ret = client.get(
                httpserver.url_for("/"),
                oauth_token="<user-token>")  # type: ignore[call-arg]
            assert ret.json()["work"]

    assert len(httpserver.log) == 2
Beispiel #3
0
def test_client_installation_HTTP_404(httpserver):
    httpserver.expect_request("/users/owner/installation").respond_with_json(
        {"message": "Repository not found"}, status=404)
    with mock.patch(
            "mergify_engine.config.GITHUB_API_URL",
            httpserver.url_for("/")[:-1],
    ):
        with github.GithubInstallationClient(
                github.get_auth("owner")) as client:
            with pytest.raises(exceptions.MergifyNotInstalled):
                client.get(httpserver.url_for("/"))

    assert len(httpserver.log) == 1

    httpserver.check_assertions()
Beispiel #4
0
def test_client_abuse_403_no_header(httpserver: httpserver.HTTPServer) -> None:

    abuse_message = ("You have triggered an abuse detection mechanism. "
                     "Please wait a few minutes before you try again.")
    httpserver.expect_request("/users/owner/installation").respond_with_json({
        "id":
        12345,
        "target_type":
        "User",
        "permissions": {
            "checks": "write",
            "contents": "write",
            "pull_requests": "write",
        },
        "account": {
            "login": "******",
            "id": 12345
        },
    })
    httpserver.expect_request(
        "/app/installations/12345/access_tokens").respond_with_json({
            "token":
            "<token>",
            "expires_at":
            "2100-12-31T23:59:59Z"
        })

    httpserver.expect_oneshot_request("/").respond_with_json(
        {"message": abuse_message},
        status=403,
    )

    with mock.patch(
            "mergify_engine.config.GITHUB_API_URL",
            httpserver.url_for("/")[:-1],
    ):
        with github.GithubInstallationClient(
                github.get_auth("owner")) as client:
            with pytest.raises(http.HTTPClientSideError) as exc_info:
                client.get(httpserver.url_for("/"))

    assert exc_info.value.message == abuse_message
    assert exc_info.value.status_code == 403
    assert exc_info.value.response.status_code == 403
    assert str(exc_info.value.request.url) == httpserver.url_for("/")
    assert len(httpserver.log) == 3

    httpserver.check_assertions()
Beispiel #5
0
def test_client_installation_token(httpserver):
    with mock.patch(
            "mergify_engine.config.GITHUB_API_URL",
            httpserver.url_for("/")[:-1],
    ):
        httpserver.expect_request(
            "/users/owner/installation").respond_with_json({
                "id": 12345,
                "target_type": "User",
                "permissions": {
                    "checks": "write",
                    "contents": "write",
                    "pull_requests": "write",
                },
                "account": {
                    "login": "******",
                    "id": 12345
                },
            })
        httpserver.expect_request(
            "/app/installations/12345/access_tokens").respond_with_json({
                "token":
                "<app_token>",
                "expires_at":
                "2100-12-31T23:59:59Z"
            })

        httpserver.expect_request("/",
                                  headers={
                                      "Authorization":
                                      "token <installation-token>"
                                  }).respond_with_json({"work": True},
                                                       status=200)

        with github.GithubInstallationClient(
                github.get_auth("owner")) as client:
            ret = client.get(httpserver.url_for("/"))
            assert ret.json()["work"]

    assert len(httpserver.log) == 3

    httpserver.check_assertions()
Beispiel #6
0
def test_client_access_token_HTTP_500(
        httpserver: httpserver.HTTPServer) -> None:
    httpserver.expect_request("/users/owner/installation").respond_with_json({
        "id":
        12345,
        "target_type":
        "User",
        "permissions": {
            "checks": "write",
            "contents": "write",
            "pull_requests": "write",
        },
        "account": {
            "login": "******",
            "id": 12345
        },
    })
    httpserver.expect_request(
        "/app/installations/12345/access_tokens").respond_with_data(
            "This is an 5XX error", status=500)

    with mock.patch(
            "mergify_engine.config.GITHUB_API_URL",
            httpserver.url_for("/")[:-1],
    ):
        with github.GithubInstallationClient(
                github.get_auth("owner")) as client:
            with pytest.raises(http.HTTPServerSideError) as exc_info:
                client.get(httpserver.url_for("/"))

    # installation request + 5 retries
    assert len(httpserver.log) == 6

    assert exc_info.value.message == "This is an 5XX error"
    assert exc_info.value.status_code == 500
    assert exc_info.value.response.status_code == 500
    assert str(exc_info.value.request.url) == httpserver.url_for(
        "/app/installations/12345/access_tokens")

    httpserver.check_assertions()
Beispiel #7
0
 def github_client(owner=None, auth=None):
     return github.GithubInstallationClient(get_auth(owner, auth))