def test_56_get_hearing_with_section_check_n_comments_property(api_client): hearing = Hearing.objects.create( title='Test Hearing', abstract='Hearing to test section comments', commenting=Commenting.OPEN, 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 = get_hearing_detail_url(hearing.id, 'sections/%s/comments' % section.id) comment_data = get_comment_data(section=section.pk) response = api_client.post(url, data=comment_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 test_56_get_hearing_with_section_check_n_comments_property(api_client): hearing = Hearing.objects.create( title='Test Hearing', abstract='Hearing to test section comments', commenting=Commenting.OPEN, 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 = get_hearing_detail_url(hearing.id, 'sections/%s/comments' % section.id) comment_data = get_comment_data(section=section.pk) response = api_client.post(url, data=comment_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 test_commenting_modes(api_client, john_doe_api_client, commenting): hearing = Hearing.objects.create( open_at=now() - datetime.timedelta(days=1), close_at=now() + datetime.timedelta(days=1), commenting=commenting ) anon_status, reg_status = comment_status_spec[commenting] response = api_client.post(get_hearing_detail_url(hearing.id, 'comments'), data=get_comment_data()) assert response.status_code == anon_status response = john_doe_api_client.post(get_hearing_detail_url(hearing.id, 'comments'), data=get_comment_data()) assert response.status_code == reg_status
def test_commenting_modes(api_client, john_doe_api_client, commenting): hearing = Hearing.objects.create( open_at=now() - datetime.timedelta(days=1), close_at=now() + datetime.timedelta(days=1), commenting=commenting) anon_status, reg_status = comment_status_spec[commenting] response = api_client.post(get_hearing_detail_url(hearing.id, 'comments'), data=get_comment_data()) assert response.status_code == anon_status response = john_doe_api_client.post(get_hearing_detail_url( hearing.id, 'comments'), data=get_comment_data()) assert response.status_code == reg_status
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_54_list_all_comments_added_to_hearing_check_votes(api_client, default_hearing): # list all comments response = api_client.get(get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) for comment in data: assert comment['n_votes'] == 0
def test_56_add_comment_to_section_invalid_key(api_client, default_hearing): section = default_hearing.sections.first() url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id) response = api_client.post(url, data={'invalidKey': 'Yes it is'}) # expect bad request, we have invalid key in payload assert response.status_code == 400
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_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( 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.last() 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_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.last() 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_54_get_hearing_with_comments_check_amount_of_comments( api_client, default_hearing): response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert 'comments' in data assert len(data['comments']) == 3 assert data['n_comments'] == 3
def test_56_add_comment_to_section_without_data(api_client, default_hearing): section = default_hearing.sections.first() url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id) response = api_client.post(url, data=None) # expect bad request, we didn't set any data assert response.status_code == 400
def test_55_add_comment_to_hearing(john_doe, john_doe_api_client, default_hearing): # post data to hearing ednpoint /v1/hearings/<hearingID>/comments/ response = john_doe_api_client.post(get_hearing_detail_url(default_hearing.id, 'comments'), data=get_comment_data()) data = get_data_from_response(response, status_code=201) assert data['created_by']['username'] == john_doe.username assert data['author_name'] == john_doe.username assert data['content'] == default_comment_content assert data['n_votes'] == 0
def test_56_add_comment_to_section_without_authentication( api_client, default_hearing): section = default_hearing.sections.first() # post data to section endpoint /v1/hearing/<hearingID>/sections/<sectionID>/comments/ url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id) response = api_client.post(url, data=get_comment_data()) assert response.status_code == 201
def test_54_list_all_comments_added_to_hearing_check_amount( api_client, default_hearing): # list all comments response = api_client.get( get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) assert len(data) == 3
def test_54_list_all_comments_added_to_hearing_check_created_at( api_client, default_hearing): response = api_client.get( get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) for comment in data: assert_datetime_fuzzy_equal(now(), comment['created_at'])
def test_54_list_all_comments_added_to_hearing_check_created_by( api_client, default_hearing, john_doe): response = api_client.get( get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) for comment in data: assert comment['created_by']['username'] == john_doe.username
def test_54_list_all_comments_added_to_hearing_check_votes( api_client, default_hearing): # list all comments response = api_client.get( get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) for comment in data: assert comment['n_votes'] == 0
def test_54_list_all_comments_added_to_hearing_check_content(api_client, default_hearing): # list all comments response = api_client.get(get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) contents = [c['content'] for c in data] assert default_comment_content in contents assert red_comment_content in contents assert green_comment_content in contents
def test_55_add_comment_to_hearing(john_doe, john_doe_api_client, default_hearing): # post data to hearing ednpoint /v1/hearings/<hearingID>/comments/ response = john_doe_api_client.post(get_hearing_detail_url( default_hearing.id, 'comments'), data=get_comment_data()) data = get_data_from_response(response, status_code=201) assert data['created_by']['username'] == john_doe.username assert data['author_name'] == john_doe.username assert data['content'] == default_comment_content assert data['n_votes'] == 0
def test_54_list_all_comments_added_to_hearing_check_all_properties(api_client, default_hearing): # list all comments response = api_client.get(get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) # get first returned comment comment = data[0] assert 'content' in comment assert 'created_at' in comment assert 'n_votes' in comment assert 'created_by' in comment
def test_54_list_all_comments_added_to_hearing_check_content( api_client, default_hearing): # list all comments response = api_client.get( get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) contents = [c['content'] for c in data] assert default_comment_content in contents assert red_comment_content in contents assert green_comment_content in contents
def test_54_get_hearing_with_comments_check_comment_properties(api_client, default_hearing): response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert 'comments' in data # get first comment to check comment = data['comments'][0] assert 'content' in comment assert 'created_at' in comment assert 'n_votes' in comment assert 'created_by' in comment
def test_54_list_all_comments_added_to_hearing_check_all_properties( api_client, default_hearing): # list all comments response = api_client.get( get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) # get first returned comment comment = data[0] assert 'content' in comment assert 'created_at' in comment assert 'n_votes' in comment assert 'created_by' in comment
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) # 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
def test_54_get_hearing_with_comments_check_comment_properties( api_client, default_hearing): response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert 'comments' in data # get first comment to check comment = data['comments'][0] assert 'content' in comment assert 'created_at' in comment assert 'n_votes' in comment assert 'created_by' in comment
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_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_list_comments_with_auth_code(api_client, default_hearing): comment_data = get_comment_data(authorization_code="foo6") url = get_hearing_detail_url(default_hearing.id, 'comments') api_client.post(url, comment_data) data = get_data_from_response(api_client.get(url, {"authorization_code": "foo6"})) assert len(data) == 1
def get_section_comment_vote_url(hearing_id, section_id, comment_id): # /v1/hearings/<hearingID>/sections/<sectionID>/comments/<commentID>/votes/ return get_hearing_detail_url(hearing_id, 'sections/%s/comments/%s/vote' % (section_id, comment_id))
def test_add_auth_code_to_comment(api_client, default_hearing): comment_data = get_comment_data(authorization_code="foo6") response = api_client.post(get_hearing_detail_url(default_hearing.id, 'comments'), comment_data) response_data = get_data_from_response(response, status_code=201) assert comment_data["authorization_code"] ==\ Hearing.objects.get(pk=default_hearing.pk).comments.get(id=response_data["id"]).authorization_code
def get_section_comment_unvote_url(hearing_id, section_id, comment_id): # /v1/hearings/<hearingID>/sections/<sectionID>/comments/<commentID>/unvote/ return get_hearing_detail_url(hearing_id, "sections/%s/comments/%s/unvote" % (section_id, comment_id))
def test_54_list_all_comments_added_to_hearing_check_created_by(api_client, default_hearing, john_doe): response = api_client.get(get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) for comment in data: assert comment['created_by']['username'] == john_doe.username
def test_54_list_all_comments_added_to_hearing_check_created_at(api_client, default_hearing): response = api_client.get(get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) for comment in data: assert_datetime_fuzzy_equal(now(), comment['created_at'])
def test_54_get_hearing_with_comments_check_amount_of_comments(api_client, default_hearing): response = api_client.get(get_hearing_detail_url(default_hearing.id)) data = get_data_from_response(response) assert 'comments' in data assert len(data['comments']) == 3 assert data['n_comments'] == 12
def test_55_add_comment_without_authentication(api_client, default_hearing): # post data to hearing ednpoint /v1/hearings/<hearingID>/comments/ response = api_client.post(get_hearing_detail_url(default_hearing.id, 'comments'), data=get_comment_data()) assert response.status_code == 201
def test_54_list_all_comments_added_to_hearing_check_amount(api_client, default_hearing): # list all comments response = api_client.get(get_hearing_detail_url(default_hearing.id, 'comments')) data = get_data_from_response(response) assert len(data) == 3
def test_56_add_comment_to_section_without_authentication(api_client, default_hearing): section = default_hearing.sections.first() # post data to section endpoint /v1/hearing/<hearingID>/sections/<sectionID>/comments/ url = get_hearing_detail_url(default_hearing.id, 'sections/%s/comments' % section.id) response = api_client.post(url, data=get_comment_data()) assert response.status_code == 201
def get_hearing_comment_unvote_url(hearing_id, comment_id): # /v1/hearings/<hearingID>/comments/<commentID>/unvotes/ return get_hearing_detail_url(hearing_id, 'comments/%s/unvote' % comment_id)
def get_section_comment_unvote_url(hearing_id, section_id, comment_id): # /v1/hearings/<hearingID>/sections/<sectionID>/comments/<commentID>/unvote/ return get_hearing_detail_url( hearing_id, 'sections/%s/comments/%s/unvote' % (section_id, comment_id))