[05b1088] | 1 | # |
---|
| 2 | # Copyright (C) 2020 Soleta Networks <info@soleta.eu> |
---|
| 3 | # |
---|
| 4 | # This program is free software: you can redistribute it and/or modify it under |
---|
| 5 | # the terms of the GNU Affero General Public License as published by the |
---|
| 6 | # Free Software Foundation, version 3. |
---|
| 7 | # |
---|
| 8 | |
---|
[df98db2] | 9 | from server import Server |
---|
| 10 | from client import Client |
---|
| 11 | import unittest |
---|
| 12 | |
---|
| 13 | class TestShellRunMethods(unittest.TestCase): |
---|
| 14 | |
---|
| 15 | def test_post_with_echo(self): |
---|
| 16 | req_json = '{"run":"echo \\"croqueta\\"", "echo":true}' |
---|
| 17 | response_json = '{"out": "\\"croqueta\\"\\n"}' |
---|
| 18 | req = 'POST /shell/run HTTP/1.0\r\nContent-Length:'+ \ |
---|
| 19 | str(len(req_json)) + \ |
---|
| 20 | '\r\nContent-Type:application/json\r\n\r\n' + req_json |
---|
| 21 | resp = 'HTTP/1.0 200 OK\r\nContent-Length:' + \ |
---|
| 22 | str(len(response_json)) + \ |
---|
| 23 | '\r\nContent-Type:application/json\r\n\r\n' + response_json |
---|
| 24 | c = Client() |
---|
| 25 | s = Server() |
---|
| 26 | s.connect() |
---|
| 27 | s.send(req) |
---|
| 28 | server_response = s.recv() |
---|
| 29 | s.stop() |
---|
| 30 | c.stop() |
---|
| 31 | self.assertEqual(server_response, resp) |
---|
| 32 | |
---|
| 33 | def test_post_without_echo(self): |
---|
| 34 | req_json = '{"run":"echo 1", "echo":false}' |
---|
| 35 | req = 'POST /shell/run HTTP/1.0\r\nContent-Length:'+ \ |
---|
| 36 | str(len(req_json)) + \ |
---|
| 37 | '\r\nContent-Type:application/json\r\n\r\n' + req_json |
---|
| 38 | resp = 'HTTP/1.0 200 OK\r\n\r\n' |
---|
| 39 | c = Client() |
---|
| 40 | s = Server() |
---|
| 41 | s.connect() |
---|
| 42 | s.send(req) |
---|
| 43 | server_response = s.recv() |
---|
| 44 | s.stop() |
---|
| 45 | c.stop() |
---|
| 46 | self.assertEqual(server_response, resp) |
---|
| 47 | |
---|
| 48 | def test_no_json(self): |
---|
| 49 | c = Client() |
---|
| 50 | s = Server() |
---|
| 51 | s.connect(probe=False) |
---|
| 52 | s.send('POST /shell/run HTTP/1.0\r\nContent-Length:0\r\n\r\n') |
---|
| 53 | response = s.recv() |
---|
| 54 | s.stop() |
---|
| 55 | c.stop() |
---|
| 56 | self.assertEqual(response, 'HTTP/1.0 400 Bad Request\r\n\r\n') |
---|
| 57 | |
---|
| 58 | def test_malformed_json(self): |
---|
| 59 | json = '{"wrong_param": 0}' |
---|
| 60 | len_json = str(len(json)) |
---|
| 61 | msg = 'POST /shell/run HTTP/1.0\r\nContent-Length:' + len_json + \ |
---|
| 62 | '\r\nContent-Type:application/json\r\n\r\n' + json |
---|
| 63 | c = Client() |
---|
| 64 | s = Server() |
---|
| 65 | s.connect(probe=False) |
---|
| 66 | s.send(msg) |
---|
| 67 | response = s.recv() |
---|
| 68 | s.stop() |
---|
| 69 | c.stop() |
---|
| 70 | self.assertEqual(response, 'HTTP/1.0 400 Bad Request\r\n\r\n') |
---|
| 71 | |
---|
| 72 | if __name__ == '__main__': |
---|
| 73 | unittest.main() |
---|