From a2d1854db192415e6e1218e60b80702b7ac58029 Mon Sep 17 00:00:00 2001 From: Hugo Gonzalez Labrador Date: Thu, 9 Apr 2015 17:17:04 +0200 Subject: [PATCH 1/2] Added test to check that the shared link points ALWAYS to the latest version of a file --- lib/share-tests/test_filesharebylinkintegrity | 89 +++++++++++++++++++ python/smashbox/utilities/__init__.py | 26 +++++- 2 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 lib/share-tests/test_filesharebylinkintegrity diff --git a/lib/share-tests/test_filesharebylinkintegrity b/lib/share-tests/test_filesharebylinkintegrity new file mode 100644 index 00000000..a69af4ae --- /dev/null +++ b/lib/share-tests/test_filesharebylinkintegrity @@ -0,0 +1,89 @@ + +__doc__ = """ + +Test public file sharing after updating file. +With this test we want to check that after updating a file we still get the lastest version via the shared link and not a previous one. + +Steps: +- 1) Create workdir +- 2) Upload file +- 3) Share file by link +- 4) Download file shared via the link and check ifit has the same contents as the original +- 5) Upload new version of a file (the fileid changes) +- 6) Re-download file shared and check that the contents are the same as the updated file and not to the original (now a version) + +""" + +from smashbox.utilities import * +import smashbox.curl +import time +import os +import owncloud + +# The name of the file to upload +filename = "shared_file.txt" + +# The contents of the file +file_data = "some data" + +# The contents to update the file, please be sure this is different from file_data +file_data_updated = "other data" + +# The name of the local file created after download the file via the shared link +filename_step4 = filename + "_step4" + +# The name of the local file created after re-download the file via shared link +filename_step6 = filename + "_step6" + +# The block size to generate random file +filesizeKB = int(config.get('share_filesizeKB',10)) + + + +@add_worker +def checker(step): + + # Create workdir + step(1) + d = make_workdir() + fullpath = os.path.join(d,filename) + createfile(fullpath, file_data, count=1000, bs=filesizeKB) + original_sum = md5sum(fullpath) + logger.info(original_sum) + + # Upload file + + step(2) + client = smashbox.curl.Client() + r = client.PUT(fullpath, os.path.join(oc_webdav_url(), filename)) + fatal_check(r.rc == 201, "we shoudl get 201 uploading the file") + + + # Share file by link + step(3) + share_info = share_file_with_link(filename, verify_certs = False) + shared_link = share_info.link + '&download' + logger.info("You can access the shared file via this link: " + shared_link) + + # Download file shared via the link and check ifit has the same contents as the original + step(4) + client = smashbox.curl.Client() + r = client.GET(shared_link, os.path.join(d,filename_step4)) + sum = md5sum(os.path.join(d,filename_step4)) + fatal_check(sum == original_sum, "the file download by link is corrupted") + + # Upload new version of a file (the fileid changes) + step(5) + createfile(fullpath, file_data_updated, count=1000, bs=filesizeKB) + updated_sum = md5sum(fullpath) + client = smashbox.curl.Client() + r = client.PUT(fullpath, os.path.join(oc_webdav_url(), filename)) + fatal_check(r.rc == 201, "we shoudl get 201 uploading the file") + + # Re-download file shared and check that the contents are the same as the updated file and not to the original (now a version) + step(6) + client = smashbox.curl.Client() + r = client.GET(shared_link, os.path.join(d,filename_step6)) + sum = md5sum(os.path.join(d,filename_step6)) + fatal_check(sum == updated_sum, "the shared link is pointing to a previous version") + logger.info('SUCCESS') \ No newline at end of file diff --git a/python/smashbox/utilities/__init__.py b/python/smashbox/utilities/__init__.py index 20fd0e5a..3440b30f 100644 --- a/python/smashbox/utilities/__init__.py +++ b/python/smashbox/utilities/__init__.py @@ -508,7 +508,7 @@ def scrape_log_file(d): # ###### API Calls ############ -def get_oc_api(): +def get_oc_api(**kwargs): """ Returns an instance of the Client class :returns: Client instance @@ -520,7 +520,7 @@ def get_oc_api(): protocol += 's' url = protocol + '://' + config.oc_server + '/' + config.oc_root - oc_api = owncloud.Client(url) + oc_api = owncloud.Client(url, **kwargs) return oc_api @@ -553,6 +553,28 @@ def share_file_with_user(filename, sharer, sharee, **kwargs): else: return -2 +def share_file_with_link(filename,**kwargs): + """ Shares a file by link + + :param filename: name of the file being shared + :param kwargs: key words args to be passed into the api, usually for share permissions + :returns: PublicShare instance + + """ + oc_api = get_oc_api(**kwargs) + oc_api.login(config.oc_account_name, config.oc_account_password) + try: + share_info = oc_api.share_file_with_link(filename) + return share_info + except Exception as err: + + # TODO: this code is not the best - the goal is to trap a share not allowed error and return that error code + + logger.info('Share failed with %s', str(err)) + if "not allowed to share" in str(err): + return -1 + else: + return -2 def delete_share(sharer, share_id): """ Deletes a share From 4379fa8ecd984deb387504c0c8f8d26585e45ca7 Mon Sep 17 00:00:00 2001 From: Hugo Gonzalez Labrador Date: Fri, 10 Apr 2015 09:27:40 +0200 Subject: [PATCH 2/2] Fixed typo --- lib/share-tests/test_filesharebylinkintegrity | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/share-tests/test_filesharebylinkintegrity b/lib/share-tests/test_filesharebylinkintegrity index a69af4ae..4d979c8f 100644 --- a/lib/share-tests/test_filesharebylinkintegrity +++ b/lib/share-tests/test_filesharebylinkintegrity @@ -49,20 +49,18 @@ def checker(step): fullpath = os.path.join(d,filename) createfile(fullpath, file_data, count=1000, bs=filesizeKB) original_sum = md5sum(fullpath) - logger.info(original_sum) # Upload file - step(2) client = smashbox.curl.Client() r = client.PUT(fullpath, os.path.join(oc_webdav_url(), filename)) - fatal_check(r.rc == 201, "we shoudl get 201 uploading the file") + fatal_check(r.rc == 201, "we should get 201 uploading the file") # Share file by link step(3) share_info = share_file_with_link(filename, verify_certs = False) - shared_link = share_info.link + '&download' + shared_link = share_info.link + '&download' # the download query params is to go directly to the file and not to the webpage showing the file logger.info("You can access the shared file via this link: " + shared_link) # Download file shared via the link and check ifit has the same contents as the original @@ -70,7 +68,7 @@ def checker(step): client = smashbox.curl.Client() r = client.GET(shared_link, os.path.join(d,filename_step4)) sum = md5sum(os.path.join(d,filename_step4)) - fatal_check(sum == original_sum, "the file download by link is corrupted") + fatal_check(sum == original_sum, "the file downloaded by link is corrupted") # Upload new version of a file (the fileid changes) step(5)