Skip to content

Commit

Permalink
added clients tests
Browse files Browse the repository at this point in the history
  • Loading branch information
k-shlomi committed Aug 21, 2023
1 parent fa06039 commit a713a25
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r requirements.txt
pytest==7.4.0
pytest-mock==3.11.1
flake8>=3.9.2
78 changes: 78 additions & 0 deletions tests/test_clients.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a713a25

Please sign in to comment.