-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: move a file (#68) * refactor: Create delete file controller (#69) * feat: delete file endpoint Endpoint to delete files, no tests implemented yet * fix: check format check format error * fix: Removing changes to init.py in controllers files_view.py files * fix: check-linter problem --------- Co-authored-by: Miguel Mateo Mendoza Rojas <[email protected]> Co-authored-by: Andrea Velasquez <[email protected]> Co-authored-by: Andvelavi <[email protected]>
- Loading branch information
1 parent
d2ab13a
commit 14ef24a
Showing
13 changed files
with
302 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
from flask import request | ||
from src.config.soap_client import soap_client | ||
from src.lib.helpers import is_valid_uuid | ||
|
||
|
||
def file_move_handler(token, file_uuid): | ||
try: | ||
data = json.loads(request.data) | ||
target_directory_uuid = data.get("targetDirectoryUUID") | ||
|
||
not_valid_target_directory = not target_directory_uuid or not is_valid_uuid( | ||
target_directory_uuid | ||
) | ||
if not_valid_target_directory: | ||
return {"msg": "The target directory is not valid or was not provided"}, 400 | ||
|
||
request_data = { | ||
"token": token, | ||
"fileUUID": file_uuid, | ||
"targetDirectoryUUID": target_directory_uuid, | ||
} | ||
|
||
response = soap_client.service.file_move(request_data) | ||
|
||
if response["error"] is True: | ||
return {"msg": response["msg"]}, response["code"] | ||
else: | ||
return {"msg": "The file has been moved"}, 200 | ||
|
||
except ValueError: | ||
return {"msg": "Not valid JSON data provided in the request"}, 400 | ||
|
||
except Exception as e: | ||
print("[Exception] file_move_handler ->", e) | ||
return {"msg": "There was an error moving the file"}, 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import json | ||
from random import randbytes | ||
from uuid import uuid4 | ||
|
||
from main import app | ||
from src.config.soap_client import soap_client | ||
from src.lib.faker import fake_username, fake_password | ||
|
||
# MOVE FILE TESTS | ||
move_file_test_data = { | ||
"username": fake_username(), | ||
"password": fake_password(), | ||
"file": { | ||
"uuid": None, | ||
"targetDirectoryUUID": None, | ||
"name": "test.txt", | ||
"content": randbytes(1024), | ||
}, | ||
} | ||
|
||
|
||
def test_move_file_success(): | ||
# Register a user | ||
register_response = soap_client.service.account_register( | ||
{ | ||
"username": move_file_test_data["username"], | ||
"password": move_file_test_data["password"], | ||
} | ||
) | ||
assert register_response.error is False | ||
|
||
# Login with the user | ||
login_response = soap_client.service.auth_login( | ||
{ | ||
"username": move_file_test_data["username"], | ||
"password": move_file_test_data["password"], | ||
} | ||
) | ||
assert login_response.error is False | ||
token = login_response.auth.token | ||
|
||
# Create a new directory | ||
new_directory_name = "test_directory" | ||
create_dir_response = soap_client.service.file_new_dir( | ||
{ | ||
"directoryName": new_directory_name, | ||
"location": None, | ||
"token": token, | ||
} | ||
) | ||
assert create_dir_response["code"] == 201 | ||
directory_uuid = create_dir_response["fileUUID"] | ||
move_file_test_data["file"]["targetDirectoryUUID"] = directory_uuid | ||
|
||
# Upload a file | ||
create_file_response = soap_client.service.file_upload( | ||
{ | ||
"token": token, | ||
"fileName": move_file_test_data["file"]["name"], | ||
"fileContent": move_file_test_data["file"]["content"], | ||
"location": None, | ||
} | ||
) | ||
assert create_file_response.error is False | ||
move_file_test_data["file"]["uuid"] = create_file_response.fileUUID | ||
|
||
# Move the file to the target directory | ||
response = app.test_client().patch( | ||
f"/file/{move_file_test_data['file']['uuid']}/move", | ||
json={"targetDirectoryUUID": directory_uuid}, | ||
headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
|
||
json_response = json.loads(response.data) | ||
assert response.status_code == 200 | ||
assert json_response["msg"].startswith("The file has been moved") | ||
|
||
|
||
def test_move_file_bad_request(): | ||
# Login with the user | ||
login_response = soap_client.service.auth_login( | ||
{ | ||
"username": move_file_test_data["username"], | ||
"password": move_file_test_data["password"], | ||
} | ||
) | ||
assert login_response.error is False | ||
token = login_response.auth.token | ||
|
||
# Missing JSON data | ||
response = app.test_client().patch( | ||
f"/file/{move_file_test_data['file']['uuid']}/move", | ||
headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
assert response.status_code == 400 | ||
|
||
# Not valid target directory UUID | ||
response = app.test_client().patch( | ||
f"/file/{move_file_test_data['file']['uuid']}/move", | ||
json={"targetDirectoryUUID": "not_valid_uuid"}, | ||
headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
assert response.status_code == 400 | ||
|
||
# Non existent file UUID | ||
response = app.test_client().patch( | ||
f"/file/{uuid4()}/move", | ||
json={"targetDirectoryUUID": uuid4()}, | ||
headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
assert response.status_code == 404 | ||
|
||
|
||
def test_move_file_conflict(): | ||
# Login with the user | ||
login_response = soap_client.service.auth_login( | ||
{ | ||
"username": move_file_test_data["username"], | ||
"password": move_file_test_data["password"], | ||
} | ||
) | ||
assert login_response.error is False | ||
token = login_response.auth.token | ||
|
||
# Try to move the file to the same directory | ||
response = app.test_client().patch( | ||
f"/file/{move_file_test_data['file']['uuid']}/move", | ||
json={ | ||
"targetDirectoryUUID": move_file_test_data["file"]["targetDirectoryUUID"] | ||
}, | ||
headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
assert response.status_code == 409 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from src.config.soap_client import soap_client | ||
|
||
|
||
def remove_file_handler(token, file_uuid): | ||
try: | ||
request_data = {"fileUUID": file_uuid, "token": token} | ||
response = soap_client.service.file_delete(request_data) | ||
|
||
if response["error"] is True: | ||
return {"msg": response["msg"]}, response["code"] | ||
else: | ||
return {"msg": "File successfully deleted"}, 200 | ||
|
||
except Exception as e: | ||
print("[Exception] remove_file_handler ->", e) | ||
return {"msg": "There was an error deleting the file"}, 500 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from src.config.soap_client import soap_client | ||
|
||
|
||
def shared_files_handler(token): | ||
try: | ||
request_data = {"token": token} | ||
response = soap_client.service.share_list(request_data) | ||
|
||
if response["error"] is True: | ||
return {"msg": response["msg"]}, response["code"] | ||
|
||
files = [ | ||
{ | ||
"name": file.name, | ||
"size": file.size, | ||
"isFile": file.isFile, | ||
"extension": file.extension, | ||
"uuid": file.uuid, | ||
"ownerusername": file.ownerusername, | ||
} | ||
for file in response.files | ||
] | ||
|
||
return { | ||
"files": files, | ||
"msg": "List of shared files obtained", | ||
}, 200 | ||
except Exception as e: | ||
print("[Exception] shared_files_handler ->", e) | ||
return {"msg": "There was an error listing the shared files"}, 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from src.config.soap_client import soap_client | ||
from src.lib.helpers import is_valid_uuid | ||
|
||
|
||
def shared_with_who_handler(token, file_uuid): | ||
try: | ||
if not is_valid_uuid(file_uuid): | ||
return {"msg": "Not valid file UUID provided"}, 400 | ||
|
||
request_data = {"fileUUID": file_uuid, "token": token} | ||
response = soap_client.service.share_list_with_who(request_data) | ||
|
||
if response.usernames is None: | ||
return {"msg": response["msg"]}, response["code"] | ||
|
||
usernames = [ | ||
{ | ||
"users": usernames, | ||
} | ||
for usernames in response.usernames | ||
] | ||
|
||
return { | ||
"users": usernames, | ||
"msg": "List of users the file is shared with", | ||
}, 200 | ||
except Exception as e: | ||
print("[Exception] shared_with_who_handler ->", e) | ||
return {"msg": "There was an error listing the shared with"}, 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
from flask import request | ||
from src.config.soap_client import soap_client | ||
from src.lib.helpers import is_valid_uuid | ||
|
||
|
||
def unshare_handler(token): | ||
try: | ||
data = json.loads(request.data) | ||
fileUUID = data["fileUUID"] | ||
otherUsername = data["otherUsername"] | ||
|
||
# Check if required fields are empty | ||
empty_file_uuid = not fileUUID or len(fileUUID) == 0 | ||
empty_other_username = not otherUsername or len(otherUsername) == 0 | ||
if empty_file_uuid or empty_other_username: | ||
return {"msg": "Required fields are missing in form data"}, 400 | ||
|
||
# Check if file UUID is valid | ||
if not is_valid_uuid(fileUUID): | ||
return {"msg": "Not valid file UUID provided"}, 400 | ||
|
||
request_data = { | ||
"fileUUID": fileUUID, | ||
"otherUsername": otherUsername, | ||
"token": token, | ||
} | ||
response = soap_client.service.unshare_file(request_data) | ||
|
||
if response["error"] is True: | ||
return {"msg": response["msg"]}, response["code"] | ||
else: | ||
return {"msg": "File unshared successfully"}, 200 | ||
|
||
except ValueError: | ||
return {"msg": "Invalid JSON data provided in the request"}, 400 | ||
|
||
except Exception: | ||
return {"msg": "There was an error unsharing the file"}, 500 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"version": "0.11.0" | ||
"version": "0.12.0" | ||
} |