示例#1
0
    async def test_get_path_metadata_with_commit_sha(self, provider,
                                                     branch_metadata,
                                                     path_metadata_file):
        path = BitbucketPath('/file0001.20bytes.txt',
                             _ids=[(COMMIT_SHA, 'develop'),
                                   (COMMIT_SHA, 'develop')])

        assert path.commit_sha == COMMIT_SHA
        provider._fetch_branch_commit_sha = MockCoroutine(
            return_value=COMMIT_SHA)

        file_metadata = json.loads(path_metadata_file)['root']
        query_params = {
            'format': 'meta',
            'fields': 'commit.hash,commit.date,path,size,links.history.href'
        }
        path_meta_url = '{}/?{}'.format(
            provider._build_v2_repo_url('src', COMMIT_SHA, *path.path_tuple()),
            urlencode(query_params))
        aiohttpretty.register_json_uri('GET',
                                       path_meta_url,
                                       body=file_metadata)

        result = await provider._fetch_path_metadata(path)
        assert not provider._fetch_branch_commit_sha.called
    async def test_get_metadata_for_file(self, provider):
        base_ref = 'a1b2c3d4'
        path = BitbucketPath('/foo-file.txt', _ids=[(base_ref, 'develop'), (base_ref, 'develop')])

        test_fixtures = fixtures.validate_path
        dir_listing_body =  test_fixtures['root_dir_listing']
        dir_listing_url = provider._build_v1_repo_url('src', base_ref)  + '/'
        aiohttpretty.register_json_uri('GET', dir_listing_url, body=dir_listing_body)

        download_url = provider._build_v1_repo_url('raw', path.commit_sha, *path.path_tuple())
        aiohttpretty.register_uri('GET', download_url, body=b'better')

        result = await provider.download(path)
        content = await result.response.read()
        assert content == b'better'
示例#3
0
    async def test_get_metadata_for_folder(
        self,
        provider,
        path_metadata_folder,
        folder_contents_page_1,
        folder_contents_page_2,
    ):

        path = BitbucketPath('/', _ids=[(None, 'develop')], folder=True)

        provider._fetch_branch_commit_sha = MockCoroutine(
            return_value=COMMIT_SHA)

        folder_metadata = json.loads(path_metadata_folder)['root']
        query_params = {
            'format': 'meta',
            'fields': 'commit.hash,commit.date,path,size,links.history.href'
        }
        path_meta_url = '{}/?{}'.format(
            provider._build_v2_repo_url('src', COMMIT_SHA, *path.path_tuple()),
            urlencode(query_params))
        aiohttpretty.register_json_uri('GET',
                                       path_meta_url,
                                       body=folder_metadata)

        dir_contents_first_page = json.loads(folder_contents_page_1)
        query_params = {
            'pagelen': provider.RESP_PAGE_LEN,
            'fields': 'values.path,values.size,values.type,next',
        }
        dir_list_base_url = provider._build_v2_repo_url(
            'src', COMMIT_SHA, *path.path_tuple())
        dir_list_first_url = '{}/?{}'.format(dir_list_base_url,
                                             urlencode(query_params))
        aiohttpretty.register_json_uri('GET',
                                       dir_list_first_url,
                                       body=dir_contents_first_page)

        dir_contents_next_page = json.loads(folder_contents_page_2)
        dir_list_next_url = dir_contents_first_page['next']
        aiohttpretty.register_json_uri('GET',
                                       dir_list_next_url,
                                       body=dir_contents_next_page)

        result = await provider.metadata(path)
        assert provider._fetch_branch_commit_sha.called
        assert len(result) == 15
示例#4
0
    async def test_get_metadata_for_file_cached(self, provider):
        base_ref = 'a1b2c3d4'
        path = BitbucketPath('/foo-file.txt',
                             _ids=[(base_ref, 'develop'),
                                   (base_ref, 'develop')])
        provider._parent_dir = fixtures.validate_path['root_dir_listing']
        result = await provider.metadata(path)

        assert result is not None
示例#5
0
    async def test_get_metadata_for_file(self, provider):
        base_ref = 'a1b2c3d4'
        path = BitbucketPath('/foo-file.txt',
                             _ids=[(base_ref, 'develop'),
                                   (base_ref, 'develop')])

        test_fixtures = fixtures.validate_path
        dir_listing_body = test_fixtures['root_dir_listing']
        dir_listing_url = provider._build_v1_repo_url('src', base_ref) + '/'
        aiohttpretty.register_json_uri('GET',
                                       dir_listing_url,
                                       body=dir_listing_body)

        download_url = provider._build_v1_repo_url('raw', path.commit_sha,
                                                   *path.path_tuple())
        aiohttpretty.register_uri('GET', download_url, body=b'better')

        result = await provider.download(path)
        content = await result.response.read()
        assert content == b'better'
示例#6
0
    async def test_get_metadata_for_folder(self, provider):

        path = BitbucketPath('/', _ids=[(None, 'develop')], folder=True)

        test_fixtures = fixtures.validate_path
        dir_listing_body = test_fixtures['root_dir_listing']
        dir_listing_url = provider._build_v1_repo_url('src', 'develop') + '/'
        aiohttpretty.register_json_uri('GET',
                                       dir_listing_url,
                                       body=dir_listing_body)

        result = await provider.metadata(path)

        assert len(result) == 4
示例#7
0
    async def test_get_revisions(self, provider):
        path = BitbucketPath('/aaa-01.txt',
                             _ids=[('a1b2c3d4', 'master'),
                                   ('a1b2c3d4', 'master')])

        filehistory = fixtures.revisions['filehistory_complex']

        revisions_url = provider._build_v1_repo_url('filehistory', 'a1b2c3d4',
                                                    'aaa-01.txt')
        aiohttpretty.register_json_uri('GET', revisions_url, body=filehistory)

        result = await provider.revisions(path)

        assert len(result) == 4
示例#8
0
    async def test_file_download(self, provider, file_metadata):

        full_path = '/folder2-lvl1/folder1-lvl2/folder1-lvl3/ile0002.20bytes.txt'
        bb_path = BitbucketPath(full_path,
                                _ids=[(COMMIT_SHA, BRANCH)
                                      for _ in full_path.split('/')])
        bb_file_metadata = BitbucketFileMetadata(file_metadata,
                                                 bb_path,
                                                 owner=owner,
                                                 repo=repo)

        # Use mock coroutine instead of aiohttpretty here since file metadata has already been
        # covered in another test. See ``TestMetadata::test_get_metadata_for_file()``.
        provider.metadata = MockCoroutine(return_value=bb_file_metadata)

        file_data = b'file0002.20bytes.txt'
        download_url = provider._build_v2_repo_url('src', bb_path.ref,
                                                   *bb_path.path_tuple())
        aiohttpretty.register_uri('GET', download_url, body=file_data)

        result = await provider.download(bb_path)
        content = await result.response.read()
        assert content == file_data
示例#9
0
    async def test_get_metadata_for_file(self, provider):
        base_ref = 'a1b2c3d4'
        path = BitbucketPath('/foo-file.txt',
                             _ids=[(base_ref, 'develop'),
                                   (base_ref, 'develop')])

        test_fixtures = fixtures.validate_path
        dir_listing_body = test_fixtures['root_dir_listing']
        dir_listing_url = provider._build_v1_repo_url('src', base_ref) + '/'
        aiohttpretty.register_json_uri('GET',
                                       dir_listing_url,
                                       body=dir_listing_body)

        result = await provider.metadata(path)

        assert result is not None
示例#10
0
    def test_path_from_metadata(self, provider):
        name = 'aaa-01-2.txt'
        subdir = 'plaster'
        full_path = '/{}/{}'.format(subdir, name)
        branch = 'master'
        commit_sha = '123abc456def'

        path = BitbucketPath(full_path,
                             _ids=[(commit_sha, branch), (commit_sha, branch),
                                   (commit_sha, branch)])

        metadata = BitbucketFileMetadata(fixtures.file_metadata,
                                         path,
                                         owner=fixtures.owner,
                                         repo=fixtures.repo)
        child_path = provider.path_from_metadata(path.parent, metadata)

        assert child_path.full_path == path.full_path
        assert child_path == path
示例#11
0
    def test_path_from_metadata(self, provider, file_metadata, owner, repo):
        name = 'file0002.20bytes.txt'
        subdir = 'folder2-lvl1/folder1-lvl2/folder1-lvl3'
        full_path = '/{}/{}'.format(subdir, name)

        # Note: When building Bitbucket Path, the length of ``_ids`` array must be equal to the
        #       number of path segments, including the root.
        path = BitbucketPath(full_path,
                             _ids=[(COMMIT_SHA, BRANCH)
                                   for _ in full_path.split('/')])

        metadata = BitbucketFileMetadata(file_metadata,
                                         path,
                                         owner=owner,
                                         repo=repo)
        child_path = provider.path_from_metadata(path.parent, metadata)

        assert child_path.full_path == path.full_path
        assert child_path == path
示例#12
0
    async def test_get_revisions(self, provider, file_history_page_1,
                                 file_history_page_2):
        path = BitbucketPath('/file0001.20bytes.txt',
                             _ids=[(COMMIT_SHA, 'develop'),
                                   (COMMIT_SHA, 'develop')])

        file_history_page_1 = json.loads(file_history_page_1)
        query_params = {
            'pagelen':
            provider.RESP_PAGE_LEN,
            'fields':
            'values.commit.hash,values.commit.date,values.commit.author.raw,'
            'values.size,values.path,values.type,next'
        }
        file_history_first_url = provider._build_v2_repo_url(
            'filehistory', COMMIT_SHA, 'file0001.20bytes.txt', **query_params)
        aiohttpretty.register_json_uri('GET',
                                       file_history_first_url,
                                       body=file_history_page_1)

        file_history_page_2 = json.loads(file_history_page_2)
        file_history_next_url = file_history_page_1['next']
        aiohttpretty.register_json_uri('GET',
                                       file_history_next_url,
                                       body=file_history_page_2)

        result = await provider.revisions(path)

        assert len(result) == 14
        assert result[0].modified == '2019-04-25T11:58:30+00:00'
        assert result[0].modified_utc == '2019-04-25T11:58:30+00:00'
        assert result[0].version_identifier == 'commitSha'
        assert result[0].version == 'ad0412ab6f8e6d614701e290843e160d002cc124'
        assert result[0].extra == {
            'user': {
                'name': 'longze chen'
            },
            'branch': None
        }
示例#13
0
    async def test_get_metadata_for_file(self, provider, path_metadata_file,
                                         file_history_page_1,
                                         file_history_page_2):
        path = BitbucketPath('/file0001.20bytes.txt',
                             _ids=[(COMMIT_SHA, 'develop'),
                                   (COMMIT_SHA, 'develop')])

        provider._fetch_branch_commit_sha = MockCoroutine(
            return_value=COMMIT_SHA)

        file_metadata = json.loads(path_metadata_file)['root']
        query_params = {
            'format': 'meta',
            'fields': 'commit.hash,commit.date,path,size,links.history.href'
        }
        path_meta_url = '{}/?{}'.format(
            provider._build_v2_repo_url('src', COMMIT_SHA, *path.path_tuple()),
            urlencode(query_params))
        aiohttpretty.register_json_uri('GET',
                                       path_meta_url,
                                       body=file_metadata)

        file_history_page_1 = json.loads(file_history_page_1)
        query_params = {
            'pagelen':
            provider.RESP_PAGE_LEN,
            'fields':
            'values.commit.hash,values.commit.date,values.commit.author.raw,'
            'values.size,values.path,values.type,next'
        }
        file_history_first_url = '{}?{}'.format(
            file_metadata['links']['history']['href'], urlencode(query_params))
        aiohttpretty.register_json_uri('GET',
                                       file_history_first_url,
                                       body=file_history_page_1)

        file_history_page_2 = json.loads(file_history_page_2)
        file_history_next_url = file_history_page_1['next']
        aiohttpretty.register_json_uri('GET',
                                       file_history_next_url,
                                       body=file_history_page_2)

        metadata = await provider.metadata(path)

        assert not provider._fetch_branch_commit_sha.called
        assert metadata is not None
        assert metadata.name == 'file0001.20bytes.txt'
        assert metadata.path == '/file0001.20bytes.txt'
        assert metadata.kind == 'file'
        assert metadata.modified == '2019-04-25T11:58:30+00:00'
        assert metadata.modified_utc == '2019-04-25T11:58:30+00:00'
        assert metadata.created_utc == '2019-04-24T12:18:21+00:00'
        assert metadata.content_type is None
        assert metadata.size == 20
        assert metadata.size_as_int == 20
        assert metadata.etag == '{}::{}'.format('/file0001.20bytes.txt',
                                                COMMIT_SHA)
        assert metadata.provider == 'bitbucket'
        assert metadata.last_commit_sha == 'ad0412ab6f8e'
        assert metadata.commit_sha == COMMIT_SHA
        assert metadata.branch_name == BRANCH