def test_github_api_get_file_download_when_file_does_not_exist_raises_GitObjectNotFound(
        m_get_repo):
    class MockProject:
        def get_branch(self, name):
            return Mock()

        def get_file_contents(self, filepath, ref):
            raise UnknownObjectException(404, data={'message': 'Not Found'})

    m_get_repo.return_value = MockProject()

    api = GithubAPI('github.com', 'owner', 'repository', 'my-branch')

    with raises(GitObjectNotFound):
        api.get_file_download('README.md')
def test_github_api_get_file_download_when_filepath_is_a_directory_raises_GitError(
        m_get_repo):
    class MockProject:
        def get_branch(self, name):
            return Mock()

        def get_file_contents(self, filepath, ref):
            assert filepath == 'dir'
            return [Mock(path="dir/a.txt"), Mock(path='dir/b.txt')]

    m_get_repo.return_value = MockProject()

    api = GithubAPI('github.com', 'owner', 'repository', 'my-branch')

    with raises(GitError):
        api.get_file_download('dir')
def test_github_api_get_file_download(m_get_repo):
    class MockProject:
        def get_branch(self, name):
            mock = Mock()
            mock.name = name
            mock.commit.sha = 'mybranchsha'
            return mock

        def get_file_contents(self, filepath, ref):
            assert filepath == 'README.md'
            assert ref == 'mybranchsha'
            return Mock(download_url='https://raw.githubusercontent.com/owner/'
                        'repository/mybranchsha/README.md',
                        size=21)

    m_get_repo.return_value = MockProject()

    api = GithubAPI('github.com', 'owner', 'repository', 'my-branch')

    assert api.get_file_download('README.md') == (
        'https://raw.githubusercontent.com/owner/repository'
        '/mybranchsha/README.md', 21)