| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django import forms |
| 4 |
from django.urls import reverse |
| 5 |
|
| 6 |
|
| 7 |
def test_openapi_json(client): |
| 8 |
resp = client.get(reverse("headless:openapi_json")) |
| 9 |
assert resp.status_code == HTTPStatus.OK |
| 10 |
data = resp.json() |
| 11 |
assert data["openapi"] == "3.0.3" |
| 12 |
assert data["info"]["description"].startswith("# Introduction") |
| 13 |
|
| 14 |
|
| 15 |
class SpecCustomSignupForm(forms.Form): |
| 16 |
favorite_color = forms.ChoiceField(choices=[("red", "Red"), ("blue", "Blue")]) |
| 17 |
hobbies = forms.MultipleChoiceField( |
| 18 |
choices=[("chess", "Chess"), ("poker", "Poker")], |
| 19 |
required=True, |
| 20 |
help_text="Pick your hobbies.", |
| 21 |
) |
| 22 |
nickname = forms.CharField(min_length=2, max_length=30, help_text="Your nickname.") |
| 23 |
age = forms.IntegerField() |
| 24 |
rating = forms.FloatField() |
| 25 |
newsletter = forms.BooleanField() |
| 26 |
birth_date = forms.DateField() |
| 27 |
signed_up_at = forms.DateTimeField() |
| 28 |
contact_email = forms.EmailField() |
| 29 |
website = forms.URLField() |
| 30 |
balance = forms.DecimalField() |
| 31 |
|
| 32 |
def signup(self, request, user): |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
def test_openapi_json_with_custom_signup_form(settings, client): |
| 37 |
settings.ACCOUNT_SIGNUP_FORM_CLASS = ( |
| 38 |
"tests.apps.headless.spec.test_views.SpecCustomSignupForm" |
| 39 |
) |
| 40 |
resp = client.get(reverse("headless:openapi_json")) |
| 41 |
assert resp.status_code == HTTPStatus.OK |
| 42 |
data = resp.json() |
| 43 |
base_signup = data["components"]["schemas"]["BaseSignup"] |
| 44 |
assert base_signup["properties"]["favorite_color"] == { |
| 45 |
"type": "string", |
| 46 |
"enum": ["red", "blue"], |
| 47 |
} |
| 48 |
assert base_signup["properties"]["hobbies"] == { |
| 49 |
"type": "array", |
| 50 |
"items": {"type": "string", "enum": ["chess", "poker"]}, |
| 51 |
"description": "Pick your hobbies.", |
| 52 |
} |
| 53 |
assert base_signup["properties"]["nickname"] == { |
| 54 |
"type": "string", |
| 55 |
"maxLength": 30, |
| 56 |
"minLength": 2, |
| 57 |
"description": "Your nickname.", |
| 58 |
} |
| 59 |
assert base_signup["properties"]["age"] == {"type": "integer"} |
| 60 |
assert base_signup["properties"]["rating"] == {"type": "number"} |
| 61 |
assert base_signup["properties"]["newsletter"] == {"type": "boolean"} |
| 62 |
assert base_signup["properties"]["birth_date"] == { |
| 63 |
"type": "string", |
| 64 |
"format": "date", |
| 65 |
} |
| 66 |
assert base_signup["properties"]["signed_up_at"] == { |
| 67 |
"type": "string", |
| 68 |
"format": "date-time", |
| 69 |
} |
| 70 |
assert base_signup["properties"]["contact_email"] == { |
| 71 |
"type": "string", |
| 72 |
"format": "email", |
| 73 |
"maxLength": 320, |
| 74 |
} |
| 75 |
assert base_signup["properties"]["website"] == { |
| 76 |
"type": "string", |
| 77 |
"format": "uri", |
| 78 |
} |
| 79 |
assert base_signup["properties"]["balance"] == { |
| 80 |
"type": "string", |
| 81 |
"format": "decimal", |
| 82 |
"pattern": r"^\d+(\.\d+)?$", |
| 83 |
} |
| 84 |
assert "hobbies" in base_signup["required"] |