~fkfd/sophon

49c22e5a79eed11ec66d3cfa650809b4b4e6c81c — Frederick Yin 3 years ago e670aa4 master
Update test suite
3 files changed, 23 insertions(+), 14 deletions(-)

M test/demo_client.py
M test/gcat.py
M test/test_edit.py
M test/demo_client.py => test/demo_client.py +5 -1
@@ 8,10 8,13 @@ from diff_match_patch import diff_match_patch
from gcat import gemini_request

argparser = argparse.ArgumentParser()
argparser.add_argument("-s", "--server", help="gemini://wiki.server.tld")
argparser.add_argument(
    "-s", "--server", help="gemini://wiki.server.tld", default="gemini://localhost"
)
args = argparser.parse_args()
server = args.server


def generate_unidiff(old: str, new: str) -> str:
    differ = diff_match_patch()
    diff_list = differ.diff_main(old, new)


@@ 35,6 38,7 @@ def preq(path: str):


print("Welcome to the demo client for Sophon")
print("Your wiki server is: " + server)
preq("/")
preq("/pages")
found_page = False

M test/gcat.py => test/gcat.py +1 -2
@@ 7,7 7,6 @@ import urllib.parse
from typing import NamedTuple

Response = NamedTuple("Response", [("status", int), ("meta", str), ("body", str)])
base_url = "gemini://localhost:1965"


def absolutise_url(base, relative):


@@ 20,7 19,7 @@ def absolutise_url(base, relative):
    return relative


def gemini_request(url, base=base_url):
def gemini_request(url, base_url):
    # url should look like /path/to/resource?optional_query
    url = base_url + url
    parsed_url = urllib.parse.urlparse(url)

M test/test_edit.py => test/test_edit.py +17 -11
@@ 8,14 8,18 @@ def _is_success(resp):
    return resp.status == 20 and resp.meta == "text/gemini"


def req(path: str):
    return gemini_request(path, "localhost")


def test_edit(commit=False):
    # blank request
    resp = gemini_request("/wiki/test/edit")
    resp = req("/wiki/test/edit")
    assert resp.status == 10
    print("Blank request: pass")

    # initialize new session
    resp = gemini_request("/wiki/test/edit?1.1.")
    resp = req("/wiki/test/edit?1.1.")
    match = re.match(r"^/wiki/test/edit/([0-9a-f]{16})/", resp.meta)
    assert resp.status == 30 and match
    session_id = match.group(1)


@@ 23,14 27,16 @@ def test_edit(commit=False):

    # redirection to canonical URL
    # no trailing slash:
    resp = gemini_request(f"/wiki/test/edit/{session_id}")
    print(session_id)
    resp = req(f"/wiki/test/edit/{session_id}")
    print(resp)
    # with trailing slash:
    assert resp.status == 30 and resp.meta.endswith(f"{session_id}/")
    print(f"Auto append trailing slash: pass")

    # enter edit session
    url_base = f"/wiki/test/edit/{session_id}/"
    resp = gemini_request(url_base + "preview?stage")
    resp = req(url_base + "preview?stage")
    assert _is_success(resp)
    print(f"Preview stage: \n<<====\n" + resp.body + "\n====>>")



@@ 54,33 60,33 @@ Here's a new line."""
    patch_list = differ.patch_make(old_string, diff_list)
    unidiff = differ.patch_toText(patch_list)

    resp = gemini_request(url_base + "diff?" + urllib.parse.quote(unidiff))
    resp = req(url_base + "diff?" + urllib.parse.quote(unidiff))
    assert _is_success(resp) and resp.body == unidiff
    print("Send diff: pass")

    # ensure diff is correctly applied
    resp = gemini_request(url_base + "preview")
    resp = req(url_base + "preview")
    assert _is_success(resp) and resp.body == new_string
    print(f"Preview stage: \n<<====\n" + resp.body + "\n====>>")
    print("Apply diff: pass")

    # preview page
    resp = gemini_request(url_base + "preview?page")
    resp = req(url_base + "preview?page")
    assert _is_success(resp)
    print(f"Preview page: \n<<====\n" + resp.body + "\n====>>")
    print("Preview page: pass")

    if commit:
        # make backup of original test page
        shutil.copy("../server/wiki/test.gmi", "/tmp/test.gmi")
        resp = gemini_request(url_base + "commit?Improve page")
        shutil.copy("/var/sophon/wiki/test.gmi", "/tmp/test.gmi")
        resp = req(url_base + "commit?Improve page")
        assert resp.status == 30 and resp.meta == "/wiki/test"
        print("Commit sesison: pass")
        shutil.copy("/tmp/test.gmi", "../server/wiki/test.gmi")
        shutil.copy("/tmp/test.gmi", "/var/sophon/wiki/test.gmi")
        print("Clean up: finished")
    else:
        # abort session
        resp = gemini_request(url_base + "abort")
        resp = req(url_base + "abort")
        assert resp.status == 30 and resp.meta == "/wiki/test"
        print("Abort session: pass")