Skip to content

Commit

Permalink
Adding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdlaird committed Sep 6, 2020
1 parent ef6ffff commit ee7fa5b
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ dist/
MANIFEST
*.egg-info
_build

tests/.config
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/alexdlaird/hookee/compare/0.0.8...HEAD)
### Added
- Stable test suite.

### Changed
- Replaced `response_json` and `response_xml` plugins with `response_echo` plugin.
- Renamed `--list-plugins` to `--available-plugins`.
Expand Down
7 changes: 7 additions & 0 deletions hookee/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ def update_config(ctx, key, value):
"""
cli_manager = ctx.obj["cli_manager"]

if key == "plugins":
ctx.fail("Enable and disable plugins through the `enable-plugin` and `disable-plugin` commands.")

if value.isdigit():
value = int(value)
print(value)

try:
cli_manager.config.set(key, value)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.8"
__version__ = "0.0.9"

with open("README.md", "r") as f:
long_description = f.read()
Expand Down
30 changes: 30 additions & 0 deletions tests/managedtestcase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import time

from hookee.climanager import CliManager

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.9"

from tests.testcase import HookeeTestCase


class ManagedTestCase(HookeeTestCase):
port = 5000
cli_manager = None
webhook_url = None

@classmethod
def setUpClass(cls):
cls.cli_manager = CliManager(cls.ctx)

cls.cli_manager._init_server_and_tunnel()

cls.webhook_url = "{}/webhook".format(cls.cli_manager.tunnel.public_url)

@classmethod
def tearDownClass(cls):
if cls.cli_manager:
cls.cli_manager.stop()

time.sleep(2)
109 changes: 109 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
import shutil
from unittest import mock

from hookee.cli import hookee

from tests.testcase import HookeeTestCase

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.9"


class TestCli(HookeeTestCase):
def test_update_config(self):
# WHEN
result = self.runner.invoke(hookee, ["update-config", "port", "1000"])

# THEN
self.assertEqual(result.exit_code, 0)
self.assertIn("\"port\" has been updated", result.output)

def test_update_invalid_config(self):
# WHEN
result = self.runner.invoke(hookee, ["update-config", "foo", "bar"])

# THEN
self.assertEqual(result.exit_code, 2)
self.assertIn("No such key", result.output)

def test_available_plugins_config(self):
# GIVEN
custom_plugin_path = os.path.join(self.plugins_dir, "custom_plugin.py")
shutil.copy(
os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", "hookee", "plugins", "request_body.py"),
custom_plugin_path)

# WHEN
result = self.runner.invoke(hookee, ["available-plugins"])

# THEN
self.assertEqual(result.exit_code, 0)
self.assertIn(
"['blueprint_default', 'custom_plugin', 'request_body', 'request_files', 'request_headers', "
"'request_query_params', 'request_url_info', 'response_echo', 'response_info']",
result.output)

def test_enabled_plugins_config(self):
# GIVEN
self.config.remove("plugins", "request_files")
self.config.remove("plugins", "request_query_params")

# WHEN
result = self.runner.invoke(hookee, ["enabled-plugins"])

# THEN
self.assertEqual(result.exit_code, 0)
self.assertIn(
"['blueprint_default', 'request_body', 'request_headers', 'request_url_info', 'response_echo', "
"'response_info']",
result.output)

def test_enable_plugin(self):
# WHEN
result = self.runner.invoke(hookee, ["enable-plugin", "request_body"])

# THEN
self.assertEqual(result.exit_code, 0)
self.assertIn("\"request_body\" has been enabled", result.output)

def test_enable_plugin_invalid(self):
# WHEN
result = self.runner.invoke(hookee, ["enable-plugin", "fake_plugin"])

# THEN
self.assertEqual(result.exit_code, 2)
self.assertIn("\"fake_plugin\" could not be found", result.output)

def test_disable_plugin(self):
# WHEN
result = self.runner.invoke(hookee, ["disable-plugin", "request_body"])

# THEN
self.assertEqual(result.exit_code, 0)
self.assertIn("\"request_body\" is disabled", result.output)

def test_disable_required_plugin(self):
# WHEN
result = self.runner.invoke(hookee, ["disable-plugin", "blueprint_default"])

# THEN
self.assertEqual(result.exit_code, 2)
self.assertIn("can't disable", result.output)

@mock.patch("hookee.climanager.CliManager.start")
def test_start(self, mock_cli_start):
# WHEN
self.runner.invoke(hookee, ["start"])

# THEN
mock_cli_start.assert_called_once()

@mock.patch("hookee.climanager.CliManager.start")
def test_no_command_calls_start(self, mock_cli_start):
# WHEN
self.runner.invoke(hookee)

# THEN
mock_cli_start.assert_called_once()
5 changes: 2 additions & 3 deletions tests/test_cli_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import requests

from tests.testcase import ManagedTestCase
from tests.managedtestcase import ManagedTestCase

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.8"
__version__ = "0.0.9"


class TestCliManager(ManagedTestCase):

def test_cli_manager(self):
# GIVEN / WHEN
# The test setup started the manager for us, so just assert the assumed state
Expand Down
33 changes: 29 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import unittest
from tests.testcase import HookeeTestCase

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.8"
__version__ = "0.0.9"


class TestConfig(unittest.TestCase):
pass
class TestCli(HookeeTestCase):
def test_config_set(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_config_append(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_config_remove(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN
87 changes: 83 additions & 4 deletions tests/test_plugin_manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,88 @@
import unittest
from tests.testcase import HookeeTestCase

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.8"
__version__ = "0.0.9"


class TestPluginManager(unittest.TestCase):
pass
class TestPluginManager(HookeeTestCase):
def test_validate_plugin(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_validate_plugin_not_conform_to_spec(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_validate_plugin_no_plugin_type(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_validate_plugin_no_run(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_validate_plugin_wrong_args(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_validate_plugin_no_blueprint(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_load_plugin_not_found(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_load_plugins(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_load_plugins_missing_required_plugin(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN
29 changes: 27 additions & 2 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@

__author__ = "Alex Laird"
__copyright__ = "Copyright 2020, Alex Laird"
__version__ = "0.0.8"
__version__ = "0.0.9"


class TestUtil(unittest.TestCase):
pass
def test_is_python_3(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def test_get_functions(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN

def get_args(self):
# TODO implement
pass
# GIVEN

# WHEN

# THEN
Loading

0 comments on commit ee7fa5b

Please sign in to comment.