Skip to content

Commit

Permalink
add tests/test_api_c3voc.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunsi committed Sep 1, 2024
1 parent ac82add commit 46e80fd
Showing 1 changed file with 306 additions and 0 deletions.
306 changes: 306 additions & 0 deletions tests/test_api_c3voc.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 46e80fd

Please sign in to comment.