test_blueprints.py 3.78 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

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

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

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

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

class BlueprintsTest(TestCase):
16

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
17
18
19
20
21
22
    def create_app(self):
        self.app = Flask("test_extensions")
        self.senpy = Senpy()
        self.senpy.init_app(self.app)
        self.dir = os.path.join(os.path.dirname(__file__), "..")
        self.senpy.add_folder(self.dir)
23
        self.senpy.activate_plugin("Dummy", sync=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
24
25
26
        return self.app

    def test_home(self):
27
28
29
        """
        Calling with no arguments should ask the user for more arguments
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
30
        resp = self.client.get("/api/")
31
        self.assert404(resp)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
32
        logging.debug(resp.json)
33
        assert resp.json["status"] == 404
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
34
        atleast = {
35
            "status": 404,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
36
37
38
39
            "message": "Missing or invalid parameters",
        }
        assert check_dict(resp.json, atleast)

40
    def test_analysis(self):
41
42
43
44
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
45
        resp = self.client.get("/api/?i=My aloha mohame")
46
        self.assert200(resp)
47
        logging.debug("Got response: %s", resp.json)
48
        assert "@context" in resp.json
49
50
51
        assert check_dict(
            resp.json["@context"],
            {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
52
53
54
55
        assert "entries" in resp.json

    def test_list(self):
        """ List the plugins """
Ignacio Corcuera's avatar
Ignacio Corcuera committed
56
        resp = self.client.get("/api/plugins/")
57
58
        self.assert200(resp)
        logging.debug(resp.json)
59
60
61
62
        assert 'plugins' in resp.json
        plugins = resp.json['plugins']
        assert len(plugins) > 1
        assert list(p for p in plugins if p['name'] == "Dummy")
63
64
65
        assert "@context" in resp.json

    def test_headers(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
66
        for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
Ignacio Corcuera's avatar
Ignacio Corcuera committed
67
68
69
70
71
72
73
74
75
                            ["headers", "inHeaders"]):
            resp = self.client.get("%s" % (i))
            assert "@context" in resp.json
            resp = self.client.get("%s&%s=0" % (i, j))
            assert "@context" in resp.json
            resp = self.client.get("%s&%s=1" % (i, j))
            assert "@context" not in resp.json
            resp = self.client.get("%s&%s=true" % (i, j))
            assert "@context" not in resp.json
76
77
78

    def test_detail(self):
        """ Show only one plugin"""
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
79
        resp = self.client.get("/api/plugins/Dummy/")
80
81
82
83
84
85
86
        self.assert200(resp)
        logging.debug(resp.json)
        assert "@id" in resp.json
        assert resp.json["@id"] == "Dummy_0.1"

    def test_activate(self):
        """ Activate and deactivate one plugin """
Ignacio Corcuera's avatar
Ignacio Corcuera committed
87
        resp = self.client.get("/api/plugins/Dummy/deactivate")
88
89
        self.assert200(resp)
        sleep(0.5)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
90
        resp = self.client.get("/api/plugins/Dummy/")
91
92
93
        self.assert200(resp)
        assert "is_activated" in resp.json
        assert resp.json["is_activated"] == False
Ignacio Corcuera's avatar
Ignacio Corcuera committed
94
        resp = self.client.get("/api/plugins/Dummy/activate")
95
96
        self.assert200(resp)
        sleep(0.5)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
97
        resp = self.client.get("/api/plugins/Dummy/")
98
99
100
        self.assert200(resp)
        assert "is_activated" in resp.json
        assert resp.json["is_activated"] == True
101
102
103

    def test_default(self):
        """ Show only one plugin"""
104
        resp = self.client.get("/api/plugins/default/")
105
106
107
108
        self.assert200(resp)
        logging.debug(resp.json)
        assert "@id" in resp.json
        assert resp.json["@id"] == "Dummy_0.1"
Ignacio Corcuera's avatar
Ignacio Corcuera committed
109
        resp = self.client.get("/api/plugins/Dummy/deactivate")
110
111
        self.assert200(resp)
        sleep(0.5)
112
        resp = self.client.get("/api/plugins/default/")
113
        self.assert404(resp)