def access_control_page(client, page_name, is_control_associated_with_user, profile_type):
    control = factories.ControlFactory()
    if is_control_associated_with_user:
        user = utils.make_user(profile_type, control)
    else:
        user = utils.make_user(profile_type, None)

    utils.login(client, user=user)
    url = reverse(page_name, args=[control.id])
    response = client.get(url)
    return response
def access_questionnaire_page(
        client, page_name, is_control_associated_with_user,
        profile_type, is_draft=False, assign_questionnaire_editor=True):
    questionnaire = factories.QuestionnaireFactory(is_draft=is_draft)
    control = questionnaire.control
    if is_control_associated_with_user:
        user = utils.make_user(
            profile_type, control, assign_questionnaire_editor=assign_questionnaire_editor)
    else:
        user = utils.make_user(profile_type, None)

    utils.login(client, user=user)
    url = reverse(page_name, args=[questionnaire.id])
    response = client.get(url)
    return response
def test_can_access_questionnaire_edit_page_if_control_is_associated_with_the_inspector_user_and_user_is_editor(client):
    # Create questionnaire through API so that the editor is set properly.
    control = ControlFactory()
    user = utils.make_user(UserProfile.INSPECTOR, control)
    payload = test_api_questionnaire.make_create_payload(control.id)
    create_response = test_api_questionnaire.create_questionnaire(user, payload)
    assert create_response.status_code == 201
    questionnaire_id = create_response.data['id']

    utils.login(client, user=user)
    url = reverse('questionnaire-edit', args=[questionnaire_id])
    response = client.get(url)

    assert response.status_code == 200