Skip to content

Commit

Permalink
tests: Remove classes from tests (environ)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jul 19, 2024
1 parent 0daf6ec commit d890975
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 58 deletions.
6 changes: 3 additions & 3 deletions tests/test_eggstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


@implementer(IEggStorage)
class SomeFakeEggStorage:
class FakeEggStorage:
def __init__(self, config):
self.config = config

Expand Down Expand Up @@ -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"]


Expand Down
107 changes: 52 additions & 55 deletions tests/test_environ.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import pytest
from twisted.trial import unittest
from zope.interface.verify import verifyObject

from scrapyd.config import Config
Expand All @@ -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"
)

0 comments on commit d890975

Please sign in to comment.