From 46e80fd18b8886b884e8e3eaef4f194d96184bba Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sun, 1 Sep 2024 09:00:52 +0200 Subject: [PATCH] add tests/test_api_c3voc.py --- tests/test_api_c3voc.py | 306 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 306 insertions(+) create mode 100644 tests/test_api_c3voc.py diff --git a/tests/test_api_c3voc.py b/tests/test_api_c3voc.py new file mode 100644 index 000000000..c614843b0 --- /dev/null +++ b/tests/test_api_c3voc.py @@ -0,0 +1,306 @@ +import pytest + +from models import event_year +from models.cfp import TalkProposal, Proposal + + +# base webhook. current year, no videos enabled. +WEBHOOK_CONTENTS_BASE = { + "message": "none", + "is_master": True, + "fahrplan": { + "conference": f"emf{event_year()}" + "id": 0, + }, + "voctoweb": { + "enabled": False, + }, + "youtube": { + "enabled": False, + }, +} + + +@pytest.fixture(scope="module") +def proposal(db, user): + proposal = TalkProposal() + proposal.title = "Title" + proposal.description = "Description" + proposal.user = user + + db.session.add(proposal) + db.session.commit() + + return proposal + + +def test_denies_request_without_api_key(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + json=WEBHOOK_CONTENTS_BASE + ) + assert rv.status_code == 401 + + +def test_denies_request_no_master(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["is_master"] = False + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 403 + + +def test_denies_request_wrong_year(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["fahrplan"]["conference"] = "emf1970" + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 422 + + +def test_request_none_update_none(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["fahrplan"]["id"] = proposal.id + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 204 + + proposal = Proposal.query.get(proposal.id) + assert proposal.youtube_url == None + assert proposal.c3voc_url == None + + +def test_request_voctoweb_update_voctoweb_correct_url(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["fahrplan"]["id"] = proposal.id + webhook["voctoweb"] = { + "enabled": True, + "frontend_url": "https://media.ccc.de/", + } + + proposal.video_recording_lost = True + db.session.add(proposal) + db.session.commit() + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 204 + + proposal = Proposal.query.get(proposal.id) + assert proposal.c3voc_url == "https://media.ccc.de/" + assert proposal.video_recording_lost == False + assert proposal.youtube_url == None + + +def test_request_voctoweb_update_voctoweb_wrong_url(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["fahrplan"]["id"] = proposal.id + webhook["voctoweb"] = { + "enabled": True, + "frontend_url": "https://example.org", + } + + proposal.c3voc_url = "https://example.com" + db.session.add(proposal) + db.session.commit() + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 406 + + proposal = Proposal.query.get(proposal.id) + assert proposal.c3voc_url == "https://example.com" + + +def test_request_voctoweb_clears_voctoweb(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["fahrplan"]["id"] = proposal.id + webhook["voctoweb"] = { + "enabled": True, + "frontend_url": "", + } + + proposal.c3voc_url = "https://example.com" + db.session.add(proposal) + db.session.commit() + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 406 + + proposal = Proposal.query.get(proposal.id) + assert proposal.c3voc_url == None + + +def test_request_youtube_update_youtube_correct_url(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["fahrplan"]["id"] = proposal.id + webhook["youtube"] = { + "enabled": True, + "urls": [ + "https://www.youtube.com/watch", + ], + } + + proposal.video_recording_lost = True + db.session.add(proposal) + db.session.commit() + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 204 + + proposal = Proposal.query.get(proposal.id) + assert proposal.c3voc_url == None + assert proposal.video_recording_lost == False + assert proposal.youtube_url == "https://www.youtube.com/watch" + + +def test_request_youtube_update_youtube_wrong_url(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["fahrplan"]["id"] = proposal.id + webhook["youtube"] = { + "enabled": True, + "urls": [ + "https://example.org", + ], + } + + proposal.youtube_url = "https://example.com" + db.session.add(proposal) + db.session.commit() + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 204 + + proposal = Proposal.query.get(proposal.id) + assert proposal.youtube_url == "https://example.com" + + +def test_request_youtube_clears_youtube(client, app, proposal): + app.config.update( + { + "VIDEO_API_KEY": "api-key", + } + ) + + webhook = WEBHOOK_CONTENTS_BASE + webhook["fahrplan"]["id"] = proposal.id + webhook["youtube"] = { + "enabled": True, + "urls": [], + } + + proposal.youtube_url = "https://example.com" + db.session.add(proposal) + db.session.commit() + + rv = client.patch( + f"/api/proposal/c3voc-publishing-webhook", + headers={ + "Authorization": "Bearer api-key", + }, + json=webhook + ) + assert rv.status_code == 204 + + proposal = Proposal.query.get(proposal.id) + assert proposal.youtube_url == None