|
| 1 | +import pytest |
| 2 | +from django.contrib.contenttypes.models import ContentType |
| 3 | +from django.utils.http import urlencode |
| 4 | + |
| 5 | +from ansible_base.lib.utils.response import get_relative_url |
| 6 | +from ansible_base.resource_registry.models import Resource |
| 7 | + |
| 8 | + |
| 9 | +class TestAnsibleIdAliasFilterBackend: |
| 10 | + |
| 11 | + @pytest.mark.django_db |
| 12 | + def test_filter_user_ansible_id(self, admin_api_client, org_inv_rd, inv_rd, inventory, rando, organization): |
| 13 | + ''' |
| 14 | + Test filtering RoleUserAssignment by user_ansible_id and object_ansible_id. |
| 15 | + ''' |
| 16 | + # user - org assigment |
| 17 | + user_resource = Resource.objects.get(object_id=rando.pk, content_type=ContentType.objects.get_for_model(rando).pk) |
| 18 | + organization_resource = Resource.objects.get(object_id=organization.pk, content_type=ContentType.objects.get_for_model(organization).pk) |
| 19 | + url = get_relative_url('roleuserassignment-list') |
| 20 | + data = dict(role_definition=org_inv_rd.id, content_type='shared.organization', user_ansible_id=user_resource.ansible_id, object_id=organization.id) |
| 21 | + response = admin_api_client.post(url, data=data, format="json") |
| 22 | + assert response.status_code == 201, response.data |
| 23 | + |
| 24 | + # user - inventory assignment (just a random assignment to make total count > 1) |
| 25 | + data = dict(role_definition=inv_rd.id, content_type='shared.inventory', user_ansible_id=user_resource.ansible_id, object_id=inventory.id) |
| 26 | + response = admin_api_client.post(url, data=data, format="json") |
| 27 | + assert response.status_code == 201, response.data |
| 28 | + |
| 29 | + # make sure > 1 assignments total to ensure filtering is not returning undesired results |
| 30 | + response = admin_api_client.get(url) |
| 31 | + assert response.data["count"] > 1, response.data |
| 32 | + |
| 33 | + # filter by user_ansible_id |
| 34 | + query_params = {'user_ansible_id': user_resource.ansible_id} |
| 35 | + response = admin_api_client.get(url + '?' + urlencode(query_params)) |
| 36 | + assert response.status_code == 200, response.data |
| 37 | + assert response.data["count"] == 2, response.data |
| 38 | + |
| 39 | + # filter by object_ansible_id |
| 40 | + query_params = {'object_ansible_id': organization_resource.ansible_id} |
| 41 | + response = admin_api_client.get(url + '?' + urlencode(query_params)) |
| 42 | + assert response.status_code == 200, response.data |
| 43 | + assert response.data["count"] == 1, response.data |
| 44 | + |
| 45 | + # filter by both user_ansible_id and object_ansible_id |
| 46 | + query_params = {'user_ansible_id': user_resource.ansible_id, 'object_ansible_id': organization_resource.ansible_id} |
| 47 | + response = admin_api_client.get(url + '?' + urlencode(query_params)) |
| 48 | + assert response.status_code == 200, response.data |
| 49 | + assert response.data["count"] == 1, response.data |
| 50 | + |
| 51 | + @pytest.mark.django_db |
| 52 | + def test_filter_team_ansible_id(self, admin_api_client, team, inv_rd, inventory): |
| 53 | + ''' |
| 54 | + Test filtering RoleTeamAssignment by team_ansible_id. |
| 55 | + ''' |
| 56 | + team_resource = Resource.objects.get(object_id=team.pk, content_type=ContentType.objects.get_for_model(team).pk) |
| 57 | + url = get_relative_url('roleteamassignment-list') |
| 58 | + data = dict(role_definition=inv_rd.id, content_type='shared.organization', team_ansible_id=team_resource.ansible_id, object_id=inventory.id) |
| 59 | + response = admin_api_client.post(url, data=data, format="json") |
| 60 | + assert response.status_code == 201, response.data |
| 61 | + |
| 62 | + # filter by team_ansible_id |
| 63 | + query_params = {'team_ansible_id': team_resource.ansible_id} |
| 64 | + response = admin_api_client.get(url + '?' + urlencode(query_params)) |
| 65 | + assert response.status_code == 200, response.data |
| 66 | + assert response.data["count"] == 1, response.data |
| 67 | + |
| 68 | + @pytest.mark.parametrize("ansible_id_type", ["user", "team", "object"]) |
| 69 | + @pytest.mark.django_db |
| 70 | + def test_invalid_ansible_id_format(self, admin_api_client, ansible_id_type): |
| 71 | + ''' |
| 72 | + Test that invalid UUID formats for ansible_id raise a 400 error. |
| 73 | + ''' |
| 74 | + if ansible_id_type == "team": |
| 75 | + actor = "team" |
| 76 | + else: |
| 77 | + actor = "user" |
| 78 | + url = get_relative_url(f'role{actor}assignment-list') |
| 79 | + query_params = {f'{ansible_id_type}_ansible_id': 'invalid-uuid-format'} |
| 80 | + response = admin_api_client.get(url + '?' + urlencode(query_params)) |
| 81 | + assert response.status_code == 400, response.data |
| 82 | + assert "Invalid UUID format for" in str(response.data), response.data |
0 commit comments