waybackpy/tests/test_cli.py

401 lines
10 KiB
Python
Raw Normal View History

2020-07-23 17:14:14 +02:00
import sys
import os
import pytest
2021-01-03 21:24:40 +01:00
import random
import string
2020-07-23 17:14:14 +02:00
import argparse
sys.path.append("..")
import waybackpy.cli as cli # noqa: E402
2020-12-13 20:48:04 +01:00
from waybackpy.wrapper import Url # noqa: E402
from waybackpy.__version__ import __version__
2020-07-23 17:14:14 +02:00
2020-11-21 12:30:11 +01:00
def test_save():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent=None,
url="https://pypi.org/user/akamhy/",
total=False,
version=False,
oldest=False,
save=True,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
2020-11-21 12:30:11 +01:00
reply = cli.args_handler(args)
assert "pypi.org/user/akamhy" in str(reply)
2021-01-03 21:24:40 +01:00
args = argparse.Namespace(
user_agent=None,
url="https://hfjfjfjfyu6r6rfjvj.fjhgjhfjgvjm",
total=False,
version=False,
oldest=False,
save=True,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
reply = cli.args_handler(args)
assert "could happen because either your waybackpy" in str(reply)
2020-12-13 20:48:04 +01:00
def test_json():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent=None,
url="https://pypi.org/user/akamhy/",
total=False,
version=False,
oldest=False,
save=False,
json=True,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
reply = cli.args_handler(args)
assert "archived_snapshots" in str(reply)
2020-12-13 20:48:04 +01:00
def test_archive_url():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent=None,
url="https://pypi.org/user/akamhy/",
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=True,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
reply = cli.args_handler(args)
assert "https://web.archive.org/web/" in str(reply)
2020-07-23 17:14:14 +02:00
2020-12-13 20:48:04 +01:00
2020-07-23 17:14:14 +02:00
def test_oldest():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent=None,
url="https://pypi.org/user/akamhy/",
total=False,
version=False,
oldest=True,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
reply = cli.args_handler(args)
assert "pypi.org/user/akamhy" in str(reply)
2020-07-23 17:14:14 +02:00
2021-01-03 21:24:40 +01:00
uid = "".join(
random.choice(string.ascii_lowercase + string.digits) for _ in range(6)
)
url = "https://pypi.org/yfvjvycyc667r67ed67r" + uid
args = argparse.Namespace(
user_agent=None,
url=url,
total=False,
version=False,
oldest=True,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
reply = cli.args_handler(args)
assert "Can not find archive for" in str(reply)
2020-12-13 20:48:04 +01:00
2020-07-23 17:14:14 +02:00
def test_newest():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
url="https://pypi.org/user/akamhy/",
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=True,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
reply = cli.args_handler(args)
assert "pypi.org/user/akamhy" in str(reply)
2021-01-03 21:24:40 +01:00
uid = "".join(
random.choice(string.ascii_lowercase + string.digits) for _ in range(6)
)
url = "https://pypi.org/yfvjvycyc667r67ed67r" + uid
args = argparse.Namespace(
user_agent=None,
url=url,
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=True,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
reply = cli.args_handler(args)
assert "Can not find archive for" in str(reply)
2020-12-13 20:48:04 +01:00
def test_total_archives():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
url="https://pypi.org/user/akamhy/",
total=True,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get=None,
)
reply = cli.args_handler(args)
assert isinstance(reply, int)
2020-07-23 17:14:14 +02:00
2020-12-13 20:48:04 +01:00
def test_known_urls():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
2021-01-05 21:28:38 +01:00
url="https://www.keybr.com",
2020-12-13 20:48:04 +01:00
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
2021-01-05 21:28:38 +01:00
subdomain=False,
2020-12-13 20:48:04 +01:00
known_urls=True,
get=None,
)
reply = cli.args_handler(args)
2021-01-05 21:28:38 +01:00
assert "keybr" in str(reply)
2021-01-03 21:24:40 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
url="https://akfyfufyjcujfufu6576r76r6amhy.gitd6r67r6u6hub.yfjyfjio",
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=True,
known_urls=True,
get=None,
)
reply = cli.args_handler(args)
assert "No known URLs found" in str(reply)
2020-12-13 20:48:04 +01:00
2020-07-23 17:14:14 +02:00
def test_near():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
url="https://pypi.org/user/akamhy/",
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=True,
subdomain=False,
known_urls=False,
get=None,
year=2020,
month=7,
day=15,
hour=1,
minute=1,
)
reply = cli.args_handler(args)
assert "202007" in str(reply)
2021-01-03 21:24:40 +01:00
uid = "".join(
random.choice(string.ascii_lowercase + string.digits) for _ in range(6)
)
url = "https://pypi.org/yfvjvycyc667r67ed67r" + uid
args = argparse.Namespace(
user_agent=None,
url=url,
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=True,
subdomain=False,
known_urls=False,
get=None,
year=2020,
month=7,
day=15,
hour=1,
minute=1,
)
reply = cli.args_handler(args)
assert "Can not find archive for" in str(reply)
2020-12-13 20:48:04 +01:00
def test_get():
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
2021-01-05 21:28:38 +01:00
url="https://github.com/akamhy",
2020-12-13 20:48:04 +01:00
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get="url",
)
reply = cli.args_handler(args)
assert "waybackpy" in str(reply)
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
2021-01-05 21:28:38 +01:00
url="https://github.com/akamhy/waybackpy",
2020-12-13 20:48:04 +01:00
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get="oldest",
)
reply = cli.args_handler(args)
assert "waybackpy" in str(reply)
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
2021-01-05 21:28:38 +01:00
url="https://akamhy.github.io/waybackpy/",
2020-12-13 20:48:04 +01:00
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get="newest",
)
reply = cli.args_handler(args)
assert "waybackpy" in str(reply)
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
url="https://pypi.org/user/akamhy/",
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
get="save",
)
2020-11-21 12:30:11 +01:00
reply = cli.args_handler(args)
assert "waybackpy" in str(reply)
2020-12-13 20:48:04 +01:00
args = argparse.Namespace(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
url="https://pypi.org/user/akamhy/",
total=False,
version=False,
oldest=False,
save=False,
json=False,
archive_url=False,
newest=False,
near=False,
subdomain=False,
known_urls=False,
2021-01-03 19:44:38 +01:00
get="foobar",
2020-12-13 20:48:04 +01:00
)
reply = cli.args_handler(args)
assert "get the source code of the" in str(reply)
2020-12-13 20:48:04 +01:00
def test_args_handler():
args = argparse.Namespace(version=True)
reply = cli.args_handler(args)
2020-10-02 18:53:58 +02:00
assert ("waybackpy version %s" % (__version__)) == reply
args = argparse.Namespace(url=None, version=False)
reply = cli.args_handler(args)
assert ("waybackpy %s" % (__version__)) in str(reply)
2020-12-13 20:48:04 +01:00
def test_main():
# This also tests the parse_args method in cli.py
2020-12-13 20:48:04 +01:00
cli.main(["temp.py", "--version"])