def test_inspector_cannot_update_theme_if_questionnaire_is_published(): theme = factories.ThemeFactory() questionnaire = theme.questionnaire questionnaire.is_draft = False questionnaire.save() user = utils.make_inspector_user(questionnaire.control) assert 400 <= update_theme(user, make_update_theme_payload(theme)).status_code < 500
def test_questionnaire_update__theme_update(): increment_ids() theme = factories.ThemeFactory() questionnaire = theme.questionnaire user = utils.make_inspector_user(questionnaire.control) payload = make_update_payload(questionnaire) payload['themes'][0]['title'] = 'this is a great theme.' assert Questionnaire.objects.all().count() == 1 assert Theme.objects.all().count() == 1 assert payload['themes'][0]['title'] != theme.title response = update_questionnaire(user, payload) assert response.status_code == 200 # data is saved assert Questionnaire.objects.all().count() == 1 saved_qr = Questionnaire.objects.get(id=questionnaire.id) assert saved_qr == questionnaire assert Theme.objects.all().count() == 1 saved_theme = Theme.objects.get(id=theme.id) assert saved_theme.title != theme.title assert saved_theme.title == payload['themes'][0]['title'] # Response data is filled in assert len(response.data['themes']) == 1 assert response.data['themes'][0]['title'] == payload['themes'][0]['title']
def run_test_questionnaire_update__theme_create(added_theme): increment_ids() theme = factories.ThemeFactory() questionnaire = theme.questionnaire user = utils.make_inspector_user(questionnaire.control) payload = make_update_payload(questionnaire) payload['themes'].append(added_theme) assert Questionnaire.objects.all().count() == 1 assert Theme.objects.all().count() == 1 response = update_questionnaire(user, payload) assert response.status_code == 200 # data is saved assert Questionnaire.objects.all().count() == 1 saved_qr = Questionnaire.objects.get(id=questionnaire.id) assert saved_qr == questionnaire assert Theme.objects.all().count() == 2 new_theme = Theme.objects.last() assert new_theme.title == payload['themes'][1]['title'] assert new_theme.questionnaire == saved_qr # Response data is filled in assert len(response.data['themes']) == 2 assert response.data['themes'][1]['title'] == payload['themes'][1]['title']
def test_send_response_file_list_does_not_contais_files_from_other_questionnaire( client): response_file_1 = factories.ResponseFileFactory(is_deleted=False) questionnaire_1 = response_file_1.question.theme.questionnaire questionnaire_1.is_draft = False questionnaire_1.save() assert not questionnaire_1.is_draft # Questionnaire 2 in same control as questionnaire 1 questionnaire_2 = factories.QuestionnaireFactory( control=questionnaire_1.control) questionnaire_2.is_draft = False questionnaire_2.save() assert not questionnaire_2.is_draft theme_2 = factories.ThemeFactory(questionnaire=questionnaire_2) question_2 = factories.QuestionFactory(theme=theme_2) response_file_2 = factories.ResponseFileFactory(is_deleted=False, question=question_2) user = utils.make_audited_user(questionnaire_1.control) files = get_files_for_export(questionnaire_1) assert len(files) == 1 assert files[0].file.name == response_file_1.file.name
def increment_ids(): # We create objects for nothing, to increment ids. Otherwise question.id = theme.id = qr.id = 1, and some errors # are not detected. for _ in range(5): factories.ThemeFactory() for _ in range(5): factories.QuestionnaireFactory() clear_saved_data()
def test_audited_cannot_delete_theme_from_draft_questionnaire(): theme = factories.ThemeFactory() audited_user = utils.make_audited_user(theme.questionnaire.control) theme.questionnaire.is_draft = True theme.questionnaire.save() assert Questionnaire.objects.get(id=theme.questionnaire.id).is_draft assert delete_theme(audited_user, theme.id).status_code == 403
def test_cannot_delete_theme_if_questionnaire_is_published(): theme = factories.ThemeFactory() audited_user = utils.make_audited_user(theme.questionnaire.control) inspector_user = utils.make_inspector_user(theme.questionnaire.control) theme.questionnaire.is_draft = False theme.questionnaire.save() assert delete_theme(audited_user, theme.id).status_code == 403 assert delete_theme(inspector_user, theme.id).status_code == 405
def test_cannot_retrieve_theme_even_if_user_belongs_to_control(): theme = factories.ThemeFactory() audited_user = utils.make_audited_user(theme.questionnaire.control) inspector_user = utils.make_inspector_user(theme.questionnaire.control) theme.questionnaire.is_draft = False theme.questionnaire.save() assert get_theme(audited_user, theme.id).status_code == 405 assert get_theme(inspector_user, theme.id).status_code == 405
def test_audited_cannot_update_theme(): theme = factories.ThemeFactory() user = utils.make_audited_user(theme.questionnaire.control) # Audited cannot update draft questionnaire theme.questionnaire.is_draft = True theme.questionnaire.save() assert 400 <= update_theme(user, make_update_theme_payload(theme)).status_code < 500 # Audited cannot update published questionnaire theme.questionnaire.is_draft = False theme.questionnaire.save() assert 400 <= update_theme(user, make_update_theme_payload(theme)).status_code < 500
def test_inspector_can_update_theme_order(): theme = factories.ThemeFactory() user = utils.make_inspector_user(theme.questionnaire.control) original_order = theme.order new_order = 123 assert new_order != original_order payload = { "id": str(theme.id), "order": str(new_order), "title": theme.title } response = utils.update_resource(client, user, 'theme', payload) assert response.status_code == 200 assert response.data['order'] == new_order
def test_inspector_has_no_access_to_theme_api_for_deleted_control(): theme = factories.ThemeFactory() user = utils.make_inspector_user(theme.questionnaire.control) theme.questionnaire.control.delete() assert update_theme(user, make_update_theme_payload(theme)).status_code == 404
def test_no_access_to_theme_api_for_anonymous(): theme = factories.ThemeFactory() response = utils.get_resource_without_login(client, 'theme', theme.id) assert response.status_code == 403
def test_inspector_cannot_update_theme_if_control_is_deleted(): theme = factories.ThemeFactory() control = theme.questionnaire.control control.delete() user = utils.make_inspector_user(control) assert 400 <= update_theme(user, make_update_theme_payload(theme)).status_code < 500
def test_can_access_theme_api_if_control_is_associated_with_the_user(): theme = factories.ThemeFactory() user = utils.make_audited_user(theme.questionnaire.control) assert get_theme(user, theme.id).status_code == 200
def test_no_update_to_theme_if_control_is_not_associated_with_the_user(): theme_in = factories.ThemeFactory() theme_out = factories.ThemeFactory() user = utils.make_inspector_user(theme_in.questionnaire.control) assert 400 <= update_theme(user, make_update_theme_payload(theme_out)).status_code < 500
def test_inspector_can_update_theme_if_control_is_associated_with_the_user(): theme = factories.ThemeFactory() user = utils.make_inspector_user(theme.questionnaire.control) assert update_theme(user, make_update_theme_payload(theme)).status_code == 200
def test_no_access_to_theme_api_if_control_is_not_associated_with_the_user(): theme_in = factories.ThemeFactory() theme_out = factories.ThemeFactory() user = utils.make_audited_user(theme_in.questionnaire.control) assert get_theme(user, theme_out.id).status_code != 200