-
Notifications
You must be signed in to change notification settings - Fork 1
Cleanup testing and small parts of vetting details #338
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,24 @@ | ||
| from ninja.testing import TestClient | ||
| import pytest | ||
|
|
||
| from bats_ai.core.tests.factories import SuperuserFactory, UserFactory | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_check_is_admin_authenticated(api_client: TestClient): | ||
| user = UserFactory.create() | ||
|
|
||
| resp = api_client.get('configuration/is_admin/', user=user) | ||
|
|
||
| assert resp.status_code == 200 | ||
| assert resp.data['is_admin'] is False | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'client_fixture,status_code,is_admin', | ||
| [ | ||
| ('client', 401, None), | ||
| ('authenticated_client', 200, False), | ||
| ('authorized_client', 200, True), | ||
| ], | ||
| ) | ||
| @pytest.mark.django_db | ||
| def test_is_admin(client_fixture, status_code, is_admin, request): | ||
| api_client = request.getfixturevalue(client_fixture) | ||
| resp = api_client.get('/api/v1/configuration/is_admin/') | ||
| assert resp.status_code == status_code | ||
| if is_admin is not None: | ||
| assert resp.json()['is_admin'] == is_admin | ||
| def test_check_is_admin_superuser(api_client: TestClient): | ||
| user = SuperuserFactory.create() | ||
|
|
||
| resp = api_client.get('configuration/is_admin/', user=user) | ||
|
|
||
| assert resp.status_code == 200 | ||
| assert resp.data['is_admin'] is True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from django.test import Client | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'url_suffix', | ||
| [ | ||
| 'configuration/is_admin/', | ||
| ], | ||
| ) | ||
| @pytest.mark.django_db | ||
| def test_auth_anonymous_deny(url_suffix: str, client: Client): | ||
| resp = client.get(f'/api/v1/{url_suffix}') | ||
|
|
||
| assert resp.status_code == 401 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,114 +1,131 @@ | ||
| from ninja.testing import TestClient | ||
| import pytest | ||
|
|
||
| from .factories import UserFactory, VettingDetailsFactory | ||
| from bats_ai.core.models import VettingDetails | ||
|
|
||
| from .factories import SuperuserFactory, UserFactory, VettingDetailsFactory | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'client_fixture,status_code', | ||
| [ | ||
| ('client', 401), | ||
| ('authenticated_client', 200), | ||
| ('authorized_client', 200), | ||
| ], | ||
| ) | ||
| @pytest.mark.django_db | ||
| def test_get_vetting_details(client_fixture, status_code, user, vetting_details, request): | ||
| api_client = request.getfixturevalue(client_fixture) | ||
| resp = api_client.get(f'/api/v1/vetting/user/{user.id}') | ||
| assert resp.status_code == status_code | ||
| if status_code == 200: | ||
| assert resp.json()['reference_materials'] == vetting_details.reference_materials | ||
| def test_get_vetting_details(api_client: TestClient): | ||
| vetting_details = VettingDetailsFactory.create() | ||
|
|
||
| resp = api_client.get(f'vetting/user/{vetting_details.user.id}', user=vetting_details.user) | ||
|
|
||
| assert resp.status_code == 200 | ||
| assert resp.data['reference_materials'] == vetting_details.reference_materials | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_get_vetting_details_other_user(authenticated_client): | ||
| other_user = UserFactory() | ||
| VettingDetailsFactory(user=other_user) | ||
| resp = authenticated_client.get(f'/api/v1/vetting/user/{other_user.id}') | ||
| def test_get_vetting_details_other_user(api_client: TestClient): | ||
| vetting_details = VettingDetailsFactory.create() | ||
| other_user = UserFactory.create() | ||
|
|
||
| resp = api_client.get(f'vetting/user/{vetting_details.user.id}', user=other_user) | ||
|
|
||
| assert resp.status_code == 404 | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_vetting_details(client): | ||
| test_text = 'foo' | ||
| data = {'reference_materials': test_text} | ||
| test_user = UserFactory() | ||
| client.force_login(user=test_user) | ||
| resp = client.post( | ||
| f'/api/v1/vetting/user/{test_user.id}', data=data, content_type='application/json' | ||
| def test_create_vetting_details(api_client: TestClient): | ||
| user = UserFactory.create() | ||
|
|
||
| resp = api_client.post( | ||
| f'vetting/user/{user.id}', | ||
| json={'reference_materials': 'foo'}, | ||
| user=user, | ||
| ) | ||
|
|
||
| assert resp.status_code == 200 | ||
| assert resp.json()['user_id'] == test_user.id | ||
| assert resp.data['user_id'] == user.id | ||
| assert resp.data['reference_materials'] == 'foo' | ||
| assert VettingDetails.objects.filter(user=user, reference_materials='foo').exists() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'client_fixture,status_code', | ||
| [ | ||
| ('authenticated_client', 404), | ||
| ('authorized_client', 200), | ||
| ], | ||
| ) | ||
| @pytest.mark.django_db | ||
| def test_create_vetting_details_other_user(client_fixture, status_code, request): | ||
| api_client = request.getfixturevalue(client_fixture) | ||
| test_text = 'foo' | ||
| data = {'reference_materials': test_text} | ||
| other_user = UserFactory() | ||
| def test_create_vetting_details_other_user(api_client: TestClient): | ||
| user = UserFactory.create() | ||
| other_user = UserFactory.create() | ||
|
|
||
| resp = api_client.post( | ||
| f'/api/v1/vetting/user/{other_user.id}', data=data, content_type='application/json' | ||
| f'vetting/user/{user.id}', | ||
| json={'reference_materials': 'foo'}, | ||
| user=other_user, | ||
| ) | ||
| assert resp.status_code == status_code | ||
| if status_code == 200: | ||
| assert resp.json()['reference_materials'] == test_text | ||
|
|
||
| assert resp.status_code == 404 | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_update_vetting_details(client): | ||
| test_text = 'bar' | ||
| data = {'reference_materials': 'bar'} | ||
| test_user = UserFactory() | ||
| VettingDetailsFactory(user=test_user, reference_materials='foo') | ||
| client.force_login(test_user) | ||
|
|
||
| initial_resp = client.get(f'/api/v1/vetting/user/{test_user.id}') | ||
| assert initial_resp.status_code == 200 | ||
|
|
||
| resp = client.post( | ||
| f'/api/v1/vetting/user/{test_user.id}', data=data, content_type='application/json' | ||
| def test_create_vetting_details_other_superuser(api_client: TestClient): | ||
| user = UserFactory.create() | ||
| other_superuser = SuperuserFactory.create() | ||
|
|
||
| resp = api_client.post( | ||
| f'vetting/user/{user.id}', | ||
| json={'reference_materials': 'foo'}, | ||
| user=other_superuser, | ||
| ) | ||
|
|
||
| assert resp.status_code == 200 | ||
| assert resp.data['reference_materials'] == 'foo' | ||
| assert VettingDetails.objects.filter(user=user, reference_materials='foo').exists() | ||
|
|
||
| new_details_response = client.get(f'/api/v1/vetting/user/{test_user.id}') | ||
| assert new_details_response.status_code == 200 | ||
| assert new_details_response.json()['reference_materials'] == test_text | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_update_vetting_details(api_client: TestClient): | ||
| vetting_details = VettingDetailsFactory.create() | ||
|
|
||
| resp = api_client.post( | ||
| f'vetting/user/{vetting_details.user.id}', | ||
| json={'reference_materials': 'foo'}, | ||
| user=vetting_details.user, | ||
| ) | ||
|
|
||
| assert resp.status_code == 200 | ||
| assert resp.data['reference_materials'] == 'foo' | ||
| vetting_details.refresh_from_db() | ||
| assert vetting_details.reference_materials == 'foo' | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'client_fixture,status_code', | ||
| [ | ||
| ('authenticated_client', 404), | ||
| ('authorized_client', 200), | ||
| ], | ||
| ) | ||
| @pytest.mark.django_db | ||
| def test_update_vetting_details_other_user( | ||
| client_fixture, status_code, random_user_vetting_details, request | ||
| ): | ||
| api_client = request.getfixturevalue(client_fixture) | ||
| def test_update_vetting_details_other_user(api_client: TestClient): | ||
| vetting_details = VettingDetailsFactory.create() | ||
| other_user = UserFactory.create() | ||
|
|
||
| resp = api_client.post( | ||
| f'/api/v1/vetting/user/{random_user_vetting_details.user.id}', | ||
| data={'reference_materials': 'foo'}, | ||
| content_type='application/json', | ||
| f'vetting/user/{vetting_details.user.id}', | ||
| json={'reference_materials': 'foo'}, | ||
| user=other_user, | ||
| ) | ||
| assert resp.status_code == status_code | ||
| assert resp.status_code == 404 | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_update_vetting_details_length_constraint(authorized_client, random_user_vetting_details): | ||
| data = {'reference_materials': 'a' * 2001} | ||
| resp = authorized_client.post( | ||
| f'/api/v1/vetting/user/{random_user_vetting_details.user.id}', | ||
| data=data, | ||
| content_type='application/json', | ||
| def test_update_vetting_details_other_superuser(api_client: TestClient): | ||
| vetting_details = VettingDetailsFactory.create() | ||
| other_superuser = SuperuserFactory.create() | ||
|
|
||
| resp = api_client.post( | ||
| f'vetting/user/{vetting_details.user.id}', | ||
| json={'reference_materials': 'foo'}, | ||
| user=other_superuser, | ||
| ) | ||
|
|
||
| assert resp.status_code == 200 | ||
| assert resp.data['reference_materials'] == 'foo' | ||
| vetting_details.refresh_from_db() | ||
| assert vetting_details.reference_materials == 'foo' | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_update_vetting_details_length_constraint(api_client: TestClient): | ||
| vetting_details = VettingDetailsFactory.create() | ||
|
|
||
| resp = api_client.post( | ||
| f'vetting/user/{vetting_details.user.id}', | ||
| json={'reference_materials': 'a' * 2001}, | ||
| user=vetting_details.user, | ||
| ) | ||
|
|
||
| assert resp.status_code == 400 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this test parametrized? Is the intent to use this single test to make sure all of our endpoints that require auth are locked down correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. If that never happens, we should just remove the parametrization.