|
| 1 | +"""features/get_pending_request.feature feature tests.""" |
| 2 | + |
| 3 | +from pytest_bdd import ( |
| 4 | + given, |
| 5 | + scenario, |
| 6 | + then, |
| 7 | + when, |
| 8 | +) |
| 9 | +import Access |
| 10 | +import pytest |
| 11 | +from Access import accessrequest_helper |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(autouse=True) |
| 15 | +def setup_test_config(): |
| 16 | + accessrequest_helper.DECLINE_REASONS = { |
| 17 | + "declineReasons": { |
| 18 | + "reason1": "Not in adherence with Access Management Policy", |
| 19 | + "reason2": "Access can be granted to L3 or above only", |
| 20 | + "reason3": "Duplicate request", |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def new_request(mocker): |
| 27 | + req = mocker.MagicMock() |
| 28 | + req.user.user = mocker.MagicMock(spec=Access.models.User) |
| 29 | + req. user. email = "[email protected]" |
| 30 | + return req |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def context(mocker): |
| 35 | + context = mocker.MagicMock() |
| 36 | + return context |
| 37 | + |
| 38 | + |
| 39 | +@scenario("./features/get_pending_request.feature", "Error Handling") |
| 40 | +def test_error_handling(): |
| 41 | + """Error Handling.""" |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +@scenario("./features/get_pending_request.feature", "Retrieving All Pending Requests") |
| 46 | +def test_retrieving_all_pending_requests(): |
| 47 | + """Retrieving All Pending Requests.""" |
| 48 | + pass |
| 49 | + |
| 50 | + |
| 51 | +@given("an error occurs while retrieving the requests") |
| 52 | +def step_impl(context, mocker): |
| 53 | + """an error occurs while retrieving the requests.""" |
| 54 | + mock_response = {"error": "Error in request not found OR Invalid request type"} |
| 55 | + mocker.patch( |
| 56 | + "Access.accessrequest_helper.process_error_response", return_value=mock_response |
| 57 | + ) |
| 58 | + mock_exception = Exception("Error in request not found OR Invalid request type") |
| 59 | + mocker.patch( |
| 60 | + "Access.models.GroupV2.getPendingCreation", |
| 61 | + side_effect=mock_exception, |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +@given("request to view all pending requests") |
| 66 | +def step_impl(context, mocker): |
| 67 | + """request to view all pending requests.""" |
| 68 | + mocker.patch( |
| 69 | + "Access.models.GroupV2.getPendingMemberships", return_value=mocker.MagicMock |
| 70 | + ) |
| 71 | + mocker.patch("Access.models.GroupV2.getPendingCreation", return_value=[]) |
| 72 | + mocker.patch( |
| 73 | + "Access.accessrequest_helper.get_pending_accesses_from_modules", |
| 74 | + return_value=("test-user-1", "test-ser-2"), |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +@given("request to view pending requests") |
| 79 | +def step_impl(context, mocker): |
| 80 | + """request to view pending requests.""" |
| 81 | + mocker.patch( |
| 82 | + "Access.models.GroupV2.getPendingMemberships", return_value=mocker.MagicMock |
| 83 | + ) |
| 84 | + mocker.patch("Access.models.GroupV2.getPendingCreation", return_value=[]) |
| 85 | + mocker.patch( |
| 86 | + "Access.accessrequest_helper.get_pending_accesses_from_modules", |
| 87 | + return_value=("test-user-1", "test-ser-2"), |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +@when("the `get_pending_requests` method is called") |
| 92 | +def step_impl(context, new_request): |
| 93 | + """the `get_pending_requests` method is called.""" |
| 94 | + context.request = new_request |
| 95 | + context.result = accessrequest_helper.get_pending_requests(context.request) |
| 96 | + |
| 97 | + |
| 98 | +@then("all pending access requests from modules") |
| 99 | +def step_impl(context): |
| 100 | + """all pending access requests from modules.""" |
| 101 | + assert "genericRequests" in context.result |
| 102 | + assert "groupGenericRequests" in context.result |
| 103 | + assert Access.models.GroupV2.getPendingMemberships.call_count == 1 |
| 104 | + assert Access.models.GroupV2.getPendingCreation.call_count == 1 |
| 105 | + |
| 106 | + |
| 107 | +@then("return an error response") |
| 108 | +def step_impl(context): |
| 109 | + """return an error response.""" |
| 110 | + assert context.result is not None |
| 111 | + assert Access.models.GroupV2.getPendingMemberships.call_count == 1 |
| 112 | + assert Access.models.GroupV2.getPendingCreation.call_count == 1 |
| 113 | + |
| 114 | + |
| 115 | +@then("return the retrieved requests in the context variable") |
| 116 | +def step_impl(context): |
| 117 | + """return the retrieved requests in the context variable.""" |
| 118 | + assert context.result is not None |
| 119 | + |
| 120 | + |
| 121 | +@then("the method should handle the error") |
| 122 | +def step_impl(context): |
| 123 | + """the method should handle the error.""" |
| 124 | + assert context.result == { |
| 125 | + "error": "Error in request not found OR Invalid request type" |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | +@then("the method should retrieve all pending membership and group creation requests") |
| 130 | +def step_impl(context): |
| 131 | + """the method should retrieve all pending membership and group creation requests.""" |
| 132 | + assert "membershipPending" in context.result |
| 133 | + assert "newGroupPending" in context.result |
0 commit comments