Пример #1
0
async def test_status_code_handling(
    github_api: GitHubAPI,
    mock_response: MockResponse,
    status: HttpStatusCode,
    exception: GitHubException,
):

    mock_response.mock_status = status
    mock_response.mock_data = {
        "message": "Error",
        "documentation_url": "https://example.com",
    }

    with pytest.raises(exception):
        await github_api.generic("/generic")
Пример #2
0
async def test_wait_for_confirmation(
    github_device_api: GitHubDeviceAPI,
    mock_requests: MockedRequests,
    mock_response: MockResponse,
    caplog: pytest.CaptureFixture,
    asyncio_sleep: AsyncMock,
):
    mock_response.mock_data_list = [
        {
            "error": DeviceFlowError.AUTHORIZATION_PENDING,
            "error_description": "Pending user authorization",
        },
        {
            "error": DeviceFlowError.AUTHORIZATION_PENDING,
            "error_description": "Pending user authorization",
        },
        {
            "error": DeviceFlowError.AUTHORIZATION_PENDING,
            "error_description": "Pending user authorization",
        },
        {
            "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "token_type": "bearer",
            "scope": "user",
        },
    ]
    response = await github_device_api.activation(device_code=DEVICE_CODE)
    assert response.status == 200
    assert mock_requests.called == 4
    assert asyncio_sleep.call_count == 3
    assert asyncio_sleep.call_args_list[-1][0][0] == 1
    assert mock_requests.last_request[
        "url"] == "https://github.com/login/oauth/access_token"
    assert "Pending user authorization" in caplog.text
Пример #3
0
async def test_error_while_waiting(
    github_device_api: GitHubDeviceAPI,
    mock_response: MockResponse,
):
    mock_response.mock_data = {
        "error": "any",
        "error_description": "Any error message"
    }
    with pytest.raises(GitHubException, match="Any error message"):
        await github_device_api.activation(device_code=DEVICE_CODE)
Пример #4
0
async def test_zen(
    github_api: GitHubAPI,
    mock_response: MockResponse,
    mock_requests: MockedRequests,
):
    mock_response.mock_headers = {CONTENT_TYPE: HttpContentType.TEXT_PLAIN}
    response = await github_api.zen()
    assert response.status == 200
    assert response.data == "Beautiful is better than ugly."
    assert mock_requests.called == 1
    assert mock_requests.last_request["url"] == "https://api.github.com/zen"
Пример #5
0
async def test_response_object(github_api: GitHubAPI,
                               mock_response: MockResponse):
    response = await github_api.generic("/generic")
    assert response.etag == EXPECTED_ETAG
    assert response.page_number == 2
    assert response.next_page_number == 3
    assert response.last_page_number == 46
    assert not response.is_last_page

    mock_response.mock_headers = {"Link": None}
    response = await github_api.generic("/generic")
    assert response.is_last_page
    assert response.page_number == 1
    assert response.next_page_number is None
    assert response.last_page_number is None
    assert response.pages == {}

    mock_response.mock_headers = {"Link": ""}
    response = await github_api.generic("/generic")
    assert response.page_number == 1
Пример #6
0
async def test_unlock(
    github_api: GitHubAPI,
    mock_requests: MockedRequests,
    mock_response: MockResponse,
):
    mock_response.mock_status = 204
    response = await github_api.repos.issues.unlock(TEST_REPOSITORY_NAME, 1)
    assert response.status == 204
    assert response.data is None
    assert mock_requests.called == 1
    assert mock_requests.last_request["method"] == "delete"
    assert (mock_requests.last_request["url"] ==
            "https://api.github.com/repos/octocat/hello-world/issues/1/lock")
Пример #7
0
async def test_list_empty(
    github_api: GitHubAPI,
    mock_requests: MockedRequests,
    mock_response: MockResponse,
):
    mock_response.mock_data = []
    response = await github_api.repos.pulls.list(TEST_REPOSITORY_NAME)
    assert response.status == 200
    assert isinstance(response.data, list)
    assert len(response.data) == 0
    assert mock_requests.called == 1
    assert (mock_requests.last_request["url"] ==
            "https://api.github.com/repos/octocat/hello-world/pulls")
Пример #8
0
async def test_error_message_handling(
    github_api: GitHubAPI,
    mock_response: MockResponse,
    message: str,
    exception: GitHubException,
):

    mock_response.mock_data = {
        "message": message,
        "documentation_url": "https://example.com",
    }

    with pytest.raises(exception):
        await github_api.generic("/generic")
Пример #9
0
async def test_get(
    github_api: GitHubAPI,
    mock_requests: MockedRequests,
    mock_response: MockResponse,
):
    mock_response.mock_data = load_fixture(
        "repos_octocat_hello-world_issues.json",
        asjson=True,
        legacy=False,
    )[0]
    response = await github_api.repos.issues.get(TEST_REPOSITORY_NAME, 1)
    assert response.status == 200
    assert isinstance(response.data, GitHubIssueModel)
    assert response.data.title == "Test issue"
    assert mock_requests.called == 1
    assert (mock_requests.last_request["url"] ==
            "https://api.github.com/repos/octocat/hello-world/issues/1")
Пример #10
0
async def test_markdown_mode(
    github_api: GitHubAPI,
    mock_requests: MockedRequests,
    mock_response: MockResponse,
):
    mock_response.mock_headers = HEADERS_TEXT
    response = await github_api.markdown("Lorem ipsum...", mode="gfm")
    assert response.status == 200
    assert response.data == "Lorem ipsum..."
    assert mock_requests.called == 1
    assert mock_requests.last_request["method"] == "post"
    assert mock_requests.last_request[
        "url"] == "https://api.github.com/markdown"
    assert mock_requests.last_request["json"] == {
        "context": None,
        "mode": "gfm",
        "text": "Lorem ipsum...",
    }
Пример #11
0
async def test_update_comment(
    github_api: GitHubAPI,
    mock_requests: MockedRequests,
    mock_response: MockResponse,
):
    mock_response.mock_data = load_fixture(
        "repos_octocat_hello-world_issues_1_comments.json",
        asjson=True,
        legacy=False,
    )[0]
    response = await github_api.repos.issues.update_comment(
        TEST_REPOSITORY_NAME, 1, {"title": "test"})
    assert response.status == 200
    assert isinstance(response.data, GitHubIssueCommentModel)
    assert response.data.body == "No logs, no issue!"
    assert mock_requests.called == 1
    assert mock_requests.last_request["method"] == "patch"
    assert mock_requests.last_request["json"] == {"title": "test"}
    assert (
        mock_requests.last_request["url"] ==
        "https://api.github.com/repos/octocat/hello-world/issues/comments/1")
Пример #12
0
async def test_no_content_handling(github_api: GitHubAPI,
                                   mock_response: MockResponse):
    mock_response.mock_status = 204
    response = await github_api.generic("/generic")
    assert response.data is None
Пример #13
0
def mock_response():
    """Return a new mock response instanse."""
    yield MockResponse()