test_blueprints.py 4.53 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
import os
import logging
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
3
import json
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
5
6
from senpy.extensions import Senpy
from flask import Flask
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
from unittest import TestCase
8
from gevent import sleep
9
from itertools import product
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
11

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
12
def check_dict(indic, template):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
13
    return all(item in indic.items() for item in template.items())
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
14

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
15

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
17
18
def parse_resp(resp):
    return json.loads(resp.data.decode('utf-8'))

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
19
20

class BlueprintsTest(TestCase):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
21
    def setUp(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
22
        self.app = Flask("test_extensions")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
23
        self.client = self.app.test_client()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
24
25
26
27
        self.senpy = Senpy()
        self.senpy.init_app(self.app)
        self.dir = os.path.join(os.path.dirname(__file__), "..")
        self.senpy.add_folder(self.dir)
28
        self.senpy.activate_plugin("Dummy", sync=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
29

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
30
31
    def assertCode(self, resp, code):
        self.assertEqual(resp.status_code, code)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
32

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
    def test_home(self):
34
35
36
        """
        Calling with no arguments should ask the user for more arguments
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
        resp = self.client.get("/api/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
38
39
40
41
        self.assertCode(resp, 404)
        js = parse_resp(resp)
        logging.debug(js)
        assert js["status"] == 404
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
42
        atleast = {
43
            "status": 404,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
44
45
            "message": "Missing or invalid parameters",
        }
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46
        assert check_dict(js, atleast)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
47

48
    def test_analysis(self):
49
50
51
52
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
53
        resp = self.client.get("/api/?i=My aloha mohame")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
54
55
56
57
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert "@context" in js
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
58
        assert "entries" in js
59
60
61

    def test_list(self):
        """ List the plugins """
Ignacio Corcuera's avatar
Ignacio Corcuera committed
62
        resp = self.client.get("/api/plugins/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
64
65
66
67
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert 'plugins' in js
        plugins = js['plugins']
68
69
        assert len(plugins) > 1
        assert list(p for p in plugins if p['name'] == "Dummy")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
70
        assert "@context" in js
71
72

    def test_headers(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
73
        for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
74
                            ["inHeaders"]):
Ignacio Corcuera's avatar
Ignacio Corcuera committed
75
            resp = self.client.get("%s" % (i))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
76
77
            js = parse_resp(resp)
            assert "@context" in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
78
            resp = self.client.get("%s&%s=0" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
79
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
80
            assert "@context" in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
81
            resp = self.client.get("%s&%s=1" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
82
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
83
            assert "@context" not in js
Ignacio Corcuera's avatar
Ignacio Corcuera committed
84
            resp = self.client.get("%s&%s=true" % (i, j))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
85
            js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
86
            assert "@context" not in js
87
88
89

    def test_detail(self):
        """ Show only one plugin"""
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
90
        resp = self.client.get("/api/plugins/Dummy/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
91
92
93
94
95
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert "@id" in js
        assert js["@id"] == "Dummy_0.1"
96
97
98

    def test_activate(self):
        """ Activate and deactivate one plugin """
Ignacio Corcuera's avatar
Ignacio Corcuera committed
99
        resp = self.client.get("/api/plugins/Dummy/deactivate")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
100
        self.assertCode(resp, 200)
101
        sleep(0.5)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
102
        resp = self.client.get("/api/plugins/Dummy/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
103
104
105
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        assert "is_activated" in js
106
        assert not js["is_activated"]
Ignacio Corcuera's avatar
Ignacio Corcuera committed
107
        resp = self.client.get("/api/plugins/Dummy/activate")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
108
        self.assertCode(resp, 200)
109
        sleep(0.5)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
110
        resp = self.client.get("/api/plugins/Dummy/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
111
112
        self.assertCode(resp, 200)
        js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
113
        assert "is_activated" in js
114
        assert js["is_activated"]
115
116
117

    def test_default(self):
        """ Show only one plugin"""
118
        resp = self.client.get("/api/plugins/default/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
119
120
121
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
122
        assert "@id" in js
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
123
        assert js["@id"] == "Dummy_0.1"
Ignacio Corcuera's avatar
Ignacio Corcuera committed
124
        resp = self.client.get("/api/plugins/Dummy/deactivate")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
125
        self.assertCode(resp, 200)
126
        sleep(0.5)
127
        resp = self.client.get("/api/plugins/default/")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
128
        self.assertCode(resp, 404)
129
130
131

    def test_context(self):
        resp = self.client.get("/api/contexts/context.jsonld")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
132
133
134
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        assert "@context" in js
135
        assert check_dict(
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
136
            js["@context"],
137
138
139
140
            {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})

    def test_schema(self):
        resp = self.client.get("/api/schemas/definitions.json")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
141
142
        self.assertCode(resp, 200)
        js = parse_resp(resp)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
143
        assert "$schema" in js