Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iamareebjamal committed Jul 7, 2020
1 parent f5c2615 commit 0ec3f7b
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 6 deletions.
4 changes: 0 additions & 4 deletions app/api/schema/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ def validate_fields(self, data, original_data):
{'source': ''}, 'Co-organizer access is required.'
)

if 'track' in data:
if not has_access('is_coorganizer', event_id=data['event']):
raise ForbiddenError({'source': ''}, 'Co-organizer access is required.')

if 'microlocation' in data:
if not has_access('is_coorganizer', event_id=data['event']):
raise ForbiddenError({'source': ''}, 'Co-organizer access is required.')
Expand Down
4 changes: 4 additions & 0 deletions app/api/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def before_post(self, args, kwargs, data):
):
raise ForbiddenError({'pointer': ''}, "Sessions are disabled for this Event")

data['complex_field_values'] = validate_custom_form_constraints_request(
'session', self.schema, Session(event_id=data['event']), data
)

def after_create_object(self, session, data, view_kwargs):
"""
method to send email for creation of new session
Expand Down
2 changes: 1 addition & 1 deletion app/api/speakers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def before_post(self, args, kwargs, data=None):
)

data['complex_field_values'] = validate_custom_form_constraints_request(
'speaker', SpeakerSchema, Speaker(event_id=data['event']), data
'speaker', self.schema, Speaker(event_id=data['event']), data
)

def after_create_object(self, speaker, data, view_kwargs):
Expand Down
322 changes: 321 additions & 1 deletion tests/all/integration/api/session/test_session_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from app.models.custom_form import CustomForms
from app.models.session import Session
from tests.factories.session import SessionSubFactory
from tests.factories.speakers_call import SpeakersCallSubFactory
from tests.factories.track import TrackSubFactory
Expand Down Expand Up @@ -146,7 +147,7 @@ def test_create_session_required_fields_missing(db, client, user, jwt):
'/v1/sessions', content_type='application/vnd.api+json', headers=jwt, data=data,
)

# assert response.status_code == 422
assert response.status_code == 422
assert json.loads(response.data) == {
'errors': [
{
Expand All @@ -158,3 +159,322 @@ def test_create_session_required_fields_missing(db, client, user, jwt):
],
'jsonapi': {'version': '1.0'},
}


def test_edit_session_required_fields_complete(db, client, user, jwt):
session = get_simple_custom_form_session(db, user)

data = json.dumps(
{
'data': {
'type': 'session',
'id': str(session.id),
"attributes": {
"title": "Move Away",
"subtitle": "Moooove",
"level": "456345678",
"short-abstract": "Speaking since birth",
"complex-field-values": {
"bojack": "horseman"
}, # Should be ignored and saved as None
},
}
}
)

response = client.patch(
f'/v1/sessions/{session.id}',
content_type='application/vnd.api+json',
headers=jwt,
data=data,
)

db.session.refresh(session)

assert response.status_code == 200

assert session.title == 'Move Away'
assert session.subtitle == 'Moooove'
assert session.level == '456345678'
assert session.short_abstract == 'Speaking since birth'
assert session.complex_field_values is None


def test_create_session_required_fields_complete(db, client, user, jwt):
session = get_simple_custom_form_session(db, user)
track = TrackSubFactory(event=session.event)
db.session.commit()

data = json.dumps(
{
'data': {
'type': 'session',
"attributes": {
"title": "Move Away",
"subtitle": "Moooove",
"level": "456345678",
"short-abstract": "Speaking since birth",
"complex-field-values": {
"bojack": "horseman"
}, # Should be ignored and saved as None
},
"relationships": {
"event": {"data": {"id": str(session.event_id), "type": "event"}},
"track": {"data": {"id": str(track.id), "type": "track"}},
},
}
}
)

response = client.post(
'/v1/sessions', content_type='application/vnd.api+json', headers=jwt, data=data,
)

assert response.status_code == 201

session = Session.query.get(json.loads(response.data)['data']['id'])

assert session.title == 'Move Away'
assert session.subtitle == 'Moooove'
assert session.level == '456345678'
assert session.short_abstract == 'Speaking since birth'
assert session.complex_field_values is None


def get_complex_custom_form_session(db, user):
session = get_minimal_session(db, user)
CustomForms(
event=session.event,
form='session',
field_identifier='slidesUrl',
type='text',
is_included=True,
is_required=True,
)
CustomForms(
event=session.event,
form='session',
field_identifier='bestFriend',
name='Best Friend',
type='text',
is_included=True,
is_required=True,
is_complex=True,
)
CustomForms(
event=session.event,
form='session',
field_identifier='transFatContent',
name='Trans Fat Content',
type='number',
is_included=True,
is_required=False,
is_complex=True,
)
db.session.commit()

return session


def test_custom_form_complex_fields_missing_required(db, client, user, jwt):
session = get_complex_custom_form_session(db, user)

data = json.dumps(
{
'data': {
'type': 'session',
'id': str(session.id),
"attributes": {
"title": "Move Away",
"subtitle": "Moooove",
"language": "English",
},
}
}
)

response = client.patch(
f'/v1/sessions/{session.id}',
content_type='application/vnd.api+json',
headers=jwt,
data=data,
)

db.session.refresh(session)

assert response.status_code == 422
assert json.loads(response.data) == {
'errors': [
{
'detail': "Missing required fields ['best_friend', 'slides_url']",
'source': {'pointer': '/data/attributes'},
'status': 422,
'title': 'Unprocessable Entity',
}
],
'jsonapi': {'version': '1.0'},
}

assert session.title != 'Move Away'
assert session.subtitle != 'Moooove'
assert session.language is None


def test_custom_form_create_complex_fields_missing_required(db, client, user, jwt):
session = get_complex_custom_form_session(db, user)
track = TrackSubFactory(event=session.event)
db.session.commit()

data = json.dumps(
{
'data': {
'type': 'session',
"attributes": {
"title": "Move Away",
"subtitle": "Moooove",
"language": "English",
},
"relationships": {
"event": {"data": {"id": str(session.event_id), "type": "event"}},
"track": {"data": {"id": str(track.id), "type": "track"}},
},
}
}
)

response = client.post(
'/v1/sessions', content_type='application/vnd.api+json', headers=jwt, data=data,
)

db.session.refresh(session)

assert response.status_code == 422
assert json.loads(response.data) == {
'errors': [
{
'detail': "Missing required fields ['best_friend', 'slides_url']",
'source': {'pointer': '/data/attributes'},
'status': 422,
'title': 'Unprocessable Entity',
}
],
'jsonapi': {'version': '1.0'},
}

assert session.title != 'Move Away'
assert session.subtitle != 'Moooove'
assert session.language is None


def test_custom_form_complex_fields_complete(db, client, user, jwt):
session = get_complex_custom_form_session(db, user)

data = json.dumps(
{
'data': {
'type': 'session',
'id': str(session.id),
"attributes": {
"title": "Move Away",
"subtitle": "Moooove",
"slides-url": "http://gypsie.com",
"complex-field-values": {"best-friend": "Tester"},
},
}
}
)

response = client.patch(
f'/v1/sessions/{session.id}',
content_type='application/vnd.api+json',
headers=jwt,
data=data,
)

db.session.refresh(session)

assert response.status_code == 200

assert session.title == 'Move Away'
assert session.subtitle == 'Moooove'
assert session.slides_url == 'http://gypsie.com'
assert session.complex_field_values['best_friend'] == 'Tester'


def test_custom_form_create_complex_fields_complete(db, client, user, jwt):
session = get_complex_custom_form_session(db, user)
track = TrackSubFactory(event=session.event)
db.session.commit()

data = json.dumps(
{
'data': {
'type': 'session',
"attributes": {
"title": "Move Away",
"subtitle": "Moooove",
"slides-url": "http://gypsie.com",
"complex-field-values": {"best-friend": "Tester"},
},
"relationships": {
"event": {"data": {"id": str(session.event_id), "type": "event"}},
"track": {"data": {"id": str(track.id), "type": "track"}},
},
}
}
)

response = client.post(
'/v1/sessions', content_type='application/vnd.api+json', headers=jwt, data=data,
)

session = Session.query.get(json.loads(response.data)['data']['id'])

assert response.status_code == 201

assert session.title == 'Move Away'
assert session.subtitle == 'Moooove'
assert session.slides_url == 'http://gypsie.com'
assert session.complex_field_values['best_friend'] == 'Tester'


def test_ignore_complex_custom_form_fields(db, client, user, jwt):
"""Test to see that extra data from complex JSON is dropped"""
session = get_complex_custom_form_session(db, user)

data = json.dumps(
{
'data': {
'type': 'session',
'id': str(session.id),
"attributes": {
"title": "Move Away",
"subtitle": "Moooove",
"slides-url": "http://gypsie.com",
"complex-field-values": {
"bestFriend": "Bester",
"transFat-content": 20.08,
"shalimar": "sophie",
},
},
}
}
)

response = client.patch(
f'/v1/sessions/{session.id}',
content_type='application/vnd.api+json',
headers=jwt,
data=data,
)

db.session.refresh(session)

assert response.status_code == 200

assert session.title == 'Move Away'
assert session.subtitle == 'Moooove'
assert session.slides_url == 'http://gypsie.com'
assert session.complex_field_values['best_friend'] == 'Bester'
assert session.complex_field_values['trans_fat_content'] == 20.08
assert session.complex_field_values.get('shalimar') is None

0 comments on commit 0ec3f7b

Please sign in to comment.