Correct event testing
This commit is contained in:
@@ -7,7 +7,7 @@ from app.core.config import settings
|
|||||||
from app.tests.utils.event import create_random_event
|
from app.tests.utils.event import create_random_event
|
||||||
|
|
||||||
|
|
||||||
def test_event(client: TestClient, superuser_token_headers: dict[str, str]) -> None:
|
def test_create_event(client: TestClient, superuser_token_headers: dict[str, str]) -> None:
|
||||||
data = {"name": "Foo", "contact": "Someone"}
|
data = {"name": "Foo", "contact": "Someone"}
|
||||||
|
|
||||||
response = client.post(
|
response = client.post(
|
||||||
@@ -38,9 +38,9 @@ def test_read_event(
|
|||||||
assert content["name"] == event.name
|
assert content["name"] == event.name
|
||||||
assert content["contact"] == event.contact
|
assert content["contact"] == event.contact
|
||||||
assert content["id"] == str(event.id)
|
assert content["id"] == str(event.id)
|
||||||
assert content["is_active"] == str(event.is_active)
|
assert content["is_active"] == event.is_active
|
||||||
assert content["start_at"] == str(event.start_at)
|
assert str(content["start_at"]) == str(event.start_at)
|
||||||
assert content["end_at"] == str(event.end_at)
|
assert str(content["end_at"]) == str(event.end_at)
|
||||||
|
|
||||||
|
|
||||||
def test_read_event_not_found(
|
def test_read_event_not_found(
|
||||||
@@ -58,9 +58,9 @@ def test_read_event_not_found(
|
|||||||
def test_read_event_not_enough_permissions(
|
def test_read_event_not_enough_permissions(
|
||||||
client: TestClient, normal_user_token_headers: dict[str, str], db: Session
|
client: TestClient, normal_user_token_headers: dict[str, str], db: Session
|
||||||
) -> None:
|
) -> None:
|
||||||
item = create_random_event(db)
|
event = create_random_event(db)
|
||||||
response = client.get(
|
response = client.get(
|
||||||
f"{settings.API_V1_STR}/events/{item.id}",
|
f"{settings.API_V1_STR}/events/{event.id}",
|
||||||
headers=normal_user_token_headers,
|
headers=normal_user_token_headers,
|
||||||
)
|
)
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
@@ -97,9 +97,9 @@ def test_update_event(
|
|||||||
assert content["name"] == data["name"]
|
assert content["name"] == data["name"]
|
||||||
assert content["contact"] == data["contact"]
|
assert content["contact"] == data["contact"]
|
||||||
assert content["id"] == str(event.id)
|
assert content["id"] == str(event.id)
|
||||||
assert "is_active" == str(event.is_active)
|
assert content["is_active"] == event.is_active
|
||||||
assert "start_at" == str(event.start_at)
|
assert str(content["start_at"]) == str(event.start_at)
|
||||||
assert "end_at" == str(event.end_at)
|
assert str(content["end_at"]) == str(event.end_at)
|
||||||
|
|
||||||
|
|
||||||
def test_update_event_not_found(
|
def test_update_event_not_found(
|
||||||
@@ -122,7 +122,7 @@ def test_update_event_not_enough_permissions(
|
|||||||
event = create_random_event(db)
|
event = create_random_event(db)
|
||||||
data = {"name": "Updated name", "contact": "Updated contact"}
|
data = {"name": "Updated name", "contact": "Updated contact"}
|
||||||
response = client.put(
|
response = client.put(
|
||||||
f"{settings.API_V1_STR}/items/{event.id}",
|
f"{settings.API_V1_STR}/events/{event.id}",
|
||||||
headers=normal_user_token_headers,
|
headers=normal_user_token_headers,
|
||||||
json=data,
|
json=data,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,8 +7,10 @@ from sqlmodel import Session, delete
|
|||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.core.db import engine, init_db
|
from app.core.db import engine, init_db
|
||||||
from app.main import app
|
from app.main import app
|
||||||
from app.models.user import User
|
from app.models.event import Event
|
||||||
from app.tests.utils.user import authentication_token_from_email
|
from app.models.user import User, PermissionRight
|
||||||
|
from app.tests.utils.event import create_random_event
|
||||||
|
from app.tests.utils.user import authentication_token_from_email, create_random_user
|
||||||
from app.tests.utils.utils import get_superuser_token_headers
|
from app.tests.utils.utils import get_superuser_token_headers
|
||||||
|
|
||||||
|
|
||||||
@@ -38,3 +40,20 @@ def normal_user_token_headers(client: TestClient, db: Session) -> dict[str, str]
|
|||||||
return authentication_token_from_email(
|
return authentication_token_from_email(
|
||||||
client=client, email=settings.EMAIL_TEST_USER, db=db
|
client=client, email=settings.EMAIL_TEST_USER, db=db
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class EventUserHeader:
|
||||||
|
def __init__(self, user: User, event: Event, headers: dict[str, str]) -> None:
|
||||||
|
self.user = user
|
||||||
|
self.event = event
|
||||||
|
self.headers = headers
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def event_user_token_headers(client: TestClient, db: Session) -> EventUserHeader:
|
||||||
|
user = create_random_user(db)
|
||||||
|
event = create_random_event(db, name="Test event for user", contact=str(user.email))
|
||||||
|
event.add_user(user=user, rights=PermissionRight.ADMIN, session=db)
|
||||||
|
headers = authentication_token_from_email(client=client, email=str(user.email), db=db)
|
||||||
|
|
||||||
|
return EventUserHeader(user, event, headers)
|
||||||
|
|||||||
@@ -4,8 +4,12 @@ from app.models.event import Event, EventCreate
|
|||||||
from app.tests.utils.utils import random_email, random_lower_string
|
from app.tests.utils.utils import random_email, random_lower_string
|
||||||
|
|
||||||
|
|
||||||
def create_random_event(db: Session) -> Event:
|
def create_random_event(db: Session, name: str = None, contact: str = None) -> Event:
|
||||||
name = random_lower_string()
|
if not name:
|
||||||
contact = random_email()
|
name = random_lower_string()
|
||||||
|
|
||||||
|
if not contact:
|
||||||
|
contact = random_email()
|
||||||
|
|
||||||
event_in = EventCreate(name=name, contact=contact)
|
event_in = EventCreate(name=name, contact=contact)
|
||||||
return Event.create(session=db, create_obj=event_in)
|
return Event.create(session=db, create_obj=event_in)
|
||||||
|
|||||||
Reference in New Issue
Block a user