diff --git a/api/views/agreement_signature.py b/api/views/agreement_signature.py index f8aa3ff..37ceb76 100644 --- a/api/views/agreement_signature.py +++ b/api/views/agreement_signature.py @@ -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) diff --git a/api/views/agreement_signature_test.py b/api/views/agreement_signature_test.py index ed763b5..1ad8810 100644 --- a/api/views/agreement_signature_test.py +++ b/api/views/agreement_signature_test.py @@ -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 @@ -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, + )