From 7bcb34c6dabd580c34025ea834f1839579512389 Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 20:12:15 +0100 Subject: [PATCH 01/11] Attempted refactor: use `pytest` fixture setup/teardown Note: not working currently when the tests run in random order --- tests/test_api_c3voc.py | 62 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index 3a2e99e52..31984ab9d 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -6,6 +6,7 @@ @pytest.fixture(scope="module") def proposal(db, user): + # Setup proposal = TalkProposal() proposal.title = "Title" proposal.description = "Description" @@ -14,16 +15,12 @@ def proposal(db, user): db.session.add(proposal) db.session.commit() - return proposal + # Fixture lifetime + yield proposal - -def clean_proposal(db, proposal, c3voc_url=None, youtube_url=None, thumbnail_url=None, video_recording_lost=True): - proposal.c3voc_url = c3voc_url - proposal.thumbnail_url = thumbnail_url - proposal.video_recording_lost = video_recording_lost - proposal.youtube_url = youtube_url - db.session.add(proposal) - db.session.commit() + # Teardown + db.session.delete(proposal) + db.session.flush() def test_denies_request_without_api_key(client, app, proposal): @@ -117,8 +114,6 @@ def test_request_none_unchanged(client, app, db, proposal): } ) - clean_proposal(db, proposal) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ @@ -152,8 +147,6 @@ def test_update_voctoweb_with_correct_url(client, app, db, proposal): } ) - clean_proposal(db, proposal) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ @@ -190,7 +183,10 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): } ) - clean_proposal(db, proposal, c3voc_url="https://example.com") + proposal.c3voc_url = "https://example.com" + proposal.video_recording_lost = True + db.session.add(proposal) + db.session.flush() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -216,7 +212,7 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): assert rv.status_code == 406 proposal = Proposal.query.get(proposal.id) - # clean_proposal sets this to true, the api should not change that + # setup sets this to true, the api should not change that assert proposal.video_recording_lost == True assert proposal.c3voc_url == "https://example.com" @@ -228,7 +224,9 @@ def test_clears_voctoweb(client, app, db, proposal): } ) - clean_proposal(db, proposal, c3voc_url="https://example.com") + proposal.c3voc_url = "https://example.com" + db.session.add(proposal) + db.session.flush() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -264,8 +262,6 @@ def test_update_thumbnail_with_path(client, app, db, proposal): } ) - clean_proposal(db, proposal) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ @@ -302,8 +298,6 @@ def test_update_thumbnail_with_url(client, app, db, proposal): } ) - clean_proposal(db, proposal) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ @@ -340,7 +334,9 @@ def test_denies_thumbnail_not_url(client, app, db, proposal): } ) - clean_proposal(db, proposal, thumbnail_url="https://example.com/thumb.jpg") + proposal.thumb_path = "https://example.com/thumb.jpg" + db.session.add(proposal) + db.session.flush() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -376,7 +372,9 @@ def test_clears_thumbnail(client, app, db, proposal): } ) - clean_proposal(db, proposal, thumbnail_url="https://example.com/thumb.jpg") + proposal.thumb_path = "https://example.com/thumb.jpg" + db.session.add(proposal) + db.session.flush() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -412,8 +410,6 @@ def test_update_youtube_with_correct_url(client, app, db, proposal): } ) - clean_proposal(db, proposal) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ @@ -451,7 +447,10 @@ def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): } ) - clean_proposal(db, proposal, youtube_url="https://example.com") + proposal.youtube_url = "https://example.com" + proposal.video_recording_lost = True + db.session.add(proposal) + db.session.flush() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -478,7 +477,7 @@ def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): assert rv.status_code == 204 proposal = Proposal.query.get(proposal.id) - # clean_proposal sets this to true, the api should not change that + # setup sets this to true, the api should not change that assert proposal.video_recording_lost == True assert proposal.youtube_url == "https://example.com" @@ -490,7 +489,10 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): } ) - clean_proposal(db, proposal, youtube_url="https://example.com") + proposal.youtube_url = "https://example.com" + proposal.video_recording_lost = True + db.session.add(proposal) + db.session.flush() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -517,7 +519,7 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): assert rv.status_code == 406 proposal = Proposal.query.get(proposal.id) - # clean_proposal sets this to true, the api should not change that + # setup sets this to true, the api should not change that assert proposal.video_recording_lost == True assert proposal.youtube_url == "https://example.com" @@ -529,7 +531,9 @@ def test_clears_youtube(client, app, db, proposal): } ) - clean_proposal(db, proposal, youtube_url="https://example.com") + proposal.youtube_url = "https://example.com" + db.session.add(proposal) + db.session.flush() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", From 3b6d12bbf170f86667ed5465eee6316cc6b67fc2 Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 20:51:14 +0100 Subject: [PATCH 02/11] Tests: c3voc: use default `function` scope for `proposal` fixture --- tests/test_api_c3voc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index 31984ab9d..f844e2b5b 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -4,7 +4,7 @@ from models.cfp import Proposal, TalkProposal -@pytest.fixture(scope="module") +@pytest.fixture def proposal(db, user): # Setup proposal = TalkProposal() From 015c140fbdf0dbcd9ae82c17e5e4aeb2e9a635cb Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 20:53:02 +0100 Subject: [PATCH 03/11] Tests: c3voc: fixup: attribute name correction --- tests/test_api_c3voc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index f844e2b5b..23d4c31c1 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -334,7 +334,7 @@ def test_denies_thumbnail_not_url(client, app, db, proposal): } ) - proposal.thumb_path = "https://example.com/thumb.jpg" + proposal.thumbnail_url = "https://example.com/thumb.jpg" db.session.add(proposal) db.session.flush() @@ -372,7 +372,7 @@ def test_clears_thumbnail(client, app, db, proposal): } ) - proposal.thumb_path = "https://example.com/thumb.jpg" + proposal.thumbnail_url = "https://example.com/thumb.jpg" db.session.add(proposal) db.session.flush() From ce256990bebb21319cde8f146e28ebb063fb9d56 Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 20:57:10 +0100 Subject: [PATCH 04/11] Tests: c3voc: refactor: relocate API token to `test.cfg` --- config/test.cfg | 2 + tests/test_api_c3voc.py | 118 +++++----------------------------------- 2 files changed, 16 insertions(+), 104 deletions(-) diff --git a/config/test.cfg b/config/test.cfg index d24af3f07..249a685f1 100644 --- a/config/test.cfg +++ b/config/test.cfg @@ -26,6 +26,8 @@ MAILCHIMP_KEY = "" MAIL_SERVER = "localhost" MAIL_BACKEND = "locmem" +VIDEO_API_KEY = "video-api-test-token" + BANK_TRANSFER = True BANK_TRANSFER_EURO = True STRIPE = True diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index 23d4c31c1..2d761ee1f 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -24,12 +24,6 @@ def proposal(db, user): def test_denies_request_without_api_key(client, app, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", json={ @@ -50,16 +44,10 @@ def test_denies_request_without_api_key(client, app, proposal): def test_denies_request_no_master(client, app, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": False, @@ -79,16 +67,10 @@ def test_denies_request_no_master(client, app, proposal): def test_denies_request_wrong_year(client, app, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -108,16 +90,10 @@ def test_denies_request_wrong_year(client, app, proposal): def test_request_none_unchanged(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -141,16 +117,10 @@ def test_request_none_unchanged(client, app, db, proposal): def test_update_voctoweb_with_correct_url(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -177,12 +147,6 @@ def test_update_voctoweb_with_correct_url(client, app, db, proposal): def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - proposal.c3voc_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) @@ -191,7 +155,7 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -218,12 +182,6 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): def test_clears_voctoweb(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - proposal.c3voc_url = "https://example.com" db.session.add(proposal) db.session.flush() @@ -231,7 +189,7 @@ def test_clears_voctoweb(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -256,16 +214,10 @@ def test_clears_voctoweb(client, app, db, proposal): def test_update_thumbnail_with_path(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -292,16 +244,10 @@ def test_update_thumbnail_with_path(client, app, db, proposal): def test_update_thumbnail_with_url(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -328,12 +274,6 @@ def test_update_thumbnail_with_url(client, app, db, proposal): def test_denies_thumbnail_not_url(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - proposal.thumbnail_url = "https://example.com/thumb.jpg" db.session.add(proposal) db.session.flush() @@ -341,7 +281,7 @@ def test_denies_thumbnail_not_url(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -366,12 +306,6 @@ def test_denies_thumbnail_not_url(client, app, db, proposal): def test_clears_thumbnail(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - proposal.thumbnail_url = "https://example.com/thumb.jpg" db.session.add(proposal) db.session.flush() @@ -379,7 +313,7 @@ def test_clears_thumbnail(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -404,16 +338,10 @@ def test_clears_thumbnail(client, app, db, proposal): def test_update_youtube_with_correct_url(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -441,12 +369,6 @@ def test_update_youtube_with_correct_url(client, app, db, proposal): def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - proposal.youtube_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) @@ -455,7 +377,7 @@ def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -483,12 +405,6 @@ def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - proposal.youtube_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) @@ -497,7 +413,7 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, @@ -525,12 +441,6 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): def test_clears_youtube(client, app, db, proposal): - app.config.update( - { - "VIDEO_API_KEY": "api-key", - } - ) - proposal.youtube_url = "https://example.com" db.session.add(proposal) db.session.flush() @@ -538,7 +448,7 @@ def test_clears_youtube(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers={ - "Authorization": "Bearer api-key", + "Authorization": "Bearer video-api-test-token", }, json={ "is_master": True, From fdee19b9b3f5fb3fb8ba2cb5745e47e413a107c2 Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 21:00:06 +0100 Subject: [PATCH 05/11] Tests: c3voc: minify diff: rollback to `commit` instead of `flush` This was introduced in 7bcb34c6dabd580c34025ea834f1839579512389 when I was wondering why random-order tests were failing; I'd misattributed `sqlalchemy` identity map caching as a potential cause. The true reason was the `module` scope of the `proposal` fixture, resolved by 3b6d12bbf170f86667ed5465eee6316cc6b67fc2. --- tests/test_api_c3voc.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index 2d761ee1f..6309334f3 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -20,7 +20,7 @@ def proposal(db, user): # Teardown db.session.delete(proposal) - db.session.flush() + db.session.commit() def test_denies_request_without_api_key(client, app, proposal): @@ -150,7 +150,7 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): proposal.c3voc_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) - db.session.flush() + db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -184,7 +184,7 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): def test_clears_voctoweb(client, app, db, proposal): proposal.c3voc_url = "https://example.com" db.session.add(proposal) - db.session.flush() + db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -276,7 +276,7 @@ def test_update_thumbnail_with_url(client, app, db, proposal): def test_denies_thumbnail_not_url(client, app, db, proposal): proposal.thumbnail_url = "https://example.com/thumb.jpg" db.session.add(proposal) - db.session.flush() + db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -308,7 +308,7 @@ def test_denies_thumbnail_not_url(client, app, db, proposal): def test_clears_thumbnail(client, app, db, proposal): proposal.thumbnail_url = "https://example.com/thumb.jpg" db.session.add(proposal) - db.session.flush() + db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -372,7 +372,7 @@ def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): proposal.youtube_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) - db.session.flush() + db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -408,7 +408,7 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): proposal.youtube_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) - db.session.flush() + db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -443,7 +443,7 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): def test_clears_youtube(client, app, db, proposal): proposal.youtube_url = "https://example.com" db.session.add(proposal) - db.session.flush() + db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", From 162b1fe7a7ed80a50191082a982e849dee4280bd Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 21:07:09 +0100 Subject: [PATCH 06/11] Tests: c3voc: nitpick: use initializer keyword-args. --- tests/test_api_c3voc.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index 6309334f3..adf1ff381 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -7,10 +7,11 @@ @pytest.fixture def proposal(db, user): # Setup - proposal = TalkProposal() - proposal.title = "Title" - proposal.description = "Description" - proposal.user = user + proposal = TalkProposal( + title="Title", + description="Description", + user=user, + ) db.session.add(proposal) db.session.commit() From 0788cd44c65dc03d2cfaf81c29c4af9daa67734e Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 21:11:25 +0100 Subject: [PATCH 07/11] Tests: c3voc: provide valid `proposal.id` during denied requests. Rationale: reduce the likelihood that the test failure is due to other validation/logic conditions. --- tests/test_api_c3voc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index adf1ff381..469b3b73c 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -31,7 +31,7 @@ def test_denies_request_without_api_key(client, app, proposal): "is_master": True, "fahrplan": { "conference": "emf1970", - "id": 0, + "id": proposal.id, }, "voctoweb": { "enabled": False, @@ -54,7 +54,7 @@ def test_denies_request_no_master(client, app, proposal): "is_master": False, "fahrplan": { "conference": "emf1970", - "id": 0, + "id": proposal.id, }, "voctoweb": { "enabled": False, @@ -77,7 +77,7 @@ def test_denies_request_wrong_year(client, app, proposal): "is_master": True, "fahrplan": { "conference": "emf1970", - "id": 0, + "id": proposal.id, }, "voctoweb": { "enabled": False, From b996797a055ea06a88383f9483a660ed31204bbd Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 21:15:15 +0100 Subject: [PATCH 08/11] Tests: c3voc: nitpick: prefer `is` to `==` for boolean comparisons. --- tests/test_api_c3voc.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index 469b3b73c..7f8690e63 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -143,7 +143,7 @@ def test_update_voctoweb_with_correct_url(client, app, db, proposal): proposal = Proposal.query.get(proposal.id) assert proposal.c3voc_url == "https://media.ccc.de/" - assert proposal.video_recording_lost == False + assert proposal.video_recording_lost is False assert proposal.youtube_url is None @@ -178,7 +178,7 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): proposal = Proposal.query.get(proposal.id) # setup sets this to true, the api should not change that - assert proposal.video_recording_lost == True + assert proposal.video_recording_lost is True assert proposal.c3voc_url == "https://example.com" @@ -365,7 +365,7 @@ def test_update_youtube_with_correct_url(client, app, db, proposal): proposal = Proposal.query.get(proposal.id) assert proposal.c3voc_url is None - assert proposal.video_recording_lost == False + assert proposal.video_recording_lost is False assert proposal.youtube_url == "https://www.youtube.com/watch" @@ -401,7 +401,7 @@ def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): proposal = Proposal.query.get(proposal.id) # setup sets this to true, the api should not change that - assert proposal.video_recording_lost == True + assert proposal.video_recording_lost is True assert proposal.youtube_url == "https://example.com" @@ -437,7 +437,7 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): proposal = Proposal.query.get(proposal.id) # setup sets this to true, the api should not change that - assert proposal.video_recording_lost == True + assert proposal.video_recording_lost is True assert proposal.youtube_url == "https://example.com" From 435e2f1757029b12a805c7935d0a1950fa6835f2 Mon Sep 17 00:00:00 2001 From: James Addison Date: Sun, 1 Sep 2024 21:25:34 +0100 Subject: [PATCH 09/11] Tests: c3voc: extract common HTTP auth headers fixture. --- tests/test_api_c3voc.py | 89 +++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 56 deletions(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index 7f8690e63..568e568d2 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -24,6 +24,11 @@ def proposal(db, user): db.session.commit() +@pytest.fixture +def valid_auth_headers(): + return {"Authorization": "Bearer video-api-test-token"} + + def test_denies_request_without_api_key(client, app, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", @@ -44,12 +49,10 @@ def test_denies_request_without_api_key(client, app, proposal): assert rv.status_code == 401 -def test_denies_request_no_master(client, app, proposal): +def test_denies_request_no_master(client, app, proposal, valid_auth_headers): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": False, "fahrplan": { @@ -67,12 +70,10 @@ def test_denies_request_no_master(client, app, proposal): assert rv.status_code == 403 -def test_denies_request_wrong_year(client, app, proposal): +def test_denies_request_wrong_year(client, app, proposal, valid_auth_headers): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -90,12 +91,10 @@ def test_denies_request_wrong_year(client, app, proposal): assert rv.status_code == 422 -def test_request_none_unchanged(client, app, db, proposal): +def test_request_none_unchanged(client, app, db, proposal, valid_auth_headers): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -117,12 +116,10 @@ def test_request_none_unchanged(client, app, db, proposal): assert proposal.c3voc_url is None -def test_update_voctoweb_with_correct_url(client, app, db, proposal): +def test_update_voctoweb_with_correct_url(client, app, db, proposal, valid_auth_headers): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -147,7 +144,7 @@ def test_update_voctoweb_with_correct_url(client, app, db, proposal): assert proposal.youtube_url is None -def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): +def test_denies_voctoweb_with_wrong_url(client, app, db, proposal, valid_auth_headers): proposal.c3voc_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) @@ -155,9 +152,7 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -182,16 +177,14 @@ def test_denies_voctoweb_with_wrong_url(client, app, db, proposal): assert proposal.c3voc_url == "https://example.com" -def test_clears_voctoweb(client, app, db, proposal): +def test_clears_voctoweb(client, app, db, proposal, valid_auth_headers): proposal.c3voc_url = "https://example.com" db.session.add(proposal) db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -214,12 +207,10 @@ def test_clears_voctoweb(client, app, db, proposal): assert proposal.c3voc_url is None -def test_update_thumbnail_with_path(client, app, db, proposal): +def test_update_thumbnail_with_path(client, app, db, proposal, valid_auth_headers): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -244,12 +235,10 @@ def test_update_thumbnail_with_path(client, app, db, proposal): assert proposal.youtube_url is None -def test_update_thumbnail_with_url(client, app, db, proposal): +def test_update_thumbnail_with_url(client, app, db, proposal, valid_auth_headers): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -274,16 +263,14 @@ def test_update_thumbnail_with_url(client, app, db, proposal): assert proposal.youtube_url is None -def test_denies_thumbnail_not_url(client, app, db, proposal): +def test_denies_thumbnail_not_url(client, app, db, proposal, valid_auth_headers): proposal.thumbnail_url = "https://example.com/thumb.jpg" db.session.add(proposal) db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -306,16 +293,14 @@ def test_denies_thumbnail_not_url(client, app, db, proposal): assert proposal.thumbnail_url == "https://example.com/thumb.jpg" -def test_clears_thumbnail(client, app, db, proposal): +def test_clears_thumbnail(client, app, db, proposal, valid_auth_headers): proposal.thumbnail_url = "https://example.com/thumb.jpg" db.session.add(proposal) db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -338,12 +323,10 @@ def test_clears_thumbnail(client, app, db, proposal): assert proposal.thumbnail_url is None -def test_update_youtube_with_correct_url(client, app, db, proposal): +def test_update_youtube_with_correct_url(client, app, db, proposal, valid_auth_headers): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -369,7 +352,7 @@ def test_update_youtube_with_correct_url(client, app, db, proposal): assert proposal.youtube_url == "https://www.youtube.com/watch" -def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): +def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal, valid_auth_headers): proposal.youtube_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) @@ -377,9 +360,7 @@ def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -405,7 +386,7 @@ def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal): assert proposal.youtube_url == "https://example.com" -def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): +def test_denies_youtube_update_with_wrong_url(client, app, db, proposal, valid_auth_headers): proposal.youtube_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal) @@ -413,9 +394,7 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { @@ -441,16 +420,14 @@ def test_denies_youtube_update_with_wrong_url(client, app, db, proposal): assert proposal.youtube_url == "https://example.com" -def test_clears_youtube(client, app, db, proposal): +def test_clears_youtube(client, app, db, proposal, valid_auth_headers): proposal.youtube_url = "https://example.com" db.session.add(proposal) db.session.commit() rv = client.post( f"/api/proposal/c3voc-publishing-webhook", - headers={ - "Authorization": "Bearer video-api-test-token", - }, + headers=valid_auth_headers, json={ "is_master": True, "fahrplan": { From f0ae44787b13c569849ce9f54463663beb022b8b Mon Sep 17 00:00:00 2001 From: James Addison Date: Mon, 2 Sep 2024 11:28:38 +0100 Subject: [PATCH 10/11] Tests: c3voc: add missing `video_recording_lost` test preconditions. --- tests/test_api_c3voc.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index 568e568d2..ce2ce95d9 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -117,6 +117,10 @@ def test_request_none_unchanged(client, app, db, proposal, valid_auth_headers): def test_update_voctoweb_with_correct_url(client, app, db, proposal, valid_auth_headers): + proposal.video_recording_lost = True + db.session.add(proposal) + db.session.commit() + rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers=valid_auth_headers, @@ -323,7 +327,11 @@ def test_clears_thumbnail(client, app, db, proposal, valid_auth_headers): assert proposal.thumbnail_url is None -def test_update_youtube_with_correct_url(client, app, db, proposal, valid_auth_headers): +def test_update_from_youtube_with_correct_url(client, app, db, proposal, valid_auth_headers): + proposal.video_recording_lost = True + db.session.add(proposal) + db.session.commit() + rv = client.post( f"/api/proposal/c3voc-publishing-webhook", headers=valid_auth_headers, From 37f8ce438690661cbf287149db61339464d75afb Mon Sep 17 00:00:00 2001 From: James Addison Date: Mon, 2 Sep 2024 11:29:17 +0100 Subject: [PATCH 11/11] Tests: c3voc: typo fixup. --- tests/test_api_c3voc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py index ce2ce95d9..112ef51f8 100644 --- a/tests/test_api_c3voc.py +++ b/tests/test_api_c3voc.py @@ -360,7 +360,7 @@ def test_update_from_youtube_with_correct_url(client, app, db, proposal, valid_a assert proposal.youtube_url == "https://www.youtube.com/watch" -def test_denies_youtube_update_with_exisiting_url(client, app, db, proposal, valid_auth_headers): +def test_denies_youtube_update_with_existing_url(client, app, db, proposal, valid_auth_headers): proposal.youtube_url = "https://example.com" proposal.video_recording_lost = True db.session.add(proposal)