-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathtests.py
83 lines (64 loc) · 4.19 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from django.core.urlresolvers import reverse
from django.test import TestCase, override_settings
from rest_framework_docs.settings import DRFSettings
class DRFDocsViewTests(TestCase):
SETTINGS_HIDE_DOCS = {
'HIDE_DOCS': True # Default: False
}
def setUp(self):
super(DRFDocsViewTests, self).setUp()
def test_settings_module(self):
settings = DRFSettings()
self.assertEqual(settings.get_setting("HIDE_DOCS"), False)
self.assertEqual(settings.get_setting("TEST"), None)
def test_index_view_with_endpoints(self):
"""
Should load the drf focs view with all the endpoints.
NOTE: Views that do **not** inherit from DRF's "APIView" are not included.
"""
response = self.client.get(reverse('drfdocs'))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context["endpoints"]), 15)
# Test the login view
self.assertEqual(response.context["endpoints"][0].name_parent, "accounts")
self.assertEqual(response.context["endpoints"][0].allowed_methods, ['POST', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][0].path, "/accounts/login/")
self.assertEqual(response.context["endpoints"][0].docstring, "<p>A view that allows users to login providing their username and password.</p>")
self.assertEqual(len(response.context["endpoints"][0].fields), 2)
self.assertEqual(response.context["endpoints"][0].fields[0]["type"], "CharField")
self.assertTrue(response.context["endpoints"][0].fields[0]["required"])
self.assertEqual(response.context["endpoints"][1].name_parent, "accounts")
self.assertEqual(response.context["endpoints"][1].allowed_methods, ['POST', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][1].path, "/accounts/login2/")
self.assertEqual(response.context["endpoints"][1].docstring, "<p>A view that allows users to login providing their username and password. Without serializer_class</p>")
self.assertEqual(len(response.context["endpoints"][1].fields), 2)
self.assertEqual(response.context["endpoints"][1].fields[0]["type"], "CharField")
self.assertTrue(response.context["endpoints"][1].fields[0]["required"])
# The view "OrganisationErroredView" (organisations/(?P<slug>[\w-]+)/errored/) should contain an error.
self.assertEqual(str(response.context["endpoints"][9].errors), "'test_value'")
def test_index_search_with_endpoints(self):
response = self.client.get("%s?search=reset-password" % reverse("drfdocs"))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context["endpoints"]), 2)
self.assertEqual(response.context["endpoints"][1].path, "/accounts/reset-password/confirm/")
self.assertEqual(len(response.context["endpoints"][1].fields), 3)
@override_settings(REST_FRAMEWORK_DOCS=SETTINGS_HIDE_DOCS)
def test_index_view_docs_hidden(self):
"""
Should prevent the docs from loading the "HIDE_DOCS" is set
to "True" or undefined under settings
"""
response = self.client.get(reverse('drfdocs'))
self.assertEqual(response.status_code, 404)
self.assertEqual(response.reason_phrase.upper(), "NOT FOUND")
def test_model_viewset(self):
response = self.client.get(reverse('drfdocs'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["endpoints"][10].path, '/organisations/<slug>/')
self.assertEqual(response.context['endpoints'][6].fields[2]['to_many_relation'], True)
self.assertEqual(response.context["endpoints"][11].path, '/organisation-model-viewsets/')
self.assertEqual(response.context["endpoints"][12].path, '/organisation-model-viewsets/<pk>/')
self.assertEqual(response.context["endpoints"][11].allowed_methods, ['GET', 'POST', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][12].allowed_methods, ['GET', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][13].allowed_methods, ['POST', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][13].docstring, 'This is a test.')