Skip to content
Snippets Groups Projects
test_classifier_routes.py 1.67 KiB
import json
import jsonschema

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


def test_infinera_addresses(client):
    response_schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "array",
        "items": {"type": "string"}
    }

    rv = client.post(
        "/classifier/infinera-dna-addresses",
        headers=DEFAULT_REQUEST_HEADERS)
    assert rv.status_code == 200
    jsonschema.validate(
        json.loads(rv.data.decode("utf-8")),
        response_schema)


def test_juniper_addresses(mocker, client):

    test_data = [
        {"ip_address": "a.b.c.d", "project_name": "AAABBB"},
        {"ip_address": "b.c.d.e", "project_name": "CCCCDDDDD"},
        {"ip_address": "c.d.e.f", "project_name": "EFEFEFEF"},
        {"ip_address": "::1", "project_name": "GGHHGGHH"}
    ]

    class MockedRedis():
        def __init__(self):
            pass

        def get(self, ignored):
            return json.dumps(test_data).encode('utf-8')

    mocker.patch(
        'inventory_provider.routes.classifier.db.get_redis',
        return_value=MockedRedis())

    response_schema = {
            "$schema": "http://json-schema.org/draft-07/schema#",
            "type": "array",
            "items": {"type": "string"}
        }

    rv = client.post(
        "/classifier/juniper-server-addresses",
        headers=DEFAULT_REQUEST_HEADERS)
    assert rv.status_code == 200
    response_data = json.loads(rv.data.decode('utf-8'))
    jsonschema.validate(response_data, response_schema)
    assert len(response_data) == len(test_data)
    assert set([x['ip_address'] for x in test_data]) == set(response_data)