From d890975dc9195c613d62b82b1abb93d8f06dea57 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Fri, 19 Jul 2024 04:30:44 -0400 Subject: [PATCH] tests: Remove classes from tests (environ) --- tests/test_eggstorage.py | 6 +-- tests/test_environ.py | 107 +++++++++++++++++++-------------------- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/tests/test_eggstorage.py b/tests/test_eggstorage.py index 5e845751..2f8b112c 100644 --- a/tests/test_eggstorage.py +++ b/tests/test_eggstorage.py @@ -13,7 +13,7 @@ @implementer(IEggStorage) -class SomeFakeEggStorage: +class FakeEggStorage: def __init__(self, config): self.config = config @@ -52,12 +52,12 @@ def test_sorted_versions(versions, expected): def test_egg_config_application(): config = Config() - eggstore = "tests.test_eggstorage.SomeFakeEggStorage" + eggstore = "tests.test_eggstorage.FakeEggStorage" config.cp.set("scrapyd", "eggstorage", eggstore) app = application(config) app_eggstorage = app.getComponent(IEggStorage) - assert isinstance(app_eggstorage, SomeFakeEggStorage) + assert isinstance(app_eggstorage, FakeEggStorage) assert app_eggstorage.list_projects() == ["hello_world"] diff --git a/tests/test_environ.py b/tests/test_environ.py index 0c553062..59a3c15b 100644 --- a/tests/test_environ.py +++ b/tests/test_environ.py @@ -1,7 +1,6 @@ import os import pytest -from twisted.trial import unittest from zope.interface.verify import verifyObject from scrapyd.config import Config @@ -13,57 +12,55 @@ slot = 3 -class EnvironmentTest(unittest.TestCase): - def setUp(self): - d = self.mktemp() - os.mkdir(d) - config = Config(values={"eggs_dir": d, "logs_dir": d}) - config.cp.add_section("settings") - config.cp.set("settings", "newbot", "newbot.settings") - - self.environ = Environment(config, initenv={}) - - def test_interface(self): - verifyObject(IEnvironment, self.environ) - - def test_get_environment_with_eggfile(self): - env = self.environ.get_environment(msg, slot) - - self.assertEqual(env["SCRAPY_PROJECT"], "mybot") - self.assertEqual(env["SCRAPYD_SLOT"], "3") - self.assertEqual(env["SCRAPYD_SPIDER"], "myspider") - self.assertEqual(env["SCRAPYD_JOB"], "ID") - self.assertTrue(env["SCRAPYD_LOG_FILE"].endswith(os.path.join("mybot", "myspider", "ID.log"))) - if env.get("SCRAPYD_FEED_URI"): # Not compulsory - self.assertTrue(env["SCRAPYD_FEED_URI"].startswith(f"file://{os.getcwd()}")) - self.assertTrue(env["SCRAPYD_FEED_URI"].endswith(os.path.join("mybot", "myspider", "ID.jl"))) - self.assertNotIn("SCRAPY_SETTINGS_MODULE", env) - - def test_get_environment_with_no_items_dir(self): - config = Config(values={"items_dir": "", "logs_dir": ""}) - config.cp.add_section("settings") - config.cp.set("settings", "newbot", "newbot.settings") - - environ = Environment(config, initenv={}) - env = environ.get_environment(msg, slot) - - self.assertNotIn("SCRAPYD_FEED_URI", env) - self.assertNotIn("SCRAPYD_LOG_FILE", env) - - def test_get_environment_secure(self): - for values in ({"items_dir": "../items"}, {"logs_dir": "../logs"}): - with self.subTest(values=values): - config = Config(values=values) - - environ = Environment(config, initenv={}) - for k, v in (("_project", "../p"), ("_spider", "../s"), ("_job", "../j")): - with self.subTest(key=k, value=v): - with pytest.raises(DirectoryTraversalError) as exc: - environ.get_environment({**msg, k: v}, slot) - - self.assertEqual( - str(exc.value), - f"{v if k == '_project' else 'mybot'}{os.sep}" - f"{v if k == '_spider' else 'myspider'}{os.sep}" - f"{v if k == '_job' else 'ID'}.log", - ) +@pytest.fixture() +def environ(tmpdir): + config = Config(values={"eggs_dir": tmpdir, "logs_dir": tmpdir}) + config.cp.add_section("settings") + config.cp.set("settings", "newbot", "newbot.settings") + return Environment(config, initenv={}) + + +def test_interface(environ): + verifyObject(IEnvironment, environ) + + +def test_get_environment_with_eggfile(environ): + env = environ.get_environment(msg, slot) + + assert env["SCRAPY_PROJECT"] == "mybot" + assert env["SCRAPYD_SLOT"] == "3" + assert env["SCRAPYD_SPIDER"] == "myspider" + assert env["SCRAPYD_JOB"] == "ID" + assert env["SCRAPYD_LOG_FILE"].endswith(os.path.join("mybot", "myspider", "ID.log")) + if env.get("SCRAPYD_FEED_URI"): # Not compulsory + assert env["SCRAPYD_FEED_URI"].startswith(f"file://{os.getcwd()}") + assert env["SCRAPYD_FEED_URI"].endswith(os.path.join("mybot", "myspider", "ID.jl")) + assert "SCRAPY_SETTINGS_MODULE" not in env + + +def test_get_environment_with_no_items_dir(): + config = Config(values={"items_dir": "", "logs_dir": ""}) + config.cp.add_section("settings") + config.cp.set("settings", "newbot", "newbot.settings") + + environ = Environment(config, initenv={}) + env = environ.get_environment(msg, slot) + + assert "SCRAPYD_FEED_URI" not in env + assert "SCRAPYD_LOG_FILE" not in env + + +@pytest.mark.parametrize("values", [{"items_dir": "../items"}, {"logs_dir": "../logs"}]) +@pytest.mark.parametrize(("key", "value"), [("_project", "../p"), ("_spider", "../s"), ("_job", "../j")]) +def test_get_environment_secure(values, key, value): + config = Config(values=values) + environ = Environment(config, initenv={}) + + with pytest.raises(DirectoryTraversalError) as exc: + environ.get_environment({**msg, key: value}, slot) + + assert str(exc.value) == ( + f"{value if key == '_project' else 'mybot'}{os.sep}" + f"{value if key == '_spider' else 'myspider'}{os.sep}" + f"{value if key == '_job' else 'ID'}.log" + )