|
| 1 | +import pytest |
| 2 | +from requests import Session |
| 3 | + |
| 4 | +from zephyr.scale.scale import DEFAULT_BASE_URL, ZephyrSession |
| 5 | +from zephyr.scale.zephyr_session import INIT_SESSION_MSG, InvalidAuthData |
| 6 | + |
| 7 | +REQUESTS_SESSION_PATH = "requests.sessions.Session" |
| 8 | +GETLOGGER_PATH = "logging.getLogger" |
| 9 | +LOGGER_DEBUG_PATH = "logging.Logger.debug" |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.unit |
| 13 | +class TestZephyrSession: |
| 14 | + def test_creation(self, mocker): |
| 15 | + """Tests basic creation logic""" |
| 16 | + logger_mock = mocker.patch(GETLOGGER_PATH) |
| 17 | + |
| 18 | + zsession = ZephyrSession(DEFAULT_BASE_URL, token="token_test") |
| 19 | + |
| 20 | + assert zsession.base_url == DEFAULT_BASE_URL, (f"Attribute base_url expected to be {DEFAULT_BASE_URL}, " |
| 21 | + f"not {zsession.base_url}") |
| 22 | + assert isinstance(zsession._session, Session) |
| 23 | + logger_mock.assert_called_with("zephyr.scale.zephyr_session") |
| 24 | + |
| 25 | + def test_token_auth(self, mocker): |
| 26 | + """Test token auth""" |
| 27 | + token = "test_token" |
| 28 | + logger_mock = mocker.patch(LOGGER_DEBUG_PATH) |
| 29 | + |
| 30 | + zsession = ZephyrSession(DEFAULT_BASE_URL, token=token) |
| 31 | + |
| 32 | + logger_mock.assert_called_with(INIT_SESSION_MSG.format("token")) |
| 33 | + assert f"Bearer {token}" == zsession._session.headers.get("Authorization") |
| 34 | + |
| 35 | + def test_credentials_auth(self, mocker): |
| 36 | + """Test auth with username and password""" |
| 37 | + username = "usertest" |
| 38 | + password = "pwdtest" |
| 39 | + logger_mock = mocker.patch(LOGGER_DEBUG_PATH) |
| 40 | + |
| 41 | + zsession = ZephyrSession(DEFAULT_BASE_URL, username=username, password=password) |
| 42 | + |
| 43 | + logger_mock.assert_called_with(INIT_SESSION_MSG.format("username and password")) |
| 44 | + assert (username, password) == zsession._session.auth |
| 45 | + |
| 46 | + def test_cookie_auth(self, mocker): |
| 47 | + """Test auth with cookie dict""" |
| 48 | + test_cookie = {"cookies": {"cookie.token": "cookie_test"}} |
| 49 | + logger_mock = mocker.patch(LOGGER_DEBUG_PATH) |
| 50 | + |
| 51 | + zsession = ZephyrSession(DEFAULT_BASE_URL, cookies=test_cookie) |
| 52 | + |
| 53 | + logger_mock.assert_called_with(INIT_SESSION_MSG.format("cookies")) |
| 54 | + assert test_cookie['cookies'] in zsession._session.cookies.values() |
| 55 | + |
| 56 | + @pytest.mark.parametrize("auth_data, exception", [(dict(), InvalidAuthData), |
| 57 | + ({"username": "user"}, InvalidAuthData), |
| 58 | + ({"password": "pwd"}, InvalidAuthData)]) |
| 59 | + def test_auth_exception(self, auth_data, exception): |
| 60 | + """Test exceptions on auth""" |
| 61 | + with pytest.raises(exception): |
| 62 | + ZephyrSession(DEFAULT_BASE_URL, **auth_data) |
| 63 | + |
| 64 | + @pytest.mark.parametrize("creation_kwargs", |
| 65 | + [{"token": "token_test", |
| 66 | + "session_attrs": {'verify': False, "max_redirects": 333}}]) |
| 67 | + def test_requests_session_attrs(self, creation_kwargs, mocker): |
| 68 | + """The test checks ZephyrScale (not) provided with "session_attrs".""" |
| 69 | + logger_mock = mocker.patch(LOGGER_DEBUG_PATH) |
| 70 | + session_attrs = creation_kwargs.get('session_attrs') |
| 71 | + |
| 72 | + zsession = ZephyrSession(DEFAULT_BASE_URL, **creation_kwargs) |
| 73 | + |
| 74 | + logger_mock.assert_called_with( |
| 75 | + f"Modify requests session object with {session_attrs}") |
| 76 | + |
| 77 | + for attrib, value in session_attrs.items(): |
| 78 | + actual = getattr(zsession._session, attrib) |
| 79 | + assert actual == session_attrs[attrib], (f"Request session attr {attrib} is {actual}, " |
| 80 | + f"but expected{session_attrs[attrib]}") |
| 81 | + |
| 82 | + @pytest.mark.skip |
| 83 | + def test_crud_request(self): |
| 84 | + """Test GET, POST, PUT, DELETE requests""" |
| 85 | + pass |
| 86 | + |
| 87 | + @pytest.mark.skip |
| 88 | + def test_get_paginated(self): |
| 89 | + """Test paginated request""" |
| 90 | + pass |
| 91 | + |
| 92 | + @pytest.mark.skip |
| 93 | + def test_post_file(self): |
| 94 | + """Test Post file wrapper""" |
| 95 | + pass |
0 commit comments