Example #1
0
def test_admin_users_can_preview_unpublished_aids(client, superuser):
    client.force_login(superuser)
    aid = AidFactory(status='draft')
    url = aid.get_absolute_url()
    res = client.get(url)
    assert res.status_code == 200
    assert 'Cette aide <strong>n\'est actuellement pas affichée sur le site</strong>.' in res.content.decode()  # noqa
def test_local_aid_is_listed(client, perimeters):
    generic = AidFactory(perimeter=perimeters['france'], is_generic=True)
    local = AidFactory(generic_aid=generic, perimeter=perimeters['occitanie'])
    url = reverse('search_view')
    res = client.get(url, data={'perimeter': perimeters['occitanie'].pk})
    assert res.status_code == 200
    assert local in res.context['aids']
def test_get_new_aids_with_no_matching_aids(yesterday):
    """Existing aids do not match."""

    alert = AlertFactory(querystring='text=Gloubiboulga')
    AidFactory.create_batch(5, name='Test', date_published=yesterday)
    aids = alert.get_new_aids()
    assert len(aids) == 0
def test_get_new_aids_with_no_old_aids(last_month):
    """Matching aids are older than the requested threshold."""

    alert = AlertFactory(querystring='text=test')
    AidFactory.create_batch(5, name='Test', date_published=last_month)
    aids = alert.get_new_aids()
    assert len(aids) == 0
def test_get_new_aids_with_matching_aids(yesterday):
    """Matching aids are found."""

    alert = AlertFactory(querystring='text=test')
    AidFactory.create_batch(5, name='Test', date_published=yesterday)
    aids = alert.get_new_aids()
    assert len(aids) == 5
def test_sorting_field(live_server, browser):
    """Test the dynamic sorting field."""

    yesterday = timezone.now() - timedelta(days=1)
    AidFactory(name='Gloubiboulga', date_published=yesterday)
    AidFactory(name='Schtroumpf')

    search_url = reverse('search_view')
    browser.get(live_server + search_url)
    results = browser.find_elements_by_css_selector('section.aid h1')

    assert len(results) == 2
    assert 'Gloubiboulga' in results[0].get_attribute('innerHTML')

    sort_input = browser.find_element_by_id('sorting-btn')
    input_content = sort_input.get_attribute('innerHTML')
    assert 'pertinence' in input_content

    sort_input.click()
    WebDriverWait(browser, 20) \
        .until(EC.element_to_be_clickable(
            (By.ID, "sort-publication-date"))) \
        .click()

    time.sleep(0.5)

    assert 'order_by=publication_date' in browser.current_url

    sorted_results = browser.find_elements_by_css_selector('section.aid h1')
    assert len(sorted_results) == 2
    assert 'Schtroumpf' in sorted_results[0].get_attribute('innerHTML')
Example #7
0
def test_aid_edition_with_new_tags(client, contributor, aid_form_data):
    """Aid form can create new tags."""

    aid = AidFactory(name='First title', author=contributor)
    form_url = reverse('aid_edit_view', args=[aid.slug])
    client.force_login(contributor)

    TagFactory(name='pizza')
    tags = Tag.objects.all()
    assert tags.count() == 1

    aid_form_data['tags'] = ['pizza', 'tartiflette', 'gratin']
    res = client.post(form_url, data=aid_form_data)
    assert res.status_code == 302

    aid.refresh_from_db()
    assert set(aid.tags) == set(['pizza', 'gratin', 'tartiflette'])

    aid_tags = aid._tags_m2m.values_list('name', flat=True)
    assert set(aid_tags) == set(['pizza', 'gratin', 'tartiflette'])

    all_tags = tags.values_list('name', flat=True)
    assert tags.count() == 3
    assert 'gratin' in all_tags
    assert 'tartiflette' in all_tags
def test_the_france_relance_boolean_filter(client, perimeters):
    AidFactory(name='Aide France Relance 1',
               perimeter=perimeters['europe'],
               in_france_relance=True)
    AidFactory(name='Aide France Relance 2',
               perimeter=perimeters['europe'],
               in_france_relance=True)
    AidFactory(name='Aide France Relance 3',
               perimeter=perimeters['europe'],
               in_france_relance=True)
    AidFactory(name='Aide diverse 4',
               perimeter=perimeters['europe'],
               in_france_relance=False)
    AidFactory(name='Aide diverse 5',
               perimeter=perimeters['europe'],
               in_france_relance=False)

    url = reverse('search_view')
    res = client.get(url)
    assert res.context['paginator'].count == 5

    # This filter is used to select FrRel aids
    res = client.get(url, data={'in_france_relance': 'true'})
    assert res.context['paginator'].count == 3
    assert res.context['aids'][0].name == 'Aide France Relance 1'
    assert res.context['aids'][1].name == 'Aide France Relance 2'
    assert res.context['aids'][2].name == 'Aide France Relance 3'

    # Any false value disables the filter
    res = client.get(url, data={'in_france_relance': 'false'})
    assert res.context['paginator'].count == 5
def test_aid_amendments_are_saved(client, aid_form_data):
    # At the beginning of the test, no amendments exist
    amendments = Aid.amendments.all()
    assert amendments.count() == 0

    aid = AidFactory(name='This is my aid name',
                     description='This is a mistake')
    amend_url = reverse('aid_amend_view', args=[aid.slug])
    aid_form_data.update({
        'name': 'This is a better name',
        'description': 'This is a correction',
        'amendment_author_name': 'Coco rection',
        'amendment_author_email': '*****@*****.**',
    })
    res = client.post(amend_url, data=aid_form_data)
    assert res.status_code == 302
    assert amendments.count() == 1

    # The amendment was successfully created
    amendment = amendments[0]
    assert amendment.name == 'This is a better name'
    assert amendment.description == 'This is a correction'
    assert amendment.author is None
    assert amendment.amended_aid == aid

    # The amended aid was NOT modified
    aid.refresh_from_db()
    assert aid.name == 'This is my aid name'
    assert aid.description == 'This is a mistake'
Example #10
0
def test_generic_aid_is_listed(client, perimeters):
    generic = AidFactory(perimeter=perimeters['france'])
    AidFactory(generic_aid=generic, perimeter=perimeters['occitanie'])
    url = reverse('search_view')
    res = client.get(url)
    assert res.status_code == 200
    assert generic in res.context['aids']
Example #11
0
def test_aid_edition_does_not_delete_tags(client, contributor, aid_form_data):
    """Unused tags stay in db."""

    TagFactory(name='pizza')
    TagFactory(name='gratin')

    aid = AidFactory(name='First title',
                     author=contributor,
                     tags=['pizza', 'gratin'])
    form_url = reverse('aid_edit_view', args=[aid.slug])
    client.force_login(contributor)

    tags = Tag.objects.all()
    assert tags.count() == 2

    aid_form_data['tags'] = ['pizza']
    res = client.post(form_url, data=aid_form_data)
    assert res.status_code == 302

    aid.refresh_from_db()
    assert set(aid.tags) == set(['pizza'])
    assert tags.count() == 2

    aid_tags = aid._tags_m2m.values_list('name', flat=True)
    assert set(aid_tags) == set(['pizza'])

    all_tags = tags.values_list('name', flat=True)
    assert set(all_tags) == set(['pizza', 'gratin'])
Example #12
0
def test_command_with_unvalidated_address(mailoutbox):
    AlertFactory(
        validated=False,
        querystring='text=Schtroumpf')
    AidFactory.create_batch(5, name='Schtroumpf')
    call_command('send_alerts')
    assert len(mailoutbox) == 0
def test_merging_amendment(client, superuser, aid_form_data):

    client.force_login(superuser)

    aid = AidFactory()
    amendment = AidFactory(name='Amended name',
                           description='Amended description',
                           is_amendment=True,
                           amended_aid=aid)
    merge_url = reverse('admin:aids_amendment_merge', args=[amendment.pk])
    res = client.get(merge_url)
    assert res.status_code == 200

    aid_form_data.update({
        'name': 'This is a better name',
        'description': 'This is a correction'
    })
    res = client.post(merge_url, data=aid_form_data)
    assert res.status_code == 302

    # The aid was successfully updated
    aid.refresh_from_db()
    assert aid.name == 'This is a better name'
    assert aid.description == 'This is a correction'

    # The amendment was deleted
    assert Aid.amendments.all().count() == 0
def test_aids_can_be_filterd_by_published_after(client, perimeters):
    AidFactory(name='Aide A',
               perimeter=perimeters['europe'],
               date_published=timezone.make_aware(datetime(2020, 9, 3)))
    AidFactory(name='Aide B',
               perimeter=perimeters['europe'],
               date_published=timezone.make_aware(datetime(2020, 9, 2)))
    AidFactory(name='Aide C',
               perimeter=perimeters['europe'],
               date_published=timezone.make_aware(datetime(2019, 1, 1)))

    url = reverse('search_view')
    res = client.get(url)
    assert res.context['paginator'].count == 3

    # This filter is used to select aids published after the latest_alert_date
    res = client.get(url, data={'published_after': '2020-09-03'})
    assert res.context['paginator'].count == 1
    assert res.context['aids'][0].name == 'Aide A'

    res = client.get(url, data={'published_after': '2020-09-01'})
    assert res.context['paginator'].count == 2
    assert res.context['aids'][0].name == 'Aide A'
    assert res.context['aids'][1].name == 'Aide B'

    # If published_after filter doesn't match any aids there isn't any result
    res = client.get(url, data={'published_after': '2020-09-04'})
    assert res.context['paginator'].count == 0
Example #15
0
def test_anonymous_can_see_expired_aids(client, past_week):
    aid = AidFactory(status='published', submission_deadline=past_week)
    url = aid.get_absolute_url()
    res = client.get(url)
    assert res.status_code == 200
    assert 'Cette aide n\'est <strong>plus disponible</strong>' in res.content.decode(
    )  # noqa
def test_duplicate_buster(client, live_server, browser, contributor):

    client.force_login(contributor)
    cookie = client.cookies['sessionid']
    browser.get(live_server.url)
    browser.add_cookie({
        'name': 'sessionid',
        'value': cookie.value,
        'secure': False,
        'path': '/'
    })

    AidFactory(name='Schtroumpf',
               status='published',
               recurrence='ongoing',
               author=contributor,
               origin_url='https://example.com/schtroumpf/')

    aid = AidFactory(name='Gloubiboulga',
                     status='published',
                     recurrence='ongoing',
                     author=contributor,
                     origin_url='https://example.com/schtroumpf/')

    edit_url = reverse('aid_edit_view', args=[aid.slug])
    browser.get(live_server + edit_url)

    time.sleep(2)
    # browser.save_screenshot('/tmp/django.png')

    errors = browser.find_elements_by_css_selector('div.duplicate-error')
    assert len(errors) == 2
    assert 'Schtroumpf' in errors[0].get_attribute('innerHTML')
Example #17
0
def test_api_default_version(client, api_url):
    AidFactory(name='First aid')
    AidFactory(name='Second aid')

    res = client.get(api_url)
    assert res.status_code == 200
    content = json.loads(res.content.decode())
    assert content['count'] == 2
Example #18
0
def test_api_version_1_1(client, api_url):
    AidFactory(name='First aid')
    AidFactory(name='Second aid')

    res = client.get(f'{api_url}?version=1.1')
    assert res.status_code == 200
    content = json.loads(res.content.decode())
    assert content['count'] == 2
    assert 'programs' in content['results'][0]
Example #19
0
def test_amendment_form_is_only_accessible_for_published_aids(client):
    aid = AidFactory(status='draft')
    amend_url = reverse('aid_amend_view', args=[aid.slug])
    res = client.get(amend_url)
    assert res.status_code == 404

    aid.status = 'reviewable'
    res = client.get(amend_url)
    assert res.status_code == 404
def test_get_local_aid_if_search_perimeter_matches(client, perimeters):
    generic = AidFactory(perimeter=perimeters['france'], is_generic=True)
    local = AidFactory(generic_aid=generic, perimeter=perimeters['occitanie'])
    url = reverse('search_view')
    res = client.get(url, data={'perimeter': perimeters['occitanie'].pk})
    assert res.status_code == 200
    # Searching on a matching perimeter: occitanie.
    # We expect to see the local aid, not it's generic version.
    assert generic not in res.context['aids']
    assert local in res.context['aids']
def test_get_generic_search_perimeter_is_wider(client, perimeters):
    generic = AidFactory(perimeter=perimeters['france'], is_generic=True)
    local = AidFactory(generic_aid=generic, perimeter=perimeters['occitanie'])
    url = reverse('search_view')
    res = client.get(url, data={'perimeter': perimeters['europe'].pk})
    assert res.status_code == 200
    # Searching on a wider perimeter: europe is wider than occitanie.
    # We expect to see the generic aid, not it's local version.
    assert generic in res.context['aids']
    assert local not in res.context['aids']
Example #22
0
def test_get_new_aids_with_unpublished_aids(yesterday):
    """Matching aids are not published."""

    alert = AlertFactory(querystring='text=test')
    AidFactory.create_batch(5,
                            name='Test',
                            date_published=yesterday,
                            status='draft')
    aids = alert.get_new_aids()
    assert len(aids) == 0
Example #23
0
def test_get_local_if_search_perimeter_is_smaller(client, perimeters):
    generic = AidFactory(perimeter=perimeters['france'])
    local = AidFactory(generic_aid=generic, perimeter=perimeters['occitanie'])
    url = reverse('search_view')
    res = client.get(url, data={'perimeter': perimeters['herault'].pk})
    assert res.status_code == 200
    # Searching on a small perimeter: herault is smaller than occitanie.
    # We expect to see the local aid, not it's generic version.
    assert generic not in res.context['aids']
    assert local in res.context['aids']
Example #24
0
def test_share_button_is_hidden_for_anonymous_users(client):

    AidFactory.create_batch(3)
    url = reverse('search_view')
    res = client.get(url)

    assert res.status_code == 200
    content = res.content.decode('utf-8')
    assert 'envoyer ces résultats par e-mail' in content
    assert '<form id="send-results-by-email-form" method="post"' not in content
Example #25
0
def test_only_aid_author_can_delete_it(client, contributor):
    """One cannot delete other users' aids."""

    aid = AidFactory(status='published')
    client.force_login(contributor)
    delete_url = reverse('aid_delete_view', args=[aid.slug])
    res = client.post(delete_url, {'confirm': True})
    assert res.status_code == 404

    aid.refresh_from_db()
    assert aid.status == 'published'
Example #26
0
def test_deletion_requires_confirmation(client, contributor):
    """Without confirmation, aid does not get deleted."""

    aid = AidFactory(status='published', author=contributor)
    client.force_login(contributor)
    delete_url = reverse('aid_delete_view', args=[aid.slug])
    res = client.post(delete_url)
    assert res.status_code == 302

    aid.refresh_from_db()
    assert aid.status == 'published'
Example #27
0
def test_aid_deletion(client, contributor):
    """Test aid deletion."""

    aid = AidFactory(status='published', author=contributor)
    client.force_login(contributor)
    delete_url = reverse('aid_delete_view', args=[aid.slug])
    res = client.post(delete_url, {'confirm': True})
    assert res.status_code == 302

    aid.refresh_from_db()
    assert aid.status == 'deleted'
Example #28
0
def test_backer_filtering():

    BackerFactory()
    aid_draft = AidFactory(status=AidWorkflow.states.draft)
    BackerFactory(financed_aids=[aid_draft])
    aid_published_1 = AidFactory(status=AidWorkflow.states.published)
    aid_published_2 = AidFactory(status=AidWorkflow.states.published)
    BackerFactory(financed_aids=[aid_published_1, aid_published_2])

    assert Backer.objects.count() == 3
    assert Backer.objects.has_financed_aids().count() == 2
    assert Backer.objects.has_published_financed_aids().count() == 1
Example #29
0
def test_draft_list_only_display_authors_aids(client, contributor):
    """Don't display aids from other users."""

    AidFactory(name='Is this the real life?', author=contributor)
    AidFactory(name='Is this just fantasy?')

    client.force_login(contributor)
    drafts_url = reverse('aid_draft_list_view')
    res = client.get(drafts_url)

    content = res.content.decode('utf-8')
    assert 'Is this the real life?' in content
    assert 'Is this just fantasy?' not in content
Example #30
0
def test_users_cannot_search_among_aid_drafts(user_client, api_url):
    AidFactory(name='First aid')
    AidFactory(name='Draft aid', status='draft')

    res = user_client.get(f'{api_url}?version=1.1')
    assert res.status_code == 200
    content = json.loads(res.content.decode())
    assert content['count'] == 1

    res = user_client.get(f'{api_url}?version=1.1&drafts=True')
    assert res.status_code == 200
    content = json.loads(res.content.decode())
    assert content['count'] == 1