Skip to content

Commit

Permalink
test(refactor): Split --auth into --username and --password in mock s…
Browse files Browse the repository at this point in the history
…erver
  • Loading branch information
jpmckinney committed Jul 20, 2024
1 parent 3e8d3a4 commit 787f733
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
12 changes: 6 additions & 6 deletions tests/mockserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def get_ephemeral_port():


class MockScrapydServer:
def __init__(self, authentication=None):
self.authentication = authentication
def __init__(self, username=None, password=None):
self.username = username
self.password = password

def __enter__(self, authentication=None):
"""Launch Scrapyd application object with ephemeral port"""
def __enter__(self):
command = [sys.executable, os.path.join(BASEDIR, "start_mock_app.py"), get_ephemeral_port()]
if self.authentication is not None:
command.append("--auth=" + self.authentication)
if self.username and self.password:
command.extend([f"--username={self.username}", f"--password={self.password}"])

self.process = Popen(command, stdout=PIPE)

Expand Down
32 changes: 14 additions & 18 deletions tests/start_mock_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,23 @@
from scrapyd import Config
from scrapyd.app import application


def _get_config(args):
scrapyd_config = Config()
section = "scrapyd"
scrapyd_config.cp.set(section, "http_port", args.http_port)

if args.auth is not None:
username, password = args.auth.split(":")
scrapyd_config.cp.set(section, "username", username)
scrapyd_config.cp.set(section, "password", password)

return scrapyd_config


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("http_port", default=6800)
parser.add_argument("--auth", default=None)
parser.add_argument("http_port")
parser.add_argument("--username")
parser.add_argument("--password")

args = parser.parse_args()

config = Config()
config.cp.set(Config.SECTION, "http_port", args.http_port)
if args.username and args.password:
config.cp.set(Config.SECTION, "username", args.username)
config.cp.set(Config.SECTION, "password", args.password)

log.startLogging(sys.stdout)
conf = _get_config(args)
application = application(config=conf)

application = application(config=config)
app.startApplication(application, save=False)

reactor.run()
1 change: 1 addition & 0 deletions tests/test_eggstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def eggstorage(tmpdir):
[
(["zzz", "b", "ddd", "a", "x"], ["a", "b", "ddd", "x", "zzz"]),
(["10", "1", "9"], ["1", "9", "10"]),
(["r10", "r1", "r9"], ["r1", "r10", "r9"]),
(["2.11", "2.01", "2.9"], ["2.01", "2.9", "2.11"]),
],
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _deploy(mock_scrapyd, quotesbot_egg) -> Response:


def test_urljoin(mock_scrapyd):
assert mock_scrapyd.urljoin("foo") == mock_scrapyd.url + "foo"
assert mock_scrapyd.urljoin("foo") == f"{mock_scrapyd.url}foo"


def test_root(mock_scrapyd):
Expand All @@ -47,7 +47,7 @@ def test_root(mock_scrapyd):
def test_auth():
username, password = "Leonardo", "hunter2"

with MockScrapydServer(authentication=username + ":" + password) as server:
with MockScrapydServer(username=username, password=password) as server:
assert requests.get(server.url).status_code == 401

res = requests.get(server.url, auth=(username, password))
Expand Down

0 comments on commit 787f733

Please sign in to comment.