You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
senpy/tests/test_blueprints.py

144 lines
4.5 KiB
Python

10 years ago
import os
import logging
import json
10 years ago
10 years ago
from senpy.extensions import Senpy
from flask import Flask
from unittest import TestCase
from gevent import sleep
from itertools import product
10 years ago
10 years ago
10 years ago
def check_dict(indic, template):
10 years ago
return all(item in indic.items() for item in template.items())
10 years ago
8 years ago
def parse_resp(resp):
return json.loads(resp.data.decode('utf-8'))
10 years ago
class BlueprintsTest(TestCase):
def setUp(self):
10 years ago
self.app = Flask("test_extensions")
self.client = self.app.test_client()
10 years ago
self.senpy = Senpy()
self.senpy.init_app(self.app)
self.dir = os.path.join(os.path.dirname(__file__), "..")
self.senpy.add_folder(self.dir)
self.senpy.activate_plugin("Dummy", sync=True)
10 years ago
def assertCode(self, resp, code):
self.assertEqual(resp.status_code, code)
8 years ago
10 years ago
def test_home(self):
"""
Calling with no arguments should ask the user for more arguments
"""
resp = self.client.get("/api/")
self.assertCode(resp, 404)
js = parse_resp(resp)
logging.debug(js)
assert js["status"] == 404
10 years ago
atleast = {
"status": 404,
10 years ago
"message": "Missing or invalid parameters",
}
assert check_dict(js, atleast)
10 years ago
def test_analysis(self):
"""
The dummy plugin returns an empty response,\
it should contain the context
"""
resp = self.client.get("/api/?i=My aloha mohame")
self.assertCode(resp, 200)
js = parse_resp(resp)
logging.debug("Got response: %s", js)
assert "@context" in js
8 years ago
assert "entries" in js
def test_list(self):
""" List the plugins """
8 years ago
resp = self.client.get("/api/plugins/")
self.assertCode(resp, 200)
js = parse_resp(resp)
logging.debug(js)
assert 'plugins' in js
plugins = js['plugins']
assert len(plugins) > 1
assert list(p for p in plugins if p['name'] == "Dummy")
assert "@context" in js
def test_headers(self):
for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
["inHeaders"]):
8 years ago
resp = self.client.get("%s" % (i))
js = parse_resp(resp)
assert "@context" in js
8 years ago
resp = self.client.get("%s&%s=0" % (i, j))
js = parse_resp(resp)
8 years ago
assert "@context" in js
8 years ago
resp = self.client.get("%s&%s=1" % (i, j))
js = parse_resp(resp)
8 years ago
assert "@context" not in js
8 years ago
resp = self.client.get("%s&%s=true" % (i, j))
js = parse_resp(resp)
8 years ago
assert "@context" not in js
def test_detail(self):
""" Show only one plugin"""
resp = self.client.get("/api/plugins/Dummy/")
self.assertCode(resp, 200)
js = parse_resp(resp)
logging.debug(js)
assert "@id" in js
assert js["@id"] == "Dummy_0.1"
def test_activate(self):
""" Activate and deactivate one plugin """
8 years ago
resp = self.client.get("/api/plugins/Dummy/deactivate")
self.assertCode(resp, 200)
sleep(0.5)
resp = self.client.get("/api/plugins/Dummy/")
self.assertCode(resp, 200)
js = parse_resp(resp)
assert "is_activated" in js
assert not js["is_activated"]
8 years ago
resp = self.client.get("/api/plugins/Dummy/activate")
self.assertCode(resp, 200)
sleep(0.5)
resp = self.client.get("/api/plugins/Dummy/")
self.assertCode(resp, 200)
js = parse_resp(resp)
8 years ago
assert "is_activated" in js
assert js["is_activated"]
def test_default(self):
""" Show only one plugin"""
resp = self.client.get("/api/plugins/default/")
self.assertCode(resp, 200)
js = parse_resp(resp)
logging.debug(js)
8 years ago
assert "@id" in js
assert js["@id"] == "Dummy_0.1"
8 years ago
resp = self.client.get("/api/plugins/Dummy/deactivate")
self.assertCode(resp, 200)
sleep(0.5)
resp = self.client.get("/api/plugins/default/")
self.assertCode(resp, 404)
def test_context(self):
resp = self.client.get("/api/contexts/context.jsonld")
self.assertCode(resp, 200)
js = parse_resp(resp)
assert "@context" in js
assert check_dict(
js["@context"],
{"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
def test_schema(self):
resp = self.client.get("/api/schemas/definitions.json")
self.assertCode(resp, 200)
js = parse_resp(resp)
8 years ago
assert "$schema" in js