|
| 1 | +""" |
| 2 | +test_app.py |
| 3 | +
|
| 4 | +Tests that the App class methods work as expected. |
| 5 | +
|
| 6 | +Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. |
| 7 | + |
| 8 | +Last updated: 19/08/2024 |
| 9 | +""" |
| 10 | + |
| 11 | +from exceptions.exceptions import ( |
| 12 | + AuthenticationException, |
| 13 | + NotFoundException, |
| 14 | + UnknownException, |
| 15 | +) |
| 16 | + |
| 17 | +from cs3resource import Resource |
| 18 | + |
| 19 | +import cs3.rpc.v1beta1.code_pb2 as cs3code |
| 20 | +from fixtures import ( # noqa: F401 (they are used, the framework is not detecting it) |
| 21 | + mock_config, |
| 22 | + mock_logger, |
| 23 | + mock_authentication, |
| 24 | + mock_gateway, |
| 25 | + app_instance, |
| 26 | + mock_status_code_handler, |
| 27 | +) |
| 28 | + |
| 29 | +from unittest.mock import Mock, patch |
| 30 | + |
| 31 | +import pytest |
| 32 | + |
| 33 | +# Test cases for the App class |
| 34 | +# Test cases for the App class `list_app_providers` method using parameterized tests |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + "status_code, status_message, expected_exception, providers", |
| 39 | + [ |
| 40 | + (cs3code.CODE_OK, None, None, ["provider1", "provider2"]), |
| 41 | + (cs3code.CODE_UNAUTHENTICATED, "error", AuthenticationException, None), |
| 42 | + (cs3code.CODE_INTERNAL, "error", UnknownException, None), |
| 43 | + ], |
| 44 | +) |
| 45 | +def test_list_app_providers( |
| 46 | + app_instance, status_code, status_message, expected_exception, providers # noqa: F811 (not a redefinition) |
| 47 | +): |
| 48 | + mock_response = Mock() |
| 49 | + mock_response.status.code = status_code |
| 50 | + mock_response.status.message = status_message |
| 51 | + mock_response.providers = providers |
| 52 | + |
| 53 | + with patch.object(app_instance._gateway, "ListAppProviders", return_value=mock_response): |
| 54 | + if expected_exception: |
| 55 | + with pytest.raises(expected_exception): |
| 56 | + app_instance.list_app_providers() |
| 57 | + else: |
| 58 | + result = app_instance.list_app_providers() |
| 59 | + assert result == providers |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "status_code, status_message, expected_exception, open_in_app_url", |
| 64 | + [ |
| 65 | + (cs3code.CODE_OK, None, None, "url"), |
| 66 | + (cs3code.CODE_UNAUTHENTICATED, "error", AuthenticationException, None), |
| 67 | + (cs3code.CODE_NOT_FOUND, "error", NotFoundException, None), |
| 68 | + (cs3code.CODE_INTERNAL, "error", UnknownException, None), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_open_in_app( |
| 72 | + app_instance, status_code, status_message, expected_exception, open_in_app_url # noqa: F811 (not a redefinition) |
| 73 | +): |
| 74 | + resource = Resource.from_file_ref_and_endpoint(endpoint="", file="testfile") |
| 75 | + view_mode = "VIEW_MODE_VIEW_ONLY" |
| 76 | + app = "app" |
| 77 | + |
| 78 | + mock_response = Mock() |
| 79 | + mock_response.status.code = status_code |
| 80 | + mock_response.status.message = status_message |
| 81 | + mock_response.OpenInAppURL = open_in_app_url |
| 82 | + |
| 83 | + with patch.object(app_instance._gateway, "OpenInApp", return_value=mock_response): |
| 84 | + if expected_exception: |
| 85 | + with pytest.raises(expected_exception): |
| 86 | + app_instance.open_in_app(resource, view_mode, app) |
| 87 | + else: |
| 88 | + result = app_instance.open_in_app(resource, view_mode, app) |
| 89 | + assert result == open_in_app_url |
0 commit comments