2014-11-07 18:12:21 +00:00
|
|
|
import os
|
|
|
|
import logging
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
try:
|
|
|
|
import unittest.mock as mock
|
|
|
|
except ImportError:
|
|
|
|
import mock
|
|
|
|
from senpy.extensions import Senpy
|
|
|
|
from flask import Flask
|
|
|
|
from flask.ext.testing import TestCase
|
2014-12-01 17:27:20 +00:00
|
|
|
from gevent import sleep
|
2015-02-24 06:15:25 +00:00
|
|
|
from itertools import product
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
def check_dict(indic, template):
|
2014-11-20 18:29:49 +00:00
|
|
|
return all(item in indic.items() for item in template.items())
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
|
|
|
class BlueprintsTest(TestCase):
|
2015-02-24 06:15:25 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
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)
|
2014-12-01 17:27:20 +00:00
|
|
|
self.senpy.activate_plugin("Dummy", sync=True)
|
2014-11-07 18:12:21 +00:00
|
|
|
return self.app
|
|
|
|
|
|
|
|
def test_home(self):
|
2015-02-24 06:15:25 +00:00
|
|
|
"""
|
|
|
|
Calling with no arguments should ask the user for more arguments
|
|
|
|
"""
|
2014-11-07 18:12:21 +00:00
|
|
|
resp = self.client.get("/")
|
2015-02-24 06:15:25 +00:00
|
|
|
self.assert404(resp)
|
2014-11-07 18:12:21 +00:00
|
|
|
logging.debug(resp.json)
|
2015-02-24 06:15:25 +00:00
|
|
|
assert resp.json["status"] == 404
|
2014-11-07 18:12:21 +00:00
|
|
|
atleast = {
|
2015-02-24 06:15:25 +00:00
|
|
|
"status": 404,
|
2014-11-07 18:12:21 +00:00
|
|
|
"message": "Missing or invalid parameters",
|
|
|
|
}
|
|
|
|
assert check_dict(resp.json, atleast)
|
|
|
|
|
2014-12-01 17:27:20 +00:00
|
|
|
def test_analysis(self):
|
2015-02-24 06:15:25 +00:00
|
|
|
"""
|
|
|
|
The dummy plugin returns an empty response,\
|
|
|
|
it should contain the context
|
|
|
|
"""
|
2014-12-01 17:27:20 +00:00
|
|
|
resp = self.client.get("/?i=My aloha mohame")
|
|
|
|
self.assert200(resp)
|
2015-02-24 06:15:25 +00:00
|
|
|
logging.debug("Got response: %s", resp.json)
|
2014-12-01 17:27:20 +00:00
|
|
|
assert "@context" in resp.json
|
2015-02-24 06:15:25 +00:00
|
|
|
assert check_dict(
|
|
|
|
resp.json["@context"],
|
|
|
|
{"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
|
2014-12-01 17:27:20 +00:00
|
|
|
assert "entries" in resp.json
|
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
""" List the plugins """
|
|
|
|
resp = self.client.get("/plugins/")
|
|
|
|
self.assert200(resp)
|
|
|
|
logging.debug(resp.json)
|
|
|
|
assert "Dummy" in resp.json
|
2015-02-24 06:15:25 +00:00
|
|
|
assert "@context" in resp.json
|
|
|
|
|
|
|
|
def test_headers(self):
|
|
|
|
for i, j in product(["/plugins/?nothing=", "/?i=test&"],
|
|
|
|
["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
|
2014-12-01 17:27:20 +00:00
|
|
|
|
|
|
|
def test_detail(self):
|
|
|
|
""" Show only one plugin"""
|
|
|
|
resp = self.client.get("/plugins/Dummy")
|
|
|
|
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 """
|
|
|
|
resp = self.client.get("/plugins/Dummy/deactivate")
|
|
|
|
self.assert200(resp)
|
|
|
|
sleep(0.5)
|
|
|
|
resp = self.client.get("/plugins/Dummy")
|
|
|
|
self.assert200(resp)
|
|
|
|
assert "is_activated" in resp.json
|
|
|
|
assert resp.json["is_activated"] == False
|
|
|
|
resp = self.client.get("/plugins/Dummy/activate")
|
|
|
|
self.assert200(resp)
|
|
|
|
sleep(0.5)
|
|
|
|
resp = self.client.get("/plugins/Dummy")
|
|
|
|
self.assert200(resp)
|
|
|
|
assert "is_activated" in resp.json
|
|
|
|
assert resp.json["is_activated"] == True
|
2015-02-24 06:15:25 +00:00
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
""" Show only one plugin"""
|
|
|
|
resp = self.client.get("/default")
|
|
|
|
self.assert200(resp)
|
|
|
|
logging.debug(resp.json)
|
|
|
|
assert "@id" in resp.json
|
|
|
|
assert resp.json["@id"] == "Dummy_0.1"
|
|
|
|
resp = self.client.get("/plugins/Dummy/deactivate")
|
|
|
|
self.assert200(resp)
|
|
|
|
sleep(0.5)
|
|
|
|
resp = self.client.get("/default")
|
|
|
|
self.assert404(resp)
|