|
| 1 | +import pytest |
| 2 | + |
| 3 | +from ansible_base.lib.utils.response import get_relative_url |
| 4 | +from test_app.models import Team, User |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.django_db |
| 8 | +class TestRoleAssignmentAnsibleIdSerialization: |
| 9 | + """Test serialization of ansible_id fields in role assignment endpoints""" |
| 10 | + |
| 11 | + def test_role_user_assignment_serializes_user_ansible_id(self, admin_api_client, inventory, inv_rd): |
| 12 | + """Test that RoleUserAssignment API returns user_ansible_id instead of null""" |
| 13 | + user = User.objects.create(username='test-user') |
| 14 | + assignment = inv_rd.give_permission(user, inventory) |
| 15 | + |
| 16 | + # Verify user has a resource and ansible_id |
| 17 | + assert hasattr(user, 'resource') |
| 18 | + assert user.resource is not None |
| 19 | + assert user.resource.ansible_id is not None |
| 20 | + expected_ansible_id = str(user.resource.ansible_id) |
| 21 | + |
| 22 | + # Get the assignment via API |
| 23 | + url = get_relative_url('roleuserassignment-detail', kwargs={'pk': assignment.pk}) |
| 24 | + response = admin_api_client.get(url) |
| 25 | + assert response.status_code == 200 |
| 26 | + |
| 27 | + # The user_ansible_id field should be populated, not null |
| 28 | + assert response.data['user_ansible_id'] is not None |
| 29 | + assert response.data['user_ansible_id'] == expected_ansible_id |
| 30 | + |
| 31 | + # Also verify the user field contains the primary key for backward compatibility |
| 32 | + assert response.data['user'] == user.pk |
| 33 | + |
| 34 | + def test_role_team_assignment_serializes_team_ansible_id(self, admin_api_client, inventory, inv_rd, organization): |
| 35 | + """Test that RoleTeamAssignment API returns team_ansible_id instead of null""" |
| 36 | + team = Team.objects.create(name='test-team', organization=organization) |
| 37 | + assignment = inv_rd.give_permission(team, inventory) |
| 38 | + |
| 39 | + # Verify team has a resource and ansible_id |
| 40 | + assert hasattr(team, 'resource') |
| 41 | + assert team.resource is not None |
| 42 | + assert team.resource.ansible_id is not None |
| 43 | + expected_ansible_id = str(team.resource.ansible_id) |
| 44 | + |
| 45 | + # Get the assignment via API |
| 46 | + url = get_relative_url('roleteamassignment-detail', kwargs={'pk': assignment.pk}) |
| 47 | + response = admin_api_client.get(url) |
| 48 | + assert response.status_code == 200 |
| 49 | + |
| 50 | + # The team_ansible_id field should be populated, not null |
| 51 | + assert response.data['team_ansible_id'] is not None |
| 52 | + assert response.data['team_ansible_id'] == expected_ansible_id |
| 53 | + |
| 54 | + # Also verify the team field contains the primary key for backward compatibility |
| 55 | + assert response.data['team'] == team.pk |
| 56 | + |
| 57 | + def test_role_user_assignment_list_includes_ansible_id(self, admin_api_client, inventory, inv_rd): |
| 58 | + """Test that RoleUserAssignment list endpoint includes user_ansible_id""" |
| 59 | + user = User.objects.create(username='test-user-list') |
| 60 | + assignment = inv_rd.give_permission(user, inventory) |
| 61 | + expected_ansible_id = str(user.resource.ansible_id) |
| 62 | + |
| 63 | + # Get assignments list via API |
| 64 | + url = get_relative_url('roleuserassignment-list') |
| 65 | + response = admin_api_client.get(url) |
| 66 | + assert response.status_code == 200 |
| 67 | + |
| 68 | + # Find our assignment in the results |
| 69 | + assignment_data = None |
| 70 | + for item in response.data['results']: |
| 71 | + if item['id'] == assignment.pk: |
| 72 | + assignment_data = item |
| 73 | + break |
| 74 | + |
| 75 | + assert assignment_data is not None |
| 76 | + assert assignment_data['user_ansible_id'] is not None |
| 77 | + assert assignment_data['user_ansible_id'] == expected_ansible_id |
| 78 | + |
| 79 | + def test_role_team_assignment_list_includes_ansible_id(self, admin_api_client, inventory, inv_rd, organization): |
| 80 | + """Test that RoleTeamAssignment list endpoint includes team_ansible_id""" |
| 81 | + team = Team.objects.create(name='test-team-list', organization=organization) |
| 82 | + assignment = inv_rd.give_permission(team, inventory) |
| 83 | + expected_ansible_id = str(team.resource.ansible_id) |
| 84 | + |
| 85 | + # Get assignments list via API |
| 86 | + url = get_relative_url('roleteamassignment-list') |
| 87 | + response = admin_api_client.get(url) |
| 88 | + assert response.status_code == 200 |
| 89 | + |
| 90 | + # Find our assignment in the results |
| 91 | + assignment_data = None |
| 92 | + for item in response.data['results']: |
| 93 | + if item['id'] == assignment.pk: |
| 94 | + assignment_data = item |
| 95 | + break |
| 96 | + |
| 97 | + assert assignment_data is not None |
| 98 | + assert assignment_data['team_ansible_id'] is not None |
| 99 | + assert assignment_data['team_ansible_id'] == expected_ansible_id |
0 commit comments