diff options
author | Daniel Schadt <kingdread@gmx.de> | 2022-07-11 16:15:37 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2022-07-11 16:15:37 +0200 |
commit | 68313fb431a2174dadbdb0a74c56a0a65d9d8594 (patch) | |
tree | cdf16abbdafee9e752eb85aee67af8d5a34c1c1c | |
parent | d27174acf4b2e7a932cdbc2d33559b6ce6bf810e (diff) | |
download | fietsboek-68313fb431a2174dadbdb0a74c56a0a65d9d8594.tar.gz fietsboek-68313fb431a2174dadbdb0a74c56a0a65d9d8594.tar.bz2 fietsboek-68313fb431a2174dadbdb0a74c56a0a65d9d8594.zip |
add integration tests for uploading
-rw-r--r-- | tests/conftest.py | 11 | ||||
-rw-r--r-- | tests/integration/test_login.py | 16 | ||||
-rw-r--r-- | tests/integration/test_upload.py | 84 |
3 files changed, 103 insertions, 8 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 5251b35..b8e3090 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -130,3 +130,14 @@ def dummy_config(dummy_request): """ with testConfig(request=dummy_request) as config: yield config + +@pytest.fixture +def route_path(app_request): + """ + A fixture that yields a function to generate route paths. + + This is equivalent to calling request.route_path on a request. + """ + def get_route_path(*args, **kwargs): + return app_request.route_path(*args, **kwargs) + return get_route_path diff --git a/tests/integration/test_login.py b/tests/integration/test_login.py index 8be77d7..cdb1eea 100644 --- a/tests/integration/test_login.py +++ b/tests/integration/test_login.py @@ -11,11 +11,11 @@ def testuser(dbsession): return user -def test_login_working(testapp, testuser): +def test_login_working(testapp, testuser, route_path): """Ensures that a verified user can log in with the right password.""" testuser.is_verified = True - login = testapp.get('/login') + login = testapp.get(route_path('login')) form = login.form form['email'] = 'foo@bar.com' form['password'] = 'foobar' @@ -24,11 +24,11 @@ def test_login_working(testapp, testuser): assert b'Logout' in response.body -def test_login_not_verified(testapp, testuser): +def test_login_not_verified(testapp, testuser, route_path): """Ensures that a user that has not yet verified their email address can not log in. """ - login = testapp.get('/login') + login = testapp.get(route_path('login')) form = login.form form['email'] = 'foo@bar.com' form['password'] = 'foobar' @@ -38,9 +38,9 @@ def test_login_not_verified(testapp, testuser): assert b'not verified yet' in response.body -def test_login_wrong_email(testapp, testuser): +def test_login_wrong_email(testapp, testuser, route_path): """Ensures that a wrong email address won't let you log in.""" - login = testapp.get('/login') + login = testapp.get(route_path('login')) form = login.form form['email'] = 'fooooooooo@bar.com' @@ -51,11 +51,11 @@ def test_login_wrong_email(testapp, testuser): assert b'Invalid login credentials' in response.body -def test_login_wrong_password(testapp, testuser): +def test_login_wrong_password(testapp, testuser, route_path): """Ensures that a wrong password won't let you log in.""" testuser.is_verified = True - login = testapp.get('/login') + login = testapp.get(route_path('login')) form = login.form form['email'] = 'foo@bar.com' form['password'] = 'raboof' diff --git a/tests/integration/test_upload.py b/tests/integration/test_upload.py new file mode 100644 index 0000000..651aedf --- /dev/null +++ b/tests/integration/test_upload.py @@ -0,0 +1,84 @@ +import gzip +from pathlib import Path + +import pytest +from sqlalchemy import select, func +from webtest import Upload + +from fietsboek import models + + +@pytest.fixture() +def logged_in(dbsession, testapp, route_path): + user = models.User(email='foo@bar.com', is_verified=True) + user.set_password("foobar") + dbsession.add(user) + + login = testapp.get(route_path('login')) + form = login.form + form['email'] = 'foo@bar.com' + form['password'] = 'foobar' + response = form.submit() + assert response.status_code == 302 + + +def load_gpx_asset(filename): + asset_dir = Path(__file__).parent.parent / 'assets' + test_file = asset_dir / filename + with gzip.open(test_file, 'rb') as fobj: + return fobj.read() + + +def test_upload_forbidden(testapp, route_path): + upload_form = testapp.get(route_path('upload'), status="4*") + + +def test_upload(testapp, dbsession, route_path, logged_in): + # Step 1: Go to the upload form + upload_site = testapp.get(route_path('upload')) + + # Step 2: Fill it out + upload_form = upload_site.form + upload_form['gpx'] = Upload('super_track.gpx', load_gpx_asset('Teasi_1.gpx.gz')) + result = upload_form.submit().maybe_follow() + + # Step 3: We are now on the "Finish upload" page + assert "<h1>Upload</h1>" in result.text + + finish_form = result.forms[0] + finish_form["title"].value = "FoOoOo Bar's Testtrack" + finish_form["description"].value = "Super descriptive text!" + # The tagged-friend[] has a disabled attribute, which webtest does not + # respect. Therefore, we need to manually remove it: + finish_form.field_order = [ + (name, field) for (name, field) in finish_form.field_order if name != "tagged-friend[]" + ] + + result = finish_form.submit().maybe_follow() + assert "<h1>FoOoOo Bar's Testtrack</h1>" in result.text + + # Step 4: Ensure the track is stored right + track = dbsession.execute(select(models.Track)).scalar_one() + assert track.title == "FoOoOo Bar's Testtrack" + assert track.description == "Super descriptive text!" + + +def test_upload_cancel(testapp, dbsession, route_path, logged_in): + # Step 1: Go to the upload form + upload_site = testapp.get(route_path('upload')) + + # Step 2: Fill it out + upload_form = upload_site.form + upload_form['gpx'] = Upload('super_track.gpx', load_gpx_asset('Teasi_1.gpx.gz')) + result = upload_form.submit().maybe_follow() + + # Step 3: We are now on the "Finish upload" page + assert "<h1>Upload</h1>" in result.text + + cancel_form = result.forms['cancelForm'] + result = cancel_form.submit().maybe_follow() + assert "Upload cancelled" in result.text + + # Step 4: Ensure the track is deleted again + uploads = dbsession.execute(select(func.count()).select_from(models.Upload)).scalar() + assert uploads == 0 |