summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2020-01-16 11:55:56 +0100
committerAlvaro Neira Ayuso <alvaroneay@gmail.com>2020-01-19 19:50:44 +0100
commitdf98db2c3c11c7b1b96e175dd275bbeb0acb250f (patch)
treeb67db3a28fe1b1da4dcc149d7dc261919193d4f5 /tests
parent9c34a8e69f2eb59f3c0d4885010b661f784f4a96 (diff)
Add /shell/run tests
These tests cover: - Correct request with echo - Correct request without echo - No json request - Malformed json request
Diffstat (limited to 'tests')
-rw-r--r--tests/units/test_0003_shellrun.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/units/test_0003_shellrun.py b/tests/units/test_0003_shellrun.py
new file mode 100644
index 0000000..5a77346
--- /dev/null
+++ b/tests/units/test_0003_shellrun.py
@@ -0,0 +1,65 @@
+from server import Server
+from client import Client
+import unittest
+
+class TestShellRunMethods(unittest.TestCase):
+
+ def test_post_with_echo(self):
+ req_json = '{"run":"echo \\"croqueta\\"", "echo":true}'
+ response_json = '{"out": "\\"croqueta\\"\\n"}'
+ req = 'POST /shell/run HTTP/1.0\r\nContent-Length:'+ \
+ str(len(req_json)) + \
+ '\r\nContent-Type:application/json\r\n\r\n' + req_json
+ resp = 'HTTP/1.0 200 OK\r\nContent-Length:' + \
+ str(len(response_json)) + \
+ '\r\nContent-Type:application/json\r\n\r\n' + response_json
+ c = Client()
+ s = Server()
+ s.connect()
+ s.send(req)
+ server_response = s.recv()
+ s.stop()
+ c.stop()
+ self.assertEqual(server_response, resp)
+
+ def test_post_without_echo(self):
+ req_json = '{"run":"echo 1", "echo":false}'
+ req = 'POST /shell/run HTTP/1.0\r\nContent-Length:'+ \
+ str(len(req_json)) + \
+ '\r\nContent-Type:application/json\r\n\r\n' + req_json
+ resp = 'HTTP/1.0 200 OK\r\n\r\n'
+ c = Client()
+ s = Server()
+ s.connect()
+ s.send(req)
+ server_response = s.recv()
+ s.stop()
+ c.stop()
+ self.assertEqual(server_response, resp)
+
+ def test_no_json(self):
+ c = Client()
+ s = Server()
+ s.connect(probe=False)
+ s.send('POST /shell/run HTTP/1.0\r\nContent-Length:0\r\n\r\n')
+ response = s.recv()
+ s.stop()
+ c.stop()
+ self.assertEqual(response, 'HTTP/1.0 400 Bad Request\r\n\r\n')
+
+ def test_malformed_json(self):
+ json = '{"wrong_param": 0}'
+ len_json = str(len(json))
+ msg = 'POST /shell/run HTTP/1.0\r\nContent-Length:' + len_json + \
+ '\r\nContent-Type:application/json\r\n\r\n' + json
+ c = Client()
+ s = Server()
+ s.connect(probe=False)
+ s.send(msg)
+ response = s.recv()
+ s.stop()
+ c.stop()
+ self.assertEqual(response, 'HTTP/1.0 400 Bad Request\r\n\r\n')
+
+if __name__ == '__main__':
+ unittest.main()