Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
hub-client / dockerhub / test / test_generate_api_docs.py
Size: Mime:
import json

from collections import OrderedDict

from django.test import TestCase
from django.conf import settings

from dockerhub.api_docs.management.commands.generate_api_docs import Command


class GenerateSwaggerDocTestCase(TestCase):
    def setUp(self):
        self.cmd = Command()
        with open('dockerhub/test/fixtures/apis.json', 'r') as f:
            self.apis = json.loads(f.read())

        self.models = {
            'WriteGithubRepoDataSerializer': {
                'required': ['id', 'name', 'avatar_url', 'type', 'repo_list'],
                'id': 'WriteGithubRepoDataSerializer',
                'properties': OrderedDict([
                    ('id', {
                        'description': None,
                        'format': 'int64',
                        'required': True,
                        'readOnly': False,
                        'type': 'integer'
                    }),
                    ('name', {
                        'description': None,
                        'required': True,
                        'readOnly': False,
                        'type': 'string'
                    }),
                    ('avatar_url', {
                        'description': None,
                        'required': True,
                        'readOnly': False,
                        'type': 'string'
                    }),
                    ('type', {
                        'enum': ['User', 'Organization'],
                        'description': None,
                        'required': True,
                        'readOnly': False,
                        'type': 'choice'
                    }),
                    ('repo_list', {
                        'description': None,
                        'items': {'$ref': 'GithubRepoDetailsSerializer'},
                        'required': True,
                        'readOnly': False,
                        'type': 'array'
                    })
                ])
            }
        }

    def test_generate_schema_object(self):
        """
        Input Schema is generated by django rest swagger.

        """
        expected_output = {
            'required': ['id', 'name', 'avatar_url', 'type', 'repo_list'],
            'type': 'object',
            'properties': {
                'avatar_url': {
                    'readOnly': False,
                    'type': 'string',
                    'description': '',
                    'format': 'string'
                },
                'type': {
                    'readOnly': False,
                    'enum': ['User', 'Organization'],
                    'type': 'string',
                    'description': '',
                    'format': 'string'
                },
                'id': {
                    'readOnly': False,
                    'type': 'integer',
                    'description': '',
                    'format': 'int64'
                },
                'repo_list': {
                    'items': {'$ref': 'GithubRepoDetailsSerializer'},
                    'readOnly': False,
                    'type': 'array',
                    'description': '',
                    'format': 'string'
                },
                'name': {
                    'readOnly': False,
                    'type': 'string',
                    'description': '',
                    'format': 'string'
                }
            }
        }

        self.assertEqual(
            self.cmd.generate_schema_object(self.models['WriteGithubRepoDataSerializer']),
            expected_output,
        )

    def test_generate_definitions_object(self):
        expected_output = {
            'WriteGithubRepoDataSerializer': {
                'required': ['id', 'name', 'avatar_url', 'type', 'repo_list'],
                'type': 'object',
                'properties': {
                    'avatar_url': {'readOnly': False, 'type': 'string', 'description': '', 'format': 'string'},
                    'type': {
                        'readOnly': False,
                        'enum': ['User', 'Organization'],
                        'type': 'string',
                        'description': '',
                        'format': 'string'
                    },
                    'id': {'readOnly': False, 'type': 'integer', 'description': '', 'format': 'int64'},
                    'repo_list': {
                        'items': {'$ref': 'GithubRepoDetailsSerializer'},
                        'readOnly': False,
                        'type': 'array',
                        'description': '',
                        'format': 'string'
                    },
                    'name': {'readOnly': False, 'type': 'string', 'description': '', 'format': 'string'}
                }
            }
        }

        self.assertEqual(
            self.cmd.generate_definitions_object(self.models),
            expected_output,
        )

    def test_generate_responses_object(self):
        expected_output = {
            200: {'description': u'Ok'},
            401: {'description': u'Not authenticated'},
            403: {'description': u'Not Authorized'},
            404: {'description': u'Not Found'},
            500: {'description': u'Server Error'},
            'default': {'description': 'Unexpected error'}
        }

        self.assertEqual(
            self.cmd.generate_responses_object(self.apis[0]['operations'][0]['responseMessages']),
            expected_output,
        )

    def test_generate_parameter_object(self):
        expected_output = {
            'description': '',
            'format': 'string',
            'in': 'path',
            'name': u'namespace',
            'required': True,
            'type': u'string'
        }

        api_input = self.apis[0]['operations'][0]['parameters'][0]
        self.assertEqual(
            self.cmd.generate_parameter_object(api_input),
            expected_output,
        )

    def test_generate_operation_object(self):
        expected_output = {
            'responses': {
                401: {'description': u'Not authenticated'},
                'default': {'description': 'Unexpected error'},
                200: {'description': u'Ok'},
                403: {'description': u'Not Authorized'},
                404: {'description': u'Not Found'},
                500: {'description': u'Server Error'}
            },
            'schemes': [],
            'description': u'<p>API endpoint that allows Repository to be viewed or edited.  </p>\n<p>Update a repository</p>',
            'parameters': [{
                'description': '',
                'format': 'string',
                'required': True,
                'in': 'path',
                'type': u'string',
                'name': u'namespace'
            }, {
                'description': '',
                'format': 'string',
                'required': True,
                'in': 'path',
                'type': u'string',
                'name': u'name'
            }, {
                'description': u'Required. 2 - 255 characters. Only lowercase letters, digits - and . characters are allowed',
                'format': 'string',
                'required': True,
                'in': 'formData',
                'type': u'string',
                'name': u'name'
            }, {
                'description': u'',
                'format': 'string',
                'required': True,
                'in': 'formData',
                'type': u'string',
                'name': u'namespace'
            }, {
                'description': u'',
                'format': 'string',
                'required': False,
                'in': 'formData',
                'type': u'string',
                'name': u'description'
            }, {
                'description': u'',
                'format': 'string',
                'required': False,
                'in': 'formData',
                'type': u'boolean',
                'name': u'is_private'
            }, {
                'description': u'',
                'format': 'string',
                'required': False,
                'in': 'formData',
                'type': u'string',
                'name': u'full_description'
            }],
            'tags': [],
            'security': [],
            'produces': [],
            'consumes': [],
            'summary': u'Update a repository'
        }
        api_input = self.apis[0]['operations'][0]

        self.assertEqual(
            self.cmd.generate_operation_object(api_input),
            expected_output,
        )

    def test_generate_path_items(self):
        api_input = self.apis[0]
        path_item = self.cmd.generate_path_item(api_input)
        self.assertEqual(
            path_item.keys(),
            ['put', 'delete', 'patch', 'get']
        )

    def test_generate_paths_object(self):
        api_input = self.apis
        paths_object = self.cmd.generate_paths_object(api_input)
        self.assertEqual(
            paths_object.keys(),
            ['/v2/repositories/{namespace}/{name}/']
        )

    def test_generate_licence_object(self):
        license = self.cmd.generate_licence_object()
        self.assertEqual(
            license,
            {
                'name': settings.SWAGGER_SETTINGS['info']['license'],
                'url': settings.SWAGGER_SETTINGS['info']['licenseUrl'],
            }
        )

    def test_generate_contact_object(self):
        contact = self.cmd.generate_contact_object()
        self.assertEqual(
            contact,
            {
                'name': 'Docker Hub',
                'url': getattr(settings, 'WEB_APP_URL', ''),
                'email': settings.SWAGGER_SETTINGS['info']['contact'],
            }
        )

    def test_generate_info_object(self):
        info = self.cmd.generate_info_object()
        self.assertEqual(
            info,
            {
                'title': settings.SWAGGER_SETTINGS['info']['title'],
                'description': settings.SWAGGER_SETTINGS['info']['description'],
                'termsOfService': settings.SWAGGER_SETTINGS['info']['termsOfServiceUrl'],
                'contact': {
                    'name': 'Docker Hub',
                    'url': getattr(settings, 'WEB_APP_URL', ''),
                    'email': settings.SWAGGER_SETTINGS['info']['contact'],
                },
                'license': {
                    'name': settings.SWAGGER_SETTINGS['info']['license'],
                    'url': settings.SWAGGER_SETTINGS['info']['licenseUrl'],
                },
                'version': settings.SWAGGER_SETTINGS['api_version'],
            }
        )

    def test_generate_swagger_object(self):
        swagger = self.cmd.generate_swagger_object(self.apis, self.models)
        self.assertEqual(
            swagger.keys(),
            [
                'schemes',
                'responses',
                'produces',
                'host',
                'info',
                'paths',
                'parameters',
                'basePath',
                'tags',
                'securityDefinitions',
                'definitions',
                'security',
                'swagger'
            ]
        )