Exemplo n.º 1
0
 def test_message_is_empty(self):
     """Verify instance message is correct."""
     response = requests.Response()
     response.status_code = 400
     response.raw = io.BytesIO()
     error = GitHubError(response)
     assert error.message == "[No message]"
Exemplo n.º 2
0
    def test_get_session__authenticate_with_exceptions(
            self, mock_path, mock_prompt, mock_getpass, mock_authorize):
        mock_path.isfile.return_value = False

        mock_response = MockResponse(content='', status_code=401)
        mock_authorize.side_effect = GitHubError(mock_response)
        self.assertRaises(exceptions.FarcyException, helpers.get_session)

        self.assertTrue(mock_prompt.called)
        self.assertTrue(mock_getpass.called)

        mock_response = MockResponse(content='', status_code=101)
        mock_authorize.side_effect = GitHubError(mock_response)
        self.assertRaises(GitHubError, helpers.get_session)

        mock_authorize.side_effect = TypeError
        self.assertRaises(TypeError, helpers.get_session)
Exemplo n.º 3
0
 def test_delete_hook_error(self, mock_delete_hook):
     self.node_settings.hook_id = 'hook'
     self.node_settings.save()
     mock_delete_hook.side_effect = GitHubError(mock.Mock())
     args = (
         self.node_settings.user,
         self.node_settings.repo,
         self.node_settings.hook_id,
     )
     res = self.node_settings.delete_hook()
     assert_false(res)
     mock_delete_hook.assert_called_with(*args)
Exemplo n.º 4
0
    def test_get_session__from_credentials_file__handled_exception(
            self, mock_path, mock_open, mock_is_starred, mock_stderr,
            mock_prompt, mock_getpass, mock_authorize):
        mock_path.expanduser.return_value = 'mock_path'
        mock_path.isfile.return_value = True
        mock_open.return_value = MagicMock(spec=IOBase)

        mock_response = MockResponse(content='', status_code=401)
        mock_is_starred.side_effect = GitHubError(mock_response)
        self.assertTrue(isinstance(helpers.get_session(), GitHub))
        self.assertTrue(mock_stderr.write.called)
        self.assertTrue(mock_prompt.called)
        self.assertTrue(mock_getpass.called)
        self.assertTrue(mock_open.called)
Exemplo n.º 5
0
def test_create_existing_tag_at_end_rollback_failure():

    repo_objs = {
        repo_name: mock_repository(repo_name)
        for repo_name in expected_commits
    }

    repo_objs['edx/edx-platform'].create_ref.side_effect = ALREADY_EXISTS
    # when we try to delete the configuration tag, it will fail with a 500 error
    repo_objs[
        'edx/configuration'].create_ref.return_value.delete.side_effect = GitHubError(
            Mock(status_code=500))

    # creating a tag that already exists in edx-platform: we'll make sure
    # that edx-platform is attempted *last*
    ordered_commits = OrderedDict(
        (repo_objs[repo_name], expected_commits[repo_name])
        for repo_name in ('edx/configuration', 'edx/edx-platform'))

    with pytest.raises(RuntimeError) as excinfo:
        result = create_ref_for_repos(
            ordered_commits,
            "tag-exists-some-repos",
            dry=False,
        )

    repo_objs['edx/edx-platform'].create_ref.assert_called_once_with(
        sha="deadbeef12345",
        ref="refs/tags/tag-exists-some-repos",
    )

    repo_objs['edx/configuration'].create_ref.assert_called_once_with(
        sha="12345deadbeef",
        ref="refs/tags/tag-exists-some-repos",
    )

    # second response failed, so try to rollback.
    # ... but configuration fails, so we get an exception
    repo_objs[
        'edx/configuration'].create_ref.return_value.delete.assert_called_once_with(
        )
    assert "failed to delete ref on the following repos: edx/configuration" in str(
        excinfo)
Exemplo n.º 6
0
def test_remove_with_errors():
    repos = [
        Mock(spec=Repository, full_name="edx/edx-platform"),
        Mock(spec=Repository, full_name="edx/configuration"),
    ]

    # when we try to get the edx-platform tag, it will fail with a 500 error
    repos[0].ref.side_effect = GitHubError(Mock(status_code=500))

    with pytest.raises(TagReleaseError) as excinfo:
        remove_ref_for_repos(repos, "tag-exists-all-repos", dry=False)

    for repo in repos:
        repo.ref.assert_called_once_with('tags/tag-exists-all-repos')

    for repo in repos[1:]:
        ref = repo.ref.return_value
        ref.delete.assert_called_once_with()

    assert "Failed to remove the ref from the following repos: edx/edx-platform" in str(
        excinfo)
Exemplo n.º 7
0
def test_create_existing_tag_at_end_rollback_failure(expected_commits):
    # Creating a tag that already exists in edx-platform: we'll make sure
    # that edx-platform is attempted *last* so that other repos will have
    # been touched first.
    ordered_commits = OrderedDict([
        find_repo_item(expected_commits, 'edx/configuration'),
        find_repo_item(expected_commits, 'edx/edx-platform'),
    ])

    edx_edx_platform = find_repo(ordered_commits, 'edx/edx-platform')
    edx_edx_platform.create_ref.side_effect = ALREADY_EXISTS
    # When we try to delete the configuration tag, it will fail with a 500 error
    edx_configuration = find_repo(ordered_commits, 'edx/configuration')
    edx_configuration.create_ref.return_value.delete.side_effect = GitHubError(
        Mock(status_code=500))

    with pytest.raises(TagReleaseError) as excinfo:
        create_ref_for_repos(
            ordered_commits,
            "tag-exists-some-repos",
            dry=False,
        )

    edx_edx_platform.create_ref.assert_called_once_with(
        sha="deadbeef12345",
        ref="refs/tags/tag-exists-some-repos",
    )

    edx_configuration.create_ref.assert_called_once_with(
        sha="12345deadbeef",
        ref="refs/tags/tag-exists-some-repos",
    )

    # Second response failed, so try to rollback.
    # ... but configuration fails, so we get an exception
    edx_configuration.create_ref.return_value.delete.assert_called_once_with()
    assert "failed to delete ref on the following repos: edx/configuration" in str(
        excinfo)
Exemplo n.º 8
0
 def setUp(self):
     response = requests.Response()
     response.status_code = 400
     message = b'{"message": "m", "errors": ["e"]}'
     response.raw = io.BytesIO(message)
     self.instance = GitHubError(response)
Exemplo n.º 9
0
"""Tests of tag_release.py"""

from collections import OrderedDict

from github3 import GitHubError
from github3.exceptions import NotFoundError
from github3.repos.repo import Repository
from unittest.mock import Mock
import pytest

from edx_repo_tools.release.tag_release import (
    get_ref_for_repos, commit_ref_info, create_ref_for_repos,
    remove_ref_for_repos, override_repo_refs, TagReleaseError)

ALREADY_EXISTS = GitHubError(
    Mock(status_code=422,
         json=Mock(return_value={"message": "Reference already exists"})))


class FakeNotFoundError(NotFoundError):
    """A NotFoundError with enough content to please the code."""
    status_code = 404
    content = "A fake response"

    def __init__(self):
        super().__init__(self)


def mock_repository(name, has_refs=False):
    """Make a mock Repository object.