test_rest_api.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import json
  2. import os
  3. import unittest
  4. import requests
  5. import time
  6. from flask import Flask
  7. from kalliope.core.Models import Singleton
  8. from kalliope.core.ConfigurationManager import BrainLoader
  9. from kalliope.core.ConfigurationManager import SettingLoader
  10. from kalliope.core.RestAPI.FlaskAPI import FlaskAPI
  11. class TestRestAPI(unittest.TestCase):
  12. @classmethod
  13. def setUpClass(cls):
  14. """
  15. executed once at the beginning of the test
  16. """
  17. super(TestRestAPI, cls).setUpClass()
  18. current_path = os.getcwd()
  19. full_path_brain_to_test = current_path + os.sep + "Tests/brains/brain_test.yml"
  20. print full_path_brain_to_test
  21. # rest api config
  22. sl = SettingLoader()
  23. sl.settings.rest_api.password_protected = False
  24. sl.settings.active = True
  25. sl.settings.port = 5000
  26. print sl.settings.rest_api.password_protected
  27. # be sure that the singleton haven't been loaded before
  28. Singleton._instances = {}
  29. # prepare a test brain
  30. brain_to_test = full_path_brain_to_test
  31. brain_loader = BrainLoader(file_path=brain_to_test)
  32. brain = brain_loader.brain
  33. print brain_loader.yaml_config
  34. app = Flask(__name__)
  35. cls.flask_api = FlaskAPI(app, port=5000, brain=brain)
  36. cls.flask_api.start()
  37. time.sleep(1)
  38. # @classmethod
  39. # def tearDownClass(cls):
  40. # """
  41. # executed once at the end of the test
  42. # """
  43. # url = "http://127.0.0.1:5000/shutdown/"
  44. # requests.post(url=url)
  45. def setUp(self):
  46. self.base_url = "http://127.0.0.1:5000"
  47. # def test_get_synapses(self):
  48. # url = self.base_url+"/synapses/"
  49. # result = requests.get(url=url)
  50. #
  51. # print result.content
  52. #
  53. # expected_content = {
  54. # "synapses": [
  55. # {
  56. # "name": "test",
  57. # "neurons": [
  58. # {
  59. # "say": {
  60. # "message": [
  61. # "test message"
  62. # ]
  63. # }
  64. # }
  65. # ],
  66. # "signals": [
  67. # {
  68. # "order": "test_order"
  69. # }
  70. # ]
  71. # },
  72. # {
  73. # "name": "test2",
  74. # "neurons": [
  75. # {
  76. # "say": {
  77. # "message": [
  78. # "test message"
  79. # ]
  80. # }
  81. # }
  82. # ],
  83. # "signals": [
  84. # {
  85. # "order": "test_order_2"
  86. # }
  87. # ]
  88. # },
  89. # {
  90. # "includes": [
  91. # "included_brain_test.yml"
  92. # ]
  93. # },
  94. # {
  95. # "name": "test3",
  96. # "neurons": [
  97. # {
  98. # "say": {
  99. # "message": [
  100. # "test message"
  101. # ]
  102. # }
  103. # }
  104. # ],
  105. # "signals": [
  106. # {
  107. # "order": "test_order_3"
  108. # }
  109. # ]
  110. # }
  111. # ]
  112. # }
  113. #
  114. # self.assertEqual(expected_content, json.loads(result.content))
  115. # def test_get_one_synapse(self):
  116. # url = self.base_url+"/synapses/test"
  117. # result = requests.get(url=url)
  118. #
  119. # expected_content = {
  120. # "synapses": {
  121. # "name": "test",
  122. # "neurons": [
  123. # {
  124. # "say": {
  125. # "message": [
  126. # "test message"
  127. # ]
  128. # }
  129. # }
  130. # ],
  131. # "signals": [
  132. # {
  133. # "order": "test_order"
  134. # }
  135. # ]
  136. # }
  137. # }
  138. #
  139. # self.assertEqual(expected_content, json.loads(result.content))
  140. #
  141. # def test_get_synapse_not_found(self):
  142. # url = self.base_url + "/synapses/test-none"
  143. # result = requests.get(url=url)
  144. #
  145. # expected_content = {
  146. # "error": {
  147. # "synapse name not found": "test-none"
  148. # }
  149. # }
  150. #
  151. # self.assertEqual(expected_content, json.loads(result.content))
  152. # self.assertEqual(result.status_code, 404)
  153. #
  154. # def test_run_synapse_by_name(self):
  155. # url = self.base_url + "/synapses/test"
  156. # result = requests.post(url=url)
  157. #
  158. # expected_content = {
  159. # "synapses": {
  160. # "name": "test",
  161. # "neurons": [
  162. # {
  163. # "say": {
  164. # "message": [
  165. # "test message"
  166. # ]
  167. # }
  168. # }
  169. # ],
  170. # "signals": [
  171. # {
  172. # "order": "test_order"
  173. # }
  174. # ]
  175. # }
  176. # }
  177. #
  178. # self.assertEqual(expected_content, json.loads(result.content))
  179. # self.assertEqual(result.status_code, 201)
  180. #
  181. # def test_post_synapse_not_found(self):
  182. # url = self.base_url + "/synapses/test-none"
  183. # result = requests.post(url=url)
  184. #
  185. # expected_content = {
  186. # "error": {
  187. # "synapse name not found": "test-none"
  188. # }
  189. # }
  190. #
  191. # self.assertEqual(expected_content, json.loads(result.content))
  192. # self.assertEqual(result.status_code, 404)
  193. #
  194. # def test_run_synapse_with_order(self):
  195. # url = self.base_url + "/order/"
  196. # headers = {"Content-Type": "application/json"}
  197. # data = {"order": "test_order"}
  198. # result = requests.post(url=url, headers=headers, json=data)
  199. #
  200. # expected_content = {
  201. # "synapses": [
  202. # {
  203. # "name": "test",
  204. # "neurons": [
  205. # {
  206. # "name": "say",
  207. # "parameters": "{'message': ['test message']}"
  208. # }
  209. # ],
  210. # "signals": [
  211. # {
  212. # "order": "test_order"
  213. # }
  214. # ]
  215. # }
  216. # ]
  217. # }
  218. #
  219. # self.assertEqual(expected_content, json.loads(result.content))
  220. # self.assertEqual(result.status_code, 201)
  221. #
  222. # def test_post_synapse_by_order_not_found(self):
  223. # url = self.base_url + "/order/"
  224. # data = {"order": "non existing order"}
  225. # headers = {"Content-Type": "application/json"}
  226. # result = requests.post(url=url, headers=headers, json=data)
  227. #
  228. # expected_content = {'error': {'error': "The given order doesn't match any synapses"}}
  229. #
  230. # self.assertEqual(expected_content, json.loads(result.content))
  231. # self.assertEqual(result.status_code, 400)
  232. if __name__ == '__main__':
  233. unittest.main()