Esempio n. 1
0
def test_full_people_output_is_valid_and_expected(people_descriptor):
    swagger = convert_descriptor_to_swagger([people_descriptor])
    assert swagger == people_output

    output = deepcopy(swagger)
    errors = validate_spec(output)
    assert errors is None
Esempio n. 2
0
def test_full_mn_output_is_valid_and_expected(people_descriptor,
                                              team_descriptor):
    descriptors = [people_descriptor, team_descriptor]
    swagger = convert_descriptor_to_swagger(descriptors, [["people", "team"]])

    assert swagger == full_output

    output = deepcopy(swagger)
    errors = validate_spec(output)
    assert errors is None
def generate_all_swagger(descriptors):
    swagger = convert_descriptor_to_swagger(descriptors)
    return swagger
Esempio n. 4
0
def generate_swagger(descriptor):
    logger.info(json.dumps(descriptor, indent=4))
    swagger = convert_descriptor_to_swagger([descriptor])
    return swagger
def test_load_desc(swagger):
    expected_output = {
        "openapi":
        "3.0.0",
        "info": {
            "title": "Swagger Data Resource",
            "description":
            "Autogenerated Data Resource API swagger file.  You can find\nout more about Swagger at\n[http://swagger.io](http://swagger.io) or on\n[irc.freenode.net, #swagger](http://swagger.io/irc/).\n",
            "termsOfService": "http://swagger.io/terms/",
            "contact": {
                "email": "*****@*****.**"
            },
            "license": {
                "name": "Apache 2.0",
                "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
            },
            "version": "1.0.0"
        },
        "servers": [{
            "description": "Local server.",
            "url": "http://localhost:8000"
        }],
        "components": {
            "parameters": {
                "offsetParam": {
                    "name": "offset",
                    "in": "query",
                    "description": "the offset",
                    "required": False,
                    "style": "form",
                    "explode": True,
                    "schema": {
                        "type": "integer"
                    }
                },
                "limitParam": {
                    "name": "limit",
                    "in": "query",
                    "description": "the limit",
                    "required": False,
                    "style": "form",
                    "explode": True,
                    "schema": {
                        "type": "integer"
                    }
                }
            },
            "schemas": {
                "Links": {
                    "type": "object",
                    "properties": {
                        "rel": {
                            "type": "string",
                            "description": "...",
                            "example": "first",
                            "enum": ["self", "first", "prev", "next", "last"]
                        },
                        "href": {
                            "type": "string",
                            "description": "...",
                            "example": "/credentials?offset=0&limit=20"
                        }
                    },
                    "description": "..."
                },
                "Created": {
                    "type": "object",
                    "properties": {
                        "message": {
                            "type": "string",
                            "description": "...",
                            "example": "Successfully added new resource."
                        },
                        "id": {
                            "type": "integer",
                            "description": "...",
                            "format": "int64",
                            "example": 1
                        }
                    }
                },
                "Credential": {
                    "required": ["credential_name"],
                    "type": "object",
                    "properties": {
                        "id": {
                            "type":
                            "integer",
                            "format":
                            "int64",
                            "description":
                            "Credential ID - Credential's unique identifier"
                        },
                        "credential_name": {
                            "type": "string",
                            "description":
                            "Credential Name - Credential's Name"
                        }
                    },
                    "description": "..."
                },
                "AllCredentials": {
                    "type": "object",
                    "properties": {
                        "credentials": {
                            "type": "array",
                            "items": {
                                "$ref": "#/components/schemas/Credential"
                            }
                        },
                        "links": {
                            "type": "array",
                            "items": {
                                "$ref": "#/components/schemas/Links"
                            }
                        }
                    },
                    "description": "..."
                }
            },
            "responses": {
                "Created": {
                    "description": "...",
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/Created"
                            }
                        }
                    }
                },
                "AllCredentials": {
                    "description": "...",
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/AllCredentials"
                            }
                        }
                    }
                }
            },
            "requestBodies": {
                "Credential": {
                    "description":
                    "Pet object that needs to be added to the store",
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/Credential"
                            }
                        }
                    },
                    "required": True
                }
            }
        },
        "tags": [{
            "name": "credentials",
            "description": "...",
            "externalDocs": {
                "description": "Find out more",
                "url": "http://swagger.io"
            }
        }],
        "paths": {
            "/credentials": {
                "get": {
                    "tags": ["credentials"],
                    "summary":
                    "Get all items",
                    "parameters": [{
                        "$ref":
                        "#/components/parameters/offsetParam"
                    }, {
                        "$ref": "#/components/parameters/limitParam"
                    }],
                    "responses": {
                        "200": {
                            "$ref": "#/components/responses/AllCredentials"
                        }
                    }
                },
                "post": {
                    "tags": ["credentials"],
                    "summary": "Create an item",
                    "requestBody": {
                        "$ref": "#/components/requestBodies/Credential"
                    },
                    "responses": {
                        "201": {
                            "$ref": "#/components/responses/Created"
                        }
                    }
                }
            },
            "/credentials/{id}": {
                "get": {
                    "tags": ["credentials"],
                    "summary":
                    "Get one item",
                    "parameters": [{
                        "name": "id",
                        "in": "path",
                        "description": "User ID",
                        "required": True,
                        "style": "simple",
                        "explode": False,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }],
                    "responses": {
                        "200": {
                            "description": "ok"
                        }
                    }
                },
                "put": {
                    "tags": ["credentials"],
                    "summary":
                    "Put one item",
                    "parameters": [{
                        "name": "id",
                        "in": "path",
                        "description": "User ID",
                        "required": True,
                        "style": "simple",
                        "explode": False,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }],
                    "requestBody": {
                        "$ref": "#/components/requestBodies/Credential"
                    },
                    "responses": {
                        "200": {
                            "description": "ok"
                        }
                    }
                },
                "delete": {
                    "tags": ["credentials"],
                    "summary":
                    "Get one item",
                    "parameters": [{
                        "name": "id",
                        "in": "path",
                        "description": "User ID",
                        "required": True,
                        "style": "simple",
                        "explode": False,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }],
                    "responses": {
                        "200": {
                            "description": "ok"
                        }
                    }
                },
                "patch": {
                    "tags": ["credentials"],
                    "summary":
                    "Get one item",
                    "parameters": [{
                        "name": "id",
                        "in": "path",
                        "description": "User ID",
                        "required": True,
                        "style": "simple",
                        "explode": False,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }],
                    "requestBody": {
                        "$ref": "#/components/requestBodies/Credential"
                    },
                    "responses": {
                        "200": {
                            "description": "ok"
                        }
                    }
                }
            }
        }
    }

    credentials_descriptor = {
        "api": {
            "resource":
            "credentials",
            "methods": [{
                "get": {
                    "enabled": True,
                    "secured": False,
                    "grants": ["get:users"]
                },
                "post": {
                    "enabled": True,
                    "secured": False,
                    "grants": []
                },
                "put": {
                    "enabled": True,
                    "secured": False,
                    "grants": []
                },
                "patch": {
                    "enabled": True,
                    "secured": False,
                    "grants": []
                },
                "delete": {
                    "enabled": True,
                    "secured": False,
                    "grants": []
                }
            }]
        },
        "datastore": {
            "tablename": "credentials",
            "restricted_fields": [],
            "schema": {
                "fields": [{
                    "name": "id",
                    "title": "Credential ID",
                    "type": "integer",
                    "description": "Credential's unique identifier",
                    "required": False
                }, {
                    "name": "credential_name",
                    "title": "Credential Name",
                    "type": "string",
                    "description": "Credential's Name",
                    "required": True
                }],
                "primaryKey":
                "id"
            }
        }
    }

    output = convert_descriptor_to_swagger([credentials_descriptor])

    assert not DeepDiff(expected_output, output)
def test_produces_valid_swagger():
    programs_descriptor = {
        "api": {
            "resource":
            "programs",
            "methods": [{
                "get": {
                    "enabled": True,
                    "secured": False,
                    "grants": ["get:users"]
                },
                "post": {
                    "enabled": True,
                    "secured": False,
                    "grants": ["get:users"]
                },
                "put": {
                    "enabled": True,
                    "secured": True,
                    "grants": ["get:users"]
                },
                "patch": {
                    "enabled": True,
                    "secured": True,
                    "grants": ["get:users"]
                },
                "delete": {
                    "enabled": True,
                    "secured": True,
                    "grants": ["get:users"]
                },
                "custom": [{
                    "resource":
                    "/programs/credentials",
                    "methods": [{
                        "get": {
                            "enabled": True,
                            "secured": False,
                            "grants": ["get:users"]
                        },
                        "put": {
                            "enabled": True,
                            "secured": False,
                            "grants": []
                        },
                        "patch": {
                            "enabled": True,
                            "secured": False,
                            "grants": []
                        },
                        "delete": {
                            "enabled": True,
                            "secured": False,
                            "grants": []
                        }
                    }]
                }]
            }]
        },
        "datastore": {
            "tablename": "programs",
            "restricted_fields": [],
            "schema": {
                "fields": [{
                    "name": "id",
                    "title": "Program ID",
                    "type": "integer",
                    "description": "Program's unique identifier",
                    "required": False
                }, {
                    "name": "program_name",
                    "title": "Program Name",
                    "type": "string",
                    "description": "Program's name.",
                    "required": True
                }, {
                    "name": "program_code",
                    "title": "Program Code",
                    "type": "integer",
                    "description": "Program's Code",
                    "required": True
                }, {
                    "name": "program_description",
                    "title": "Program Description",
                    "type": "string",
                    "description": "Program's Description",
                    "required": True
                }, {
                    "name": "program_status",
                    "title": "Program Status",
                    "type": "string",
                    "description": "Program's Status",
                    "required": True
                }, {
                    "name": "program_fees",
                    "title": "Program Fees",
                    "type": "number",
                    "description": "Program's tuition fees",
                    "required": True
                }, {
                    "name": "eligibility_criteria",
                    "title": "Eligibility Criteria",
                    "type": "string",
                    "description": "Program's eligibility criteria",
                    "required": True
                }, {
                    "name": "program_url",
                    "title": "Program URL",
                    "type": "string",
                    "format": "uri",
                    "description": "Program's webpage url",
                    "required": True
                }, {
                    "name": "program_contact_phone",
                    "title": "Program Contact Phone",
                    "type": "string",
                    "description": "Program's contact telephone",
                    "required": False
                }, {
                    "name": "program_contact_email",
                    "title": "Program Contact Email",
                    "type": "string",
                    "format": "email",
                    "description": "Program's contact email address",
                    "required": False
                }, {
                    "name": "languages",
                    "title": "Languages",
                    "type": "string",
                    "description": "Languages the program is offered in",
                    "required": False
                }, {
                    "name": "current_intake_capacity",
                    "title": "Current Intake Capacity",
                    "type": "integer",
                    "description": "Current intake capacity of the program",
                    "required": False
                }, {
                    "name": "program_offering_model",
                    "title": "Program Offering Model",
                    "type": "integer",
                    "description": "The program's current offering model",
                    "required": False
                }, {
                    "name": "program_length_hours",
                    "title": "Program Length (Hours)",
                    "type": "number",
                    "description": "Length of the program (in hours)",
                    "required": False
                }, {
                    "name": "program_length_weeks",
                    "title": "Program Length (Weeks)",
                    "type": "number",
                    "description": "Length of the program (in weeks)",
                    "required": False
                }, {
                    "name": "program_soc",
                    "title": "Program SOC",
                    "type": "integer",
                    "description": "Program SOC",
                    "required": False
                }, {
                    "name": "funding_sources",
                    "title": "Funding Source",
                    "type": "string",
                    "description": "The program's funding source",
                    "required": False
                }, {
                    "name": "on_etpl",
                    "title": "On ETPL",
                    "type": "integer",
                    "description": "Whether or not the student is on ETPL",
                    "required": False
                }, {
                    "name": "cost_of_books_and_supplies",
                    "title": "Cost of Books and Supplies",
                    "type": "number",
                    "description": "Cost of Books and Supplies",
                    "required": False
                }, {
                    "name": "provider_id",
                    "title": "Provider ID",
                    "type": "integer",
                    "description": "Foreign key for provider",
                    "required": False
                }, {
                    "name": "location_id",
                    "title": "Provider ID",
                    "type": "integer",
                    "description": "Foreign key for provider",
                    "required": False
                }, {
                    "name": "credential_earned",
                    "title": "Provider ID",
                    "type": "integer",
                    "description": "Foreign key for provider",
                    "required": False
                }, {
                    "name": "potential_outcome_id",
                    "title": "Provider ID",
                    "type": "integer",
                    "description": "Foreign key for provider",
                    "required": False
                }, {
                    "name": "prerequisite_id",
                    "title": "Provider ID",
                    "type": "integer",
                    "description": "Foreign key for provider",
                    "required": False
                }],
                "primaryKey":
                "id",
                "foreignKeys": [{
                    "fields": ["provider_id"],
                    "reference": {
                        "resource": "providers",
                        "fields": ["id"]
                    }
                }, {
                    "fields": ["location_id"],
                    "reference": {
                        "resource": "locations",
                        "fields": ["id"]
                    }
                }, {
                    "fields": ["credential_earned"],
                    "reference": {
                        "resource": "credentials",
                        "fields": ["id"]
                    }
                }, {
                    "fields": ["potential_outcome_id"],
                    "reference": {
                        "resource": "program_potential_outcomes",
                        "fields": ["id"]
                    }
                }, {
                    "fields": ["prerequisite_id"],
                    "reference": {
                        "resource": "program_prerequisites",
                        "fields": ["id"]
                    }
                }]
            }
        }
    }

    credentials_descriptor = {
        "api": {
            "resource":
            "credentials",
            "methods": [{
                "get": {
                    "enabled": True,
                    "secured": False,
                    "grants": ["get:users"]
                },
                "post": {
                    "enabled": True,
                    "secured": False,
                    "grants": []
                },
                "put": {
                    "enabled": True,
                    "secured": False,
                    "grants": []
                },
                "patch": {
                    "enabled": True,
                    "secured": False,
                    "grants": []
                },
                "delete": {
                    "enabled": True,
                    "secured": False,
                    "grants": []
                }
            }]
        },
        "datastore": {
            "tablename": "credentials",
            "restricted_fields": [],
            "schema": {
                "fields": [{
                    "name": "id",
                    "title": "Credential ID",
                    "type": "integer",
                    "description": "Credential's unique identifier",
                    "required": False
                }, {
                    "name": "credential_name",
                    "title": "Credential Name",
                    "type": "string",
                    "description": "Credential's Name",
                    "required": True
                }],
                "primaryKey":
                "id"
            }
        }
    }

    descriptors = [programs_descriptor, credentials_descriptor]

    swagger = convert_descriptor_to_swagger(descriptors)
    # print(json.dumps(swagger, indent=4))

    output = validate_spec(swagger)
    expect(output).to(equal(None))
from convert_descriptor_to_swagger import convert_descriptor_to_swagger
import json
from glob import glob

descriptors = []
desc_folder = 'descriptors/'

for f_name in glob(f'{desc_folder}/*.json'):
    with open(f_name, 'r') as f:
        desc = json.load(f)

    descriptors.append(desc)

swagger = convert_descriptor_to_swagger(descriptors)

# print(json.dumps(swagger, indent=4))

# wipe file
open('swag.json', 'w').close()
with open('swag.json', 'w') as outfile:
    json.dump(swagger, outfile)