aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_pydrilla.py
blob: 22022ae4aedacfe41e57915439e443525f8e4a37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# SPDX-License-Identifier: AGPL-3.0-or-later

# Repository tests
#
# This file is part of Hydrilla
#
# Copyright (C) 2021 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use this code
# in a proprietary program, I am not going to enforce this in court.

import pytest
import sys
import shutil
from pathlib import Path
from os import mkdir, unlink, environ
import json
from markupsafe import escape

from pydrilla import pydrilla, create_app

test_dir         = Path(__file__).resolve().parent
packages_dir     = test_dir.parent
development_config_path = test_dir / 'development_config.json'

@pytest.fixture
def client():
    app = create_app(development_config_path, flask_config={'TESTING': True})

    with app.test_client() as client:
        yield client

@pytest.fixture
def development_config():
    with open(development_config_path) as config_file:
        yield json.loads(pydrilla.strip_json_comments(config_file.read()))

def test_api_basic(client, development_config):
    response = client.get('/')
    assert b'html' in response.data
    sources_uri = development_config['hydrilla_sources_uri']
    assert escape(sources_uri).encode() in response.data

    for item_type in ['mapping', 'resource']:
        response = client.get(f'/{item_type}s/helloapple')
        assert response.status_code == 200
        definition = json.loads(response.data.decode())
        assert definition['type']        == item_type
        assert definition['source_name'] == 'hello'
        assert definition['version']     == [2021, 11, 10]

        response = client.get(f'/{item_type}s/helloapple?ver=2021.11.10.0')
        assert response.status_code == 200
        assert definition == json.loads(response.data.decode())

        response = client.get(f'/{item_type}s/helloapple?ver=2021.11.10.999')
        assert response.status_code == 404

        response = client.get(f'/{item_type}s/helloapple?ver=random_bad_input')
        assert response.status_code == 400

        response = client.get(f'/{item_type}s/random-bad-identifier')
        assert response.status_code == 404

        response = client.get(f'/{item_type}s/helloapple?ver=all')
        assert response.status_code == 200
        definitions = json.loads(response.data.decode())
        assert type(definitions) is list
        assert all([d['type']    == item_type      for d in definitions])
        assert any([d['version'] == [2021, 11, 10] for d in definitions])

    response = client.get('/licenses/CC0-1.0')
    assert response.status_code == 200
    definition = json.loads(response.data.decode())
    assert definition['type']        == 'license'
    assert definition['long_name']   == 'Creative Commons Zero v1.0 Universal'
    assert definition['source_name'] == 'hello'

    response = client.get('/licenses/random-bad-identifier')
    assert response.status_code == 404

    response = client.get('/sources/hello')
    assert response.status_code == 200
    definition = json.loads(response.data.decode())

    assert definition['source_name'] == 'hello'
    assert type(definition['schema_version']) is list

    response = client.get('/sources/random-bad-identifier')
    assert response.status_code == 404

    response = client.get('/query?url=https://hachettebugs.koszko.org')
    assert response.status_code == 200
    definitions = json.loads(response.data.decode())
    assert type(definitions) is list
    assert all([d['type'] == 'mapping' for d in definitions])
    assert any([p['pattern'] == 'https://hachettebugs.koszko.org/***'
                for d in definitions for p in d['payloads']])

    response = client.get('/query?url=https://random_bad_domain.org/something')
    assert response.status_code == 200
    definitions = json.loads(response.data.decode())
    assert definitions == []

    resource_uri = development_config['static_resource_uri']
    response = client.get('/sources/hello/hello.js')
    assert response.status_code == 301
    assert response.location == resource_uri + 'hello/hello.js'
    response = client.get('/sources/random-bad-identifier/hello.js')
    assert response.status_code == 404
    response = client.get('/sources/hello/random/bad/path')
    assert response.status_code == 301
    assert response.location == resource_uri + 'hello/random/bad/path'

def test_normalize_version():
    assert pydrilla.normalize_version([4, 5, 3, 0, 0]) == [4, 5, 3]
    assert pydrilla.normalize_version([1, 0, 5, 0])    == [1, 0, 5]
    assert pydrilla.normalize_version([3, 3])          == [3, 3]

def test_strip_json_comments(development_config):
    assert development_config['static_resource_uri'] == 'http://localhost:8000/'