tests-passed

This commit is contained in:
2025-07-31 18:55:59 +03:00
parent b7abb8d8a1
commit e7230ba63c
126 changed files with 8326 additions and 3207 deletions

View File

@@ -1,3 +1,5 @@
from typing import Any
from auth.orm import Author
from orm.invite import Invite, InviteStatus
from orm.shout import Shout
@@ -8,7 +10,7 @@ from services.schema import mutation
@mutation.field("accept_invite")
@login_required
async def accept_invite(_: None, info, invite_id: int):
async def accept_invite(_: None, info, invite_id: int) -> dict[str, Any]:
author_dict = info.context["author"]
author_id = author_dict.get("id")
if author_id:
@@ -16,13 +18,13 @@ async def accept_invite(_: None, info, invite_id: int):
# Check if the user exists
with local_session() as session:
# Check if the invite exists
invite = session.query(Invite).filter(Invite.id == invite_id).first()
invite = session.query(Invite).where(Invite.id == invite_id).first()
if invite and invite.author_id is author_id and invite.status is InviteStatus.PENDING.value:
# Add the user to the shout authors
shout = session.query(Shout).filter(Shout.id == invite.shout_id).first()
shout = session.query(Shout).where(Shout.id == invite.shout_id).first()
if shout:
if author_id not in shout.authors:
author = session.query(Author).filter(Author.id == author_id).first()
author = session.query(Author).where(Author.id == author_id).first()
if author:
shout.authors.append(author)
session.add(shout)
@@ -32,12 +34,12 @@ async def accept_invite(_: None, info, invite_id: int):
return {"error": "Shout not found"}
return {"error": "Invalid invite or already accepted/rejected"}
else:
return {"error": "Unauthorized"}
return {"error": "UnauthorizedError"}
@mutation.field("reject_invite")
@login_required
async def reject_invite(_: None, info, invite_id: int):
async def reject_invite(_: None, info, invite_id: int) -> dict[str, Any]:
author_dict = info.context["author"]
author_id = author_dict.get("id")
@@ -46,7 +48,7 @@ async def reject_invite(_: None, info, invite_id: int):
with local_session() as session:
author_id = int(author_id)
# Check if the invite exists
invite = session.query(Invite).filter(Invite.id == invite_id).first()
invite = session.query(Invite).where(Invite.id == invite_id).first()
if invite and invite.author_id is author_id and invite.status is InviteStatus.PENDING.value:
# Delete the invite
session.delete(invite)
@@ -58,7 +60,7 @@ async def reject_invite(_: None, info, invite_id: int):
@mutation.field("create_invite")
@login_required
async def create_invite(_: None, info, slug: str = "", author_id: int = 0):
async def create_invite(_: None, info, slug: str = "", author_id: int = 0) -> dict[str, Any]:
author_dict = info.context["author"]
viewer_id = author_dict.get("id")
roles = info.context.get("roles", [])
@@ -68,13 +70,13 @@ async def create_invite(_: None, info, slug: str = "", author_id: int = 0):
if author_id:
# Check if the inviter is the owner of the shout
with local_session() as session:
shout = session.query(Shout).filter(Shout.slug == slug).first()
inviter = session.query(Author).filter(Author.id == viewer_id).first()
shout = session.query(Shout).where(Shout.slug == slug).first()
inviter = session.query(Author).where(Author.id == viewer_id).first()
if inviter and shout and shout.authors and inviter.id is shout.created_by:
# Check if an invite already exists
existing_invite = (
session.query(Invite)
.filter(
.where(
Invite.inviter_id == inviter.id,
Invite.author_id == author_id,
Invite.shout_id == shout.id,
@@ -103,16 +105,16 @@ async def create_invite(_: None, info, slug: str = "", author_id: int = 0):
@mutation.field("remove_author")
@login_required
async def remove_author(_: None, info, slug: str = "", author_id: int = 0):
async def remove_author(_: None, info, slug: str = "", author_id: int = 0) -> dict[str, Any]:
viewer_id = info.context.get("author", {}).get("id")
is_admin = info.context.get("is_admin", False)
roles = info.context.get("roles", [])
if not viewer_id and not is_admin and "admin" not in roles and "editor" not in roles:
return {"error": "Access denied"}
with local_session() as session:
author = session.query(Author).filter(Author.id == author_id).first()
author = session.query(Author).where(Author.id == author_id).first()
if author:
shout = session.query(Shout).filter(Shout.slug == slug).first()
shout = session.query(Shout).where(Shout.slug == slug).first()
# NOTE: owner should be first in a list
if shout and author.id is shout.created_by:
shout.authors = [author for author in shout.authors if author.id != author_id]
@@ -123,16 +125,16 @@ async def remove_author(_: None, info, slug: str = "", author_id: int = 0):
@mutation.field("remove_invite")
@login_required
async def remove_invite(_: None, info, invite_id: int):
async def remove_invite(_: None, info, invite_id: int) -> dict[str, Any]:
author_dict = info.context["author"]
author_id = author_dict.get("id")
if isinstance(author_id, int):
# Check if the user exists
with local_session() as session:
# Check if the invite exists
invite = session.query(Invite).filter(Invite.id == invite_id).first()
invite = session.query(Invite).where(Invite.id == invite_id).first()
if isinstance(invite, Invite):
shout = session.query(Shout).filter(Shout.id == invite.shout_id).first()
shout = session.query(Shout).where(Shout.id == invite.shout_id).first()
if shout and shout.deleted_at is None and invite:
if invite.inviter_id is author_id or author_id == shout.created_by:
if invite.status is InviteStatus.PENDING.value:
@@ -140,9 +142,9 @@ async def remove_invite(_: None, info, invite_id: int):
session.delete(invite)
session.commit()
return {}
return None
return None
return None
return {"error": "Invite already accepted/rejected or deleted"}
return {"error": "Access denied"}
return {"error": "Shout not found"}
return {"error": "Invalid invite or already accepted/rejected"}
else:
return {"error": "Author not found"}