diff --git a/examples/app_api_example.py b/examples/app_api_example.py index 202754e..8e91cd6 100644 --- a/examples/app_api_example.py +++ b/examples/app_api_example.py @@ -6,35 +6,29 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 19/08/2024 +Last updated: 28/08/2024 """ import logging import configparser -from cs3client import CS3Client -from cs3resource import Resource +from cs3client.cs3client import CS3Client +from cs3client.cs3resource import Resource config = configparser.ConfigParser() with open("default.conf") as fdef: config.read_file(fdef) -# log log = logging.getLogger(__name__) - client = CS3Client(config, "cs3client", log) -# client.auth.set_token("") -# OR client.auth.set_client_secret("") -print(client.auth.get_token()) - # list_app_providers -res = client.app.list_app_providers() +res = client.app.list_app_providers(client.auth.get_token()) if res is not None: print(res) # open_in_app resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/collabora.odt") -res = client.app.open_in_app(resource) +res = client.app.open_in_app(client.auth.get_token(), resource) if res is not None: print(res) diff --git a/examples/auth_example.py b/examples/auth_example.py new file mode 100644 index 0000000..d8c64d6 --- /dev/null +++ b/examples/auth_example.py @@ -0,0 +1,33 @@ +""" +auth_example.py + +Example script to demonstrate the usage of the app API in the CS3Client class. +note that these are examples, and is not meant to be run as a script. + +Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. +Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch +Last updated: 28/08/2024 +""" + +import logging +import configparser +from cs3client.cs3client import CS3Client + +config = configparser.ConfigParser() +with open("default.conf") as fdef: + config.read_file(fdef) +log = logging.getLogger(__name__) + +client = CS3Client(config, "cs3client", log) + +# Set client secret +client.auth.set_client_secret("") +# Checks if token is expired if not return ('x-access-token', ) +# if expired, request a new token from reva +auth_token = client.auth.get_token() + +# OR if you already have a reva token +# Checks if token is expired if not return (x-access-token', ) +# if expired, throws an AuthenticationException (so you can refresh your reva token) +token = "" +auth_token = client.auth.check_token(token) diff --git a/examples/checkpoints_api_example.py b/examples/checkpoints_api_example.py index db4c094..48f49d9 100644 --- a/examples/checkpoints_api_example.py +++ b/examples/checkpoints_api_example.py @@ -6,36 +6,33 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 19/08/2024 +Last updated: 28/08/2024 """ import logging import configparser -from cs3client import CS3Client -from cs3resource import Resource +from cs3client.cs3client import CS3Client +from cs3client.cs3resource import Resource config = configparser.ConfigParser() with open("default.conf") as fdef: config.read_file(fdef) -# log log = logging.getLogger(__name__) client = CS3Client(config, "cs3client", log) -# client.auth.set_token("") -# OR client.auth.set_client_secret("") res = None markdown_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/test.md") -res = client.checkpoint.list_file_versions(markdown_resource) +res = client.checkpoint.list_file_versions(client.auth.get_token(), markdown_resource) if res is not None: for ver in res: print(ver) -res = client.checkpoint.restore_file_version(markdown_resource, "1722936250.0569fa2f") +res = client.checkpoint.restore_file_version(client.auth.get_token(), markdown_resource, "1722936250.0569fa2f") if res is not None: for ver in res: print(ver) diff --git a/examples/file_api_example.py b/examples/file_api_example.py index 468449b..e160eb6 100644 --- a/examples/file_api_example.py +++ b/examples/file_api_example.py @@ -13,76 +13,70 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 01/08/2024 +Last updated: 28/08/2024 """ import logging import configparser -from cs3client import CS3Client -from cs3resource import Resource +from cs3client.cs3client import CS3Client +from cs3client.cs3resource import Resource config = configparser.ConfigParser() with open("default.conf") as fdef: config.read_file(fdef) -# log log = logging.getLogger(__name__) client = CS3Client(config, "cs3client", log) -client.auth.set_token("") -# OR -# client.auth.set_client_secret("") - -# Authentication -print(client.auth.get_token()) +client.auth.set_client_secret("") res = None # mkdir for i in range(1, 4): directory_resource = Resource.from_file_ref_and_endpoint(f"/eos/user/r/rwelande/test_directory{i}") - res = client.file.make_dir(directory_resource) + res = client.file.make_dir(client.auth.get_token(), directory_resource) if res is not None: print(res) # touchfile touch_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/touch_file.txt") text_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text_file.txt") -res = client.file.touch_file(touch_resource) -res = client.file.touch_file(text_resource) +res = client.file.touch_file(client.auth.get_token(), touch_resource) +res = client.file.touch_file(client.auth.get_token(), text_resource) if res is not None: print(res) # setxattr resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text_file.txt") -res = client.file.set_xattr(resource, "iop.wopi.lastwritetime", str(1720696124)) +res = client.file.set_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime", str(1720696124)) if res is not None: print(res) # rmxattr -res = client.file.remove_xattr(resource, "iop.wopi.lastwritetime") +res = client.file.remove_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime") if res is not None: print(res) # stat -res = client.file.stat(text_resource) +res = client.file.stat(client.auth.get_token(), text_resource) if res is not None: print(res) # removefile -res = client.file.remove_file(touch_resource) +res = client.file.remove_file(client.auth.get_token(), touch_resource) if res is not None: print(res) -res = client.file.touch_file(touch_resource) +res = client.file.touch_file(client.auth.get_token(), touch_resource) # rename rename_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/rename_file.txt") -res = client.file.rename_file(resource, rename_resource) +res = client.file.rename_file(client.auth.get_token(), resource, rename_resource) if res is not None: print(res) @@ -90,20 +84,20 @@ # writefile content = b"Hello World" size = len(content) -res = client.file.write_file(rename_resource, content, size) +res = client.file.write_file(client.auth.get_token(), rename_resource, content, size) if res is not None: print(res) # rmdir (same as deletefile) -res = client.file.remove_file(directory_resource) +res = client.file.remove_file(client.auth.get_token(), directory_resource) if res is not None: print(res) # listdir list_directory_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande") -res = client.file.list_dir(list_directory_resource) +res = client.file.list_dir(client.auth.get_token(), list_directory_resource) first_item = next(res, None) if first_item is not None: @@ -114,7 +108,7 @@ print("empty response") # readfile -file_res = client.file.read_file(rename_resource) +file_res = client.file.read_file(client.auth.get_token(), rename_resource) content = b"" try: for chunk in file_res: diff --git a/examples/shares_api_example.py b/examples/shares_api_example.py index 5a42c8d..6f19e43 100644 --- a/examples/shares_api_example.py +++ b/examples/shares_api_example.py @@ -6,43 +6,41 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 19/08/2024 +Last updated: 28/08/2024 """ import logging import configparser -from cs3client import CS3Client -from cs3resource import Resource +from cs3client.cs3client import CS3Client +from cs3client.cs3resource import Resource config = configparser.ConfigParser() with open("default.conf") as fdef: config.read_file(fdef) -# log log = logging.getLogger(__name__) client = CS3Client(config, "cs3client", log) -# client.auth.set_token("") -# OR client.auth.set_client_secret("") -# Authentication -print(client.auth.get_token()) - res = None # Create share # resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text.txt") -resource_info = client.file.stat(resource) +resource_info = client.file.stat(client.auth.get_token(), resource) # VIEWER -user = client.user.get_user_by_claim("mail", "diogo.castro@cern.ch") -res = client.share.create_share(resource_info, user.id.opaque_id, user.id.idp, "VIEWER", "USER") +user = client.user.get_user_by_claim(client.auth.get_token(), "mail", "diogo.castro@cern.ch") +res = client.share.create_share( + client.auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, "VIEWER", "USER" +) if res is not None: print(res) # EDITOR -user = client.user.get_user_by_claim("username", "lopresti") -res = client.share.create_share(resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER") +user = client.user.get_user_by_claim(client.auth.get_token(), "username", "lopresti") +res = client.share.create_share( + client.auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER" +) if res is not None: print(res) @@ -54,26 +52,26 @@ filter_list.append(filter) filter = client.share.create_share_filter(share_state="SHARE_STATE_PENDING", filter_type="TYPE_STATE") filter_list.append(filter) -res, _ = client.share.list_existing_shares() +res, _ = client.share.list_existing_shares(client.auth.get_token(), filter_list=filter_list) if res is not None: for share_info in res: print(share_info.share) # Get share # share_id = "58" -res = client.share.get_share(opaque_id=share_id) +res = client.share.get_share(client.auth.get_token(), opaque_id=share_id) if res is not None: print(res) # update share # share_id = "58" -res = client.share.update_share(opaque_id=share_id, role="VIEWER") +res = client.share.update_share(client.auth.get_token(), opaque_id=share_id, role="VIEWER") if res is not None: print(res) # remove share # share_id = "58" -res = client.share.remove_share(opaque_id=share_id) +res = client.share.remove_share(client.auth.get_token(), opaque_id=share_id) if res is not None: print(res) @@ -81,13 +79,15 @@ # Create a filter list filter_list = [] -filter = client.share.create_share_filter(share_state="SHARE_STATE_ACCEPTED", filter_type="TYPE_STATE") +filter = client.share.create_share_filter( + client.auth.get_token(), share_state="SHARE_STATE_ACCEPTED", filter_type="TYPE_STATE" +) # Append the filter to the filter list filter_list.append(filter) # NOTE: filters for received shares are not implemented (14/08/2024), therefore it is left out -res, _ = client.share.list_received_existing_shares() +res, _ = client.share.list_received_existing_shares(client.auth.get_token()) if res is not None: for share_info in res: print(share_info.received_share) @@ -95,17 +95,19 @@ # get received share # share_id = "43" -received_share = client.share.get_received_share(opaque_id=share_id) +received_share = client.share.get_received_share(client.auth.get_token(), opaque_id=share_id) if received_share is not None: print(received_share) # update recieved share # -res = client.share.update_received_share(received_share=received_share, state="SHARE_STATE_ACCEPTED") +res = client.share.update_received_share( + client.auth.get_token(), received_share=received_share, state="SHARE_STATE_ACCEPTED" +) if res is not None: print(res) # create public share # -res = client.share.create_public_share(resource_info, role="VIEWER") +res = client.share.create_public_share(client.auth.get_token(), resource_info, role="VIEWER") if res is not None: print(res) @@ -116,7 +118,7 @@ filter = client.share.create_public_share_filter(resource_id=resource_info.id, filter_type="TYPE_RESOURCE_ID") filter_list.append(filter) print(filter_list) -res, _ = client.share.list_existing_public_shares(filter_list=filter_list) +res, _ = client.share.list_existing_public_shares(client.auth.get_token(), filter_list=filter_list) if res is not None: for share_info in res: print(share_info.share) @@ -125,16 +127,18 @@ share_id = "63" # OR token = "7FbP1EBXJQTqK0d" -res = client.share.get_public_share(opaque_id=share_id, sign=True) +res = client.share.get_public_share(client.auth.get_token(), opaque_id=share_id, sign=True) if res is not None: print(res) # update public share # -res = client.share.update_public_share(type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello") +res = client.share.update_public_share( + client.auth.get_token(), type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello" +) if res is not None: print(res) # remove public share # -res = client.share.remove_public_share(token=token) +res = client.share.remove_public_share(client.auth.get_token(), token=token) if res is not None: print(res) diff --git a/examples/user_api_example.py b/examples/user_api_example.py index c4a6973..99ae54d 100644 --- a/examples/user_api_example.py +++ b/examples/user_api_example.py @@ -6,29 +6,26 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 02/08/2024 +Last updated: 28/08/2024 """ import logging import configparser -from cs3client import CS3Client +from cs3client.cs3client import CS3Client config = configparser.ConfigParser() with open("default.conf") as fdef: config.read_file(fdef) -# log log = logging.getLogger(__name__) client = CS3Client(config, "cs3client", log) -# client.auth.set_token("") -# OR client.auth.set_client_secret("") res = None # find_user -res = client.user.find_users("rwel") +res = client.user.find_users(client.auth.get_token(), "rwel") if res is not None: print(res) diff --git a/tests/fixtures.py b/tests/fixtures.py index 075f03e..316d0cb 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -9,7 +9,6 @@ """ -import sys import pytest from unittest.mock import Mock, patch from configparser import ConfigParser @@ -17,16 +16,15 @@ import base64 import json -sys.path.append("src/") -from cs3client import CS3Client # noqa: E402 -from file import File # noqa: E402 -from auth import Auth # noqa: E402 -from user import User # noqa: E402 -from statuscodehandler import StatusCodeHandler # noqa: E402 -from share import Share # noqa: E402 -from app import App # noqa: E402 -from checkpoint import Checkpoint # noqa: E402 -from config import Config # noqa: E402 +from cs3client.cs3client import CS3Client +from cs3client.file import File +from cs3client.auth import Auth +from cs3client.user import User +from cs3client.statuscodehandler import StatusCodeHandler +from cs3client.share import Share +from cs3client.app import App +from cs3client.checkpoint import Checkpoint +from cs3client.config import Config @pytest.fixture @@ -112,11 +110,11 @@ def mock_authentication(mock_gateway, mock_config, mock_logger): # (patches are applied from the bottom up) # and the last two parameters are inferred by pytest from existing fixtures @pytest.fixture -@patch("cs3client.grpc.secure_channel", autospec=True) -@patch("cs3client.grpc.channel_ready_future", autospec=True) -@patch("cs3client.grpc.insecure_channel", autospec=True) -@patch("cs3client.cs3gw_grpc.GatewayAPIStub", autospec=True) -@patch("cs3client.grpc.ssl_channel_credentials", autospec=True) +@patch("cs3client.cs3client.grpc.secure_channel", autospec=True) +@patch("cs3client.cs3client.grpc.channel_ready_future", autospec=True) +@patch("cs3client.cs3client.grpc.insecure_channel", autospec=True) +@patch("cs3client.cs3client.cs3gw_grpc.GatewayAPIStub", autospec=True) +@patch("cs3client.cs3client.grpc.ssl_channel_credentials", autospec=True) def cs3_client_secure( mock_ssl_channel_credentials, mock_gateway_stub_class, @@ -143,11 +141,11 @@ def cs3_client_secure( # (patches are applied from the bottom up) # and the last two parameters are inferred by pytest from existing fixtures @pytest.fixture -@patch("cs3client.grpc.secure_channel") -@patch("cs3client.grpc.insecure_channel") -@patch("cs3client.grpc.channel_ready_future") -@patch("cs3client.cs3gw_grpc.GatewayAPIStub") -@patch("cs3client.grpc.ssl_channel_credentials") +@patch("cs3client.cs3client.grpc.secure_channel") +@patch("cs3client.cs3client.grpc.insecure_channel") +@patch("cs3client.cs3client.grpc.channel_ready_future") +@patch("cs3client.cs3client.cs3gw_grpc.GatewayAPIStub") +@patch("cs3client.cs3client.grpc.ssl_channel_credentials") def cs3_client_insecure( mock_ssl_channel_credentials, mock_gateway_stub_class, @@ -171,28 +169,27 @@ def cs3_client_insecure( @pytest.fixture -def app_instance(mock_authentication, mock_gateway, mock_config, mock_logger, mock_status_code_handler): +def app_instance(mock_gateway, mock_config, mock_logger, mock_status_code_handler): app = App( - Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_authentication, mock_status_code_handler + Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_status_code_handler ) return app @pytest.fixture -def checkpoint_instance(mock_authentication, mock_gateway, mock_config, mock_logger, mock_status_code_handler): +def checkpoint_instance(mock_gateway, mock_config, mock_logger, mock_status_code_handler): checkpoint = Checkpoint( - Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_authentication, mock_status_code_handler + Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_status_code_handler ) return checkpoint @pytest.fixture -def share_instance(mock_authentication, mock_gateway, mock_config, mock_logger, mock_status_code_handler): +def share_instance(mock_gateway, mock_config, mock_logger, mock_status_code_handler): share = Share( Config(mock_config, "cs3client"), mock_logger, mock_gateway, - mock_authentication, mock_status_code_handler, ) return share @@ -200,16 +197,16 @@ def share_instance(mock_authentication, mock_gateway, mock_config, mock_logger, # All parameters are inferred by pytest from existing fixtures @pytest.fixture -def file_instance(mock_authentication, mock_gateway, mock_config, mock_logger, mock_status_code_handler): +def file_instance(mock_gateway, mock_config, mock_logger, mock_status_code_handler): file = File( - Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_authentication, mock_status_code_handler + Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_status_code_handler ) return file @pytest.fixture -def user_instance(mock_authentication, mock_gateway, mock_config, mock_logger, mock_status_code_handler): +def user_instance(mock_gateway, mock_config, mock_logger, mock_status_code_handler): user = User( - Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_authentication, mock_status_code_handler + Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_status_code_handler ) return user diff --git a/tests/test_app.py b/tests/test_app.py index 8e3cf93..17e5713 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -5,24 +5,19 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 19/08/2024 +Last updated: 28/08/2024 """ -import sys import cs3.rpc.v1beta1.code_pb2 as cs3code from unittest.mock import Mock, patch import pytest - -sys.path.append("src/") - -from exceptions.exceptions import ( # noqa: E402 +from cs3client.exceptions.exceptions import ( AuthenticationException, NotFoundException, UnknownException, ) -from cs3resource import Resource # noqa: E402 - -from fixtures import ( # noqa: F401, E402 (they are used, the framework is not detecting it) +from cs3client.cs3resource import Resource +from fixtures import ( # noqa: F401 (they are used, the framework is not detecting it) mock_config, mock_logger, mock_authentication, @@ -32,7 +27,6 @@ ) # Test cases for the App class -# Test cases for the App class `list_app_providers` method using parameterized tests @pytest.mark.parametrize( @@ -50,13 +44,14 @@ def test_list_app_providers( mock_response.status.code = status_code mock_response.status.message = status_message mock_response.providers = providers + auth_token = ('x-access-token', "some_token") with patch.object(app_instance._gateway, "ListAppProviders", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - app_instance.list_app_providers() + app_instance.list_app_providers(auth_token) else: - result = app_instance.list_app_providers() + result = app_instance.list_app_providers(auth_token) assert result == providers @@ -80,11 +75,12 @@ def test_open_in_app( mock_response.status.code = status_code mock_response.status.message = status_message mock_response.OpenInAppURL = open_in_app_url + auth_token = ('x-access-token', "some_token") with patch.object(app_instance._gateway, "OpenInApp", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - app_instance.open_in_app(resource, view_mode, app) + app_instance.open_in_app(auth_token, resource, view_mode, app) else: - result = app_instance.open_in_app(resource, view_mode, app) + result = app_instance.open_in_app(auth_token, resource, view_mode, app) assert result == open_in_app_url diff --git a/tests/test_checkpoint.py b/tests/test_checkpoint.py index 20c679d..4ea7434 100644 --- a/tests/test_checkpoint.py +++ b/tests/test_checkpoint.py @@ -5,25 +5,19 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 19/08/2024 +Last updated: 28/08/2024 """ -import sys from unittest.mock import Mock, patch import pytest import cs3.rpc.v1beta1.code_pb2 as cs3code - -sys.path.append("src/") - -from exceptions.exceptions import ( # noqa: E402 +from cs3client.exceptions.exceptions import ( AuthenticationException, NotFoundException, UnknownException, ) - -from cs3resource import Resource # noqa: E402 - -from fixtures import ( # noqa: F401, E402 (they are used, the framework is not detecting it) +from cs3client.cs3resource import Resource +from fixtures import ( # noqa: F401 (they are used, the framework is not detecting it) mock_config, mock_logger, mock_authentication, @@ -32,7 +26,6 @@ mock_status_code_handler, ) - # Test cases for the Checkpoint class @@ -56,13 +49,14 @@ def test_list_file_versions( mock_response.status.code = status_code mock_response.status.message = status_message mock_response.versions = versions + auth_token = ('x-access-token', "some_token") with patch.object(checkpoint_instance._gateway, "ListFileVersions", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - checkpoint_instance.list_file_versions(resource, page_token, page_size) + checkpoint_instance.list_file_versions(auth_token, resource, page_token, page_size) else: - result = checkpoint_instance.list_file_versions(resource, page_token, page_size) + result = checkpoint_instance.list_file_versions(auth_token, resource, page_token, page_size) assert result == versions @@ -85,11 +79,12 @@ def test_restore_file_version( mock_response = Mock() mock_response.status.code = status_code mock_response.status.message = status_message + auth_token = ('x-access-token', "some_token") with patch.object(checkpoint_instance._gateway, "RestoreFileVersion", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - checkpoint_instance.restore_file_version(resource, version_key, lock_id) + checkpoint_instance.restore_file_version(auth_token, resource, version_key, lock_id) else: - result = checkpoint_instance.restore_file_version(resource, version_key, lock_id) + result = checkpoint_instance.restore_file_version(auth_token, resource, version_key, lock_id) assert result is None diff --git a/tests/test_cs3client.py b/tests/test_cs3client.py index 2aa169c..8ecf5e3 100644 --- a/tests/test_cs3client.py +++ b/tests/test_cs3client.py @@ -8,10 +8,6 @@ Last updated: 26/07/2024 """ -import sys - -sys.path.append("src/") - from fixtures import ( # noqa: F401, E402 (they are used, the framework is not detecting it) cs3_client_insecure, cs3_client_secure, @@ -21,6 +17,8 @@ create_mock_jwt, ) +# Test cases for the cs3client class. + def test_cs3client_initialization_secure(cs3_client_secure): # noqa: F811 (not a redefinition) client = cs3_client_secure @@ -55,7 +53,6 @@ def test_cs3client_initialization_secure(cs3_client_secure): # noqa: F811 (not # Make sure file objects are correctly set assert client.file._gateway is not None - assert client.file._auth is not None assert client.file._config is not None assert client.file._log is not None @@ -93,6 +90,5 @@ def test_cs3client_initialization_insecure(cs3_client_insecure): # noqa: F811 ( # Make sure file objects are correctly set assert client.file._gateway is not None - assert client.file._auth is not None assert client.file._config is not None assert client.file._log is not None diff --git a/tests/test_file.py b/tests/test_file.py index 24d0a94..c9ee13d 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -5,24 +5,20 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 19/08/2024 +Last updated: 28/08/2024 """ -import sys import pytest from unittest.mock import Mock, patch import cs3.rpc.v1beta1.code_pb2 as cs3code - -sys.path.append("src/") - -from cs3resource import Resource # noqa: E402 -from exceptions.exceptions import ( # noqa: E402 +from cs3client.cs3resource import Resource +from cs3client.exceptions.exceptions import ( AuthenticationException, NotFoundException, FileLockedException, UnknownException, ) -from fixtures import ( # noqa: F401, E402 (they are used, the framework is not detecting it) +from fixtures import ( # noqa: F401 (they are used, the framework is not detecting it) mock_config, mock_logger, mock_authentication, @@ -31,6 +27,8 @@ mock_status_code_handler, ) +# Test cases for the file class. + @pytest.mark.parametrize( "status_code, status_message, expected_exception, expected_result", @@ -48,15 +46,16 @@ def test_stat( mock_response = Mock() mock_response.status.code = status_code mock_response.status.message = status_message + auth_token = ('x-access-token', "some_token") if status_code == cs3code.CODE_OK: mock_response.info = status_message with patch.object(file_instance._gateway, "Stat", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - file_instance.stat(resource) + file_instance.stat(auth_token, resource) else: - result = file_instance.stat(resource) + result = file_instance.stat(auth_token, resource) assert result == expected_result @@ -76,13 +75,14 @@ def test_set_xattr(file_instance, status_code, status_message, expected_exceptio mock_response = Mock() mock_response.status.code = status_code mock_response.status.message = status_message + auth_token = ('x-access-token', "some_token") with patch.object(file_instance._gateway, "SetArbitraryMetadata", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - file_instance.set_xattr(resource, key, value) + file_instance.set_xattr(auth_token, resource, key, value) else: - file_instance.set_xattr(resource, key, value) + file_instance.set_xattr(auth_token, resource, key, value) @pytest.mark.parametrize( @@ -103,13 +103,14 @@ def test_remove_xattr( mock_response = Mock() mock_response.status.code = status_code mock_response.status.message = status_message + auth_token = ('x-access-token', "some_token") with patch.object(file_instance._gateway, "UnsetArbitraryMetadata", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - file_instance.remove_xattr(resource, key) + file_instance.remove_xattr(auth_token, resource, key) else: - file_instance.remove_xattr(resource, key) + file_instance.remove_xattr(auth_token, resource, key) @pytest.mark.parametrize( @@ -129,13 +130,14 @@ def test_rename_file(file_instance, status_code, status_message, expected_except mock_response = Mock() mock_response.status.code = status_code mock_response.status.message = status_message + auth_token = ('x-access-token', "some_token") with patch.object(file_instance._gateway, "Move", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - file_instance.rename_file(resource, newresource) + file_instance.rename_file(auth_token, resource, newresource) else: - file_instance.rename_file(resource, newresource) + file_instance.rename_file(auth_token, resource, newresource) @pytest.mark.parametrize( @@ -152,13 +154,14 @@ def test_remove_file(file_instance, status_code, status_message, expected_except mock_response = Mock() mock_response.status.code = status_code mock_response.status.message = status_message + auth_token = ('x-access-token', "some_token") with patch.object(file_instance._gateway, "Delete", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - file_instance.remove_file(resource) + file_instance.remove_file(auth_token, resource) else: - file_instance.remove_file(resource) + file_instance.remove_file(auth_token, resource) @pytest.mark.parametrize( @@ -175,13 +178,14 @@ def test_touch_file(file_instance, status_code, status_message, expected_excepti mock_response = Mock() mock_response.status.code = status_code mock_response.status.message = status_message + auth_token = ('x-access-token', "some_token") with patch.object(file_instance._gateway, "TouchFile", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - file_instance.touch_file(resource) + file_instance.touch_file(auth_token, resource) else: - file_instance.touch_file(resource) + file_instance.touch_file(auth_token, resource) @pytest.mark.parametrize( @@ -208,6 +212,7 @@ def test_write_file( mock_upload_response.status.code = status_code mock_upload_response.status.message = status_message mock_upload_response.protocols = [Mock(protocol="simple", upload_endpoint="http://example.com", token="token")] + auth_token = ('x-access-token', "some_token") mock_put_response = Mock() mock_put_response.status_code = put_response_status @@ -215,10 +220,10 @@ def test_write_file( with patch.object(file_instance._gateway, "InitiateFileUpload", return_value=mock_upload_response): if expected_exception: with pytest.raises(expected_exception): - file_instance.write_file(resource, content, size) + file_instance.write_file(auth_token, resource, content, size) else: with patch("requests.put", return_value=mock_put_response): - file_instance.write_file(resource, content, size) + file_instance.write_file(auth_token, resource, content, size) @pytest.mark.parametrize( @@ -235,13 +240,14 @@ def test_make_dir(file_instance, status_code, status_message, expected_exception mock_response = Mock() mock_response.status.code = status_code mock_response.status.message = status_message + auth_token = ('x-access-token', "some_token") with patch.object(file_instance._gateway, "CreateContainer", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - file_instance.make_dir(resource) + file_instance.make_dir(auth_token, resource) else: - file_instance.make_dir(resource) + file_instance.make_dir(auth_token, resource) @pytest.mark.parametrize( @@ -262,18 +268,19 @@ def test_list_dir( mock_response.status.code = status_code mock_response.status.message = status_message mock_response.infos = infos + auth_token = ('x-access-token', "some_token") with patch.object(file_instance._gateway, "ListContainer", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - res = file_instance.list_dir(resource) + res = file_instance.list_dir(auth_token, resource) # Lazy evaluation first_item = next(res, None) if first_item is not None: for _ in res: pass else: - res = file_instance.list_dir(resource) + res = file_instance.list_dir(auth_token, resource) # Lazy evaluation first_item = next(res, None) assert first_item == "file1" @@ -303,11 +310,12 @@ def test_read_file( mock_fileget_response = Mock() mock_fileget_response.status_code = 200 mock_fileget_response.iter_content = Mock(return_value=iter_content) + auth_token = ('x-access-token', "some_token") with patch.object(file_instance._gateway, "InitiateFileDownload", return_value=mock_download_response): if expected_exception: with pytest.raises(expected_exception): - res = file_instance.read_file(resource) + res = file_instance.read_file(auth_token, resource) # Lazy evaluation first_item = next(res, None) if first_item is not None: @@ -315,7 +323,7 @@ def test_read_file( pass else: with patch("requests.get", return_value=mock_fileget_response): - res = file_instance.read_file(resource) + res = file_instance.read_file(auth_token, resource) # Lazy evaluation chunks = list(res) assert chunks == iter_content diff --git a/tests/test_resource.py b/tests/test_resource.py index 4ce89c2..7b3fa24 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -8,13 +8,9 @@ Last updated: 26/07/2024 """ -import sys import unittest import cs3.storage.provider.v1beta1.resources_pb2 as cs3spr - -sys.path.append("src/") - -from cs3resource import Resource # noqa: E402 +from cs3client.cs3resource import Resource class TestResource(unittest.TestCase): diff --git a/tests/test_share.py b/tests/test_share.py index 9a80add..d0e7d76 100644 --- a/tests/test_share.py +++ b/tests/test_share.py @@ -5,26 +5,23 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 19/08/2024 +Last updated: 28/08/2024 """ -import sys import pytest from unittest.mock import Mock, patch import cs3.sharing.collaboration.v1beta1.resources_pb2 as cs3scr import cs3.sharing.link.v1beta1.link_api_pb2 as cs3slapi import cs3.storage.provider.v1beta1.resources_pb2 as cs3spr import cs3.rpc.v1beta1.code_pb2 as cs3code - -sys.path.append("src/") -from exceptions.exceptions import ( # noqa: E402 +from cs3client.exceptions.exceptions import ( AuthenticationException, NotFoundException, UnknownException, FileLockedException, AlreadyExistsException, ) -from fixtures import ( # noqa: F401, E402 (they are used, the framework is not detecting it) +from fixtures import ( # noqa: F401 (they are used, the framework is not detecting it) mock_config, mock_logger, mock_authentication, @@ -33,8 +30,7 @@ mock_status_code_handler, ) - -# Test cases for the Share class `get_share` method using parameterized tests +# Test cases for the Share class. @pytest.mark.parametrize( @@ -63,16 +59,27 @@ def test_create_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = status_message + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "CreateShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): share_instance.create_share( - resource_info=resource_info, opaque_id=opaque_id, idp=idp, role=role, grantee_type=grantee_type + auth_token, + resource_info=resource_info, + opaque_id=opaque_id, + idp=idp, + role=role, + grantee_type=grantee_type ) else: result = share_instance.create_share( - resource_info=resource_info, opaque_id=opaque_id, idp=idp, role=role, grantee_type=grantee_type + auth_token, + resource_info=resource_info, + opaque_id=opaque_id, + idp=idp, + role=role, + grantee_type=grantee_type ) assert result == expected_result @@ -94,13 +101,14 @@ def test_list_existing_shares( if status_code == cs3code.CODE_OK: mock_response.share_infos = expected_result[0] mock_response.next_page_token = expected_result[1] + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "ListExistingShares", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.list_existing_shares() + share_instance.list_existing_shares(auth_token) else: - result = share_instance.list_existing_shares() + result = share_instance.list_existing_shares(auth_token) assert result == expected_result @@ -123,13 +131,14 @@ def test_get_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "GetShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.get_share(share_id) + share_instance.get_share(auth_token, share_id) else: - result = share_instance.get_share(share_id) + result = share_instance.get_share(auth_token, share_id) assert result == expected_result @@ -152,13 +161,14 @@ def test_remove_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "RemoveShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.remove_share(share_id) + share_instance.remove_share(auth_token, share_id) else: - result = share_instance.remove_share(share_id) + result = share_instance.remove_share(auth_token, share_id) assert result is None @@ -183,13 +193,14 @@ def test_update_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "UpdateShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.update_share(role=role, opaque_id=opaque_id) + share_instance.update_share(auth_token, role=role, opaque_id=opaque_id) else: - result = share_instance.update_share(role=role, opaque_id=opaque_id) + result = share_instance.update_share(auth_token, role=role, opaque_id=opaque_id) assert result == expected_result @@ -211,13 +222,14 @@ def test_list_existing_received_shares( if status_code == cs3code.CODE_OK: mock_response.share_infos = expected_result[0] mock_response.next_page_token = expected_result[1] + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "ListExistingReceivedShares", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.list_received_existing_shares() + share_instance.list_received_existing_shares(auth_token) else: - result = share_instance.list_received_existing_shares() + result = share_instance.list_received_existing_shares(auth_token) assert result == expected_result @@ -240,13 +252,14 @@ def test_get_received_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "GetReceivedShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.get_received_share(share_id) + share_instance.get_received_share(auth_token, share_id) else: - result = share_instance.get_received_share(share_id) + result = share_instance.get_received_share(auth_token, share_id) assert result == expected_result @@ -271,13 +284,14 @@ def test_update_received_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "UpdateReceivedShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.update_received_share(received_share=received_share) + share_instance.update_received_share(auth_token, received_share=received_share) else: - result = share_instance.update_received_share(received_share=received_share) + result = share_instance.update_received_share(auth_token, received_share=received_share) assert result == expected_result @@ -304,13 +318,14 @@ def test_create_public_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "CreatePublicShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.create_public_share(resource_info=resource_info, role=role) + share_instance.create_public_share(auth_token, resource_info=resource_info, role=role) else: - result = share_instance.create_public_share(resource_info=resource_info, role=role) + result = share_instance.create_public_share(auth_token, resource_info=resource_info, role=role) assert result == expected_result @@ -331,13 +346,14 @@ def list_existing_public_shares( if status_code == cs3code.CODE_OK: mock_response.share_infos = expected_result[0] mock_response.next_page_token = expected_result[1] + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "ListExistingPublicShares", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.list_existing_public_shares() + share_instance.list_existing_public_shares(auth_token) else: - result = share_instance.list_existing_public_shares() + result = share_instance.list_existing_public_shares(auth_token) assert result == expected_result @@ -360,13 +376,14 @@ def test_get_public_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "GetPublicShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.get_public_share(share_id) + share_instance.get_public_share(auth_token, share_id) else: - result = share_instance.get_public_share(share_id) + result = share_instance.get_public_share(auth_token, share_id) assert result == expected_result @@ -391,13 +408,14 @@ def test_update_public_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "UpdatePublicShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.update_public_share(type=type, role=role, opaque_id=opqaue_id) + share_instance.update_public_share(auth_token, type=type, role=role, opaque_id=opqaue_id) else: - result = share_instance.update_public_share(type=type, role=role, opaque_id=opqaue_id) + result = share_instance.update_public_share(auth_token, type=type, role=role, opaque_id=opqaue_id) assert result == expected_result @@ -423,13 +441,14 @@ def test_remove_public_share( mock_response.status.message = status_message if status_code == cs3code.CODE_OK: mock_response.share = expected_result + auth_token = ('x-access-token', "some_token") with patch.object(share_instance._gateway, "RemovePublicShare", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - share_instance.remove_public_share(share_id) + share_instance.remove_public_share(auth_token, share_id) else: - result = share_instance.remove_public_share(share_id) + result = share_instance.remove_public_share(auth_token, share_id) assert result is None diff --git a/tests/test_user.py b/tests/test_user.py index 8c51879..dcc5c45 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -5,22 +5,18 @@ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch -Last updated: 19/08/2024 +Last updated: 28/08/2024 """ -import sys import pytest from unittest.mock import Mock, patch import cs3.rpc.v1beta1.code_pb2 as cs3code - -sys.path.append("src/") - -from exceptions.exceptions import ( # noqa: E402 +from cs3client.exceptions.exceptions import ( AuthenticationException, NotFoundException, UnknownException, ) -from fixtures import ( # noqa: F401, E402 (they are used, the framework is not detecting it) +from fixtures import ( # noqa: F401 (they are used, the framework is not detecting it) mock_config, mock_logger, mock_authentication, @@ -29,8 +25,9 @@ mock_status_code_handler, ) - # Test cases for the User class + + @pytest.mark.parametrize( "status_code, status_message, expected_exception, user_data", [ @@ -133,11 +130,12 @@ def test_find_users( mock_response.status.code = status_code mock_response.status.message = status_message mock_response.users = users + auth_token = ('x-access-token', "some_token") with patch.object(user_instance._gateway, "FindUsers", return_value=mock_response): if expected_exception: with pytest.raises(expected_exception): - user_instance.find_users(filter) + user_instance.find_users(auth_token, filter) else: - result = user_instance.find_users(filter) + result = user_instance.find_users(auth_token, filter) assert result == users