From 91e948580052cb46f2301a5552c3cc17e76a1825 Mon Sep 17 00:00:00 2001 From: Gerald Iakobinyi-Pich Date: Fri, 3 Dec 2021 16:14:32 +0200 Subject: [PATCH] GITC-478: Adding tests --- app/git/tests/factories/__init__.py | 0 app/git/tests/factories/git_cache_factory.py | 19 ++++++ app/git/tests/models/__init__.py | 0 app/git/tests/models/test_git_cache.py | 40 ++++++++++++ app/git/tests/test_utils.py | 64 +++++++++++++++++++- requirements/base.txt | 5 -- 6 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 app/git/tests/factories/__init__.py create mode 100644 app/git/tests/factories/git_cache_factory.py create mode 100644 app/git/tests/models/__init__.py create mode 100644 app/git/tests/models/test_git_cache.py diff --git a/app/git/tests/factories/__init__.py b/app/git/tests/factories/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/app/git/tests/factories/git_cache_factory.py b/app/git/tests/factories/git_cache_factory.py new file mode 100644 index 00000000000..ece87c8ecd0 --- /dev/null +++ b/app/git/tests/factories/git_cache_factory.py @@ -0,0 +1,19 @@ +import factory +import pytest +from git.models import GitCache + + +@pytest.mark.django_db +class GitCacheFactory(factory.django.DjangoModelFactory): + class Meta: + model = GitCache + + # Unique user handle + handle = factory.Sequence(lambda n: f"user_handle_{n}") + + # Cycle through the choices and select one + category = factory.Sequence(lambda n: GitCache.CATEGORY_CHOICES[n % len(GitCache.CATEGORY_CHOICES)][0]) + + # Generate binary data depending on n + data = factory.Sequence(lambda n: ("{n}" * 100).encode("utf-8")) + diff --git a/app/git/tests/models/__init__.py b/app/git/tests/models/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/app/git/tests/models/test_git_cache.py b/app/git/tests/models/test_git_cache.py new file mode 100644 index 00000000000..63e542b0539 --- /dev/null +++ b/app/git/tests/models/test_git_cache.py @@ -0,0 +1,40 @@ +from git.tests.factories.git_cache_factory import GitCacheFactory +import pytest +from git.models import GitCache + + +@pytest.mark.django_db +class TestGitCache: + """Test CLRMatch model.""" + + def test_creation(self): + """Test GitCache returned by factory is valid.""" + + git_cache = GitCacheFactory() + + assert isinstance(git_cache, GitCache) + + def test_get_user(self): + """Test get_user helper function.""" + + git_cache = GitCacheFactory() + git_cache.category = GitCache.Category.USER + handle = git_cache.handle + git_cache.save() + + saved = GitCache.get_user(handle) + assert git_cache.id == saved.id + + def test_update_data(self): + """Test update_data helper function.""" + + git_cache = GitCacheFactory() + git_cache.category = GitCache.Category.USER + handle = git_cache.handle + git_cache.save() + + new_data = "This is updated data".encode("utf-8") + git_cache.update_data(new_data) + + saved = GitCache.get_user(handle) + assert new_data == saved.data.tobytes() diff --git a/app/git/tests/test_utils.py b/app/git/tests/test_utils.py index 93a70659c07..39403c92ac2 100644 --- a/app/git/tests/test_utils.py +++ b/app/git/tests/test_utils.py @@ -19,16 +19,21 @@ """ from datetime import timedelta from urllib.parse import quote_plus, urlencode +from unittest.mock import MagicMock, patch +from github import NamedUser from django.conf import settings from django.test.utils import override_settings from django.utils import timezone +from git.models import GitCache +from git.tests.factories.git_cache_factory import GitCacheFactory +from faker import Faker import responses from git.utils import ( HEADERS, TOKEN_URL, build_auth_dict, delete_issue_comment, get_github_emails, get_github_primary_email, - get_issue_comments, get_issue_timeline_events, is_github_token_valid, org_name, patch_issue_comment, - post_issue_comment, post_issue_comment_reaction, repo_url, reset_token, revoke_token, + get_issue_comments, get_issue_timeline_events, github_connect, is_github_token_valid, org_name, patch_issue_comment, + post_issue_comment, post_issue_comment_reaction, repo_url, reset_token, revoke_token, _get_user ) from test_plus.test import TestCase @@ -230,3 +235,58 @@ def test_is_github_token_valid(self): # post_issue_comment_reaction(owner, repo, comment_id, 'A comment.') # assert responses.calls[0].request.url == url + + @responses.activate + @patch('git.utils.github_connect') + def test_get_user_caching(self, mock_github_connect): + """Test the github utility _get_user method.""" + fake = Faker() + + # Create a dummy handle and some binary data that is supposed to be the serialized user + user_handle = fake.user_name() + user_binary_data = fake.text().encode('utf-8') + + def dump_mock(user_obj, file_obj): + file_obj.write(user_binary_data) + + gh_user = MagicMock(spec=NamedUser) + gh_user.update = MagicMock() + + # Mock the gh client + gh_client = mock_github_connect() + gh_client.load = MagicMock(return_value=gh_user) + gh_client.dump = dump_mock + gh_client.get_user = MagicMock(return_value=gh_user) + + # Step 1: Make the call + _get_user(gh_client, user_handle) + + # Verify what was called and that the user has been cached: + # - expected: get_user + # - not expected: loaded, update - because user is new + gh_client.get_user.assert_called_once_with(user_handle) + gh_client.load.assert_not_called() + gh_user.update.assert_not_called() + + # Verify that user has been cached + saved_user = GitCache.get_user(user_handle) + + assert saved_user.handle == user_handle + assert saved_user.data.tobytes() == user_binary_data + + # Step 2: Repeat the call, user should be leaded from DB + gh_client.reset_mock() + gh_client.load.reset_mock() + gh_client.get_user.reset_mock() + + loaded_user = _get_user(gh_client, user_handle) + + # Verify what was called and that the user has been cached: + # - expected: load and update (for the user loaded from DB) + # - not expected: get_user (because user is already in DB) + gh_client.load.assert_called_once() + assert gh_client.load.call_args[0][0].getbuffer() == user_binary_data + gh_client.get_user.assert_not_called() + gh_user.update.assert_called_once() + + assert loaded_user == gh_user \ No newline at end of file diff --git a/requirements/base.txt b/requirements/base.txt index beb683cf1b9..e84143c62c3 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -21,8 +21,6 @@ requests_oauthlib Pillow==8.3.2 premailer populus -psycopg2-binary==2.7.5 -PyJWT==1.5.3 PyPdf2 python-twitter==3.2 sendgrid==5.6.0 @@ -44,7 +42,6 @@ google-api-python-client==1.7.3 django-environ==0.4.5 eth-utils==1.4.1 jsondiff==1.1.1 -social-auth-app-django==5.0.0 django-ipware==2.0.2 geoip2==2.8.0 django-silk==2.0.0 @@ -52,7 +49,6 @@ django-extensions==2.2.1 ipdb==0.13.9 django-autotranslate==1.1.1 svgutils==0.3.0 -watchdog==0.9.0 Werkzeug[watchdog]==0.15.5 imageio boto3==1.18.22 @@ -109,7 +105,6 @@ pyPEG2==2.15.2 base58==2.1.0 libnacl==1.7.2 pyaes==1.6.1 -numpy==1.19.5 rjsmin==1.1.0 rcssmin==1.0.6 libsass==0.20.1