diff --git a/Access/tests/features/get_grant_failed_requests.feature b/Access/tests/features/get_grant_failed_requests.feature new file mode 100644 index 00000000..e400b66c --- /dev/null +++ b/Access/tests/features/get_grant_failed_requests.feature @@ -0,0 +1,11 @@ +Feature: test get_grant_fail_requests + + Scenario: Retrieving all grant failure requests + Given there are grant failure requests in the database + When the get_grant_failed_requests method is called + Then it should return a context containing all grant failure requests sorted by requested_on date in descending order + + Scenario: Handling errors + Given an error occurs while executing the method + When the get_grant_failed_requests method is called + Then it should return an error response with details of the error. diff --git a/Access/tests/test_get_grant_failure_requests.py b/Access/tests/test_get_grant_failure_requests.py new file mode 100644 index 00000000..cfd69cc7 --- /dev/null +++ b/Access/tests/test_get_grant_failure_requests.py @@ -0,0 +1,100 @@ +"""test get_grant_fail_requests feature tests.""" + +from pytest_bdd import ( + given, + scenario, + then, + when, +) + +import pytest +from .. import accessrequest_helper +from Access import helpers +import Access +from Access.models import User +from Access.models import UserAccessMapping +from django.db.models.query import QuerySet + +@pytest.fixture +def user(mocker): + user = mocker.MagicMock() + user.email = "test@test.com" + user.user.username = "test-user" + return user + +@pytest.fixture +def context(mocker): + context = mocker.MagicMock() + return context + +@pytest.fixture +def new_request(mocker): + req = mocker.MagicMock() + return req + +@scenario('./features/get_grant_failed_requests.feature', 'Handling errors') +def test_handling_errors(): + """Handling errors.""" + pass + + +@scenario('./features/get_grant_failed_requests.feature', 'Retrieving all grant failure requests') +def test_retrieving_all_grant_failure_requests(): + """Retrieving all grant failure requests.""" + pass + + +@given('an error occurs while executing the method') +def step_impl(mocker,context): + """an error occurs while executing the method.""" + mock_response = {"error": "Error in request not found OR Invalid request type"} + + context.mock_user_access_mapping = mocker.MagicMock(spec=UserAccessMapping) + context.mock_user = mocker.MagicMock(spec=User) + orderByPatch = mocker.MagicMock() + + mock_response = {"error": "Error in request not found OR Invalid request type"} + mocker.patch( + "Access.accessrequest_helper.process_error_response", return_value=mock_response + ) + mock_exception = Exception("Error in request not found OR Invalid request type") + orderByPatch.order_by.return_value = mocker.MagicMock() + mocker.patch( + "Access.models.UserAccessMapping.objects.filter", side_effect=mock_exception, + ) + + +@given('there are grant failure requests in the database') +@pytest.mark.django_db +def step_impl(mocker,context): + """there are grant failure requests in the database.""" + context.mock_user_access_mapping = mocker.MagicMock(spec=UserAccessMapping) + context.mock_user = mocker.MagicMock(spec=User) + orderByPatch = mocker.MagicMock() + orderByPatch.order_by.return_value = mocker.MagicMock() + mocker.patch( + "Access.models.UserAccessMapping.objects.filter", return_value=orderByPatch + ) + queryset_mock = mocker.MagicMock() + context.request.GET.get.return_value = False + mocker.patch("Access.models.User.objects.get",return_value=mocker.MagicMock()) + queryset_mock.filter.order_by.return_value="request_1" + + +@when('the get_grant_failed_requests method is called') +def step_impl(mocker,new_request,context): + """the get_grant_failed_requests method is called.""" + context.request = mocker.MagicMock() + context.result = accessrequest_helper.get_grant_failed_requests(context.request) + +@then('it should return a context containing all grant failure requests sorted by requested_on date in descending order') +def step_impl(mocker,context): + """it should return a context containing all grant failure requests sorted by requested_on date in descending order.""" + assert context.result['failures'] is not None + +@then('it should return an error response with details of the error.') +def step_impl(mocker,context): + """it should return an error response with details of the error..""" + assert context.result == { + "error": "Error in request not found OR Invalid request type" + }