Skip to content

test: adding bdd test for get_grant_failed_request #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Access/tests/features/get_grant_failed_requests.feature
Original file line number Diff line number Diff line change
@@ -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.
100 changes: 100 additions & 0 deletions Access/tests/test_get_grant_failure_requests.py
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
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"
}