Exemplo n.º 1
0
async def test_github_auth_headers():
    """github_auth_headers should have appropriate headers for autentication"""
    github_access_token = "access"
    assert github_auth_headers(github_access_token) == {
        "Authorization": f"Bearer {github_access_token}",
        "Accept": "application/vnd.github.v3+json",
    }
Exemplo n.º 2
0
def test_github_auth_headers():
    """github_auth_headers should have appropriate headers for autentication"""
    github_access_token = 'access'
    assert github_auth_headers(github_access_token) == {
        "Authorization": "Bearer {}".format(github_access_token),
        "Accept": "application/vnd.github.v3+json",
    }
Exemplo n.º 3
0
async def test_get_pull_request(mocker, all_prs, has_pr):
    """get_pull_request should fetch a pull request from GitHub's API"""
    org = "org"
    repo = "repo"
    access_token = "access"
    branch = "release-candidate"

    get_mock = mocker.async_patch(
        "client_wrapper.ClientWrapper.get",
        return_value=mocker.Mock(json=mocker.Mock(
            return_value=[RELEASE_PR] if has_pr else [])),
    )
    response = await get_pull_request(
        github_access_token=access_token,
        org=org,
        repo=repo,
        branch=branch,
        all_prs=all_prs,
    )
    assert response == (RELEASE_PR if has_pr else None)
    get_mock.return_value.raise_for_status.assert_called_once_with()
    state = "all" if all_prs else "open"
    get_mock.assert_called_once_with(
        mocker.ANY,
        f"https://api.github.com/repos/{org}/{repo}/pulls?state={state}&head={org}:{branch}&per_page=1",
        headers=github_auth_headers(access_token),
    )
Exemplo n.º 4
0
async def test_add_label(mocker):
    """add_label should add a new label on a pr"""
    response = mocker.Mock()
    patched = mocker.async_patch("client_wrapper.ClientWrapper.post",
                                 return_value=response)
    token = "token"
    org = "mitodl"
    repo = "release-script"
    repo_url = f"[email protected]:{org}/{repo}.git"
    pr_number = 1234
    label = "new label"

    await add_label(
        github_access_token=token,
        repo_url=repo_url,
        pr_number=pr_number,
        label=label,
    )
    patched.assert_called_once_with(
        mocker.ANY,
        f"https://api.github.com/repos/{org}/{repo}/issues/{pr_number}/labels",
        json={"labels": [label]},
        headers=github_auth_headers(token),
    )
    response.raise_for_status.assert_called_once_with()
Exemplo n.º 5
0
def test_get_status_of_pr(status_code, status_data, expected_status):
    """get_status_of_pr should get the status of a PR"""

    org = 'org'
    repo = 'repo'
    token = 'token'
    branch = 'branch'

    with patch('requests.get', autospec=True) as patched:
        resp = patched.return_value
        resp.status_code = status_code
        resp.json.return_value = status_data
        assert get_status_of_pr(
            github_access_token=token,
            org=org,
            repo=repo,
            branch=branch,
        ) == expected_status

    if status_code != 404:
        resp.raise_for_status.assert_called_once_with()

    endpoint = "https://api.github.com/repos/{org}/{repo}/commits/{ref}/statuses".format(
        org=org,
        repo=repo,
        ref=branch,
    )
    patched.assert_called_once_with(endpoint,
                                    headers=github_auth_headers(token))
Exemplo n.º 6
0
async def test_get_issue(mocker):
    """get_issue should fetch an issue via the REST API"""
    issue = make_issue(12345)
    token = "token"
    issue_json = {
        "title": issue.title,
        "number": issue.number,
        "state": issue.status,
        "updated_at": issue.updatedAt.isoformat(),
        "html_url": issue.url,
    }
    response = mocker.Mock(json=mocker.Mock(return_value=issue_json))
    patched = mocker.async_patch('client_wrapper.ClientWrapper.get',
                                 return_value=response)

    assert await get_issue(
        github_access_token=token,
        org=TEST_ORG,
        repo=TEST_REPO,
        issue_number=issue.number,
    ) == issue
    patched.assert_called_once_with(
        mocker.ANY,
        f"https://api.github.com/repos/{TEST_ORG}/{TEST_REPO}/issues/{issue.number}",
        headers=github_auth_headers(token),
    )
Exemplo n.º 7
0
async def test_get_status_of_pr(mocker, status_code, status_data,
                                expected_status):
    """get_status_of_pr should get the status of a PR"""

    org = 'org'
    repo = 'repo'
    token = 'token'
    branch = 'branch'

    patched = mocker.async_patch('client_wrapper.ClientWrapper.get')
    resp = patched.return_value
    resp.status_code = status_code
    resp.json.return_value = status_data
    assert await get_status_of_pr(
        github_access_token=token,
        org=org,
        repo=repo,
        branch=branch,
    ) == expected_status

    if status_code != 404:
        resp.raise_for_status.assert_called_once_with()

    endpoint = "https://api.github.com/repos/{org}/{repo}/commits/{ref}/statuses".format(
        org=org,
        repo=repo,
        ref=branch,
    )
    patched.assert_called_once_with(mocker.ANY,
                                    endpoint,
                                    headers=github_auth_headers(token))
Exemplo n.º 8
0
def test_get_release_pr():
    """get_release_pr should grab a release from GitHub's API"""
    org = 'org'
    repo = 'repo'
    access_token = 'access'

    with patch('github.requests.get', return_value=Mock(json=Mock(return_value=FAKE_PULLS))) as get_mock:
        pr = get_release_pr(
            github_access_token=access_token,
            org=org,
            repo=repo,
        )
    get_mock.assert_called_once_with("https://api.github.com/repos/{org}/{repo}/pulls".format(
        org=org,
        repo=repo,
    ), headers=github_auth_headers(access_token))
    assert pr.body == RELEASE_PR['body']
    assert pr.url == RELEASE_PR['html_url']
    assert pr.version == '0.53.3'
Exemplo n.º 9
0
async def test_get_issue_but_its_a_pr(mocker):
    """get_issue should return None if it's actually fetching a PR"""
    issue_json = {"pull_request": "some pull request info"}
    response = mocker.Mock(json=mocker.Mock(return_value=issue_json))
    patched = mocker.async_patch('client_wrapper.ClientWrapper.get',
                                 return_value=response)
    token = "token"

    assert await get_issue(
        github_access_token=token,
        org=TEST_ORG,
        repo=TEST_REPO,
        issue_number=1234,
    ) is None
    patched.assert_called_once_with(
        mocker.ANY,
        f"https://api.github.com/repos/{TEST_ORG}/{TEST_REPO}/issues/1234",
        headers=github_auth_headers(token),
    )
Exemplo n.º 10
0
async def test_get_release_pr(mocker):
    """get_release_pr should grab a release from GitHub's API"""
    org = 'org'
    repo = 'repo'
    access_token = 'access'

    get_mock = mocker.async_patch('client_wrapper.ClientWrapper.get', return_value=mocker.Mock(
        json=mocker.Mock(return_value=FAKE_PULLS)
    ))
    pr = await get_release_pr(
        github_access_token=access_token,
        org=org,
        repo=repo,
    )
    get_mock.assert_called_once_with(mocker.ANY, "https://api.github.com/repos/{org}/{repo}/pulls".format(
        org=org,
        repo=repo,
    ), headers=github_auth_headers(access_token))
    assert pr.body == RELEASE_PR['body']
    assert pr.url == RELEASE_PR['html_url']
    assert pr.version == '0.53.3'
Exemplo n.º 11
0
async def test_delete_label(mocker, status, expected_raise_for_status):
    """delete_label should remove a label from a pr"""
    response = mocker.Mock(status_code=status)
    patched = mocker.async_patch("client_wrapper.ClientWrapper.delete",
                                 return_value=response)
    token = "token"
    org = "mitodl"
    repo = "release-script"
    repo_url = f"[email protected]:{org}/{repo}.git"
    pr_number = 1234
    label = "existing label"

    await delete_label(
        github_access_token=token,
        repo_url=repo_url,
        pr_number=pr_number,
        label=label,
    )
    patched.assert_called_once_with(
        mocker.ANY,
        f"https://api.github.com/repos/{org}/{repo}/issues/{pr_number}/labels/{quote(label)}",
        headers=github_auth_headers(token),
    )
    assert response.raise_for_status.called is expected_raise_for_status
Exemplo n.º 12
0
async def test_get_labels(mocker):
    """get_labels should retrieve labels from github"""
    response = mocker.Mock(json=mocker.Mock(
        return_value=[NEEDS_REVIEW_LABEL_JSON, TESTING_LABEL_JSON]))
    patched = mocker.async_patch("client_wrapper.ClientWrapper.get",
                                 return_value=response)
    token = "token"
    org = "mitodl"
    repo = "release-script"
    repo_url = f"[email protected]:{org}/{repo}.git"
    pr_number = 1234

    assert await get_labels(github_access_token=token,
                            repo_url=repo_url,
                            pr_number=pr_number) == [
                                NEEDS_REVIEW_LABEL_JSON["name"],
                                TESTING_LABEL_JSON["name"]
                            ]
    patched.assert_called_once_with(
        mocker.ANY,
        f"https://api.github.com/repos/{org}/{repo}/issues/{pr_number}/labels",
        headers=github_auth_headers(token),
    )
    response.raise_for_status.assert_called_once_with()