def test_PUT_hearing_two_closure_sections(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) data['sections'][2]['type'] = 'closure-info' response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') data = get_data_from_response(response, status_code=400) assert 'A hearing cannot have more than one closure info sections' in data['sections']
def test_add_plugin_data_to_comment(api_client, default_hearing, case): with override_settings( DEMOCRACY_PLUGINS={ "test_plugin": "democracy.tests.plug.TestPlugin" } ): section = default_hearing.sections.first() if case.startswith("plug"): section.plugin_identifier = "test_plugin" section.save() url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id) comment_data = get_comment_data( content="", plugin_data=("foo6" if case == "plug-valid" else "invalid555") ) response = api_client.post(url, data=comment_data) if case == "plug-valid": assert response.status_code == 201 created_comment = SectionComment.objects.first() assert created_comment.plugin_identifier == section.plugin_identifier assert created_comment.plugin_data == comment_data["plugin_data"][::-1] # The TestPlugin reverses data elif case == "plug-invalid": data = get_data_from_response(response, status_code=400) assert data == {"plugin_data": ["The data must contain a 6."]} elif case == "noplug": data = get_data_from_response(response, status_code=400) assert "no plugin data is allowed" in data["plugin_data"][0] else: raise NotImplementedError("...")
def test_PUT_hearing_two_main_sections(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) data['sections'][2]['type'] = 'main' response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') data = get_data_from_response(response, status_code=400) assert 'A hearing must have exactly one main section' in data['sections']
def test_add_empty_comment_with_label(john_doe_api_client, default_hearing, get_comments_url_and_data): label_one = Label(id=1, label='The Label') label_one.save() section = default_hearing.sections.first() url, data = get_comments_url_and_data(default_hearing, section) old_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in old_comment_list: old_comment_list = old_comment_list['results'] # set section explicitly comment_data = get_comment_data(section=section.pk, content='', label={'id': 1}, geojson=None) response = john_doe_api_client.post(url, data=comment_data, format='json') data = get_data_from_response(response, status_code=201) assert 'label' in data assert data["label"] == {'id': 1, 'label': {default_lang_code: 'The Label'}} # Check that the comment is available in the comment endpoint now new_comment_list = get_data_from_response(john_doe_api_client.get(url)) comment_data = get_comment_data(section=section.pk, label={'label': {default_lang_code: 'The Label'}, 'id': 1}, content='', geojson=None) # If pagination is used the actual data is in "results" if 'results' in new_comment_list: new_comment_list = new_comment_list['results'] assert len(new_comment_list) == len(old_comment_list) + 1 new_comment = [c for c in new_comment_list if c["id"] == data["id"]][0] assert_common_keys_equal(new_comment, comment_data) assert new_comment["is_registered"] == True assert new_comment["label"] == {'id': 1, 'label': {default_lang_code: 'The Label'}} assert new_comment["author_name"] is None assert new_comment["content"] is ''
def test_comment_edit_auth(john_doe_api_client, jane_doe_api_client, api_client, default_hearing, lookup_field): url = get_main_comments_url(default_hearing, lookup_field) # John posts an innocuous comment: johns_message = "Hi! I'm John!" response = john_doe_api_client.post(url, data={"content": johns_message}) data = get_data_from_response(response, 201) comment_id = data["id"] # Now Jane (in the guise of Mallory) attempts a rogue edit: response = jane_doe_api_client.patch('%s%s/' % (url, comment_id), data={"content": "hOI! I'M TEMMIE"}) # But her attempts are foiled! data = get_data_from_response(response, 403) assert SectionComment.objects.get(pk=comment_id).content == johns_message # Jane, thinking she can bamboozle our authentication by logging out, tries again! response = api_client.patch('%s%s/' % (url, comment_id), data={"content": "I'm totally John"}) # But still, no! data = get_data_from_response(response, 403) assert SectionComment.objects.get(pk=comment_id).content == johns_message
def test_56_add_empty_comment_with_geojson(john_doe_api_client, default_hearing, get_comments_url_and_data, geojson_feature): section = default_hearing.sections.first() url, data = get_comments_url_and_data(default_hearing, section) old_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in old_comment_list: old_comment_list = old_comment_list['results'] # set section explicitly comment_data = get_comment_data(section=section.pk, content='') response = john_doe_api_client.post(url, data=comment_data) data = get_data_from_response(response, status_code=201) assert 'section' in data assert data['section'] == section.pk assert 'geojson' in data assert data['geojson'] == geojson_feature # Check that the comment is available in the comment endpoint now new_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in new_comment_list: new_comment_list = new_comment_list['results'] assert len(new_comment_list) == len(old_comment_list) + 1 new_comment = [c for c in new_comment_list if c["id"] == data["id"]][0] assert_common_keys_equal(new_comment, comment_data) assert new_comment["is_registered"] == True assert new_comment["author_name"] is None
def test_add_comment_to_section_user_update_nickname(john_doe_api_client, john_doe, default_hearing, get_comments_url_and_data): john_doe.first_name = 'John' john_doe.last_name = 'Doe' john_doe.nickname = 'Johnny' john_doe.save() section = default_hearing.sections.first() url, data = get_comments_url_and_data(default_hearing, section) # set section explicitly comment_data = get_comment_data(section=section.pk, author_name='Jo') response = john_doe_api_client.post(url, data=comment_data) data = get_data_from_response(response, status_code=201) # Check that the comment is available in the comment endpoint now new_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in new_comment_list: new_comment_list = new_comment_list['results'] new_comment = [c for c in new_comment_list if c["id"] == data["id"]][0] john_doe.refresh_from_db() assert john_doe.nickname == 'Jo' assert new_comment["author_name"] == 'Jo'
def test_PUT_hearing_unauthorized_user(valid_hearing_json, john_doe_api_client, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) _update_hearing_data(data) response = john_doe_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') data = get_data_from_response(response, status_code=403) assert data == {'status': 'User without organization cannot PUT hearings.'}
def test_comment_editing_disallowed_after_closure(john_doe_api_client): hearing = Hearing.objects.create( open_at=now() - datetime.timedelta(days=1), close_at=now() + datetime.timedelta(days=1), commenting=Commenting.OPEN) # Post a comment: response = john_doe_api_client.post(get_hearing_detail_url( hearing.id, 'comments'), data=get_comment_data()) data = get_data_from_response(response, status_code=201) comment_id = data["id"] # Successfully edit the comment: response = john_doe_api_client.patch('/v1/hearing/%s/comments/%s/' % (hearing.id, comment_id), data={"content": "Hello"}) data = get_data_from_response(response, status_code=200) assert data["content"] == "Hello" # Close the hearing: hearing.close_at = hearing.open_at hearing.save() # Futilely attempt to edit the comment: response = john_doe_api_client.patch('/v1/hearing/%s/comments/%s/' % (hearing.id, comment_id), data={"content": "No"}) assert response.status_code == 403
def test_put_section_with_poll(valid_hearing_json_with_poll, john_smith_api_client): response = john_smith_api_client.post('/v1/hearing/', data=valid_hearing_json_with_poll, format='json') data = get_data_from_response(response, status_code=201) data['sections'][0]['questions'] = list( reversed(data['sections'][0]['questions'])) data['sections'][0]['questions'][0]['options'] = list( reversed(data['sections'][0]['questions'][0]['options'])) data['sections'][0]['questions'][0]['text']['en'] = 'Edited question' data['sections'][0]['questions'][0]['options'][0]['text'][ 'en'] = 'Edited option' response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') updated_data = get_data_from_response(response, status_code=200) assert updated_data['sections'][0]['questions'][0]['text'][ 'en'] == 'Edited question' assert updated_data['sections'][0]['questions'][1]['text'][ 'en'] == 'Which is better?' assert updated_data['sections'][0]['questions'][0]['options'][0]['text'][ 'en'] == 'Edited option' assert updated_data['sections'][0]['questions'][0]['options'][1]['text'][ 'en'] == 'Yes'
def test_56_add_comment_to_section(john_doe_api_client, default_hearing): section = default_hearing.sections.first() url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id) old_comment_list = get_data_from_response(john_doe_api_client.get(url)) # set section explicitly comment_data = get_comment_data(section=section.pk) response = john_doe_api_client.post(url, data=comment_data) data = get_data_from_response(response, status_code=201) assert 'section' in data assert data['section'] == section.pk assert 'content' in data assert data['content'] == default_comment_content # Check that the comment is available in the comment endpoint now new_comment_list = get_data_from_response(john_doe_api_client.get(url)) assert len(new_comment_list) == len(old_comment_list) + 1 new_comment = [c for c in new_comment_list if c["id"] == data["id"]][0] assert_common_keys_equal(new_comment, comment_data) assert_common_keys_equal(new_comment["created_by"], { "first_name": john_doe_api_client.user.first_name, "last_name": john_doe_api_client.user.last_name, "username": john_doe_api_client.user.username, })
def test_hearing_geo(api_client, random_hearing): random_hearing.geojson = get_geojson() random_hearing.save() data = get_data_from_response(api_client.get(get_detail_url(random_hearing.id))) assert data["geojson"] == random_hearing.geojson map_data = get_data_from_response(api_client.get(list_endpoint + "map/")) assert map_data['results'][0]["geojson"] == random_hearing.geojson
def test_add_empty_comment_with_label(john_doe_api_client, default_hearing, get_comments_url_and_data): label_one = Label(id=1, label='The Label') label_one.save() section = default_hearing.sections.first() url, data = get_comments_url_and_data(default_hearing, section) old_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in old_comment_list: old_comment_list = old_comment_list['results'] # set section explicitly comment_data = get_comment_data(section=section.pk, content='', label={'id': 1}) response = john_doe_api_client.post(url, data=comment_data, format='json') data = get_data_from_response(response, status_code=201) assert 'label' in data assert data["label"] == {'id': 1, 'label': 'The Label'} # Check that the comment is available in the comment endpoint now new_comment_list = get_data_from_response(john_doe_api_client.get(url)) comment_data = get_comment_data(section=section.pk, label={'label': 'The Label', 'id': 1}, content='') # If pagination is used the actual data is in "results" if 'results' in new_comment_list: new_comment_list = new_comment_list['results'] assert len(new_comment_list) == len(old_comment_list) + 1 new_comment = [c for c in new_comment_list if c["id"] == data["id"]][0] assert_common_keys_equal(new_comment, comment_data) assert new_comment["is_registered"] == True assert new_comment["label"] == {'id': 1, 'label': 'The Label'} assert new_comment["author_name"] is None assert new_comment["content"] is ''
def test_56_add_comment_to_section(john_doe_api_client, default_hearing, get_comments_url_and_data): section = default_hearing.sections.first() url, data = get_comments_url_and_data(default_hearing, section) old_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in old_comment_list: old_comment_list = old_comment_list['results'] # set section explicitly comment_data = get_comment_data(section=section.pk) response = john_doe_api_client.post(url, data=comment_data) data = get_data_from_response(response, status_code=201) assert 'section' in data assert data['section'] == section.pk assert 'content' in data assert data['content'] == default_comment_content assert 'geojson' in data assert data['geojson'] == get_geojson() # Check that the comment is available in the comment endpoint now new_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in new_comment_list: new_comment_list = new_comment_list['results'] assert len(new_comment_list) == len(old_comment_list) + 1 new_comment = [c for c in new_comment_list if c["id"] == data["id"]][0] assert_common_keys_equal(new_comment, comment_data) assert new_comment["is_registered"] == True assert new_comment["author_name"] is None
def test_closure_info_visibility(api_client, closure_info_section, get_sections_url): hearing = closure_info_section.hearing # hearing closed, closure info section should be in results hearing.close_at = now() - datetime.timedelta(days=1) hearing.save() # check sections field in the hearing response = api_client.get(get_hearing_detail_url(hearing.id)) data = get_data_from_response(response) assert_id_in_results(closure_info_section.id, data['sections']) # check nested and root level sections endpoint response = api_client.get(get_sections_url(hearing)) data = get_results_from_response(response) assert_id_in_results(closure_info_section.id, data) # hearing open, closure info section should not be in results hearing.close_at = now() + datetime.timedelta(days=1) hearing.save() # check sections field in the hearing response = api_client.get(get_hearing_detail_url(hearing.id)) data = get_data_from_response(response) assert_id_in_results(closure_info_section.id, data['sections'], False) # check nested and root level sections endpoint response = api_client.get(get_sections_url(hearing)) data = get_results_from_response(response) assert_id_in_results(closure_info_section.id, data, False)
def test_PUT_hearing_delete_sections(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) closure_section_id = data['sections'][0]['id'] part_section_id = data['sections'][2]['id'] image_id = data['sections'][2]['images'][0]['id'] created_at = data['created_at'] data['sections'] = [ data['sections'][1], ] response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') updated_data = get_data_from_response(response, status_code=200) assert updated_data['created_at'] == created_at assert_hearing_equals(data, updated_data, john_smith_api_client.user, create=False) closure_section = Section.objects.deleted().filter( id=closure_section_id).first() part_section = Section.objects.deleted().filter(id=part_section_id).first() image = SectionImage.objects.deleted().filter(id=image_id).first() assert closure_section assert part_section assert image
def test_root_endpoint_filters(api_client, default_hearing, random_hearing): url = '/v1/comment/' section = default_hearing.sections.first() for i, comment in enumerate(section.comments.all()): comment.authorization_code = 'auth_code_%s' % i label = Label.objects.create(label='Label_%s') comment.label = label comment.save(update_fields=('authorization_code', 'label')) response = api_client.get( '%s?authorization_code=%s' % (url, section.comments.first().authorization_code)) response_data = get_data_from_response(response) assert len(response_data['results']) == 1 response = api_client.get('%s?section=%s' % (url, section.id)) response_data = get_data_from_response(response) assert len(response_data['results']) == 3 response = api_client.get('%s?hearing=%s' % (url, default_hearing.id)) response_data = get_data_from_response(response) assert len(response_data['results']) == 9 response = api_client.get('%s?label=%s' % (url, section.comments.first().label.pk)) response_data = get_data_from_response(response) assert len(response_data['results']) == 1
def test_comment_edit_auth(john_doe_api_client, jane_doe_api_client, api_client, random_hearing): random_hearing.commenting = Commenting.OPEN random_hearing.save() # John posts an innocuous comment: johns_message = "Hi! I'm John!" response = john_doe_api_client.post('/v1/hearing/%s/comments/' % random_hearing.pk, data={"content": johns_message}) data = get_data_from_response(response, 201) comment_id = data["id"] # Now Jane (in the guise of Mallory) attempts a rogue edit: response = jane_doe_api_client.patch('/v1/hearing/%s/comments/%s/' % (random_hearing.pk, comment_id), data={"content": "hOI! I'M TEMMIE"}) # But her attempts are foiled! data = get_data_from_response(response, 403) assert HearingComment.objects.get(pk=comment_id).content == johns_message # Jane, thinking she can bamboozle our authentication by logging out, tries again! response = api_client.patch('/v1/hearing/%s/comments/%s/' % (random_hearing.pk, comment_id), data={"content": "I'm totally John"}) # But still, no! data = get_data_from_response(response, 403) assert HearingComment.objects.get(pk=comment_id).content == johns_message
def test_section_images_ordering(api_client, default_hearing): """ Check images order matches ordering-field """ section_images = default_hearing.sections.first().images.all() # Test some initial order ordered_image_names = list(IMAGES.values()) set_images_ordering(section_images, ordered_image_names) data = get_data_from_response( api_client.get(get_hearing_detail_url(default_hearing.id, 'sections'))) first_section_data = data[0] assert [ im['title'][default_lang_code] for im in first_section_data['images'] ] == ordered_image_names # Test same order reversed reversed_image_names = list(reversed(ordered_image_names)) set_images_ordering(section_images, reversed_image_names) data = get_data_from_response( api_client.get(get_hearing_detail_url(default_hearing.id, 'sections'))) first_section_data = data[0] assert [ im['title'][default_lang_code] for im in first_section_data['images'] ] == reversed_image_names
def test_unpublished_section_images_excluded(client, expected, request, default_hearing): api_client = request.getfuncargvalue(client) image = default_hearing.get_main_section().images.first() image.published = False image.save(update_fields=('published',)) image = default_hearing.sections.all()[2].images.get(title=IMAGES['ORIGINAL']) image.published = False image.save(update_fields=('published',)) # /v1/hearing/<id>/ main image field response = api_client.get(get_hearing_detail_url(default_hearing.id)) main_image = get_data_from_response(response)['main_image'] if expected: assert main_image['title'] == default_hearing.get_main_section().images.first().title else: assert main_image is None # /v1/hearing/<id>/ section images field image_set_1 = get_data_from_response(response)['sections'][2]['images'] # /v1/hearing/<id>/section/ images field response = api_client.get(get_hearing_detail_url(default_hearing.id, 'sections')) image_set_2 = get_data_from_response(response)[2]['images'] response = api_client.get('/v1/image/?section=%s' % default_hearing.sections.all()[2].id) image_set_3 = get_data_from_response(response)['results'] for image_set in (image_set_1, image_set_2, image_set_3): assert (IMAGES['ORIGINAL'] in [image['title'] for image in image_set]) is expected
def test_PATCH_hearing_unsupported_language(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) patch_data = {"title": {"fr": data["title"]["en"]}} response = john_smith_api_client.patch('%s%s/' % (endpoint, data['id']), data=patch_data, format='json') data = get_data_from_response(response, status_code=400) assert "fr is not a supported languages (['en', 'fi', 'sv'])" in data['title']
def test_get_next_closing_and_open_hearings(api_client): create_hearings(0) # Clear out old hearings closed_hearing_1 = Hearing.objects.create(title='Gone', close_at=now() - datetime.timedelta(days=1)) closed_hearing_2 = Hearing.objects.create(title='Gone too', close_at=now() - datetime.timedelta(days=2)) future_hearing_1 = Hearing.objects.create(title='Next up', close_at=now() + datetime.timedelta(days=1)) future_hearing_2 = Hearing.objects.create(title='Next up', close_at=now() + datetime.timedelta(days=5)) response = api_client.get(list_endpoint, {"next_closing": now().isoformat()}) data = get_data_from_response(response) assert len(data['results']) == 1 assert data['results'][0]['title'] == future_hearing_1.title response = api_client.get( list_endpoint, {"next_closing": future_hearing_1.close_at.isoformat()}) data = get_data_from_response(response) assert len(data['results']) == 1 assert data['results'][0]['title'] == future_hearing_2.title response = api_client.get(list_endpoint, {"open": 'true'}) data = get_data_from_response(response) assert len(data['results']) == 2 assert data['results'][0]['title'].startswith('Next') assert data['results'][1]['title'].startswith('Next') response = api_client.get(list_endpoint, {"open": 'false'}) data = get_data_from_response(response) assert len(data['results']) == 2 assert data['results'][0]['title'].startswith('Gone') assert data['results'][1]['title'].startswith('Gone')
def test_PUT_hearing_anonymous_user(valid_hearing_json, api_client, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) _update_hearing_data(data) response = api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') data = get_data_from_response(response, status_code=401) assert data == {'detail': 'Authentication credentials were not provided.'}
def test_PATCH_hearing_untranslated(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) patch_data = {"title": data["title"]["en"]} response = john_smith_api_client.patch('%s%s/' % (endpoint, data['id']), data=patch_data, format='json') updated_data = get_data_from_response(response, status_code=400) assert 'Not a valid translation format. Expecting {"lang_code": %s}' % patch_data['title'] in updated_data['title']
def test_unpublished_section_images_excluded(client, expected, request, default_hearing): api_client = request.getfuncargvalue(client) image = default_hearing.get_main_section().images.first() image.published = False image.save(update_fields=('published',)) image = default_hearing.sections.all()[2].images.get(translations__title=IMAGES['ORIGINAL']) image.published = False image.save(update_fields=('published',)) # /v1/hearing/<id>/ main image field response = api_client.get(get_hearing_detail_url(default_hearing.id)) main_image = get_data_from_response(response)['main_image'] if expected: assert main_image['title'][default_lang_code] == default_hearing.get_main_section().images.first().title else: assert main_image is None # /v1/hearing/<id>/ section images field image_set_1 = get_data_from_response(response)['sections'][2]['images'] # /v1/hearing/<id>/section/ images field response = api_client.get(get_hearing_detail_url(default_hearing.id, 'sections')) image_set_2 = get_data_from_response(response)[2]['images'] response = api_client.get('/v1/image/?section=%s' % default_hearing.sections.all()[2].id) image_set_3 = get_data_from_response(response)['results'] for image_set in (image_set_1, image_set_2, image_set_3): assert (IMAGES['ORIGINAL'] in [image['title'][default_lang_code] for image in image_set]) is expected
def test_56_add_comment_to_section_test_geojson(john_doe_api_client, default_hearing, get_comments_url_and_data): section = default_hearing.sections.first() url, data = get_comments_url_and_data(default_hearing, section) old_comment_list = get_data_from_response( john_doe_api_client.get(url, {'format': 'geojson'})) # set section explicitly comment_data = get_comment_data(section=section.pk) response = john_doe_api_client.post(url, data=comment_data, format='json') data = get_data_from_response(response, status_code=201) # Check that the comment is available in the comment endpoint now new_comment_list = get_data_from_response( john_doe_api_client.get(url, {'format': 'geojson'})) assert len( new_comment_list["features"]) == len(old_comment_list["features"]) + 1 new_comment = [ c for c in new_comment_list["features"] if c["id"] == data["id"] ][0] assert_common_keys_equal(new_comment["geometry"], comment_data["geojson"]["geometry"]) assert_common_keys_equal(new_comment["properties"], comment_data["geojson"]["properties"])
def test_POST_image_root_endpoint(john_smith_api_client, default_hearing): # Check original image count data = get_data_from_response(john_smith_api_client.get('/v1/image/')) assert len(data['results']) == 9 # Get some section data = get_data_from_response( john_smith_api_client.get( get_hearing_detail_url(default_hearing.id, 'sections'))) first_section = data[0] # POST new image to the section post_data = sectionimage_test_json() post_data['section'] = first_section['id'] data = get_data_from_response(john_smith_api_client.post('/v1/image/', data=post_data, format='json'), status_code=201) # Save order of the newly created image ordering = data['ordering'] # Make sure new image was created data = get_data_from_response(john_smith_api_client.get('/v1/image/')) assert len(data['results']) == 10 # Create another image and make sure it gets higher ordering than the last one data = get_data_from_response(john_smith_api_client.post('/v1/image/', data=post_data, format='json'), status_code=201) assert data['ordering'] == ordering + 1
def test_update_poll_having_answers(valid_hearing_json_with_poll, john_doe_api_client, john_smith_api_client): valid_hearing_json_with_poll['close_at'] = datetime.datetime.now( ) + datetime.timedelta(days=5) valid_hearing_json_with_poll['sections'][0]['commenting'] = 'open' response = john_smith_api_client.post('/v1/hearing/', data=valid_hearing_json_with_poll, format='json') data = get_data_from_response(response, status_code=201) hearing_id = data['id'] section_id = data['sections'][0]['id'] poll_id = data['sections'][0]['questions'][0]['id'] option_id = data['sections'][0]['questions'][0]['options'][0]['id'] data = get_comment_data() data['answers'] = [{ 'question': poll_id, 'type': SectionPoll.TYPE_MULTIPLE_CHOICE, 'answers': [option_id] }] comment_response = john_doe_api_client.post( '/v1/hearing/%s/sections/%s/comments/' % (hearing_id, section_id), data=data) assert comment_response.status_code == 201 # Edit question data = get_data_from_response(response, status_code=201) data['sections'][0]['questions'][0]['text']['en'] = 'Edited question' update_response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') assert update_response.status_code == 400 # Edit option data = get_data_from_response(response, status_code=201) data['sections'][0]['questions'][0]['options'][0]['text'][ 'en'] = 'Edited option' update_response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') assert update_response.status_code == 400 # Add option data = get_data_from_response(response, status_code=201) new_option = deepcopy(data['sections'][0]['questions'][0]['options'][0]) data['sections'][0]['questions'][0]['options'].append(new_option) update_response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') assert update_response.status_code == 400 # Remove option data = get_data_from_response(response, status_code=201) del data['sections'][0]['questions'][0]['options'][1] update_response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') assert update_response.status_code == 400
def test_PUT_hearing_unsupported_language(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) _update_hearing_data(data) data["title"]["fr"] = data["title"]["en"] response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') data = get_data_from_response(response, status_code=400) assert "fr is not a supported languages (['en', 'fi', 'sv'])" in data['title']
def test_get_images_root_endpoint(api_client, default_hearing): data = get_data_from_response(api_client.get('/v1/image/')) assert len(data['results']) == 9 data = get_data_from_response( api_client.get('/v1/image/?section=%s' % default_hearing.sections.first().id)) check_entity_images(data['results'], False)
def test_GET_unpublished_hearing_regular_user(unpublished_hearing_json, john_smith_api_client, john_doe_api_client): response = john_smith_api_client.post(endpoint, data=unpublished_hearing_json, format='json') data = get_data_from_response(response, status_code=201) response = john_doe_api_client.get('%s%s/' % (endpoint, data['id']), format='json') data = get_data_from_response(response, status_code=404) response = john_doe_api_client.get(endpoint, format='json') data = get_data_from_response(response, status_code=200) assert data['count'] == 0
def test_POST_image_root_endpoint_wrong_user(john_doe_api_client, default_hearing): # Get some section data = get_data_from_response(john_doe_api_client.get(get_hearing_detail_url(default_hearing.id, 'sections'))) first_section = data[0] # POST new image to the section post_data = sectionimage_test_json() post_data['section'] = first_section['id'] data = get_data_from_response(john_doe_api_client.post('/v1/image/', data=post_data, format='json'), status_code=403)
def test_can_see_unpublished_with_preview_code(api_client): hearings = create_hearings(1) unpublished_hearing = hearings[0] unpublished_hearing.published = False unpublished_hearing.save() get_data_from_response(api_client.get(get_detail_url(unpublished_hearing.id)), status_code=404) preview_url = "{}?preview={}".format(get_detail_url(unpublished_hearing.id), unpublished_hearing.preview_code) get_data_from_response(api_client.get(preview_url), status_code=200)
def test_PATCH_hearing_patch_language(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) patch_data = {"title": {"fi": data["title"]["en"]}} response = john_smith_api_client.patch('%s%s/' % (endpoint, data['id']), data=patch_data, format='json') data = get_data_from_response(response, status_code=200) assert data["title"]["fi"] == valid_hearing_json["title"]["en"] assert data["title"]["en"] == valid_hearing_json["title"]["en"] assert data["title"]["sv"] == valid_hearing_json["title"]["sv"]
def test_PATCH_hearing_update_section(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) response = john_smith_api_client.patch('%s%s/' % (endpoint, data['id']), data={'sections': [{ 'id': '3adn7MGkOJ8e4NlhsElxKggbfdmrSmVE', 'title': 'New title', }]}, format='json') data = get_data_from_response(response, status_code=400) assert 'Sections cannot be updated by PATCHing the Hearing' in data['sections']
def test_PUT_hearing(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) created_at = data['created_at'] _update_hearing_data(data) response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') updated_data = get_data_from_response(response, status_code=200) assert updated_data['created_at'] == created_at assert_hearing_equals(data, updated_data, john_smith_api_client.user, create=False)
def test_PATCH_hearing(valid_hearing_json, john_smith_api_client): valid_hearing_json['close_at'] = datetime.datetime.now() + datetime.timedelta(days=1) response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) assert data['closed'] == False before = datetime.datetime.now() - datetime.timedelta(days=1) response = john_smith_api_client.patch('%s%s/' % (endpoint, data['id']), data={'close_at': before}, format='json') data = get_data_from_response(response, status_code=200) assert data['closed'] == True
def test_root_endpoint_filters(api_client, default_hearing, random_hearing): url = '/v1/section/' response = api_client.get('%s?hearing=%s' % (url, default_hearing.id)) response_data = get_data_from_response(response) assert len(response_data['results']) == 3 response = api_client.get('%s?hearing=%s&type=%s' % (url, default_hearing.id, 'main')) response_data = get_data_from_response(response) assert len(response_data['results']) == 1
def test_PUT_hearing_other_organization_hearing(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) hearing = Hearing.objects.first() hearing.organization = Organization.objects.create(name='The department for squirrel warfare') hearing.save() _update_hearing_data(data) response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') data = get_data_from_response(response, status_code=403) assert data == {'detail': "User cannot update hearings from different organizations."}
def test_PUT_hearing_no_organization(valid_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) _update_hearing_data(data) hearing = Hearing.objects.filter(id=data['id']).first() hearing.organization = None hearing.save() response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') data = get_data_from_response(response, status_code=403) assert data == {'detail': "User cannot update hearings from different organizations."}
def test_PUT_hearing_steal_section(valid_hearing_json, john_smith_api_client, default_hearing): hearing = default_hearing hearing.save() other_hearing_section_id = hearing.get_main_section().id response = john_smith_api_client.post(endpoint, data=valid_hearing_json, format='json') data = get_data_from_response(response, status_code=201) data['sections'][0]['id'] = other_hearing_section_id response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') updated_data = get_data_from_response(response, status_code=400) assert ('The Hearing does not have a section with ID %s' % other_hearing_section_id) in updated_data['sections']
def test_GET_unpublished_hearing_other_org(unpublished_hearing_json, john_smith_api_client, john_doe_api_client): response = john_smith_api_client.post(endpoint, data=unpublished_hearing_json, format='json') data = get_data_from_response(response, status_code=201) hearing = Hearing.objects.filter(id=data['id']).first() hearing.organization = Organization.objects.create(name='The department for squirrel warfare') hearing.save() response = john_smith_api_client.get('%s%s/' % (endpoint, data['id']), format='json') data = get_data_from_response(response, status_code=404) response = john_smith_api_client.get(endpoint, format='json') data = get_data_from_response(response, status_code=200) assert data['count'] == 0
def test_GET_unpublished_hearing(unpublished_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=unpublished_hearing_json, format='json') data_created = get_data_from_response(response, status_code=201) assert data_created['published'] == False response = john_smith_api_client.get('%s%s/' % (endpoint, data_created['id']), format='json') data = get_data_from_response(response, status_code=200) response = john_smith_api_client.get(endpoint, format='json') list_data = get_data_from_response(response, status_code=200) assert list_data['count'] == 1 assert list_data['results'][0]['id'] == data['id'] assert_hearing_equals(data_created, data, john_smith_api_client.user)
def test_GET_unpublished_hearing_no_organization(unpublished_hearing_json, john_smith_api_client): response = john_smith_api_client.post(endpoint, data=unpublished_hearing_json, format='json') data = get_data_from_response(response, status_code=201) hearing = Hearing.objects.filter(id=data['id']).first() hearing.organization = None hearing.save() response = john_smith_api_client.get('%s%s/' % (endpoint, data['id']), format='json') data = get_data_from_response(response, status_code=404) response = john_smith_api_client.get(endpoint, format='json') data = get_data_from_response(response, status_code=200) assert data['count'] == 0
def test_abstract_is_populated_from_main_abstract(api_client, default_hearing): main_section = default_hearing.get_main_section() main_section.abstract = 'very abstract' main_section.save(update_fields=('abstract',)) response = api_client.get(list_endpoint) data = get_data_from_response(response) assert data['results'][0]['abstract'] == 'very abstract' response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert data['abstract'] == 'very abstract'
def test_admin_can_see_unpublished(api_client, john_doe_api_client, admin_api_client): hearings = create_hearings(3) unpublished_hearing = hearings[0] unpublished_hearing.published = False unpublished_hearing.save() data = get_data_from_response(api_client.get(list_endpoint)) assert len(data['results']) == 2 # Can't see it as anon data = get_data_from_response(john_doe_api_client.get(list_endpoint)) assert len(data['results']) == 2 # Can't see it as registered data = get_data_from_response(admin_api_client.get(list_endpoint)) assert len(data['results']) == 3 # Can see it as admin assert len([1 for h in data['results'] if not h["published"]]) == 1 # Only one unpublished, yeah?
def test_access_hearing_using_slug(api_client, default_hearing): default_hearing.slug = 'new-slug' default_hearing.save() endpoint = list_endpoint + 'new-slug/' data = get_data_from_response(api_client.get(endpoint)) assert data['id'] == default_hearing.id endpoint += 'sections/' get_data_from_response(api_client.get(endpoint)) endpoint += '%s/' % default_hearing.sections.all()[0].id get_data_from_response(api_client.get(endpoint))
def test_get_next_closing_hearings(api_client): create_hearings(0) # Clear out old hearings closed_hearing_1 = Hearing.objects.create(title='Gone', close_at=now() - datetime.timedelta(days=1)) closed_hearing_2 = Hearing.objects.create(title='Gone too', close_at=now() - datetime.timedelta(days=2)) future_hearing_1 = Hearing.objects.create(title='Next up', close_at=now() + datetime.timedelta(days=1)) future_hearing_2 = Hearing.objects.create(title='Next up', close_at=now() + datetime.timedelta(days=5)) response = api_client.get(list_endpoint, {"next_closing": now().isoformat()}) data = get_data_from_response(response) assert len(data['results']) == 1 assert data['results'][0]['title'] == future_hearing_1.title response = api_client.get(list_endpoint, {"next_closing": future_hearing_1.close_at.isoformat()}) data = get_data_from_response(response) assert len(data['results']) == 1 assert data['results'][0]['title'] == future_hearing_2.title
def test_56_add_comment_to_section_with_no_geometry_in_geojson(john_doe_api_client, default_hearing, get_comments_url_and_data): section = default_hearing.sections.first() url, data = get_comments_url_and_data(default_hearing, section) old_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in old_comment_list: old_comment_list = old_comment_list['results'] # set section and invalid geojson explicitly comment_data = get_comment_data(section=section.pk, geojson={'hello': 'world'}) response = john_doe_api_client.post(url, data=comment_data, format='json') data = get_data_from_response(response, status_code=400) assert data["geojson"][0] == 'Invalid geojson format. "geometry" field is required. Got {\'hello\': \'world\'}'
def test_comment_edit_versioning(john_doe_api_client, default_hearing, lookup_field): url = get_main_comments_url(default_hearing, lookup_field) response = john_doe_api_client.post(url, data={"content": "THIS SERVICE SUCKS"}) data = get_data_from_response(response, 201) comment_id = data["id"] comment = SectionComment.objects.get(pk=comment_id) assert comment.content.isupper() # Oh my, all that screaming :( assert not revisions.get_for_object(comment) # No revisions response = john_doe_api_client.patch('%s%s/' % (url, comment_id), data={ "content": "Never mind, it's nice :)" }) data = get_data_from_response(response, 200) comment = SectionComment.objects.get(pk=comment_id) assert not comment.content.isupper() # Screaming is gone assert len(revisions.get_for_object(comment)) == 1 # One old revision
def test_56_add_comment_with_inexistant_label(john_doe_api_client, default_hearing, get_comments_url_and_data): section = default_hearing.sections.first() url, data = get_comments_url_and_data(default_hearing, section) old_comment_list = get_data_from_response(john_doe_api_client.get(url)) # If pagination is used the actual data is in "results" if 'results' in old_comment_list: old_comment_list = old_comment_list['results'] # set section explicitly comment_data = get_comment_data(section=section.pk, label={'id': 1}) response = john_doe_api_client.post(url, data=comment_data, format='json') data = get_data_from_response(response, status_code=400) assert data['label'][0] == 'Invalid pk "1" - object does not exist.'