Skip to content

Commit

Permalink
add mock tests for views
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmanAsh committed Jul 22, 2024
1 parent 6605223 commit 55876c6
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 45 deletions.
2 changes: 1 addition & 1 deletion api/views/agreement_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def check_signed(self, _, **url_params: str):
url=f"https://api.github.com/repos/{settings.GH_ORG}/{settings.GH_REPO}/commits",
headers={"X-GitHub-Api-Version": "2022-11-28"},
params=t.cast(DataDict, {"path": settings.GH_FILE, "per_page": 1}),
timeout=10,
timeout=5,
)
if not response.ok:
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Expand Down
164 changes: 120 additions & 44 deletions api/views/agreement_signature_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
Created on 16/07/2024 at 14:59:49(+01:00).
"""

from unittest.mock import patch
import json
import typing as t
from datetime import timedelta
from unittest.mock import call, patch

import requests
from codeforlife.tests import ModelViewSetTestCase
from codeforlife.types import DataDict
from codeforlife.user.models import User
from django.utils import timezone
from rest_framework import status

import settings

from ..models import AgreementSignature, Contributor
from .agreement_signature import AgreementSignatureViewSet

Expand Down Expand Up @@ -74,66 +82,134 @@ def test_list(self):
models=[self.agreement1, self.agreement2, self.agreement3]
)

# TODO: salman
def test_create(self):
"""Can create a contributor signature."""
self.client.create(
data={
"contributor": 4,
"agreement_id": "81efd9e68f161104071f7bef7f9256e4840c1af7",
"signed_at": "2024-01-02T12:00:00Z",
"signed_at": timezone.now() - timedelta(days=1),
},
)

# TODO: salman
def test_check_signed(self):
def test_check_signed__signed(self):
"""
Can check if user has signed the latest contribution agreement.
Contributor has signed the latest agreement.
"""
self.client.get(
self.reverse_action(
"check_signed",
kwargs={"contributor_pk": 1},
),
status_code_assertion=status.HTTP_200_OK,
)
agreement_id = "76241fa5e96ce9a620472842fee1ddadfd13cd86"
response = requests.Response()
response.status_code = status.HTTP_200_OK
response.encoding = "utf-8"
# pylint: disable-next=protected-access
response._content = json.dumps([{"sha": agreement_id}]).encode("utf-8")
with patch.object(
requests, "get", return_value=response
) as requests_get:
self.client.get(
self.reverse_action(
"check_signed",
kwargs={"contributor_pk": 1},
),
status_code_assertion=status.HTTP_200_OK,
)

requests_get.assert_called_once_with(
# pylint: disable-next=line-too-long
url=f"https://api.github.com/repos/{settings.GH_ORG}/{settings.GH_REPO}/commits",
headers={"X-GitHub-Api-Version": "2022-11-28"},
params=t.cast(
DataDict, {"path": settings.GH_FILE, "per_page": 1}
),
timeout=5,
)

def test_check_signed__no_response(self):
"""
API cannot process the get request.
"""
agreement_id = "76241fa5e96ce9a620472842fee1ddadfd13cd86"
response = requests.Response()
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
with patch.object(
requests, "get", return_value=response
) as requests_get:
self.client.get(
self.reverse_action(
"check_signed",
kwargs={"contributor_pk": 3},
),
status_code_assertion=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

requests_get.assert_called_once_with(
# pylint: disable-next=line-too-long
url=f"https://api.github.com/repos/{settings.GH_ORG}/{settings.GH_REPO}/commits",
headers={"X-GitHub-Api-Version": "2022-11-28"},
params=t.cast(
DataDict, {"path": settings.GH_FILE, "per_page": 1}
),
timeout=5,
)

# TODO: salman
def test_check_signed__not_signed(self):
"""
Can check if user has NOT signed ANY contribution agreement.
"""
self.client.get(
self.reverse_action(
"check_signed",
kwargs={"contributor_pk": 3},
),
status_code_assertion=status.HTTP_404_NOT_FOUND,
)
agreement_id = "76241fa5e96ce9a620472842fee1ddadfd13cd86"
response = requests.Response()
response.status_code = status.HTTP_200_OK
response.encoding = "utf-8"
# pylint: disable-next=protected-access
response._content = json.dumps([{"sha": agreement_id}]).encode("utf-8")
with patch.object(
requests, "get", return_value=response
) as requests_get:
self.client.get(
self.reverse_action(
"check_signed",
kwargs={"contributor_pk": 3},
),
status_code_assertion=status.HTTP_404_NOT_FOUND,
)

requests_get.assert_called_once_with(
# pylint: disable-next=line-too-long
url=f"https://api.github.com/repos/{settings.GH_ORG}/{settings.GH_REPO}/commits",
headers={"X-GitHub-Api-Version": "2022-11-28"},
params=t.cast(
DataDict, {"path": settings.GH_FILE, "per_page": 1}
),
timeout=5,
)

# TODO: salman
def test_check_signed__not_latest_agreement(self):
"""
Can check if user has signed an contribution agreement
but it is not the latest one.
Contributor has signed an agreement but it is not the latest one.
"""
self.client.get(
self.reverse_action(
"check_signed",
kwargs={"contributor_pk": 2},
),
status_code_assertion=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS,
)

# TODO: salman
def test_check_signed__no_contributor(self):
"""
Can check if user is not a contributor at all.
"""
self.client.get(
self.reverse_action(
"check_signed",
kwargs={"contributor_pk": 190},
),
status_code_assertion=status.HTTP_404_NOT_FOUND,
)
agreement_id = "76241fa5e96ce9a620472842fee1ddadfd13cd86"
response = requests.Response()
response.status_code = status.HTTP_200_OK
response.encoding = "utf-8"
# pylint: disable-next=protected-access
response._content = json.dumps([{"sha": agreement_id}]).encode("utf-8")
with patch.object(
requests, "get", return_value=response
) as requests_get:
self.client.get(
self.reverse_action(
"check_signed",
kwargs={"contributor_pk": 2},
),
# pylint: disable-next=line-too-long
status_code_assertion=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS,
)

requests_get.assert_called_once_with(
# pylint: disable-next=line-too-long
url=f"https://api.github.com/repos/{settings.GH_ORG}/{settings.GH_REPO}/commits",
headers={"X-GitHub-Api-Version": "2022-11-28"},
params=t.cast(
DataDict, {"path": settings.GH_FILE, "per_page": 1}
),
timeout=5,
)

0 comments on commit 55876c6

Please sign in to comment.