-
Notifications
You must be signed in to change notification settings - Fork 212
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
Use api_client, move fixtures around to share basic defaults #3560
Merged
sarayourfriend
merged 1 commit into
int_test_using_client
from
int_test_using_client---potential_fixes
Dec 20, 2023
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 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,23 @@ | ||
"""Fixtures usable by or necessary for both unit and integration tests.""" | ||
|
||
from test.fixtures.asynchronous import ensure_asgi_lifecycle, get_new_loop, session_loop | ||
from test.fixtures.cache import ( | ||
django_cache, | ||
redis, | ||
unreachable_django_cache, | ||
unreachable_redis, | ||
) | ||
from test.fixtures.rest_framework import api_client, request_factory | ||
|
||
|
||
__all__ = [ | ||
"ensure_asgi_lifecycle", | ||
"get_new_loop", | ||
"session_loop", | ||
"django_cache", | ||
"redis", | ||
"unreachable_django_cache", | ||
"unreachable_redis", | ||
"api_client", | ||
"request_factory", | ||
] |
This file contains 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 rest_framework.test import APIClient, APIRequestFactory | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def api_client(): | ||
return APIClient() | ||
|
||
|
||
@pytest.fixture | ||
def request_factory() -> APIRequestFactory(): | ||
request_factory = APIRequestFactory(defaults={"REMOTE_ADDR": "192.0.2.1"}) | ||
|
||
return request_factory |
This file contains 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 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 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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
from test.constants import API_URL | ||
|
||
from django.urls import reverse | ||
from django.utils.http import urlencode | ||
|
||
import pytest | ||
from oauth2_provider.models import AccessToken | ||
|
@@ -36,13 +35,13 @@ def unreachable_oauth_cache(unreachable_django_cache, monkeypatch): | |
|
||
@pytest.mark.django_db | ||
@pytest.fixture | ||
def test_auth_tokens_registration(client): | ||
def test_auth_tokens_registration(api_client): | ||
data = { | ||
"name": f"INTEGRATION TEST APPLICATION {uuid.uuid4()}", | ||
"description": "A key for testing the OAuth2 registration process.", | ||
"email": "[email protected]", | ||
} | ||
res = client.post( | ||
res = api_client.post( | ||
"/v1/auth_tokens/register/", | ||
data, | ||
verify=False, | ||
|
@@ -54,20 +53,19 @@ def test_auth_tokens_registration(client): | |
|
||
@pytest.mark.django_db | ||
@pytest.fixture | ||
def test_auth_token_exchange(client, test_auth_tokens_registration): | ||
client_id = test_auth_tokens_registration["client_id"] | ||
client_secret = test_auth_tokens_registration["client_secret"] | ||
data = urlencode( | ||
{ | ||
"client_id": client_id, | ||
"client_secret": client_secret, | ||
"grant_type": "client_credentials", | ||
} | ||
) | ||
res = client.post( | ||
def test_auth_token_exchange(api_client, test_auth_tokens_registration): | ||
api_client_id = test_auth_tokens_registration["client_id"] | ||
api_client_secret = test_auth_tokens_registration["client_secret"] | ||
data = { | ||
"client_id": api_client_id, | ||
"client_secret": api_client_secret, | ||
"grant_type": "client_credentials", | ||
} | ||
|
||
res = api_client.post( | ||
"/v1/auth_tokens/token/", | ||
data, | ||
"application/x-www-form-urlencoded", | ||
"multipart", | ||
verify=False, | ||
) | ||
res_data = res.json() | ||
|
@@ -76,20 +74,20 @@ def test_auth_token_exchange(client, test_auth_tokens_registration): | |
|
||
|
||
@pytest.mark.django_db | ||
def test_auth_token_exchange_unsupported_method(client): | ||
res = client.get( | ||
def test_auth_token_exchange_unsupported_method(api_client): | ||
res = api_client.get( | ||
"/v1/auth_tokens/token/", | ||
verify=False, | ||
) | ||
assert res.status_code == 405 | ||
assert res.json()["detail"] == 'Method "GET" not allowed.' | ||
|
||
|
||
def _integration_verify_most_recent_token(client): | ||
def _integration_verify_most_recent_token(api_client): | ||
verify = OAuth2Verification.objects.last() | ||
code = verify.code | ||
path = reverse("verify-email", args=[code]) | ||
return client.get(path) | ||
return api_client.get(path) | ||
|
||
|
||
@pytest.mark.django_db | ||
|
@@ -108,17 +106,17 @@ def _integration_verify_most_recent_token(client): | |
) | ||
def test_auth_email_verification( | ||
request, | ||
client, | ||
api_client, | ||
is_cache_reachable, | ||
cache_name, | ||
rate_limit_model, | ||
test_auth_token_exchange, | ||
): | ||
res = _integration_verify_most_recent_token(client) | ||
res = _integration_verify_most_recent_token(api_client) | ||
assert res.status_code == 200 | ||
test_auth_rate_limit_reporting( | ||
request, | ||
client, | ||
api_client, | ||
is_cache_reachable, | ||
cache_name, | ||
rate_limit_model, | ||
|
@@ -135,7 +133,7 @@ def test_auth_email_verification( | |
@cache_availability_params | ||
def test_auth_rate_limit_reporting( | ||
request, | ||
client, | ||
api_client, | ||
is_cache_reachable, | ||
cache_name, | ||
rate_limit_model, | ||
|
@@ -151,7 +149,7 @@ def test_auth_rate_limit_reporting( | |
application = AccessToken.objects.get(token=token).application | ||
application.rate_limit_model = rate_limit_model | ||
application.save() | ||
res = client.get("/v1/rate_limit/", HTTP_AUTHORIZATION=f"Bearer {token}") | ||
res = api_client.get("/v1/rate_limit/", HTTP_AUTHORIZATION=f"Bearer {token}") | ||
res_data = res.json() | ||
if is_cache_reachable: | ||
assert res.status_code == 200 | ||
|
@@ -174,14 +172,14 @@ def test_auth_rate_limit_reporting( | |
(True, False), | ||
) | ||
def test_auth_response_headers( | ||
client, verified, test_auth_tokens_registration, test_auth_token_exchange | ||
api_client, verified, test_auth_tokens_registration, test_auth_token_exchange | ||
): | ||
if verified: | ||
_integration_verify_most_recent_token(client) | ||
_integration_verify_most_recent_token(api_client) | ||
|
||
token = test_auth_token_exchange["access_token"] | ||
|
||
res = client.get("/v1/images/", HTTP_AUTHORIZATION=f"Bearer {token}") | ||
res = api_client.get("/v1/images/", HTTP_AUTHORIZATION=f"Bearer {token}") | ||
|
||
assert ( | ||
res.headers["x-ov-client-application-name"] | ||
|
@@ -190,8 +188,8 @@ def test_auth_response_headers( | |
assert res.headers["x-ov-client-application-verified"] == str(verified) | ||
|
||
|
||
def test_unauthed_response_headers(client): | ||
res = client.get("/v1/images") | ||
def test_unauthed_response_headers(api_client): | ||
res = api_client.get("/v1/images") | ||
|
||
assert "x-ov-client-application-name" not in res.headers | ||
assert "x-ov-client-application-verified" not in res.headers | ||
|
@@ -206,15 +204,17 @@ def test_unauthed_response_headers(client): | |
], | ||
) | ||
def test_sorting_authed( | ||
client, monkeypatch, test_auth_token_exchange, sort_dir, exp_indexed_on | ||
api_client, monkeypatch, test_auth_token_exchange, sort_dir, exp_indexed_on | ||
): | ||
# Prevent DB lookup for ES results because DB is empty. | ||
monkeypatch.setattr("api.views.image_views.ImageSerializer.needs_db", False) | ||
|
||
time.sleep(1) | ||
token = test_auth_token_exchange["access_token"] | ||
query_params = {"unstable__sort_by": "indexed_on", "unstable__sort_dir": sort_dir} | ||
res = client.get("/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}") | ||
res = api_client.get( | ||
"/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}" | ||
) | ||
assert res.status_code == 200 | ||
|
||
res_data = res.json() | ||
|
@@ -231,7 +231,7 @@ def test_sorting_authed( | |
], | ||
) | ||
def test_authority_authed( | ||
client, monkeypatch, test_auth_token_exchange, authority_boost, exp_source | ||
api_client, monkeypatch, test_auth_token_exchange, authority_boost, exp_source | ||
): | ||
# Prevent DB lookup for ES results because DB is empty. | ||
monkeypatch.setattr("api.views.image_views.ImageSerializer.needs_db", False) | ||
|
@@ -243,7 +243,9 @@ def test_authority_authed( | |
"unstable__authority": "true", | ||
"unstable__authority_boost": authority_boost, | ||
} | ||
res = client.get("/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}") | ||
res = api_client.get( | ||
"/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}" | ||
) | ||
assert res.status_code == 200 | ||
|
||
res_data = res.json() | ||
|
@@ -252,23 +254,27 @@ def test_authority_authed( | |
|
||
|
||
@pytest.mark.django_db | ||
def test_page_size_limit_unauthed(client): | ||
def test_page_size_limit_unauthed(api_client): | ||
query_params = {"page_size": 20} | ||
res = client.get("/v1/images/", query_params) | ||
res = api_client.get("/v1/images/", query_params) | ||
assert res.status_code == 200 | ||
query_params["page_size"] = 21 | ||
res = client.get("/v1/images/", query_params) | ||
res = api_client.get("/v1/images/", query_params) | ||
assert res.status_code == 401 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_page_size_limit_authed(client, test_auth_token_exchange): | ||
def test_page_size_limit_authed(api_client, test_auth_token_exchange): | ||
time.sleep(1) | ||
token = test_auth_token_exchange["access_token"] | ||
query_params = {"page_size": 21} | ||
res = client.get("/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}") | ||
res = api_client.get( | ||
"/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}" | ||
) | ||
assert res.status_code == 200 | ||
|
||
query_params = {"page_size": 500} | ||
res = client.get("/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}") | ||
res = api_client.get( | ||
"/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}" | ||
) | ||
assert res.status_code == 200 |
This file contains 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
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.
What is the reason for this change?
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.
It's part of DRF's
api_client
's testing request renderer formats. It takes a slug corresponding to the renderer rather than the content type string: https://www.django-rest-framework.org/api-guide/testing/#setting-the-available-formatsHere's an SO answer explaining it as well: https://stackoverflow.com/questions/68528408/difference-between-content-type-application-json-and-format-json-in-drf-apiclien
DRF's testing APIClient actually gave a pretty easy to understand error message about this, I was not aware of this until fixing that error when I changed this test to api_client.