Skip to content

Commit

Permalink
tests: add initial tests for web_util module
Browse files Browse the repository at this point in the history
Signed-off-by: Thore Sommer <mail@thson.de>
  • Loading branch information
THS-on committed Jan 19, 2022
1 parent e7807ae commit d28b6db
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions test/test_web_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import http.server
import unittest
from unittest.mock import patch, MagicMock

import tornado.web

from keylime import json
from keylime import web_util


class TestConfig(unittest.TestCase):

@patch("keylime.web_util.config")
def test_get_restful_params(self, _):
"""Tests if the parsing of the parameters works"""
version_url = "/v1.0/quotes/integrity?nonce=1234567890ABCDEFHIJ&mask=0x408000&vmask=0x808000&partial=0"
version_params = {
"api_version": "1.0",
"quotes": "integrity",
"nonce": "1234567890ABCDEFHIJ",
"mask": "0x408000",
"vmask": "0x808000",
"partial": "0"
}
self.assertEqual(web_util.get_restful_params(version_url), version_params)

basic_url = "/version"
basic_params = {
"version": None,
"api_version": 0
}
self.assertEqual(web_util.get_restful_params(basic_url), basic_params)

def test_json_response_tornado(self):
"""Tests JSON response output for Tornado"""
mock_handler = MagicMock(spec=tornado.web.RequestHandler)
test_data = {"key_1": "value", "key_2": 2}
expected_output = json.dumps({
"code": 200,
"status": "Success",
"results": test_data,
}).encode("utf-8")
res = web_util.echo_json_response(mock_handler, 200, "Success", test_data)
self.assertTrue(res)
mock_handler.set_status.assert_called_once_with(200)
mock_handler.set_header.assert_called_once_with('Content-Type', 'application/json')
mock_handler.write.assert_called_once_with(expected_output)
mock_handler.finish.assert_called_once()

def test_json_response_http_server(self):
"""Tests JSON response output for Tornado"""
mock_handler = MagicMock(spec=http.server.BaseHTTPRequestHandler)
mock_handler.wfile = MagicMock()
test_data = {"key_1": "value", "key_2": 2}
expected_output = json.dumps({
"code": 200,
"status": "Success",
"results": test_data,
}).encode("utf-8")
res = web_util.echo_json_response(mock_handler, 200, "Success", test_data)
self.assertTrue(res)
mock_handler.send_response.assert_called_once_with(200)
mock_handler.send_header.assert_called_once_with('Content-Type', 'application/json')
mock_handler.end_headers.assert_called_once()
mock_handler.wfile.write.assert_called_once_with(expected_output)


if __name__ == "__main__":
unittest.main()

0 comments on commit d28b6db

Please sign in to comment.