test_position_form.py 2.67 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1
2
3
import copy
import datetime

Nicolas Joyard's avatar
Nicolas Joyard committed
4
5
from .base import BaseTest, RepresentativeBaseTest, ThemeBaseTest

Nicolas Joyard's avatar
Nicolas Joyard committed
6
7
from representatives_positions.models import Position

Nicolas Joyard's avatar
Nicolas Joyard committed
8
9
10

class PositionFormTest(BaseTest):
    url = '/'
Nicolas Joyard's avatar
Nicolas Joyard committed
11
12
13
14
15
16
17
18
    create_url = RepresentativeBaseTest.base_url % 'none'
    position_fixture = {
        'position-representative': 1,
        'position-datetime': '2016-09-01',
        'position-link': 'http://example.com/test',
        'position-text': 'position test text',
        'position-themes': '1'
    }
Nicolas Joyard's avatar
Nicolas Joyard committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

    def test_position_form(self):
        self.client.cookies['csrftoken'] = 'csrftoken'
        self.selector_test('#add-position-form form')

    def test_select_representative(self):
        self.selector_test(
            '#add-position-form #id_position-representative option[selected]',
            RepresentativeBaseTest.base_url % 'none'
        )

    def test_select_theme(self):
        self.selector_test(
            '#add-position-form #id_position-themes input[checked]',
            ThemeBaseTest.base_url % 'none'
        )
Nicolas Joyard's avatar
Nicolas Joyard committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

    def test_create_position(self):
        response = self.client.post(self.create_url, self.position_fixture)

        self.assertResponseDiffEmpty(response, '#add-position-success')
        position = Position.objects.get(text='position test text')

        assert position.datetime == datetime.date(2016, 9, 1)
        assert position.representative.pk == \
            self.position_fixture['position-representative']
        assert position.link == self.position_fixture['position-link']
        assert ''.join(['%s' % t.pk for t in position.themes.all()]) == '1'
        assert position.published is False

    def test_create_position_without_representative(self):
        fixture = copy.copy(self.position_fixture)
        fixture.pop('position-representative')

        response = self.client.post(self.create_url, fixture)
        self.assertResponseDiffEmpty(response, '#add-position-form .has-error')
        assert response.context['position_form'].is_valid() is False

    def test_create_position_without_datetime(self):
        fixture = copy.copy(self.position_fixture)
        fixture.pop('position-datetime')

        response = self.client.post(self.create_url, fixture)
        self.assertResponseDiffEmpty(response, '#add-position-form .has-error')
        assert response.context['position_form'].is_valid() is False

    def test_create_position_without_link(self):
        fixture = copy.copy(self.position_fixture)
        fixture.pop('position-link')

        response = self.client.post(self.create_url, fixture)
        self.assertResponseDiffEmpty(response, '#add-position-form .has-error')
        assert response.context['position_form'].is_valid() is False