import hashlib import pytest from ucc import InvalidError from ucc.abstract import Permission from ucc.database import ProtectedConnection from ucc.gshortcut import HMGShortcutClient client = None @pytest.fixture(scope='module', autouse=True) def set_globals(path_client, ucc_client): global client client = HMGShortcutClient(ucc_client, ProtectedConnection(':memory:')) def test_get_file_node(): # external_file_shortcut.json check_node(client.get_node('1wTJiqdazuQKRfmYiC1ZQbhyZMr1qxxZm')) # internal_file_shortcut.pdf check_node(client.get_node('1PkBOx59xuftWoNbbNzrGWRG4mlSjdUr4')) # broken_file_shortcut.txt.gshortcut check_node(client.get_node('1pZCmxZrqV-wAG8DH5uk-jgtiUzcL5mWx')) # gdoc_shortcut.gslides check_node(client.get_node('1Gt_ddPgO8wI5bPhC19l3SAi-98CMM_m8')) # broken_gdoc_shortcut.gshortcut check_node(client.get_node('1gaIxOxlqZpcW9r6y_ofOT1UGwnTbMIbH')) # gdoc.gslides check_node(client.get_node('15nMbkJMhD8lPbomw5kgiT2NUOk8lVbyqHxPJMLguK-U')) def test_list_children_file(): parent_id = '14YrEdyAIkWxcQzWcML5p-neeSV9fqaFv' children = client.list_children(parent_id) nodes = dict() for node in children: nodes[node.name] = node if node.id == '1PG4kO_pQm5uAbqv-7DBcBjToBv4kMx4k': continue assert parent_id == node.parent_id check_node(nodes['external_file_shortcut.json']) check_node(nodes['internal_file_shortcut.pdf']) check_node(nodes['broken_file_shortcut.txt.gshortcut']) check_node(nodes['gdoc_shortcut.gslides']) check_node(nodes['broken_gdoc_shortcut.gshortcut']) check_node(nodes['gdoc.gslides']) def test_find_file(): parent_id = '14YrEdyAIkWxcQzWcML5p-neeSV9fqaFv' check_node(client.find(parent_id, 'external_file_shortcut.json')) check_node(client.find(parent_id, 'internal_file_shortcut.pdf')) check_node_is_none(client.find(parent_id, 'broken_file_shortcut.txt')) check_node(client.find(parent_id, 'gdoc_shortcut')) check_node(client.find(parent_id, 'gdoc_shortcut.gslides')) check_node_is_none(client.find(parent_id, 'gdoc_shortcut.gdoc')) check_node_is_none(client.find(parent_id, 'broken_gdoc_shortcut')) check_node_is_none(client.find(parent_id, 'gdoc')) check_node(client.find(parent_id, 'gdoc.gslides')) def test_find_gshortcut_file(): parent_id = '14YrEdyAIkWxcQzWcML5p-neeSV9fqaFv' check_node(client.find(parent_id, 'external_file_shortcut.json.gshortcut')) check_node(client.find(parent_id, 'internal_file_shortcut.pdf.gshortcut')) check_node(client.find(parent_id, 'broken_file_shortcut.txt.gshortcut')) check_node(client.find(parent_id, 'gdoc_shortcut.gshortcut')) check_node_is_none(client.find(parent_id, 'gdoc_shortcut.gslides.gshortcut')) # TODO minor bug check_node(client.find(parent_id, 'broken_gdoc_shortcut.gshortcut')) # def test_permission(): pass # alreay covered by test_get_node def test_download(): # external_file_shortcut.json assert_download(b'fsfsfs\n12345\nhello', '1wTJiqdazuQKRfmYiC1ZQbhyZMr1qxxZm') # internal_file_shortcut.pdf assert_download_content_md5("4a97576302fe787ec2108654469412a9", '1PkBOx59xuftWoNbbNzrGWRG4mlSjdUr4') # broken_file_shortcut.txt.gshortcut assert_download( b'{"url":"https://docs.google.com/open?id=1pZCmxZrqV-wAG8DH5uk-jgtiUzcL5mWx"}', '1pZCmxZrqV-wAG8DH5uk-jgtiUzcL5mWx') # gdoc_shortcut.gslides assert_download( b'{"url":"https://docs.google.com/presentation/d/1HtAyK0vrFmC7ZAqhbi-QaNSu2K9MzjngXdZtiMbSEdA/edit?usp=drivesdk"}', '1Gt_ddPgO8wI5bPhC19l3SAi-98CMM_m8') # broken_gdoc_shortcut.gshortcut assert_download( b'{"url":"https://docs.google.com/open?id=1gaIxOxlqZpcW9r6y_ofOT1UGwnTbMIbH"}', '1gaIxOxlqZpcW9r6y_ofOT1UGwnTbMIbH') # gdoc.gslides assert_download( b'{"url":"https://docs.google.com/presentation/d/15nMbkJMhD8lPbomw5kgiT2NUOk8lVbyqHxPJMLguK-U/edit?usp=drivesdk"}', '15nMbkJMhD8lPbomw5kgiT2NUOk8lVbyqHxPJMLguK-U') def test_export(): # gdoc_shortcut.gshortcut _id = '1Gt_ddPgO8wI5bPhC19l3SAi-98CMM_m8' fp = client.export(_id, '.pptx') assert len(fp.read()) > 0 # TODO minor bug # broken_gdoc_shortcut.gshortcut # After file was purged from Trash, it returns "File not found" which will # translated into InvalidError. _id = '1gaIxOxlqZpcW9r6y_ofOT1UGwnTbMIbH' with pytest.raises(InvalidError): client.export(_id, '.pptx') _id = '15nMbkJMhD8lPbomw5kgiT2NUOk8lVbyqHxPJMLguK-U' fp = client.export(_id, '.pptx') assert len(fp.read()) > 0 def test_overwrite_file(): with pytest.raises(InvalidError): # broken_file_shortcut.txt.gshortcut _id = '1pZCmxZrqV-wAG8DH5uk-jgtiUzcL5mWx' client.overwrite_file(_id, None) with pytest.raises(InvalidError): # broken_gdoc_shortcut.gshortcut _id = '1Gt_ddPgO8wI5bPhC19l3SAi-98CMM_m8' client.overwrite_file(_id, None) with pytest.raises(InvalidError): # gdoc_shortcut.gslides _id = '1gaIxOxlqZpcW9r6y_ofOT1UGwnTbMIbH' client.overwrite_file(_id, None) def test_update_node(): pass def test_delete(): pass def assert_download(content, _id): fp = client.download(_id) assert content == fp.read() def assert_download_content_md5(_md5, _id): fp = client.download(_id) assert _md5 == hashlib.md5(fp.read()).hexdigest() def check_node_is_none(node): assert None == node def check_node(node): check_file_node(node) def check_file_node(node): # external_file_shortcut.json if node.id == '1wTJiqdazuQKRfmYiC1ZQbhyZMr1qxxZm': assert 'external_file_shortcut.json' == node.name assert False == node.is_dir assert False == node.is_shortcut assert 18 == node.size assert PERMISSION_RD == node.permission return # internal_file_shortcut.pdf elif node.id == '1PkBOx59xuftWoNbbNzrGWRG4mlSjdUr4': assert 'internal_file_shortcut.pdf' == node.name assert False == node.is_dir assert False == node.is_shortcut assert 1478479 == node.size assert PERMISSION_RWD == node.permission return # broken_file_shortcut.txt.gshortcut elif node.id == '1pZCmxZrqV-wAG8DH5uk-jgtiUzcL5mWx': assert 'broken_file_shortcut.txt.gshortcut' == node.name assert False == node.is_dir assert True == node.is_shortcut assert 75 == node.size assert PERMISSION_RWD == node.permission return # gdoc_shortcut.gslides elif node.id == '1Gt_ddPgO8wI5bPhC19l3SAi-98CMM_m8': assert 'gdoc_shortcut.gslides' == node.name assert False == node.is_dir assert False == node.is_shortcut assert 111 == node.size assert PERMISSION_RWD == node.permission return # broken_gdoc_shortcut.gshortcut elif node.id == '1gaIxOxlqZpcW9r6y_ofOT1UGwnTbMIbH': assert 'broken_gdoc_shortcut.gshortcut' == node.name assert False == node.is_dir assert True == node.is_shortcut assert 75 == node.size assert PERMISSION_RWD == node.permission return # gdoc.gslides elif node.id == '15nMbkJMhD8lPbomw5kgiT2NUOk8lVbyqHxPJMLguK-U': assert 'gdoc.gslides' == node.name assert False == node.is_dir assert False == node.is_shortcut assert 111 == node.size assert PERMISSION_RWD == node.permission return else: assert 0, 'Unknown node: %s' % node PERMISSION_RWD = Permission() PERMISSION_RWD.can_read = True PERMISSION_RWD.can_write = True PERMISSION_RWD.can_delete = True PERMISSION_RD = Permission() PERMISSION_RD.can_read = True PERMISSION_RD.can_write = False PERMISSION_RD.can_delete = True PERMISSION_R = Permission() PERMISSION_R.can_read = True PERMISSION_R.can_write = False PERMISSION_R.can_delete = False