From a713a25e0b6fd8ac5e3a911b7e4382d1f7c76c7b Mon Sep 17 00:00:00 2001 From: jit-shlomi Date: Mon, 21 Aug 2023 14:10:48 +0300 Subject: [PATCH] added clients tests --- requirements-test.txt | 1 + tests/test_clients.py | 78 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tests/test_clients.py diff --git a/requirements-test.txt b/requirements-test.txt index 71a6e3a..b932079 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,3 +1,4 @@ -r requirements.txt pytest==7.4.0 +pytest-mock==3.11.1 flake8>=3.9.2 diff --git a/tests/test_clients.py b/tests/test_clients.py new file mode 100644 index 0000000..0311872 --- /dev/null +++ b/tests/test_clients.py @@ -0,0 +1,78 @@ +import pytest + +from clients.frontegg import get_jwt_token, FRONTEGG_AUTH_URL +from clients.github import get_repos_from_github +from models import RepositoryDetails + + +# Sample data +class MockRepo: + def __init__(self, name, topics): + self.name = name + self._topics = topics + + def get_topics(self): + return self._topics + + +@pytest.mark.parametrize( + "mock_repos, expected_result", + [ + ([], []), + ([MockRepo("repo1", [])], []), + ([MockRepo("repo1", ["topic1"]), MockRepo("repo2", ["topic2"])], + [RepositoryDetails(name="repo1", topics=["topic1"]), RepositoryDetails(name="repo2", topics=["topic2"])]), + ] +) +def test_get_repos_from_github(mock_repos, expected_result, mocker): + # Mocking Github instance methods + github_mock = mocker.Mock() + organization_mock = mocker.Mock() + + organization_mock.get_repos.return_value = mock_repos + github_mock.get_organization.return_value = organization_mock + mocker.patch("clients.github.Github", return_value=github_mock) # Adjust the import path. + + # Run the function + repositories = get_repos_from_github() + + assert repositories == expected_result + + +def test_get_repos_from_github_exception(mocker): + # Mocking Github to raise an exception + mocker.patch("clients.github.Github", side_effect=Exception("Sample exception")) # Adjust the import path. + + # Test that the function logs an error and returns None + result = get_repos_from_github() + assert result is None + + +@pytest.mark.parametrize( + "status_code, expected_result", + [ + (200, "access_token"), + (401, None), + (500, None), + ] +) +def test_get_jwt_token(status_code, expected_result, mocker): + # Mocking the response + response_mock = mocker.Mock() + response_mock.status_code = status_code + + if status_code == 200: + response_mock.json.return_value = {"accessToken": "access_token"} + else: + response_mock.json.return_value = {} + + requests_post_mock = mocker.patch("requests.post", return_value=response_mock) + + token = get_jwt_token() + + requests_post_mock.assert_called_once_with( + FRONTEGG_AUTH_URL, + json={"clientId": None, "secret": None}, + headers={"accept": "application/json", "content-type": "application/json"} + ) + assert token == expected_result