|
8 | 8 |
|
9 | 9 | from rest_framework import filters, pagination, permissions, serializers
|
10 | 10 | from rest_framework.compat import coreapi, coreschema
|
11 |
| -from rest_framework.decorators import detail_route, list_route |
| 11 | +from rest_framework.decorators import ( |
| 12 | + api_view, detail_route, list_route, schema |
| 13 | +) |
12 | 14 | from rest_framework.request import Request
|
13 | 15 | from rest_framework.routers import DefaultRouter
|
14 | 16 | from rest_framework.schemas import (
|
15 | 17 | AutoSchema, ManualSchema, SchemaGenerator, get_schema_view
|
16 | 18 | )
|
| 19 | +from rest_framework.schemas.generators import EndpointEnumerator |
17 | 20 | from rest_framework.test import APIClient, APIRequestFactory
|
18 | 21 | from rest_framework.utils import formatting
|
19 | 22 | from rest_framework.views import APIView
|
@@ -613,3 +616,107 @@ def post(self, request, *args, **kwargs):
|
613 | 616 | descr = schema.get_description('example', 'get')
|
614 | 617 | # the first and last character are '\n' correctly removed by get_description
|
615 | 618 | assert descr == formatting.dedent(ExampleDocstringAPIView.__doc__[1:][:-1])
|
| 619 | + |
| 620 | + |
| 621 | +# Views for SchemaGenerationExclusionTests |
| 622 | +class ExcludedAPIView(APIView): |
| 623 | + schema = None |
| 624 | + |
| 625 | + def get(self, request, *args, **kwargs): |
| 626 | + pass |
| 627 | + |
| 628 | + |
| 629 | +@api_view(['GET']) |
| 630 | +@schema(None) |
| 631 | +def excluded_fbv(request): |
| 632 | + pass |
| 633 | + |
| 634 | + |
| 635 | +@api_view(['GET']) |
| 636 | +def included_fbv(request): |
| 637 | + pass |
| 638 | + |
| 639 | + |
| 640 | +@unittest.skipUnless(coreapi, 'coreapi is not installed') |
| 641 | +class SchemaGenerationExclusionTests(TestCase): |
| 642 | + def setUp(self): |
| 643 | + self.patterns = [ |
| 644 | + url('^excluded-cbv/$', ExcludedAPIView.as_view()), |
| 645 | + url('^excluded-fbv/$', excluded_fbv), |
| 646 | + url('^included-fbv/$', included_fbv), |
| 647 | + ] |
| 648 | + |
| 649 | + def test_schema_generator_excludes_correctly(self): |
| 650 | + """Schema should not include excluded views""" |
| 651 | + generator = SchemaGenerator(title='Exclusions', patterns=self.patterns) |
| 652 | + schema = generator.get_schema() |
| 653 | + expected = coreapi.Document( |
| 654 | + url='', |
| 655 | + title='Exclusions', |
| 656 | + content={ |
| 657 | + 'included-fbv': { |
| 658 | + 'list': coreapi.Link(url='/included-fbv/', action='get') |
| 659 | + } |
| 660 | + } |
| 661 | + ) |
| 662 | + |
| 663 | + assert len(schema.data) == 1 |
| 664 | + assert 'included-fbv' in schema.data |
| 665 | + assert schema == expected |
| 666 | + |
| 667 | + def test_endpoint_enumerator_excludes_correctly(self): |
| 668 | + """It is responsibility of EndpointEnumerator to exclude views""" |
| 669 | + inspector = EndpointEnumerator(self.patterns) |
| 670 | + endpoints = inspector.get_api_endpoints() |
| 671 | + |
| 672 | + assert len(endpoints) == 1 |
| 673 | + path, method, callback = endpoints[0] |
| 674 | + assert path == '/included-fbv/' |
| 675 | + |
| 676 | + def test_should_include_endpoint_excludes_correctly(self): |
| 677 | + """This is the specific method that should handle the exclusion""" |
| 678 | + inspector = EndpointEnumerator(self.patterns) |
| 679 | + |
| 680 | + # Not pretty. Mimics internals of EndpointEnumerator to put should_include_endpoint under test |
| 681 | + pairs = [(inspector.get_path_from_regex(pattern.regex.pattern), pattern.callback) |
| 682 | + for pattern in self.patterns] |
| 683 | + |
| 684 | + should_include = [ |
| 685 | + inspector.should_include_endpoint(*pair) for pair in pairs |
| 686 | + ] |
| 687 | + |
| 688 | + expected = [False, False, True] |
| 689 | + |
| 690 | + assert should_include == expected |
| 691 | + |
| 692 | + def test_deprecations(self): |
| 693 | + with pytest.warns(PendingDeprecationWarning) as record: |
| 694 | + @api_view(["GET"], exclude_from_schema=True) |
| 695 | + def view(request): |
| 696 | + pass |
| 697 | + |
| 698 | + assert len(record) == 1 |
| 699 | + assert str(record[0].message) == ( |
| 700 | + "The `exclude_from_schema` argument to `api_view` is pending " |
| 701 | + "deprecation. Use the `schema` decorator instead, passing `None`." |
| 702 | + ) |
| 703 | + |
| 704 | + class OldFashionedExcludedView(APIView): |
| 705 | + exclude_from_schema = True |
| 706 | + |
| 707 | + def get(self, request, *args, **kwargs): |
| 708 | + pass |
| 709 | + |
| 710 | + patterns = [ |
| 711 | + url('^excluded-old-fashioned/$', OldFashionedExcludedView.as_view()), |
| 712 | + ] |
| 713 | + |
| 714 | + inspector = EndpointEnumerator(patterns) |
| 715 | + with pytest.warns(PendingDeprecationWarning) as record: |
| 716 | + inspector.get_api_endpoints() |
| 717 | + |
| 718 | + assert len(record) == 1 |
| 719 | + assert str(record[0].message) == ( |
| 720 | + "The `OldFashionedExcludedView.exclude_from_schema` attribute is " |
| 721 | + "pending deprecation. Set `schema = None` instead." |
| 722 | + ) |
0 commit comments