Skip to content

Instantly share code, notes, and snippets.

@kalsmic
Last active March 20, 2019 13:52
Show Gist options
  • Save kalsmic/5944cb7461234ecb567bf1caa6e8db1d to your computer and use it in GitHub Desktop.
Save kalsmic/5944cb7461234ecb567bf1caa6e8db1d to your computer and use it in GitHub Desktop.
Test the meetup feature
#test_meetups.py
"""
Tests for the meetup app
"""
import json
from django.urls import reverse
meetup_wrong = {"date": "2019-03-07", "start": "10:21:39", "end": "12:21:39"}
def test_non_admin_cannot_edit_meetup(api_client, db, user1, meetup1):
api_client.force_authenticate(user=user1)
response = api_client.put(
reverse("meeting", kwargs={"meeting_id": meetup1.id}),
content_type="application/json",
data=json.dumps(meetup),
)
if not response.status_code == 401:
raise AssertionError()
if not response.data == {
"status": 401,
"error": "Action restricted to Admins!",
}:
raise AssertionError()
def test_admin_can_edit_meetup(api_client, db, admin_user, meetup1):
api_client.force_authenticate(user=admin_user)
meetup["created_by"] = admin_user.id
response = api_client.put(
reverse("meeting", kwargs={"meeting_id": meetup1.id}),
content_type="application/json",
data=json.dumps(meetup),
)
if not response.status_code == 200:
raise AssertionError()
if (
response.data["data"][0]["meetup"]["title"] != meetup["title"]
or response.data["data"][0]["meetup"]["start"] != meetup["start"]
or response.data["data"][0]["meetup"]["end"] != meetup["end"]
):
raise AssertionError()
def test_edit_meetup_with_missing_data(api_client, db, admin_user, meetup1):
api_client.force_authenticate(user=admin_user)
response = api_client.put(
reverse("meeting", kwargs={"meeting_id": meetup1.id}),
content_type="application/json",
data=json.dumps(meetup_wrong),
)
if not response.status_code == 400:
raise AssertionError()
if not response.data == {
"status": 400,
"error": {"title": ["This field is required."], "body": ["This field is required."], "created_by": ["This field is required."]},
}:
raise AssertionError()
#link to complete file:- https://github.com/kalsmic/questioner/blob/develop/meetup/tests/test_meetups.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment