def test_negative_case2(self): """ # Rename pseudo-directory: # * pseudo-directory exists ( but in lower/uppercase ) """ # 1.1 create a directory with lower_case name dir_name1 = generate_random_name().lower() prefix1 = encode_to_hex(dir_name1) random_dir_name1 = generate_random_name() self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name1) # 1.2 try to rename lower_case name directory by upper_case key res = self.client.rename(TEST_BUCKET_1, encode_to_hex(dir_name1.upper()), random_dir_name1) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {"error": 11}) # "11": "Prefix do not exist.", # 2.1 create a directory with upper_case name dir_name2 = generate_random_name().upper() prefix2 = encode_to_hex(dir_name2) random_dir_name2 = generate_random_name() self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name2) # 2.2 try to rename upper_case name directory by lower_case key res = self.client.rename(TEST_BUCKET_1, encode_to_hex(dir_name2.lower()), random_dir_name2) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {"error": 11}) # Clean: delete created directories self.client.delete(TEST_BUCKET_1, [prefix1, prefix2])
def test_delete_pseudodirectories_from_root(self): """ # Delete pseudo-directories from root: one and many """ # create 1 pseudo-directory dir_name = generate_random_name() response = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) self.assertEqual(response.status_code, 204) dir_name_prefix = dir_name.encode().hex() + "/" # delete created pseudo-directory response = self.client.delete(TEST_BUCKET_1, [dir_name_prefix]) result = response.json() self.assertEqual(result, [dir_name_prefix]) # create directories dir_names = [generate_random_name() for _ in range(3)] for name in dir_names: response = self.client.create_pseudo_directory(TEST_BUCKET_1, name) assert response.status_code == 204 # delete directories object_keys = [x.encode().hex() + "/" for x in dir_names] response = self.client.delete(TEST_BUCKET_1, object_keys) assert response.status_code == 200 result = response.json() self.assertEqual(set(result), set(object_keys))
def test_negative_case8(self): """ # 1. to lock file # 2. to rename it from other user2 # 3. make sure it is not renamed """ # 1. Upload a file fn = '025587.jpg' res = self.client.upload(TEST_BUCKET_1, fn) object_key = res['object_key'] # 2. Lock uploaded file res = self.client.patch(TEST_BUCKET_1, 'lock', [object_key]) self.assertEqual(res.status_code, 200) self.assertEqual(res.json()[0]['is_locked'], True) # 3. Try to rename uploaded locked file from user2 self.client.login(USERNAME_2, PASSWORD_2) random_name = generate_random_name() res = self.client.rename(TEST_BUCKET_1, object_key, random_name) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {'error': 43}) # "43": "Locked", # Clean: delete all created self.client.login(USERNAME_1, PASSWORD_1) res = self.client.patch(TEST_BUCKET_1, 'unlock', [object_key]) self.client.delete(TEST_BUCKET_1, [object_key])
def test_negative_case7(self): """ # 1. to create directory on client in UPPERCASE when LOWERCASE exists on server # 2. to make sure files put in NTFS directory uploaded to remote dir using server name, # not client name. """ # 1.1 Create directory on lowercase dir_name_lower = generate_random_name().lower() dir_prefix = encode_to_hex(dir_name_lower) self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name_lower) # 1.2 Create directory on same but uppercase dir_name_upper = dir_name_lower.upper() res = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name_upper) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {'error': 10}) # Clean: delete all created self.client.delete(TEST_BUCKET_1, [dir_prefix]) # 2.1 Upload a file in lowercase, and try to upload same file with new version in uppercase fn = "requirements.txt" res = self.client.upload(TEST_BUCKET_1, fn) version = res['version'] fn2 = 'REQUIREMENTS.Txt' res = self.client.upload(TEST_BUCKET_1, fn2, last_seen_version=version) self.assertEqual(res['orig_name'], fn2) self.assertNotEqual(version, res['version']) # Clean: delete all created self.client.delete(TEST_BUCKET_1, [res['object_key']])
def test_negative_case5(self): """ # 1. rename file # 2. create directory with the same name # 3. make sure error appears """ # 1. Upload file fn = "20180111_165127.jpg" res = self.client.upload(TEST_BUCKET_1, fn) object_key = res['object_key'] # 2. Rename it random_name = generate_random_name() res = self.client.rename(TEST_BUCKET_1, object_key, random_name) self.assertEqual(res.status_code, 200) self.assertEqual(res.json()['orig_name'], random_name) # 3. Create directory with the same name dir_name = random_name res = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {'error': 29}) # "29": "Object exists already.", # Clean: delete all created res = self.client.get_list(TEST_BUCKET_1) for el in res.json()['list']: if el['orig_name'] == random_name: object_key = el['object_key'] break else: raise Exception('File gone somewhere') self.client.delete(TEST_BUCKET_1, [object_key])
def test_negative_case4(self): """ # To make sure error returned in the following cases: # * rename directory to the name of existing file # * rename file to the name of existing directory """ # 1. Upload a file and create a directory dir_name = generate_random_name() dir_name_prefix = encode_to_hex(dir_name) self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) fn = "20180111_165127.jpg" res = self.client.upload(TEST_BUCKET_1, fn) object_key = res['object_key'] orig_name = res["orig_name"] # 2.1 Try rename directory to the name of existing file res = self.client.rename(TEST_BUCKET_1, dir_name_prefix, orig_name) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {'error': 29}) # "29": "Object exists already.", # 2.2 Try rename file to the name of existing directory res = self.client.rename(TEST_BUCKET_1, object_key, dir_name) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {'error': 10}) # "10": "Directory exists already.", # Clean: delete all created self.client.delete(TEST_BUCKET_1, [dir_name_prefix, object_key])
def test_delete_pseudodirectories_from_pseudodirectory(self): """ # Delete pseudo-directories from pseudo-directory: one and many """ # create main pseudo-directory dir_name = generate_random_name() main_dir_name_prefix = encode_to_hex(dir_name) response = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) self.assertEqual(response.status_code, 204) # create 1 pseudo-directory in main pseudo-directory dir_name = generate_random_name() dir_name_prefix = encode_to_hex(dir_name) response = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name, main_dir_name_prefix) self.assertFalse(bool(response.content), msg=response.content.decode()) self.assertEqual(response.status_code, 204) # delete 1 created pseudo-directory from main pseudo-directory object_keys = [dir_name_prefix] response = self.client.delete(TEST_BUCKET_1, object_keys, main_dir_name_prefix) assert response.status_code == 200 self.assertEqual(set(response.json()), set(object_keys)) # create 2-10 pseudo-directories in main pseudo-directory dir_names = [ generate_random_name() for _ in range(random.randint(2, 10)) ] object_keys = encode_to_hex(dir_names=dir_names) for name in dir_names: response = self.client.create_pseudo_directory(TEST_BUCKET_1, name) assert response.status_code == 204 # delete created pseudo-directories from main pseudo-directory response = self.client.delete(TEST_BUCKET_1, object_keys) assert response.status_code == 200 self.assertEqual(set(response.json()), set(object_keys)) # delete main pseudo-directory object_keys = [main_dir_name_prefix] response = self.client.delete(TEST_BUCKET_1, object_keys) assert response.status_code == 200 self.assertEqual(set(response.json()), set(object_keys))
def test_negative_case3(self): """ # Rename pseudo-directory: # * server failed to rename some nested objects """ # 1.1 create a directory and a nested directory into dir_names = [generate_random_name() for i in range(2)] print("Test 3") print("dir names: ", dir_names) hex_decoder = lambda x: bytes.fromhex(x).decode('utf-8') prefixes = encode_to_hex(dir_names=dir_names) print("prefixes: ", prefixes) self.client.create_pseudo_directory(TEST_BUCKET_1, dir_names[0]) self.client.create_pseudo_directory(TEST_BUCKET_1, dir_names[1], prefixes[0]) # 1.2 delete nested directory self.client.delete(TEST_BUCKET_1, [prefixes[1]], prefixes[0]) # 1.3 try to rename nested deleted directory res = self.client.get_list(TEST_BUCKET_1, prefixes[0]) print("GET list:") from pprint import pprint pprint(res.json()) pseudo_dir = None for obj in res.json()['dirs']: if hex_decoder(obj['prefix'].split('/')[1]).split("-")[0] == dir_names[1]: pseudo_dir = obj break else: raise Exception('Something went wrong') # print(hex_decoder(dir['prefix'].split('/')[1]).split("-")[0]) dst_name = generate_random_name() print("dst_name: ", dst_name) print("pseudo_dir prefix: ", pseudo_dir['prefix']) res = self.client.rename(TEST_BUCKET_1, pseudo_dir['prefix'].split("/")[1] + '/', dst_name, prefixes[0]) print("status code: ", res.status_code, "Expected: ", 400) print("result: ", res.json(), "- renamed and undelete") # self.assertEqual(res.status_code, 400) # self.assertEqual(res.json(), {"error": 9}) # "9": "Incorrect object name.", # Clean: delete all created self.client.delete(TEST_BUCKET_1, [prefixes[0]])
def test_negative_case1(self): """ # Rename pseudo-directory: # * source prefix do not exist """ # 1. create a directory dir_name = generate_random_name() prefix = encode_to_hex(dir_name) random_dir_name = (generate_random_name(), generate_random_name()) self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) # 2.1 try to rename directory that doesn't exists in root res = self.client.rename(TEST_BUCKET_1, encode_to_hex(random_dir_name[0]), random_dir_name[1]) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {"error": 11}) # "11": "Prefix do not exist.", # 2.2 try to rename directory that doesn't exists in created directory res = self.client.rename(TEST_BUCKET_1, encode_to_hex(random_dir_name[0]), random_dir_name[1], prefix) self.assertEqual(res.status_code, 400) self.assertEqual(res.json(), {"error": 11}) # Clean: delete all created self.client.delete(TEST_BUCKET_1, [prefix])
def test_negative_case10(self): """ # 1. upload file # 2. rename it # 3. rename it to its key # 4. file should not disappear """ # 1. Upload file fn = '025587.jpg' res = self.client.upload(TEST_BUCKET_1, fn) object_key = res['object_key'] # 2. Rename uploaded file random_name = generate_random_name() res = self.client.rename(TEST_BUCKET_1, object_key, random_name) orig_name = res.json()['orig_name'] # 3. Try rename renamed file to its object key res = self.client.get_list(TEST_BUCKET_1) for el in res.json()['list']: if el['orig_name'] == orig_name: object_key2 = el['object_key'] print(object_key2) break else: raise Exception('Uploaded renamed file gone somewhere') res = self.client.rename(TEST_BUCKET_1, object_key2, object_key2) print(res) print(res.json()) # 4. Check for file is not disapeared res = self.client.get_list(TEST_BUCKET_1) for el in res.json()['list']: if el['orig_name'] == object_key2: obj = el object_key3 = el['object_key'] print(object_key3) break self.assertEqual(obj['orig_name'], object_key2) # Clean: delete all created self.client.delete(TEST_BUCKET_1, [object_key3, object_key2])
def test_delete_files_from_pseudodirectory(self): """ # Delete from pseudo-directory: one and many """ # 1 create main pseudo-directory dir_name = generate_random_name() response = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) assert response.status_code == 204 dir_name_prefix = dir_name.encode().hex() + "/" # 2 upload file to main pseudo-directory fn = "20180111_165127.jpg" result = self.client.upload(TEST_BUCKET_1, fn, dir_name_prefix) self.assertEqual(result['orig_name'], '20180111_165127.jpg') object_key = [result['object_key']] # 2.1 delete file from pseudo-directory and check for is_deleted: True response = self.client.delete(TEST_BUCKET_1, object_keys=object_key, prefix=dir_name_prefix) self.assertEqual(response.json(), object_key) result = self.client.get_list(TEST_BUCKET_1).json() for obj in result['list']: if fn in obj['orig_name']: self.assertTrue(obj['is_deleted']) # 3 upload many files to main pseudo-directory fn = ["025587.jpg", "README.md", "requirements.txt"] object_keys = [] for file in fn: result = self.client.upload(TEST_BUCKET_1, file, dir_name_prefix) object_keys.append(result['object_key']) self.assertEqual(result['orig_name'], file) # 4 delete created pseudo-directory, with uploaded files dir_name_prefix = [dir_name_prefix] response = self.client.delete(TEST_BUCKET_1, object_keys=dir_name_prefix) self.assertEqual(response.json(), dir_name_prefix)
def test_mkdir(self): """ 1. Сreates pseudo-directory 2. Makes request to create another one with same name 3. Checks for error returned """ # 1 Create pseudo-directory with random name dir_name = generate_random_name() response = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) self.assertEqual(response.status_code, 204) # 2 Try to create pseudo-directory with same name response = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) # 3 Check for error is returned self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), {"error": 10}) # "10": "Directory exists already." # 4 Delete created pseudo-directory dir_name_prefix = [encode_to_hex(dir_name)] response = self.client.delete(TEST_BUCKET_1, dir_name_prefix) self.assertEqual(response.json(), dir_name_prefix)
def test_lock3(self): """ Make sure locked file can't be replaced using COPY/MOVE operations """ # 1.1 upload a file fn = "20180111_165127.jpg" result = self.client.upload(TEST_BUCKET_1, fn) object_key1 = result['object_key'] version = result['version'] self.assertEqual(result['orig_name'], fn) # 1.2 create a directory dir_name = generate_random_name() dir_name_prefix = encode_to_hex(dir_name) response = self.client.create_pseudo_directory(TEST_BUCKET_1, dir_name) self.assertEqual(response.status_code, 204) result = self.client.upload(TEST_BUCKET_1, fn, prefix=dir_name_prefix, last_seen_version=version) object_key2 = result['object_key'] self.assertNotEqual(version, result['version']) # 2. lock first file and check for "is_locked": True response = self.client.patch(TEST_BUCKET_1, "lock", [object_key1]) result = response.json() self.assertEqual(response.status_code, 200) self.assertEqual(result[0]['is_locked'], True) # 3.1 Try to replace locked file by second file from directory by move operation src_object_keys = [object_key2] response = self.client.move(TEST_BUCKET_1, TEST_BUCKET_1, src_object_keys, src_prefix=dir_name_prefix) self.assertEqual(response.status_code, 400) self.assertEqual( response.json(), {"error": 15}) # "15": "Incorrect \"src_object_keys\"." # 3.2 check for locked file existance and is_locked: True response = self.client.get_list(TEST_BUCKET_1) for obj in response.json()['list']: if obj['object_key'] == object_key1: self.assertEqual(obj['orig_name'], fn) self.assertEqual(obj['is_locked'], True) break else: self.assertTrue(False, msg='Uploaded file disapeared somewhere') # 3.3 Delete uploaded file from directory response = self.client.delete(TEST_BUCKET_1, [object_key2], dir_name_prefix) self.assertEqual(response.json(), [object_key2]) # 4.1 Upload the same file with new version from User2 self.client.login(USERNAME_2, PASSWORD_2) result = self.client.upload(TEST_BUCKET_1, fn, prefix=dir_name_prefix, last_seen_version=version) object_key3 = result['object_key'] self.assertEqual(result['orig_name'], fn) self.assertNotEqual(version, result['version']) # 4.2 Try to replace locked file by second file from directory by copy operation object_keys = {object_key3: fn} response = self.client.copy(TEST_BUCKET_1, TEST_BUCKET_1, object_keys, dir_name_prefix) # print(response.content.decode()) self.assertEqual(response.json()['skipped'], 'locked') # 4.2 check for locked file existance and is_locked: True response = self.client.get_list(TEST_BUCKET_1) for obj in response.json()['list']: if obj['object_key'] == object_key1: self.assertEqual(obj['orig_name'], fn) self.assertEqual(obj['is_locked'], True) break else: self.assertTrue(False, msg='Uploaded file disapeared somewhere') # 4.3 Clean: delete uploaded file from User2 response = self.client.delete(TEST_BUCKET_1, [object_key3], dir_name_prefix) self.assertEqual(response.json(), [object_key3]) # Clean: delete the uploaded file and directory from User1 self.client.login(USERNAME_1, PASSWORD_1) response = self.client.patch(TEST_BUCKET_1, "unlock", [object_key1]) self.assertEqual(response.json()[0]['is_locked'], False) self.assertEqual(response.status_code, 200) object_keys = [object_key1, dir_name_prefix] response = self.client.delete(TEST_BUCKET_1, object_keys) self.assertEqual(response.status_code, 200)