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_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(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_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_45_get_hearing_with_one_section_check_content(api_client, default_hearing): sections = create_sections(default_hearing, 1) response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert data['sections'][0]['content'][default_lang_code] == sections[0].content
def test_37_list_hearing_images_check_titles(api_client, default_hearing): """ Check images exist in hearing image payloads """ data = get_data_from_response( api_client.get(get_hearing_detail_url(default_hearing.id, 'images'))) check_entity_images({"images": data})
def test_45_get_hearing_with_many_sections_check_amount(api_client, default_hearing): create_sections(default_hearing, 3) response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert len(data['sections']) == 3
def test_get_hearing_check_section_type(api_client, default_hearing): response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) introduction = data['sections'][0] assert introduction['type'] == InitialSectionType.INTRODUCTION assert introduction['type_name_singular'] == 'johdanto' assert introduction['type_name_plural'] == 'johdannot'
def test_get_hearing_check_section_type(api_client, default_hearing): response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) main = data['sections'][0] assert main['type'] == InitialSectionType.MAIN assert main['type_name_singular'] == 'pääosio' assert main['type_name_plural'] == 'pääosiot'
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_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_45_get_hearing_with_one_section_check_content(api_client, default_hearing): sections = create_sections(default_hearing, 1) response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert data['sections'][0]['content'] == sections[0].content
def test_38_get_section_with_images(api_client, default_hearing): """ Check images exist in section payloads """ data = get_data_from_response(api_client.get(get_hearing_detail_url(default_hearing.id, 'sections'))) first_section = data[0] check_entity_images(first_section)
def test_45_get_one_section_check_abstract(api_client, default_hearing): sections = create_sections(default_hearing, 1) response = api_client.get(get_hearing_detail_url(default_hearing.id, 'sections')) data = get_data_from_response(response) assert data[0]['abstract'] == sections[0].abstract
def test_45_get_one_section_check_amount(api_client, default_hearing): create_sections(default_hearing, 1) response = api_client.get(get_hearing_detail_url(default_hearing.id, 'sections')) data = get_data_from_response(response) assert len(data) == 1
def test_38_get_hearing_check_section_with_images(api_client, default_hearing): """ Check images exist in sections nested in hearing payloads """ data = get_data_from_response(api_client.get(get_hearing_detail_url(default_hearing.id))) assert 'sections' in data first_section = data['sections'][0] check_entity_images(first_section)
def test_45_get_one_section_check_amount(api_client, default_hearing): create_sections(default_hearing, 1) response = api_client.get( get_hearing_detail_url(default_hearing.id, 'sections')) data = get_data_from_response(response) assert len(data) == 1
def test_45_get_one_section_check_abstract(api_client, default_hearing): sections = create_sections(default_hearing, 1) response = api_client.get( get_hearing_detail_url(default_hearing.id, 'sections')) data = get_data_from_response(response) assert data[0]['abstract'] == sections[0].abstract
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_unpublished_section_files_excluded(client, expected, request, default_hearing): api_client = request.getfuncargvalue(client) file_obj = default_hearing.sections.all()[2].files.get(translations__title=FILES['TXT']) file_obj.published = False file_obj.save(update_fields=('published',)) # /v1/hearing/<id>/ section files field response = api_client.get(get_hearing_detail_url(default_hearing.id)) file_set_1 = get_data_from_response(response)['sections'][2]['files'] # /v1/hearing/<id>/section/ files field response = api_client.get(get_hearing_detail_url(default_hearing.id, 'sections')) file_set_2 = get_data_from_response(response)[2]['files'] for set_num, file_set in zip(range(1,3), (file_set_1, file_set_2)): assert (FILES['TXT'] in [file_obj['title'][default_lang_code] for file_obj in file_set]) is expected, "Set %d failed" % set_num
def test_8_get_detail_check_properties(api_client, default_hearing): response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert set(data.keys()) >= { 'abstract', 'borough', 'close_at', 'closed', 'created_at', 'id', 'labels', 'n_comments', 'open_at', 'sections', 'servicemap_url', 'title', 'organization' }
def test_45_get_hearing_with_one_section_check_fields(api_client, default_hearing): create_sections(default_hearing, 1) response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) section = data['sections'][0] assert all(key in section for key in SectionSerializer.Meta.fields)
def test_45_get_section_type(api_client, default_hearing): sections = create_sections(default_hearing, 1) response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert data['sections'][0]['type'] == InitialSectionType.PART assert data['sections'][0]['type_name_singular'] == 'osa-alue' assert data['sections'][0]['type_name_plural'] == 'osa-alueet'
def test_45_get_hearing_with_one_section_check_fields(api_client, default_hearing): create_sections(default_hearing, 1) response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) section = data['sections'][0] assert all( key in section for key in SectionSerializer.Meta.fields )
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_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_hearing_open_at_filtering(default_hearing, request, client, expected): api_client = request.getfuncargvalue(client) default_hearing.open_at = now() + datetime.timedelta(hours=1) default_hearing.save(update_fields=('open_at',)) response = api_client.get(list_endpoint) data = get_data_from_response(response) ids = [hearing['id'] for hearing in data['results']] assert bool(default_hearing.id in ids) == expected expected_code = (200 if expected else 404) response = api_client.get(get_hearing_detail_url(default_hearing.id)) assert response.status_code == expected_code
def test_45_get_hearing_with_many_sections_check_content(api_client, default_hearing): sections = create_sections(default_hearing, 3) response = api_client.get(get_hearing_detail_url(default_hearing.id, )) data = get_data_from_response(response) contents = [s['content'][default_lang_code] for s in data['sections']] # ensure we have 3 contents assert len(contents) == 3 assert sections[0].content in contents assert sections[1].content in contents assert sections[2].content in contents
def test_45_get_many_sections_check_abstract(api_client, default_hearing): sections = create_sections(default_hearing, 3) response = api_client.get(get_hearing_detail_url(default_hearing.id, 'sections')) data = get_data_from_response(response) abstracts = [s['abstract'] for s in data] # ensure we have 3 abstracts assert len(abstracts) == 3 assert sections[0].abstract in abstracts assert sections[1].abstract in abstracts assert sections[2].abstract in abstracts
def test_45_get_hearing_with_many_sections_check_content(api_client, default_hearing): sections = create_sections(default_hearing, 3) response = api_client.get(get_hearing_detail_url(default_hearing.id, )) data = get_data_from_response(response) contents = [s['content'] for s in data['sections']] # ensure we have 3 contents assert len(contents) == 3 assert sections[0].content in contents assert sections[1].content in contents assert sections[2].content in contents
def test_hearing_open_at_filtering(default_hearing, request, client, expected): api_client = request.getfuncargvalue(client) default_hearing.open_at = now() + datetime.timedelta(hours=1) default_hearing.save(update_fields=('open_at', )) response = api_client.get(list_endpoint) data = get_data_from_response(response) ids = [hearing['id'] for hearing in data['results']] assert bool(default_hearing.id in ids) == expected expected_code = (200 if expected else 404) response = api_client.get(get_hearing_detail_url(default_hearing.id)) assert response.status_code == expected_code
def test_45_get_hearing_with_many_sections_check_abstract( api_client, default_hearing): sections = create_sections(default_hearing, 3) response = api_client.get(get_hearing_detail_url(default_hearing.id, )) data = get_data_from_response(response) abstracts = [s['abstract'] for s in data['sections']] # ensure we have 3 abstracts assert len(abstracts) == 3 assert sections[0].abstract in abstracts assert sections[1].abstract in abstracts assert sections[2].abstract in abstracts
def test_PUT_file_json_section(john_smith_api_client, default_hearing): """ File with empty section should be able to be updated with section data """ # 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 file to the section post_data = sectionfile_base64_test_data() data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data), status_code=201) file_obj_id = data['id'] data['section'] = first_section['id'] data = get_data_from_response(john_smith_api_client.put('/v1/file/%s/' % file_obj_id, data=data), status_code=200) assert data['section'] == first_section['id'] assert data['hearing'] == default_hearing.pk
def test_do_not_get_plugin_data_for_comment(api_client, default_hearing): with override_settings(DEMOCRACY_PLUGINS={ "test_plugin": "democracy.tests.plug.TestPlugin" }): section = default_hearing.sections.first() 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") response = api_client.post(url, data=comment_data) response_data = get_data_from_response(response, status_code=201) comment_list = get_data_from_response(api_client.get(url)) created_comment = [ c for c in comment_list if c["id"] == response_data["id"] ][0] assert "plugin_data" not in created_comment
def test_PUT_file_multipart_section(john_smith_api_client, default_hearing): """ File with empty section should be able to be updated with section data """ # 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 file to the section post_data = sectionfile_multipart_test_data() with open(get_file_path(FILES['TXT']), 'rb') as fp: post_data['file'] = fp data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data, format='multipart'), status_code=201) file_obj_id = data['id'] put_data = sectionfile_multipart_test_data() put_data['section'] = first_section['id'] data = get_data_from_response(john_smith_api_client.put('/v1/file/%s/' % file_obj_id, data=put_data, format='multipart'), status_code=200) assert data['section'] == first_section['id'] assert data['hearing'] == default_hearing.pk
def test_get_plugin_data_for_comment(api_client, default_hearing): with override_settings( DEMOCRACY_PLUGINS={ "test_plugin": "democracy.tests.plug.TestPlugin" } ): section = default_hearing.sections.first() 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" ) response = api_client.post(url, data=comment_data) response_data = get_data_from_response(response, status_code=201) comment_list = get_data_from_response(api_client.get(url, {"include": "plugin_data"})) created_comment = [c for c in comment_list if c["id"] == response_data["id"]][0] assert created_comment["plugin_data"] == comment_data["plugin_data"][::-1] # The TestPlugin reverses data
def test_POST_file_base64_root_endpoint(john_smith_api_client, default_hearing): # Check original file count data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 3 # 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 file to the section post_data = sectionfile_base64_test_data() post_data['section'] = first_section['id'] data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data), status_code=201) # Save order of the newly created file ordering = data['ordering'] # Make sure new file was created data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 4 # Create another file and make sure it gets higher ordering than the last one data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data), status_code=201) assert data['ordering'] == ordering + 1
def test_POST_file_root_endpoint_empty_section(john_smith_api_client, default_hearing): """ File should be able to be uploaded without section data """ # Check original file count data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 3 # 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 file to the section post_data = sectionfile_multipart_test_data() with open(get_file_path(FILES['TXT']), 'rb') as fp: post_data['file'] = fp data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data, format='multipart'), status_code=201) assert data['section'] is None assert data['hearing'] is None # Make sure new file was created data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 4
def test_POST_file_multipart_root_endpoint(john_smith_api_client, default_hearing): # Check original file count data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 3 # 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 file to the section post_data = sectionfile_multipart_test_data() post_data['section'] = first_section['id'] with open(get_image_path(IMAGES['ORIGINAL']), 'rb') as fp: post_data['file'] = fp data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data, format='multipart'), status_code=201) # Save order of the newly created file ordering = data['ordering'] # Make sure new file was created data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 4 # Create another file and make sure it gets higher ordering than the last one fp.seek(0) data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data, format='multipart'), status_code=201) assert data['ordering'] == ordering + 1
def test_56_get_hearing_with_section_check_n_comments_property(api_client, get_comments_url_and_data): hearing = Hearing.objects.create( title='Test Hearing', open_at=now() - datetime.timedelta(days=1), close_at=now() + datetime.timedelta(days=1), ) section = Section.objects.create( title='Section to comment', hearing=hearing, commenting=Commenting.OPEN, type=SectionType.objects.get(identifier=InitialSectionType.PART) ) url, data = get_comments_url_and_data(hearing, section) response = api_client.post(url, data=data) assert response.status_code == 201, ("response was %s" % response.content) # get hearing and check sections's n_comments property url = get_hearing_detail_url(hearing.id) response = api_client.get(url) data = get_data_from_response(response) assert 'n_comments' in data['sections'][0] assert data['sections'][0]['n_comments'] == 1
def get_hearing_follow_url(hearing_id): # /v1/hearings/<hearingID>/follow/ return get_hearing_detail_url(hearing_id, "follow")
def test_8_list_hearing_images(api_client, default_hearing): data = get_data_from_response(api_client.get(get_hearing_detail_url(default_hearing.id))) check_entity_images(data)
def get_hearing_unfollow_url(hearing_id): # /v1/hearings/<hearingID>/unfollow/ return get_hearing_detail_url(hearing_id, 'unfollow')
def test_37_list_hearing_images_check_titles(api_client, default_hearing): """ Check images exist in hearing image payloads """ data = get_data_from_response(api_client.get(get_hearing_detail_url(default_hearing.id, 'images'))) check_entity_images({"images": data})