FreeDATA/tests/test_server.py

63 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):
cmd = "flask --app modem/server run"
2023-11-06 14:36:11 +00:00
my_env = os.environ.copy()
my_env["FREEDATA_CONFIG"] = "modem/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):
config = {'NETWORK': {'modemport' : 3050}}
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()
self.assertEqual(config['NETWORK']['modemport'], 3050)
2023-11-06 19:52:33 +00:00
2023-11-06 12:49:37 +00:00
if __name__ == '__main__':
unittest.main()