def test_events_attachments_are_created_from_nested_data(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile serializer = ProjectSerializer(data={ 'slug': 'test-slug', 'title': 'test title', 'location': 'test location', 'description': 'test description', 'events': [ { 'label': 'test label', 'attachments': [ {'label': 'attachment 1', 'url': 'http://example.com/1'}, {'label': 'attachment 2', 'url': 'http://example.com/1'} ] }, ], 'sections': [], 'owner': profile.slug }) ok_(serializer.is_valid(), serializer.errors) project = serializer.save() assert_equal([int(a.label.split()[-1]) for a in project.events.all()[0].attachments.all()], [1, 2])
def test_null_events_are_invalid_for_existing_project(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=profile) Event.objects.create(label='test label 1', project=project), Event.objects.create(label='test label 3', project=project), serializer = ProjectSerializer(project, data={ 'id': project.pk, 'slug': 'test-slug', 'title': 'test new title', 'location': 'test location', 'events': None, 'owner': { 'slug': profile.slug } }) ok_(not serializer.is_valid()) assert_in('events', serializer.errors)
def test_events_are_updated_from_nested_data(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', description='test description', owner=profile) events = [ Event.objects.create(label='test label 1', project=project), Event.objects.create(label='test label 3', project=project), ] serializer = ProjectSerializer(project, data={ 'id': project.pk, 'slug': 'test-slug', 'title': 'test new title', 'location': 'test location', 'description': 'test description', 'events': [ {'label': 'test label 3', 'id': events[1].pk}, {'label': 'test label 2'}, {'label': 'test label 1', 'id': events[0].pk} ], 'sections': [], 'owner': profile.slug }) ok_(serializer.is_valid(), serializer.errors) new_project = serializer.save() assert_equal(project.pk, new_project.pk) assert_equal(new_project.title, 'test new title') assert_equal([int(e.label.split()[-1]) for e in project.events.all()], [3, 2, 1]) assert_equal(project.events.all()[0].pk, events[1].pk) assert_equal(project.events.all()[2].pk, events[0].pk)
def test_events_are_created_from_nested_data(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile serializer = ProjectSerializer( data={ 'slug': 'test-slug', 'title': 'test title', 'location': 'test location', 'events': [{ 'label': 'test label 1' }, { 'label': 'test label 2' }, { 'label': 'test label 3' }], 'sections': [], 'owner': { 'slug': profile.slug } }) ok_(serializer.is_valid(), serializer.errors) project = serializer.save() assert_equal([int(e.label.split()[-1]) for e in project.events.all()], [1, 2, 3])
def test_project_geometry_update(self): auth = UserAuth.objects.create_user(username='******', password='******') user = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=user, geometry='POINT(2 3)') serializer = ProjectSerializer(project, data={ 'id': project.pk, 'slug': 'test-slug', 'title': 'test new title', 'location': 'test location', 'events': [], 'sections': [], 'owner': { 'slug': user.slug }, 'geometry': { u'type': u'Point', u'coordinates': [4.0, 5.0] } }) ok_(serializer.is_valid(), serializer.errors) serializer.save() project = serializer.object assert_equal(project.geometry.x, 4.0) assert_equal(project.geometry.y, 5.0)
def test_events_are_updated_from_nested_data(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=profile) events = [ Event.objects.create(label='test label 1', slug='test-1', project=project), Event.objects.create(label='test label 2', slug='test-2', project=project), Event.objects.create(label='test label 3', slug='test-3', project=project), Event.objects.create(label='test label 4', slug='test-4', project=project), ] serializer = ProjectSerializer(project, data={ 'id': project.pk, 'slug': 'test-slug', 'title': 'test new title', 'location': 'test location', 'events': [{ 'label': 'test label 3', 'slug': 'test-3', 'id': events[1].pk }, { 'label': 'test label 2', 'slug': 'test-2' }, { 'label': 'test label 1', 'slug': 'test-1', 'id': events[0].pk }], 'sections': [], 'owner': { 'slug': profile.slug } }) ok_(serializer.is_valid(), serializer.errors) new_project = serializer.save() assert_equal(project.pk, new_project.pk) assert_equal(new_project.title, 'test new title') assert_equal([int(e.label.split()[-1]) for e in project.events.all()], [3, 2, 1]) assert_equal(project.events.all()[0].pk, events[1].pk) assert_equal(project.events.all()[2].pk, events[0].pk)
def test_project_with_empty_title_is_invalid(self): auth = UserAuth.objects.create_user(username='******', password='******') user = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', description='test description', owner=user) serializer = ProjectSerializer(project, data={'slug': '', 'title': ''}) ok_(not serializer.is_valid(), 'Project with empty slug and title should not validate') assert_in('title', serializer.errors)
def test_invalid_project_does_not_raise_exception(self): serializer = ProjectSerializer(data={ # Title and owner is required 'events': [ {'label': 'test label 1'}, {'label': 'test label 2'}, {'label': 'test label 3'} ], }) ok_(not serializer.is_valid())
def test_project_with_invalid_markup_in_description(self): auth = UserAuth.objects.create_user(username='******', password='******') user = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', description='test description', owner=user) description = """<div>oh</div> <b>hello</b> <script>alert('hacked');</script> <div class="hi">new line</div>""" expected = """<br>oh <b>hello</b> alert('hacked'); <br>new line""" serializer = ProjectSerializer(project, data={'slug': '', 'title': 'Test Title', 'description': description, 'owner': user.slug, 'events': [], 'sections': []}) ok_(serializer.is_valid(), 'Project with markup should validate: %s' % (serializer.errors,)) assert_not_equal(serializer.object.description, description) assert_equal(serializer.object.description, expected)
def test_project_with_empty_title_is_invalid(self): auth = UserAuth.objects.create_user(username='******', password='******') user = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=user) serializer = ProjectSerializer(project, data={'slug': '', 'title': ''}) ok_(not serializer.is_valid(), 'Project with empty slug and title should not validate') assert_in('title', serializer.errors)
def test_invalid_project_does_not_raise_exception(self): serializer = ProjectSerializer( data={ # Title and owner is required 'events': [{ 'label': 'test label 1' }, { 'label': 'test label 2' }, { 'label': 'test label 3' }], }) ok_(not serializer.is_valid())
def test_null_events_are_invalid_for_new_project(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile serializer = ProjectSerializer(data={ 'slug': 'test-slug', 'title': 'test title', 'location': 'test location', 'events': None, 'owner': {'slug': profile.slug} }) ok_(not serializer.is_valid()) assert_in('events', serializer.errors)
def test_null_events_are_invalid_for_new_project(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile serializer = ProjectSerializer( data={ 'slug': 'test-slug', 'title': 'test title', 'location': 'test location', 'events': None, 'owner': { 'slug': profile.slug } }) ok_(not serializer.is_valid()) assert_in('events', serializer.errors)
def test_null_events_are_invalid_for_existing_project(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=profile) Event.objects.create(label='test label 1', project=project), Event.objects.create(label='test label 3', project=project), serializer = ProjectSerializer(project, data={ 'id': project.pk, 'slug': 'test-slug', 'title': 'test new title', 'location': 'test location', 'events': None, 'owner': {'slug': profile.slug} }) ok_(not serializer.is_valid()) assert_in('events', serializer.errors)
def test_events_are_created_from_nested_data(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile serializer = ProjectSerializer(data={ 'slug': 'test-slug', 'title': 'test title', 'location': 'test location', 'events': [ {'label': 'test label 1'}, {'label': 'test label 2'}, {'label': 'test label 3'} ], 'sections': [], 'owner': {'slug': profile.slug} }) ok_(serializer.is_valid(), serializer.errors) project = serializer.save() assert_equal([int(e.label.split()[-1]) for e in project.events.all()], [1, 2, 3])
def test_events_attachments_are_updated_from_nested_data(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', description='test description', owner=profile) event = Event.objects.create(label='test label', project=project) attachments = [ Attachment.objects.create(attached_to=event, label='attachment 1'), Attachment.objects.create(attached_to=event, label='attachment 2'), ] serializer = ProjectSerializer(project, data={ 'id': project.pk, 'slug': 'test-slug', 'title': 'test new title', 'location': 'test location', 'description': 'test description', 'events': [ { 'label': 'test label', 'attachments': [ {'label': 'attachment 1', 'url': 'http://example.com/1', 'id': attachments[1].pk}, {'label': 'attachment 3', 'url': 'http://example.com/2'}, {'label': 'attachment 2', 'url': 'http://example.com/3', 'id': attachments[0].pk}, ], 'id': event.pk, }, ], 'sections': [], 'owner': profile.slug }) ok_(serializer.is_valid(), serializer.errors) new_project = serializer.save() assert_equal(project.pk, new_project.pk) assert_equal(new_project.title, 'test new title') new_event = new_project.events.all()[0] assert_equal([int(a.label.split()[-1]) for a in new_event.attachments.all()], [1, 3, 2]) assert_equal([int(a.url.split('/')[-1]) for a in new_event.attachments.all()], [1, 2, 3]) assert_equal(new_event.attachments.all()[0].pk, attachments[1].pk) assert_equal(new_event.attachments.all()[2].pk, attachments[0].pk)
def test_project_geometry_update(self): auth = UserAuth.objects.create_user(username='******', password='******') user = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=user, geometry='POINT(2 3)') serializer = ProjectSerializer(project, data={ 'id': project.pk, 'slug': 'test-slug', 'title': 'test new title', 'location': 'test location', 'events': [], 'sections': [], 'owner': {'slug': user.slug}, 'geometry': {u'type': u'Point', u'coordinates': [4.0, 5.0]} }) ok_(serializer.is_valid(), serializer.errors) serializer.save() project = serializer.object assert_equal(project.geometry.x, 4.0) assert_equal(project.geometry.y, 5.0)
def test_events_attachments_are_created_from_nested_data(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile serializer = ProjectSerializer( data={ 'slug': 'test-slug', 'title': 'test title', 'location': 'test location', 'events': [ { 'label': 'test label', 'attachments': [{ 'label': 'attachment 1', 'url': 'http://example.com/1' }, { 'label': 'attachment 2', 'url': 'http://example.com/1' }] }, ], 'sections': [], 'owner': { 'slug': profile.slug } }) ok_(serializer.is_valid(), serializer.errors) project = serializer.save() assert_equal([ int(a.label.split()[-1]) for a in project.events.all()[0].attachments.all() ], [1, 2])
def test_project_geometry_create(self): auth = UserAuth.objects.create_user(username='******', password='******') user = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=user, geometry='POINT(2 3)') serializer = ProjectSerializer(project) assert_in('geometry', serializer.data) assert_equal(serializer.data['geometry'], { u'type': u'Point', u'coordinates': [2.0, 3.0] })
def test_events_are_nested_in_data(self): auth = UserAuth.objects.create_user(username='******', password='******') user = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=user) events = [ Event.objects.create(label='test label 1', project=project), Event.objects.create(label='test label 2', project=project), Event.objects.create(label='test label 3', project=project), ] serializer = ProjectSerializer(project) data = serializer.data assert_in('events', data) assert_equal(len(data['events']), 3) assert_equal([int(e['label'].split()[-1]) for e in data['events']], [1, 2, 3])
def get_project_serialized_data(self): project_serializer = ProjectSerializer(self.project) return project_serializer.data
def test_events_attachments_are_updated_from_nested_data(self): auth = UserAuth.objects.create_user(username='******', password='******') profile = auth.profile project = Project.objects.create(slug='test-slug', title='test title', location='test location', owner=profile) event = Event.objects.create(label='test label', project=project) attachments = [ Attachment.objects.create(attached_to=event, label='attachment 1'), Attachment.objects.create(attached_to=event, label='attachment 2'), ] serializer = ProjectSerializer( project, data={ 'id': project.pk, 'slug': 'test-slug', 'title': 'test new title', 'location': 'test location', 'events': [ { 'label': 'test label', 'attachments': [ { 'label': 'attachment 1', 'url': 'http://example.com/1', 'id': attachments[1].pk }, { 'label': 'attachment 3', 'url': 'http://example.com/2' }, { 'label': 'attachment 2', 'url': 'http://example.com/3', 'id': attachments[0].pk }, ], 'id': event.pk, }, ], 'sections': [], 'owner': { 'slug': profile.slug } }) ok_(serializer.is_valid(), serializer.errors) new_project = serializer.save() assert_equal(project.pk, new_project.pk) assert_equal(new_project.title, 'test new title') new_event = new_project.events.all()[0] assert_equal( [int(a.label.split()[-1]) for a in new_event.attachments.all()], [1, 3, 2]) assert_equal( [int(a.url.split('/')[-1]) for a in new_event.attachments.all()], [1, 2, 3]) assert_equal(new_event.attachments.all()[0].pk, attachments[1].pk) assert_equal(new_event.attachments.all()[2].pk, attachments[0].pk)