2014-11-07 18:12:21 +00:00
|
|
|
import os
|
|
|
|
import logging
|
2016-09-21 16:59:28 +00:00
|
|
|
import json
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
from senpy.extensions import Senpy
|
|
|
|
from flask import Flask
|
2016-09-21 16:59:28 +00:00
|
|
|
from unittest 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
|
|
|
|
2016-09-21 16:59:28 +00:00
|
|
|
def parse_resp(resp):
|
|
|
|
return json.loads(resp.data.decode('utf-8'))
|
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
|
|
|
class BlueprintsTest(TestCase):
|
2015-02-24 06:15:25 +00:00
|
|
|
|
2016-09-21 16:59:28 +00:00
|
|
|
def setUp(self):
|
2014-11-07 18:12:21 +00:00
|
|
|
self.app = Flask("test_extensions")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.client = self.app.test_client()
|
2014-11-07 18:12:21 +00:00
|
|
|
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
|
|
|
|
2016-09-21 16:59:28 +00:00
|
|
|
def assertCode(self, resp, code):
|
|
|
|
self.assertEqual(resp.status_code, code)
|
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
def test_home(self):
|
2015-02-24 06:15:25 +00:00
|
|
|
"""
|
|
|
|
Calling with no arguments should ask the user for more arguments
|
|
|
|
"""
|
2016-02-19 18:24:09 +00:00
|
|
|
resp = self.client.get("/api/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 404)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug(js)
|
|
|
|
assert js["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",
|
|
|
|
}
|
2016-09-21 16:59:28 +00:00
|
|
|
assert check_dict(js, atleast)
|
2014-11-07 18:12:21 +00:00
|
|
|
|
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
|
|
|
|
"""
|
2016-02-19 18:24:09 +00:00
|
|
|
resp = self.client.get("/api/?i=My aloha mohame")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug("Got response: %s", js)
|
|
|
|
assert "@context" in js
|
|
|
|
assert "entries" in js
|
2014-12-01 17:27:20 +00:00
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
""" List the plugins """
|
2016-02-02 10:34:30 +00:00
|
|
|
resp = self.client.get("/api/plugins/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug(js)
|
|
|
|
assert 'plugins' in js
|
|
|
|
plugins = js['plugins']
|
2016-02-20 17:15:04 +00:00
|
|
|
assert len(plugins) > 1
|
|
|
|
assert list(p for p in plugins if p['name'] == "Dummy")
|
2016-09-21 16:59:28 +00:00
|
|
|
assert "@context" in js
|
2015-02-24 06:15:25 +00:00
|
|
|
|
|
|
|
def test_headers(self):
|
2016-02-19 18:24:09 +00:00
|
|
|
for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
|
2016-03-02 04:07:48 +00:00
|
|
|
["inHeaders"]):
|
2016-02-02 10:27:56 +00:00
|
|
|
resp = self.client.get("%s" % (i))
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "@context" in js
|
2016-02-02 10:27:56 +00:00
|
|
|
resp = self.client.get("%s&%s=0" % (i, j))
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "@context" in js
|
2016-02-02 10:27:56 +00:00
|
|
|
resp = self.client.get("%s&%s=1" % (i, j))
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "@context" not in js
|
2016-02-02 10:27:56 +00:00
|
|
|
resp = self.client.get("%s&%s=true" % (i, j))
|
2016-09-21 16:59:28 +00:00
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "@context" not in js
|
2014-12-01 17:27:20 +00:00
|
|
|
|
|
|
|
def test_detail(self):
|
|
|
|
""" Show only one plugin"""
|
2016-02-19 18:24:09 +00:00
|
|
|
resp = self.client.get("/api/plugins/Dummy/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug(js)
|
|
|
|
assert "@id" in js
|
|
|
|
assert js["@id"] == "Dummy_0.1"
|
2014-12-01 17:27:20 +00:00
|
|
|
|
|
|
|
def test_activate(self):
|
|
|
|
""" Activate and deactivate one plugin """
|
2016-02-02 09:39:59 +00:00
|
|
|
resp = self.client.get("/api/plugins/Dummy/deactivate")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
2014-12-01 17:27:20 +00:00
|
|
|
sleep(0.5)
|
2016-02-19 18:24:09 +00:00
|
|
|
resp = self.client.get("/api/plugins/Dummy/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "is_activated" in js
|
|
|
|
assert js["is_activated"] == False
|
2016-02-02 09:39:59 +00:00
|
|
|
resp = self.client.get("/api/plugins/Dummy/activate")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
2014-12-01 17:27:20 +00:00
|
|
|
sleep(0.5)
|
2016-02-19 18:24:09 +00:00
|
|
|
resp = self.client.get("/api/plugins/Dummy/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "is_activated" in js
|
|
|
|
assert js["is_activated"] == True
|
2015-02-24 06:15:25 +00:00
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
""" Show only one plugin"""
|
2016-02-20 17:15:04 +00:00
|
|
|
resp = self.client.get("/api/plugins/default/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
logging.debug(js)
|
|
|
|
assert "@id" in js
|
|
|
|
assert js["@id"] == "Dummy_0.1"
|
2016-02-02 09:39:59 +00:00
|
|
|
resp = self.client.get("/api/plugins/Dummy/deactivate")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
2015-02-24 06:15:25 +00:00
|
|
|
sleep(0.5)
|
2016-02-20 17:15:04 +00:00
|
|
|
resp = self.client.get("/api/plugins/default/")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 404)
|
2016-02-21 18:36:24 +00:00
|
|
|
|
|
|
|
def test_context(self):
|
|
|
|
resp = self.client.get("/api/contexts/context.jsonld")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "@context" in js
|
2016-02-21 18:36:24 +00:00
|
|
|
assert check_dict(
|
2016-09-21 16:59:28 +00:00
|
|
|
js["@context"],
|
2016-02-21 18:36:24 +00:00
|
|
|
{"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
|
|
|
|
|
|
|
|
def test_schema(self):
|
|
|
|
resp = self.client.get("/api/schemas/definitions.json")
|
2016-09-21 16:59:28 +00:00
|
|
|
self.assertCode(resp, 200)
|
|
|
|
js = parse_resp(resp)
|
|
|
|
assert "$schema" in js
|