Skip to content
Snippets Groups Projects
test_lg_routes.py 2.87 KiB
import json
import jsonschema
import pytest

DEFAULT_REQUEST_HEADERS = {
    "Content-type": "application/json",
    "Accept": ["application/json"]
}

LG_ROUTERS_SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema#",

    "definitions": {
        "pop-info": {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "abbreviation": {"type": "string"},
                "country": {"type": "string"},
                "country code": {"type": "string"},
                "city": {"type": "string"},
                "longitude": {"type": "number"},
                "latitude": {"type": "number"}
            },
            "required": [
                "name",
                "abbreviation",
                "country",
                "country code",
                "city",
                "longitude",
                "latitude"
            ],
            "additionalProperties": False
        },
        "router": {
            "type": "object",
            "properties": {
                "equipment name": {"type": "string"},
                "type": {
                    "type": "string",
                    "enum": ["INTERNAL", "CORE"]
                },
                "pop": {"$ref": "#/definitions/pop-info"}
            },
            "required": ["equipment name", "type", "pop"],
            "additionalProperties": False
        }
    },

    "type": "array",
    "items": {"$ref": "#/definitions/router"}
}


def test_public_routers(client):
    rv = client.get(
        '/lg/routers/public',
        headers=DEFAULT_REQUEST_HEADERS)
    assert rv.status_code == 200
    assert rv.is_json
    response_data = json.loads(rv.data.decode('utf-8'))
    jsonschema.validate(response_data, LG_ROUTERS_SCHEMA)

    assert response_data  # test data is non-empty
    # no internal routers should be present
    assert all(r['type'] == 'CORE' for r in response_data)


def test_internal_routers(client):
    rv = client.get(
        '/lg/routers/all',
        headers=DEFAULT_REQUEST_HEADERS)
    assert rv.status_code == 200
    assert rv.is_json
    response_data = json.loads(rv.data.decode('utf-8'))
    jsonschema.validate(response_data, LG_ROUTERS_SCHEMA)

    assert response_data  # test data is non-empty

    # response should contain both public & internal routers
    assert any(r['type'] == 'INTERNAL' for r in response_data)
    assert any(r['type'] == 'CORE' for r in response_data)


@pytest.mark.parametrize('bad_endpoint', [

    # bad access param keyword
    '/lg/routers/ALL',
    '/lg/routers/any',
    '/lg/routers/internal',

    # no access param keyword
    '/lg/routers',
    '/lg/routers/',

    # other endpoints
    '/lg',
    '/lg/',
    '/lg/something'
    ])
def test_bad_endpoints(client, bad_endpoint):
    rv = client.get(
        bad_endpoint,
        headers=DEFAULT_REQUEST_HEADERS)
    assert rv.status_code == 404