FreeDATA/tests/test_server.py

64 lines
1.8 KiB
Python
Raw Normal View History

2023-11-06 12:49:37 +00:00
import unittest
from subprocess import Popen, PIPE
2023-11-06 14:36:11 +00:00
import shlex, os
2023-11-06 12:49:37 +00:00
import requests
import time
2023-11-06 19:52:33 +00:00
import json
2023-11-06 12:49:37 +00:00
2023-11-06 14:36:11 +00:00
# API Server integration testst
class TestIntegration(unittest.TestCase):
2023-11-06 12:49:37 +00:00
process = None
url = "http://127.0.0.1:5000"
@classmethod
def setUpClass(cls):
2024-04-18 09:04:25 +00:00
#cmd = "flask --app freedata_server/server run"
cmd = "python3 freedata_server/server.py"
2023-11-06 14:36:11 +00:00
my_env = os.environ.copy()
2024-04-18 09:04:25 +00:00
my_env["FREEDATA_CONFIG"] = "freedata_server/config.ini.example"
2023-11-06 14:36:11 +00:00
cls.process = Popen(shlex.split(cmd), stdin=PIPE, env=my_env)
2023-11-06 12:49:37 +00:00
time.sleep(1)
@classmethod
def tearDownClass(cls):
cls.process.stdin.close()
cls.process.terminate()
2023-11-06 14:36:11 +00:00
cls.process.wait()
2023-11-06 12:49:37 +00:00
def test_index(self):
r = requests.get(self.url)
self.assertEqual(r.status_code, 200)
data = r.json()
2023-11-12 22:39:49 +00:00
self.assertEqual(data['api_version'], 1)
2023-11-06 14:36:11 +00:00
def test_config_get(self):
2023-11-06 14:36:11 +00:00
r = requests.get(self.url + '/config')
self.assertEqual(r.status_code, 200)
2023-11-12 22:39:49 +00:00
config = r.json()
2023-11-06 14:36:11 +00:00
self.assertIsInstance(config, dict)
self.assertIn('NETWORK', config)
self.assertIn('STATION', config)
self.assertIn('AUDIO', config)
2023-11-12 22:39:49 +00:00
self.assertIn('MODEM', config)
self.assertIn('TCI', config)
self.assertIn('RADIO', config)
2023-11-06 12:49:37 +00:00
2023-11-06 19:52:33 +00:00
def test_config_post(self):
2024-02-28 08:56:28 +00:00
config = {'STATION': {'mygrid' : 'JN48ea'}}
2023-11-06 19:52:33 +00:00
r = requests.post(self.url + '/config',
headers={'Content-type': 'application/json'},
data = json.dumps(config))
self.assertEqual(r.status_code, 200)
2023-11-06 12:49:37 +00:00
r = requests.get(self.url + '/config')
self.assertEqual(r.status_code, 200)
2023-11-12 22:39:49 +00:00
config = r.json()
2024-02-28 18:41:06 +00:00
self.assertEqual(config['NETWORK']['modemport'], 5000)
2023-11-06 19:52:33 +00:00
2023-11-06 12:49:37 +00:00
if __name__ == '__main__':
unittest.main()