Skip to content

Commit

Permalink
feat: Shared with who (#75)
Browse files Browse the repository at this point in the history
* feat: Get the users with whom a file is being shared

Endpoint to get the users with whom a file is shared.
Testing has not been implemented yet

* refactor: Enpoint correction in addition to updating the spec

* feat: The init and view are added for the endpoint shared with whom, in addition to implementing the tests

The tests exceed the required 70%

* fix: check-linter problem

* docs(openapi): Update spec

* fix: Return plain usernames with whom a file is being shared

Return an array of strings instead of an array of:

```json
{
    "username": "string"
}
```

* test: Minimally increase "shared with who" coverage

A new test case is added to test the SOAP domain error thrown when a file is not found

* style: Format files

---------

Co-authored-by: Andvelavi <[email protected]>
Co-authored-by: Pedro Andrés Chaparro Quintero <[email protected]>
Co-authored-by: Pedro Andrés Chaparro Quintero <[email protected]>
  • Loading branch information
4 people authored Oct 20, 2023
1 parent da830c0 commit 3281cca
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/spec.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ paths:
- bearer: []
description: Get the list of users the file is shared with
parameters:
- in: query
- in: path
name: fileUUID
required: true
schema:
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .download_file._handler import download_file_handler
from .share_file._handler import share_handler
from .move_a_file._handler import file_move_handler
from .shared_with_who._handler import shared_with_who_handler

FILES_HANDLERS = {
"CHECK_STATE": check_state_handler,
Expand All @@ -18,4 +19,5 @@
"DOWNLOAD_FILE": download_file_handler,
"SHARE": share_handler,
"MOVE_FILE": file_move_handler,
"SHARED_WITH_WHO": shared_with_who_handler,
}
15 changes: 6 additions & 9 deletions src/controllers/files/shared_with_who/_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@ def shared_with_who_handler(token, file_uuid):
request_data = {"fileUUID": file_uuid, "token": token}
response = soap_client.service.share_list_with_who(request_data)

if response.usernames is None:
if response.error is True:
return {"msg": response["msg"]}, response["code"]

usernames = [
{
"users": usernames,
}
for usernames in response.usernames
]
usernames = [username for username in response.usernames]

return {
"users": usernames,
"msg": "List of users the file is shared with",
"msg": "The users that have access to this file were listed successfully",
}, 200
except Exception as e:
print("[Exception] shared_with_who_handler ->", e)
return {"msg": "There was an error listing the shared with"}, 500
return {
"msg": "There was an error listing the users that have access to this file"
}, 500
125 changes: 125 additions & 0 deletions src/controllers/files/shared_with_who/_shared_with_who_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
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

# SHARED WITH FILE TESTS
shared_with_who_test_data = {
"username": fake_username(),
"password": fake_password(),
"otherUsername": fake_username(),
"otherPassword": fake_password(),
"file": {
"uuid": None,
"name": "picture.jpeg",
"content": randbytes(1024),
},
}


def test_shared_with_who_bad_request():
# Register an user
register_response = soap_client.service.account_register(
{
"username": shared_with_who_test_data["username"],
"password": shared_with_who_test_data["password"],
}
)
assert register_response.error is False

# Login with the user
login_response = soap_client.service.auth_login(
{
"username": shared_with_who_test_data["username"],
"password": shared_with_who_test_data["password"],
}
)
assert login_response.error is False
token = login_response.auth.token

# Unexistent file UUID
response = app.test_client().get(
f"/file/{123}/shared-with-who",
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 400

# NO TOKEN
response = app.test_client().get(
f"/file/{shared_with_who_test_data['file']['uuid']}/shared-with-who"
)
assert response.status_code == 401


def test_shared_with_who_success():
# Register the second user
register_response2 = soap_client.service.account_register(
{
"username": shared_with_who_test_data["otherUsername"],
"password": shared_with_who_test_data["otherPassword"],
}
)
assert register_response2.error is False

# Login with the user
login_response = soap_client.service.auth_login(
{
"username": shared_with_who_test_data["username"],
"password": shared_with_who_test_data["password"],
}
)
assert login_response.error is False
token = login_response.auth.token

# Upload a file as the first user
upload_response = soap_client.service.file_upload(
{
"token": token,
"fileName": shared_with_who_test_data["file"]["name"],
"fileContent": shared_with_who_test_data["file"]["content"],
"location": None,
}
)
assert upload_response.error is False
shared_with_who_test_data["file"]["uuid"] = upload_response.fileUUID

# share a file
create_share_response = soap_client.service.share_file(
{
"fileUUID": shared_with_who_test_data["file"]["uuid"],
"otherUsername": shared_with_who_test_data["otherUsername"],
"token": token,
}
)
assert create_share_response.error is False

# list users
file_response = app.test_client().get(
f"/file/{shared_with_who_test_data['file']['uuid']}/shared-with-who",
headers={"Authorization": f"Bearer {token}"},
)
json_response = file_response.get_json()

assert file_response.status_code == 200
assert len(json_response["users"]) == 1
assert json_response["users"][0] == shared_with_who_test_data["otherUsername"]


def test_shared_with_who_not_found():
# Login with the user
login_response = soap_client.service.auth_login(
{
"username": shared_with_who_test_data["username"],
"password": shared_with_who_test_data["password"],
}
)
assert login_response.error is False
token = login_response.auth.token

# Unexistent file UUID
response = app.test_client().get(
f"/file/{uuid4()}/shared-with-who",
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 404
6 changes: 6 additions & 0 deletions src/views/_file_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ def file_share(token):
@auth_middlewares.token_required
def move_file(token, file_uuid):
return FILES_HANDLERS["MOVE_FILE"](token, file_uuid)


@views.route("/file/<string:file_uuid>/shared-with-who", methods=["GET"])
@auth_middlewares.token_required
def shared_with_who(token, file_uuid):
return FILES_HANDLERS["SHARED_WITH_WHO"](token, file_uuid)

0 comments on commit 3281cca

Please sign in to comment.