Skip to content

Commit

Permalink
tests: convert server tests to pytests
Browse files Browse the repository at this point in the history
  - Convert qpc/scan tests to pytest:
      tests_configure_host.py
      tests_login_host.py
      tests_logout_host.py
      tests_status.py
  • Loading branch information
abellotti committed Jul 17, 2023
1 parent aa1eede commit c5f12fa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 63 deletions.
47 changes: 27 additions & 20 deletions qpc/server/tests_configure_host.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Test the CLI module."""

import logging
import sys
import unittest

import pytest

from qpc import messages
from qpc.cli import CLI
Expand All @@ -11,17 +13,22 @@
DEFAULT_PORT = 9443


class ConfigureHostTests(unittest.TestCase):
class TestConfigureHost:
"""Class for testing the server host configuration."""

def setUp(self):
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
"""Inject caplog as we need to use it during teardown."""
self._caplog = caplog

def setup_method(self, _test_method):
"""Create test setup."""
# Temporarily disable stderr for these tests, CLI errors clutter up
# nosetests command.
self.orig_stderr = sys.stderr
sys.stderr = HushUpStderr()

def tearDown(self):
def teardown_method(self, _test_method):
"""Remove test case setup."""
# Reset server config to default ip/port
sys.argv = [
Expand All @@ -34,29 +41,29 @@ def tearDown(self):
str(DEFAULT_PORT),
]

with self.assertLogs(level="INFO") as log:
with self._caplog.at_level(logging.INFO):
CLI().main()
config = read_server_config()
self.assertEqual(config["host"], "127.0.0.1")
self.assertEqual(config["port"], DEFAULT_PORT)
assert config["host"] == "127.0.0.1"
assert config["port"] == DEFAULT_PORT
expected_message = messages.SERVER_CONFIG_SUCCESS % {
"protocol": "https",
"host": "127.0.0.1",
"port": str(DEFAULT_PORT),
}
self.assertIn(expected_message, log.output[-1])
assert expected_message in self._caplog.text
# Restore stderr
sys.stderr = self.orig_stderr

def test_config_host_req_args_err(self):
"""Testing the configure server requires host arg."""
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
sys.argv = ["/bin/qpc", "server", "config"]
CLI().main()

def test_config_host_alpha_port_err(self):
"""Testing the configure server requires bad port."""
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
sys.argv = [
"/bin/qpc",
"server",
Expand All @@ -68,7 +75,7 @@ def test_config_host_alpha_port_err(self):
]
CLI().main()

def test_success_config_server(self):
def test_success_config_server(self, caplog):
"""Testing the configure server green path."""
sys.argv = [
"/bin/qpc",
Expand All @@ -79,25 +86,25 @@ def test_success_config_server(self):
"--port",
"8005",
]
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
CLI().main()
config = read_server_config()
self.assertEqual(config["host"], "127.0.0.1")
self.assertEqual(config["port"], 8005)
assert config["host"] == "127.0.0.1"
assert config["port"] == 8005
expected_message = messages.SERVER_CONFIG_SUCCESS % {
"protocol": "https",
"host": "127.0.0.1",
"port": "8005",
}
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text

def test_config_server_default_port(self):
"""Testing the configure server default port."""
sys.argv = ["/bin/qpc", "server", "config", "--host", "127.0.0.1"]
CLI().main()
config = read_server_config()
self.assertEqual(config["host"], "127.0.0.1")
self.assertEqual(config["port"], DEFAULT_PORT)
assert config["host"] == "127.0.0.1"
assert config["port"] == DEFAULT_PORT

def test_invalid_configuration(self):
"""Test reading bad JSON on cli start."""
Expand All @@ -106,13 +113,13 @@ def test_invalid_configuration(self):
sys.argv = ["/bin/qpc", "server", "config", "--host", "127.0.0.1"]
CLI().main()
config = read_server_config()
self.assertEqual(config["host"], "127.0.0.1")
self.assertEqual(config["port"], DEFAULT_PORT)
assert config["host"] == "127.0.0.1"
assert config["port"] == DEFAULT_PORT

def test_run_command_no_config(self):
"""Test running command without config."""
write_server_config({})

with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
sys.argv = ["/bin/qpc", "cred"]
CLI().main()
34 changes: 17 additions & 17 deletions qpc/server/tests_login_host.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Test the CLI module."""

import logging
import sys
import unittest
from argparse import ArgumentParser, Namespace # noqa: I100
from io import StringIO
from unittest.mock import patch

import pytest
import requests_mock

from qpc import messages
Expand All @@ -17,17 +18,16 @@
TMP_KEY = "/tmp/testkey"


class LoginCliTests(unittest.TestCase):
class TestLoginCli:
"""Class for testing the login server command for qpc."""

@classmethod
def setUpClass(cls):
def setup_class(cls):
"""Set up test case."""
argument_parser = ArgumentParser()
subparser = argument_parser.add_subparsers(dest="subcommand")
cls.command = LoginHostCommand(subparser)

def setUp(self):
def setup_method(self, _test_method):
"""Create test setup."""
write_server_config(DEFAULT_CONFIG)
# Temporarily disable stderr for these tests, CLI errors clutter up
Expand All @@ -37,7 +37,7 @@ def setUp(self):
self.login_url = get_server_location() + LOGIN_URI
self.success_json = {"token": "a_token"}

def tearDown(self):
def teardown_method(self, _test_method):
"""Remove test setup."""
# Restore stderr
sys.stderr = self.orig_stderr
Expand All @@ -54,44 +54,44 @@ def test_login_bad_cred(self, do_mock_raw_input):
mocker.post(self.login_url, status_code=400, json=error)
args = Namespace(username="admin")
do_mock_raw_input.return_value = "abc"
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
with redirect_stdout(server_out):
self.command.main(args)
self.assertTrue(e_msg in server_out.getvalue())
assert e_msg in server_out.getvalue()

@patch("getpass._raw_input")
def test_login_good(self, do_mock_raw_input):
def test_login_good(self, do_mock_raw_input, caplog):
"""Testing the login with good creds."""
with requests_mock.Mocker() as mocker, patch.object(
self.command, "password", "password"
):
mocker.post(self.login_url, status_code=200, json=self.success_json)
args = Namespace(username="admin")
do_mock_raw_input.return_value = "abc"
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
self.assertIn(messages.LOGIN_SUCCESS, log.output[-1])
assert messages.LOGIN_SUCCESS in caplog.text

@patch("builtins.input")
@patch("getpass._raw_input")
def test_prompts_with_no_args(self, user_mock, pass_mock):
def test_prompts_with_no_args(self, user_mock, pass_mock, caplog):
"""Testing the login with no args passed."""
pass_mock.return_value = "abc"
user_mock.return_value = "admin"
with requests_mock.Mocker() as mocker:
mocker.post(self.login_url, status_code=200, json=self.success_json)

args = Namespace()
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
self.assertIn(messages.LOGIN_SUCCESS, log.output[-1])
assert messages.LOGIN_SUCCESS in caplog.text

def test_no_prompts_with_args(self):
def test_no_prompts_with_args(self, caplog):
"""Testing no prompts with args passed."""
with requests_mock.Mocker() as mocker:
mocker.post(self.login_url, status_code=200, json=self.success_json)

args = Namespace(username="admin", password="pass")
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
self.assertIn(messages.LOGIN_SUCCESS, log.output[-1])
assert messages.LOGIN_SUCCESS in caplog.text
12 changes: 5 additions & 7 deletions qpc/server/tests_logout_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os
import sys
import unittest
from argparse import ArgumentParser, Namespace

import requests_mock
Expand All @@ -13,25 +12,24 @@
from qpc.tests_utilities import HushUpStderr


class LogoutTests(unittest.TestCase):
class TestLogout:
"""Class for testing the logout host function."""

@classmethod
def setUpClass(cls):
def setup_class(cls):
"""Set up test case."""
argument_parser = ArgumentParser()
subparser = argument_parser.add_subparsers(dest="subcommand")
cls.command = LogoutHostCommand(subparser)

def setUp(self):
def setup_method(self, _method):
"""Create test setup."""
# Temporarily disable stderr for these tests, CLI errors clutter up
# nosetests command.
self.orig_stderr = sys.stderr
sys.stderr = HushUpStderr()
utils.write_server_config({"host": "127.0.0.1", "port": 8000, "use_http": True})

def tearDown(self):
def teardown_method(self, _method):
"""Remove test case setup."""
# Restore stderr
sys.stderr = self.orig_stderr
Expand All @@ -43,4 +41,4 @@ def test_success_logout(self):
mocker.put(url, status_code=200)
args = Namespace()
self.command.main(args)
self.assertFalse(os.path.exists(utils.QPC_CLIENT_TOKEN))
assert not os.path.exists(utils.QPC_CLIENT_TOKEN)
34 changes: 15 additions & 19 deletions qpc/server/tests_status.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Test the CLI module."""

import json
import logging
import os
import sys
import time
import unittest
from argparse import ArgumentParser, Namespace
from io import StringIO

import pytest
import requests_mock

from qpc import messages
Expand All @@ -20,17 +21,16 @@
TMP_KEY = "/tmp/testkey"


class ServerStatusTests(unittest.TestCase):
class TestServerStatus:
"""Class for testing the server status command for qpc."""

@classmethod
def setUpClass(cls):
def setup_class(cls):
"""Set up test case."""
argument_parser = ArgumentParser()
subparser = argument_parser.add_subparsers(dest="subcommand")
cls.command = ServerStatusCommand(subparser)

def setUp(self):
def setup_method(self, _test_method):
"""Create test setup."""
write_server_config(DEFAULT_CONFIG)
# Temporarily disable stderr for these tests, CLI errors clutter up
Expand All @@ -39,7 +39,7 @@ def setUp(self):
self.test_json_filename = f"test_{time.time():.0f}.json"
sys.stderr = HushUpStderr()

def tearDown(self):
def teardown_method(self, _test_method):
"""Remove test setup."""
# Restore stderr
sys.stderr = self.orig_stderr
Expand All @@ -48,7 +48,7 @@ def tearDown(self):
except FileNotFoundError:
pass

def test_download_server_status(self):
def test_download_server_status(self, caplog):
"""Testing recording server status command in a file."""
get_status_url = get_server_location() + STATUS_URI
get_status_json_data = {
Expand All @@ -60,14 +60,14 @@ def test_download_server_status(self):
mocker.get(get_status_url, status_code=200, json=get_status_json_data)

args = Namespace(path=self.test_json_filename)
with self.assertLogs(level="INFO") as log:
with caplog.at_level(logging.INFO):
self.command.main(args)
expected_message = messages.STATUS_SUCCESSFULLY_WRITTEN
self.assertIn(expected_message, log.output[-1])
assert expected_message in caplog.text
with open(self.test_json_filename, "r", encoding="utf-8") as json_file:
data = json_file.read()
file_content_dict = json.loads(data)
self.assertDictEqual(get_status_json_data, file_content_dict)
assert get_status_json_data == file_content_dict

def test_print_server_status(self):
"""Testing recording server status command in a file."""
Expand All @@ -85,19 +85,17 @@ def test_print_server_status(self):
args = Namespace(path=None)
with redirect_stdout(status_out):
self.command.main(args)
self.assertDictEqual(
json.loads(status_out.getvalue().strip()), get_status_json_data
)
assert json.loads(status_out.getvalue().strip()) == get_status_json_data

def test_write_status_output_directory_not_exist(self):
"""Testing fail because output directory does not exist."""
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
sys.argv = ["/bin/qpc", "server", "status", "--output-file", "/foo/bar/"]
CLI().main()

def test_write_status_output_file_empty(self):
"""Testing fail because output file empty."""
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
sys.argv = ["/bin/qpc", "server", "status", "--output-file"]
CLI().main()

Expand All @@ -111,9 +109,7 @@ def test_status_unexpected_failure(self):
mocker.get(get_status_url, status_code=400, json=get_status_json_data)

args = Namespace(path=None)
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
with redirect_stdout(status_out):
self.command.main(args)
self.assertEqual(
status_out.getvalue(), messages.SERVER_STATUS_FAILURE
)
assert status_out.getvalue() == messages.SERVER_STATUS_FAILURE

0 comments on commit c5f12fa

Please sign in to comment.