2020-02-29 22:22:38 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
from tests.base_test import BaseTest
|
2021-03-11 16:38:34 +00:00
|
|
|
from crc import app, mail
|
2020-02-29 22:22:38 +00:00
|
|
|
|
|
|
|
|
2021-06-22 21:23:18 +00:00
|
|
|
class TestToolsApi(BaseTest):
|
2020-02-29 22:22:38 +00:00
|
|
|
|
|
|
|
def test_render_markdown(self):
|
|
|
|
template = "My name is {{name}}"
|
|
|
|
data = {"name": "Dan"}
|
|
|
|
rv = self.app.get('/v1.0/render_markdown?template=%s&data=%s' %
|
|
|
|
(template, json.dumps(data)))
|
|
|
|
self.assert_success(rv)
|
|
|
|
self.assertEqual("My name is Dan", rv.get_data(as_text=True))
|
|
|
|
|
|
|
|
def test_render_docx(self):
|
|
|
|
filepath = os.path.join(app.root_path, '..', 'tests', 'data', 'table.docx')
|
2020-03-03 18:50:22 +00:00
|
|
|
template_data = {"hippa": [
|
|
|
|
{"option": "Name", "selected": True, "stored": ["Record at UVA", "Stored Long Term"]},
|
2020-02-29 22:22:38 +00:00
|
|
|
{"option": "Address", "selected": False},
|
|
|
|
{"option": "Phone", "selected": True, "stored": ["Send or Transmit outside of UVA"]}]}
|
|
|
|
with open(filepath, 'rb') as f:
|
2020-05-22 19:30:22 +00:00
|
|
|
file_data = {'file': (f, 'my_new_file.bpmn'), 'data': json.dumps(template_data)}
|
|
|
|
rv = self.app.put('/v1.0/render_docx',
|
2020-02-29 22:22:38 +00:00
|
|
|
data=file_data, follow_redirects=True,
|
|
|
|
content_type='multipart/form-data')
|
|
|
|
self.assert_success(rv)
|
2020-05-22 19:30:22 +00:00
|
|
|
self.assertIsNotNone(rv.data)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual('application/octet-stream', rv.content_type)
|
2020-03-03 20:30:42 +00:00
|
|
|
|
|
|
|
def test_list_scripts(self):
|
|
|
|
rv = self.app.get('/v1.0/list_scripts')
|
|
|
|
self.assert_success(rv)
|
|
|
|
scripts = json.loads(rv.get_data(as_text=True))
|
|
|
|
self.assertTrue(len(scripts) > 1)
|
|
|
|
self.assertIsNotNone(scripts[0]['name'])
|
2021-09-22 16:12:54 +00:00
|
|
|
self.assertIsNotNone(scripts[0]['handle'])
|
2020-03-03 20:30:42 +00:00
|
|
|
self.assertIsNotNone(scripts[0]['description'])
|
2020-07-21 17:57:33 +00:00
|
|
|
|
|
|
|
def test_eval_hide_expression(self):
|
2020-08-21 17:46:10 +00:00
|
|
|
"""Assures we can use python to process a hide expression from the front end"""
|
2020-08-20 20:13:48 +00:00
|
|
|
rv = self.app.put('/v1.0/eval',
|
2021-06-22 21:23:18 +00:00
|
|
|
data='{"expression": "x.y==2", "data": {"x":{"y":2}}, "key": 1234}', follow_redirects=True,
|
2020-07-22 15:30:16 +00:00
|
|
|
content_type='application/json',
|
|
|
|
headers=self.logged_in_headers())
|
2020-07-21 17:57:33 +00:00
|
|
|
self.assert_success(rv)
|
2020-08-20 20:13:48 +00:00
|
|
|
response = json.loads(rv.get_data(as_text=True))
|
|
|
|
self.assertEqual(True, response['result'])
|
2020-08-21 17:46:10 +00:00
|
|
|
|
2021-06-11 12:27:50 +00:00
|
|
|
def test_eval_returns_query(self):
|
2021-06-22 21:23:18 +00:00
|
|
|
"""Assures that along with the result, we get the key and expression.
|
2021-06-11 12:27:50 +00:00
|
|
|
This can be useful if the calling client is caching results and needs to hash the expression and data
|
|
|
|
when it gets returned."""
|
2021-06-22 21:23:18 +00:00
|
|
|
data = '{"expression": "x.y==2", "data": {"x":{"y":2}}, "key":1234}'
|
2021-06-11 12:27:50 +00:00
|
|
|
rv = self.app.put('/v1.0/eval',
|
|
|
|
data=data, follow_redirects=True,
|
|
|
|
content_type='application/json',
|
|
|
|
headers=self.logged_in_headers())
|
|
|
|
self.assert_success(rv)
|
|
|
|
response = json.loads(rv.get_data(as_text=True))
|
|
|
|
self.assertEqual("x.y==2", response['expression'])
|
2021-06-22 21:23:18 +00:00
|
|
|
self.assertEqual(1234, response['key'])
|
2021-06-11 12:27:50 +00:00
|
|
|
|
2021-03-08 19:00:03 +00:00
|
|
|
|
2020-08-21 17:46:10 +00:00
|
|
|
def test_eval_expression_with_strings(self):
|
|
|
|
"""Assures we can use python to process a value expression from the front end"""
|
|
|
|
rv = self.app.put('/v1.0/eval',
|
|
|
|
data='{"expression": "\'Hello, \' + user.first_name + \' \' + user.last_name + \'!!!\'", '
|
2021-06-22 21:23:18 +00:00
|
|
|
'"data": {"user":{"first_name": "Trillian", "last_name": "Astra"}},'
|
|
|
|
'"key":1234}',
|
2020-08-21 17:46:10 +00:00
|
|
|
follow_redirects=True,
|
|
|
|
content_type='application/json',
|
|
|
|
headers=self.logged_in_headers())
|
|
|
|
self.assert_success(rv)
|
|
|
|
response = json.loads(rv.get_data(as_text=True))
|
|
|
|
self.assertEqual('Hello, Trillian Astra!!!', response['result'])
|
2021-03-11 16:38:34 +00:00
|
|
|
|
|
|
|
def test_send_email(self):
|
|
|
|
subject = 'CR Connect Test Email'
|
|
|
|
address = 'user@example.com'
|
|
|
|
data = {'user': 'Person of Interest'}
|
|
|
|
message = """
|
|
|
|
Dear {{ user }},
|
|
|
|
|
|
|
|
Thank you for using this email example.
|
|
|
|
I hope this makes sense.
|
|
|
|
|
|
|
|
Yours faithfully,
|
|
|
|
CR Connect
|
|
|
|
"""
|
|
|
|
with mail.record_messages() as outbox:
|
|
|
|
rv = self.app.put('/v1.0/send_email?subject=%s&address=%s&data=%s' %
|
|
|
|
(subject, address, json.dumps(data)), data=json.dumps(message), headers=self.logged_in_headers())
|
|
|
|
self.assert_success(rv)
|
|
|
|
self.assertEqual(1, len(outbox))
|
|
|
|
self.assertEqual(subject, outbox[0].subject)
|
|
|
|
self.assertEqual(['user@example.com'], outbox[0].recipients)
|
|
|
|
self.assertIn('Person of Interest', outbox[0].body)
|
2021-03-11 17:38:47 +00:00
|
|
|
|
2021-03-08 19:00:03 +00:00
|
|
|
def test_eval_to_boolean_expression_with_dot_notation(self):
|
|
|
|
"""Assures we can use python to process a value expression from the front end"""
|
|
|
|
rv = self.app.put('/v1.0/eval',
|
2021-06-22 21:23:18 +00:00
|
|
|
data='{"expression": "test.value", "data": {"test":{"value": true}}, "key": 1234}',
|
2021-03-08 19:00:03 +00:00
|
|
|
follow_redirects=True,
|
|
|
|
content_type='application/json',
|
|
|
|
headers=self.logged_in_headers())
|
|
|
|
self.assert_success(rv)
|
|
|
|
response = json.loads(rv.get_data(as_text=True))
|
2021-03-11 17:38:47 +00:00
|
|
|
self.assertEqual(True, response['result'])
|