Beispiel #1
0
    def test_is_admin(self):
        """ Test the is_admin function. """
        app = flask.Flask('fedora_elections')

        with app.test_request_context():
            flask.g.fas_user = None
            self.assertFalse(fedora_elections.is_admin(flask.g.fas_user))

            flask.g.oidc_id_token = 'foobar'

            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['foobar'])):
                flask.g.fas_user = FakeUser()
                self.assertFalse(fedora_elections.is_admin(flask.g.fas_user))

            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['elections'])):
                flask.g.fas_user = FakeUser(
                    fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP']
                )
                self.assertTrue(
                    fedora_elections.is_admin(flask.g.fas_user, ['elections']))

            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['sysadmin-main'])):

                fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'] = [
                    'sysadmin-main', 'sysadmin-elections'
                ]
                flask.g.fas_user = FakeUser(['sysadmin-main'])
                self.assertTrue(
                    fedora_elections.is_admin(flask.g.fas_user,
                                              ['sysadmin-main']))
Beispiel #2
0
    def test_is_election_admin(self):
        """ Test the is_election_admin function. """
        app = flask.Flask('fedora_elections')

        with app.test_request_context():
            flask.g.fas_user = None
            self.assertFalse(
                fedora_elections.is_election_admin(flask.g.fas_user, 1))
            flask.g.fas_user = FakeUser()
            self.assertFalse(
                fedora_elections.is_election_admin(flask.g.fas_user, 1))
            flask.g.fas_user = FakeUser(
                fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'])
            self.assertTrue(
                fedora_elections.is_election_admin(flask.g.fas_user, 1))

        self.setup_db()

        with app.test_request_context():
            flask.g.fas_user = FakeUser('testers')
            # This is user is not an admin for election #1
            self.assertFalse(
                fedora_elections.is_election_admin(flask.g.fas_user, 1))

            # This is user is an admin for election #2
            self.assertTrue(
                fedora_elections.is_election_admin(flask.g.fas_user, 2))
Beispiel #3
0
    def test_election_results(self):
        """ Test the election_results function - the preview part. """
        output = self.app.get('/about/test_election', follow_redirects=True)
        self.assertEqual(output.status_code, 200)
        self.assertTrue(
            'The election, test_election,  does not exist.' in output.data)

        self.setup_db()

        output = self.app.get('/about/test_election')
        self.assertEqual(output.status_code, 200)
        self.assertTrue(
            '<th class="nowrap" title="Number of votes received">Votes</th>' in
            output.data)
        self.assertTrue(
            '<h3>Some statistics about this election</h3>' in output.data)

        output = self.app.get('/about/test_election2', follow_redirects=True)
        self.assertEqual(output.status_code, 200)
        self.assertTrue(
            'The results for this election cannot be viewed because they are '
            in output.data)

        user = FakeUser(['packager'], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/about/test_election2',
                                  follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                'The results for this election cannot be viewed because they are '
                in output.data)

        user = FakeUser(
            fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'],
            username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/about/test_election2',
                                  follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                'You are only seeing these results because you are an admin.'
                in output.data)
            self.assertTrue(
                'The results for this election are currently embargoed ' in
                output.data)
            self.assertTrue(
                '<th class="nowrap" title="Number of votes received">Votes</th>'
                in output.data)
            self.assertTrue(
                '<h3>Some statistics about this election</h3>' in output.data)

        user = FakeUser(['gitr2spec'], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/about/test_election3',
                                  follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                '<span class="label label-success">Election Open</span>' in
                output.data)
    def test_vote_select(self):
        """ Test the vote_select function - the preview part. """
        output = self.app.get('/vote/test_election', follow_redirects=True)
        self.assertEqual(output.status_code, 200)
        self.assertTrue(
            '<title>OpenID transaction in progress</title>' in output.data
            or 'discoveryfailure' in output.data)

        self.setup_db()

        user = FakeUser(['packager'], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/vote/test_election6')
            self.assertTrue('test election 6 shortdesc' in output.data)
            self.assertTrue(
                '<input type="hidden" name="action" value="preview" />' in
                output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            # Invalid vote: No candidate
            data = {
                'action': 'preview',
            }

            output = self.app.post('/vote/test_election6', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('test election 6 shortdesc' in output.data)

            # Invalid vote: Too many candidates
            data = {
                'Kevin': True,
                'Toshio': True,
                'action': 'preview',
                'csrf_token': csrf_token,
            }

            output = self.app.post('/vote/test_election6', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('test election 6 shortdesc' in output.data)
            self.assertTrue(
                '<input type="hidden" name="action" value="preview" />' in
                output.data)
            self.assertTrue('Too many candidates submitted' in output.data)

            # Valid input
            data = {
                'Kevin': True,
                'action': 'preview',
                'csrf_token': csrf_token,
            }

            output = self.app.post('/vote/test_election6', data=data)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('test election 6 shortdesc' in output.data)
            self.assertTrue(
                '<input type="hidden" name="action" value="submit" />' in
                output.data)
            self.assertTrue('Please confirm your vote!' in output.data)
    def test_vote_range_revote(self):
        """ Test the vote_range function - the re-voting part. """
        #First we need to vote
        self.setup_db()

        user = FakeUser(['voters'], username='******')
        with user_set(fedora_elections.APP, user):
            retrieve_csrf = self.app.post('/vote/test_election3')
            csrf_token = retrieve_csrf.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
            data = {
                '4': 1,
                '5': 0,
                '6': 2,
                'action': 'submit',
                'csrf_token': csrf_token,
            }
            self.app.post('/vote/test_election3',
                          data=data,
                          follow_redirects=True)
            vote = fedora_elections.models.Vote
            votes = vote.of_user_on_election(self.session, "nerdsville", '3')
            self.assertEqual(votes[0].value, 1)
            self.assertEqual(votes[1].value, 0)
            self.assertEqual(votes[2].value, 2)
            #Let's not do repetition of what is tested above we aren't testing the
            #functionality of voting as that has already been asserted

            obj = fedora_elections.models.Candidate(  # id:16
                election_id=3,
                name='Josh',
                url='https://fedoraproject.org/wiki/User:Nerdsville',
            )
            self.session.add(obj)
            self.session.commit()

            #Next, we need to try revoting
            newdata = {
                '4': 2,
                '5': 1,
                '6': 1,
                '16': 0,
                'action': 'submit',
                'csrf_token': csrf_token,
            }
            output = self.app.post('/vote/test_election3',
                                   data=newdata,
                                   follow_redirects=True)
            #Next, we need to check if the vote has been recorded
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                'Your vote has been recorded.  Thank you!' in output.data)
            self.assertTrue('Open elections' in output.data)
            vote = fedora_elections.models.Vote
            votes = vote.of_user_on_election(self.session, "nerdsville", '3')
            self.assertEqual(votes[0].value, 2)
            self.assertEqual(votes[1].value, 1)
            self.assertEqual(votes[2].value, 1)
            self.assertEqual(votes[3].value, 0)
Beispiel #6
0
    def test_start_date_delete_meeting_form(self):
        """ Test the content of the start_date in the delete meeting form.
        """
        self.__setup_calendar()

        calendar = model.Calendar.by_id(self.session, 'test_calendar')
        # Create a meeting
        obj = model.Meeting(  # id:1
            meeting_name='recursive meeting',
            meeting_date=TODAY,
            meeting_date_end=TODAY,
            meeting_time_start=time(19, 50),
            meeting_time_stop=time(20, 50),
            meeting_information='This is a test meeting',
            calendar_name='test_calendar',
            recursion_frequency=14,
            recursion_ends=TODAY + timedelta(days=90))
        obj.add_manager(self.session, 'pingou,')
        obj.save(self.session)
        self.session.commit()
        self.assertNotEqual(obj, None)

        user = FakeUser(['fi-apprentice'], username='******')
        with user_set(fedocal.APP, user):
            output = self.app.get('/meeting/delete/1/')
            self.assertEqual(output.status_code, 200)

            next_date = TODAY + timedelta(days=14)
            if date.today() <= TODAY:
                next_date = TODAY

            # If no date is specified, it returns the next occurence
            self.assertTrue('<li>Date: %s</li>' % next_date in output.data)

            # If a date in the future is specified, return the next occurence
            # for this date
            url = '/meeting/delete/1/?from_date=%s' % (TODAY +
                                                       timedelta(days=20))
            output2 = self.app.get(url)
            self.assertEqual(output2.status_code, 200)

            self.assertTrue('<li>Date: %s</li>' %
                            (TODAY + timedelta(days=28)) in output2.data)

            # If an exact date in the future is specified, return that date
            url = '/meeting/delete/1/?from_date=%s' % (TODAY +
                                                       timedelta(days=14))
            output2 = self.app.get(url)
            self.assertEqual(output2.status_code, 200)

            self.assertTrue('<li>Date: %s</li>' % next_date in output.data)

            # If an old date in the future is specified, return the first date
            output2 = self.app.get('/meeting/delete/1/?from_date=2000-01-01')
            self.assertEqual(output2.status_code, 200)

            self.assertTrue('<li>Date: %s</li>' % (TODAY) in output2.data)
Beispiel #7
0
    def test_auth_login(self):
        """ Test the auth_login function. """
        app = flask.Flask('fedora_elections')

        with app.test_request_context():
            flask.g.fas_user = FakeUser(['gitr2spec'])
            output = self.app.get('/login')
            self.assertEqual(output.status_code, 200)

            output = self.app.get('/login?next=http://localhost/')
            self.assertEqual(output.status_code, 200)

        self.setup_db()
        user = FakeUser([], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/login', follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('<title>Fedora elections</title>' in output.data)
Beispiel #8
0
    def test_index_filled(self):
        """ Test the index function. """
        self.setup_db()
        output = self.app.get('/')
        self.assertEqual(output.status_code, 200)
        output_text = output.get_data(as_text=True)
        self.assertTrue('<h2>Elections' in output_text)
        self.assertTrue(
            '<h4><strong>Open elections</strong></h4>' in output_text)
        self.assertTrue(
            '<strong>Upcoming elections</strong></h4>' in output_text)
        self.assertTrue('<a href="/login">Log In</a>' in output_text)

        user = FakeUser([], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/')
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertTrue('<h2>Elections' in output_text)
            self.assertTrue(
                '<h4><strong>Open elections</strong></h4>' in output_text)
            self.assertTrue(
                '<strong>Upcoming elections</strong></h4>' in output_text)
            self.assertTrue(
                '<a class="btn btn-sm btn-primary m-l-2" href="/vote/' in
                output_text)
            self.assertTrue(
                '<a class="dropdown-item" href="/logout">log out</a>' in
                output_text)
            self.assertEqual(output_text.count('Vote now!'), 4)

        user = FakeUser([], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/')
            self.assertEqual(output.status_code, 200)
            output_text = output.get_data(as_text=True)
            self.assertTrue('<h2>Elections' in output_text)
            self.assertTrue(
                '<h4><strong>Open elections</strong></h4>' in output_text)
            self.assertTrue(
                '<strong>Upcoming elections</strong></h4>' in output_text)
            self.assertTrue(
                '<a class="dropdown-item" href="/logout">log out</a>' in
                output_text)
Beispiel #9
0
    def test_get_api_token(self):
        """ Test the get_api_token method. """
        user = FakeUser()

        infos = fedoratagger.lib.get_api_token(self.session, user)
        self.assertEqual(infos['name'], 'fake_username')
        self.assertTrue(infos['token'].startswith('dGFnZ2VyYXBp##'))

        dbuser = model.FASUser.by_name(self.session, infos['name'])
        self.assertTrue(infos['token'], dbuser.api_token)
Beispiel #10
0
    def setUp(self):
        """ Set up the environnment, ran before every tests. """
        super(Flasktests, self).setUp()

        fedoratagger.APP.config['TESTING'] = True
        fedoratagger.SESSION = self.session
        fedoratagger.api.SESSION = self.session
        self.app = fedoratagger.APP.test_client()
        wrappers.BaseRequest.remote_addr = '1.2.3'
        user = FakeUser()
        self.infos = None
Beispiel #11
0
    def test_open_elections(self):
        """ Test the open_elections function. """
        self.setup_db()

        output = self.app.get('/')
        self.assertEqual(output.status_code, 200)
        self.assertTrue('<h2>Elections' in output.data)
        self.assertTrue(
            '<h4><strong>Open elections</strong></h4>' in output.data)
        self.assertTrue(
            '<strong>Upcoming elections</strong></h4>' in output.data)
        self.assertTrue('href="/vote/' in output.data)
        self.assertTrue('<a href="/login">Log In</a>' in output.data)

        user = FakeUser([], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/')
            self.assertEqual(output.status_code, 200)
            self.assertTrue('<h2>Elections' in output.data)
            self.assertTrue(
                '<h4><strong>Open elections</strong></h4>' in output.data)
            self.assertTrue(
                '<strong>Upcoming elections</strong></h4>' in output.data)
            self.assertTrue('href="/vote/' in output.data)
            self.assertTrue(
                '<a class="dropdown-item" href="/logout">log out</a>' in
                output.data)
            self.assertEqual(output.data.count('Vote now!'), 4)

        user = FakeUser([], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/')
            self.assertEqual(output.status_code, 200)
            self.assertTrue('<h2>Elections' in output.data)
            self.assertTrue(
                '<h4><strong>Open elections</strong></h4>' in output.data)
            self.assertTrue(
                '<strong>Upcoming elections</strong></h4>' in output.data)
            self.assertTrue(
                '<a class="dropdown-item" href="/logout">log out</a>' in
                output.data)
Beispiel #12
0
    def test_vote_irc_revote(self):
        """ Test the vote_irc function - the re-voting part. """
        #First we need to vote
        self.setup_db()

        user = FakeUser(['voters'], username='******')
        with user_set(fedora_elections.APP, user, oidc_id_token='foobar'):
            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['packager'])):
                retrieve_csrf = self.app.post('/vote/test_election7')
                output_text = retrieve_csrf.get_data(as_text=True)
                csrf_token = output_text.split(
                    'name="csrf_token" type="hidden" value="')[1].split(
                        '">')[0]
                # Valid input
                data = {
                    'Kevin': -1,
                    'Toshio': 1,
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }
                self.app.post('/vote/test_election7',
                              data=data,
                              follow_redirects=True)
                vote = fedora_elections.models.Vote
                votes = vote.of_user_on_election(self.session, "nerdsville",
                                                 '7')
                self.assertEqual(votes[0].value, 1)
                self.assertEqual(votes[1].value, -1)
                #Let's not do repetition of what is tested above we aren't testing the
                #functionality of voting as that has already been asserted

                #Next, we need to try revoting
                newdata = {
                    'Kevin': 1,
                    'Toshio': -1,
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }
                output = self.app.post('/vote/test_election7',
                                       data=newdata,
                                       follow_redirects=True)
                #Next, we need to check if the vote has been recorded
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue(
                    'Your vote has been recorded.  Thank you!' in output_text)
                self.assertTrue('Open elections' in output_text)
                vote = fedora_elections.models.Vote
                votes = vote.of_user_on_election(self.session, "nerdsville",
                                                 '7')
                self.assertEqual(votes[0].value, -1)
                self.assertEqual(votes[1].value, 1)
Beispiel #13
0
    def test_is_admin(self):
        """ Test the is_admin function. """
        app = flask.Flask('fedora_elections')

        with app.test_request_context():
            flask.g.fas_user = None
            self.assertFalse(fedora_elections.is_admin(flask.g.fas_user))

            flask.g.fas_user = FakeUser()
            self.assertFalse(fedora_elections.is_admin(flask.g.fas_user))

            flask.g.fas_user = FakeUser(
                fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'])
            self.assertTrue(fedora_elections.is_admin(flask.g.fas_user))

            fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'] = [
                'sysadmin-main', 'sysadmin-elections'
            ]
            flask.g.fas_user = FakeUser(
                fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'])
            self.assertTrue(fedora_elections.is_admin(flask.g.fas_user))
Beispiel #14
0
    def test_vote_irc(self):
        """ Test the vote_irc function - the preview part. """
        output = self.app.get('/vote/test_election')
        self.assertEqual(output.status_code, 302)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '/login?next=http%3A%2F%2Flocalhost%2Fvote%2Ftest_election',
            output_text)

        self.setup_db()

        user = FakeUser(['packager'], username='******')
        with user_set(fedora_elections.APP, user, oidc_id_token='foobar'):
            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['packager'])):
                output = self.app.get('/vote/test_election7')
                output_text = output.get_data(as_text=True)
                self.assertTrue('test election 7 shortdesc' in output_text)
                self.assertTrue(
                    '<input type="hidden" name="action" value="preview" />' in
                    output_text)

                csrf_token = output_text.split(
                    'name="csrf_token" type="hidden" value="')[1].split(
                        '">')[0]

                # Invalid vote: No candidate
                data = {
                    'action': 'preview',
                }

                output = self.app.post('/vote/test_election7', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue('test election 7 shortdesc' in output_text)

                # Valid input
                data = {
                    'Kevin': -1,
                    'Toshio': '0',
                    'action': 'preview',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election7', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue('test election 7 shortdesc' in output_text)
                self.assertTrue(
                    '<input type="hidden" name="action" value="submit" />' in
                    output_text)
                self.assertTrue('Please confirm your vote!' in output_text)
Beispiel #15
0
    def test_vote_select_revote(self):
        """ Test the vote_select function - the re-voting part. """
        #First we need to vote
        self.setup_db()

        user = FakeUser(['voters'], username='******')
        with user_set(fedora_elections.APP, user, oidc_id_token='foobar'):
            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['voters'])):
                retrieve_csrf = self.app.post('/vote/test_election6')
                csrf_token = retrieve_csrf.get_data(as_text=True).split(
                    'name="csrf_token" type="hidden" value="')[1].split(
                        '">')[0]

                # Valid input
                data = {
                    'Kevin': True,
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }
                self.app.post('/vote/test_election6',
                              data=data,
                              follow_redirects=True)
                vote = fedora_elections.models.Vote
                votes = vote.of_user_on_election(self.session, "nerdsville",
                                                 '6')
                self.assertEqual(votes[0].candidate.name, "Toshio")
                self.assertEqual(votes[0].value, 0)
                self.assertEqual(votes[1].candidate.name, "Kevin")
                self.assertEqual(votes[1].value, 1)

                #Next, we need to try revoting
                newdata = {
                    'Toshio': True,
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }
                output = self.app.post('/vote/test_election6',
                                       data=newdata,
                                       follow_redirects=True)
                #Next, we need to check if the vote has been recorded
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue(
                    'Your vote has been recorded.  Thank you!' in output_text)
                self.assertTrue('Open elections' in output_text)
                vote = fedora_elections.models.Vote
                votes = vote.of_user_on_election(self.session, "nerdsville",
                                                 '6')
                self.assertEqual(votes[0].value, 1)
                self.assertEqual(votes[1].value, 0)
Beispiel #16
0
    def test_is_election_admin(self):
        """ Test the is_election_admin function. """
        app = flask.Flask('fedora_elections')

        with app.test_request_context():
            flask.g.fas_user = None
            self.assertFalse(
                fedora_elections.is_election_admin(flask.g.fas_user, 1))

            flask.g.oidc_id_token = 'foobar'

            flask.g.fas_user = FakeUser()
            self.assertFalse(
                fedora_elections.is_election_admin(flask.g.fas_user, 1))
            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['elections'])):
                flask.g.fas_user = FakeUser(
                    fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP']
                )
                self.assertTrue(
                    fedora_elections.is_election_admin(flask.g.fas_user, 1))

        self.setup_db()

        with app.test_request_context():
            flask.g.fas_user = FakeUser('testers')
            flask.g.oidc_id_token = 'foobar'

            # This is user is not an admin for election #1
            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['foobar'])):
                self.assertFalse(
                    fedora_elections.is_election_admin(flask.g.fas_user, 1))

                # This is user is an admin for election #2
                self.assertTrue(
                    fedora_elections.is_election_admin(flask.g.fas_user, 2))
Beispiel #17
0
    def test_auth_logout(self):
        """ Test the auth_logout function. """
        user = FakeUser(
            fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'])
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/logout', follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('<title>Fedora elections</title>' in output.data)
            self.assertTrue('You have been logged out' in output.data)

        user = None
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/logout', follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('<title>Fedora elections</title>' in output.data)
            self.assertFalse('You have been logged out' in output.data)
Beispiel #18
0
    def test_vote_irc_process(self):
        """ Test the vote_irc function - the voting part. """
        output = self.app.get('/vote/test_election')
        self.assertEqual(output.status_code, 302)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '/login?next=http%3A%2F%2Flocalhost%2Fvote%2Ftest_election',
            output_text)

        self.setup_db()

        user = FakeUser(['packager'], username='******')
        with user_set(fedora_elections.APP, user, oidc_id_token='foobar'):
            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['packager'])):
                # Invalid candidate id - no csrf
                data = {
                    'candidate': 1,
                    'action': 'submit',
                }

                output = self.app.post('/vote/test_election7',
                                       data=data,
                                       follow_redirects=True)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)

                csrf_token = output_text.split(
                    'name="csrf_token" type="hidden" value="')[1].split(
                        '">')[0]

                # Valid input
                data = {
                    'Toshio': '0',
                    'Kevin': '1',
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election7',
                                       data=data,
                                       follow_redirects=True)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue(
                    'Your vote has been recorded.  Thank you!' in output_text)
                self.assertTrue('Open elections' in output_text)
Beispiel #19
0
    def test_admin_view_election(self):
        """ Test the admin_view_election function. """
        user = FakeUser(
            fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'],
            username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/election_test/')
            self.assertEqual(output.status_code, 404)

        self.setup_db()

        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test_election/')
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                'Candidates <span class="label label-pill label-default">3</span>'
                in output.data)
Beispiel #20
0
    def test_index_empty(self):
        """ Test the index function. """
        output = self.app.get('/')
        self.assertEqual(output.status_code, 200)
        self.assertTrue('<title>Fedora elections</title>' in output.data)
        self.assertFalse('<h3>Current elections</h3>' in output.data)
        self.assertFalse('<h3>Next' in output.data)
        self.assertFalse('<h3>Last' in output.data)
        self.assertTrue('<a href="/login">Log In</a>' in output.data)

        user = FakeUser([], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/', follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('<title>Fedora elections</title>' in output.data)
            self.assertTrue(
                '<a class="dropdown-item" href="/logout">log out</a>' in
                output.data)
Beispiel #21
0
    def test_vote_simple_revote(self):
        """ Test the vote_simple function - the re-voting part. """
        #First we need to vote
        self.setup_db()

        user = FakeUser(['voters'], username='******')
        with user_set(fedora_elections.APP, user):
            retrieve_csrf = self.app.post('/vote/test_election5')
            csrf_token = retrieve_csrf.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
            # Valid input
            data = {
                'candidate': 8,
                'action': 'submit',
                'csrf_token': csrf_token,
            }

            self.app.post('/vote/test_election5',
                          data=data,
                          follow_redirects=True)
            vote = fedora_elections.models.Vote
            votes = vote.of_user_on_election(self.session, "nerdsville", '5')
            self.assertEqual(votes[0].candidate_id, 8)
            #Let's not do repetition of what is tested above we aren't testing the
            #functionality of voting as that has already been asserted

            #Next, we need to try revoting
            # Valid input
            newdata = {
                'candidate': 9,
                'action': 'submit',
                'csrf_token': csrf_token,
            }
            output = self.app.post('/vote/test_election5',
                                   data=newdata,
                                   follow_redirects=True)
            #Next, we need to check if the vote has been recorded
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                'Your vote has been recorded.  Thank you!' in output.data)
            self.assertTrue('Open elections' in output.data)
            vote = fedora_elections.models.Vote
            votes = vote.of_user_on_election(self.session, "nerdsville", '5')
            self.assertEqual(votes[0].candidate_id, 9)
Beispiel #22
0
    def test_vote_empty(self):
        """ Test the vote function without data. """
        output = self.app.get('/vote/test_election')
        self.assertEqual(output.status_code, 302)

        output = self.app.get('/vote/test_election', follow_redirects=True)
        self.assertEqual(output.status_code, 200)
        self.assertTrue(
            '<title>OpenID transaction in progress</title>' in output.data
            or 'discoveryfailure' in output.data)

        user = FakeUser(['packager'], username='******')
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/vote/test_election')
            self.assertEqual(output.status_code, 302)

            output = self.app.get('/vote/test_election', follow_redirects=True)
            self.assertTrue(
                'The election, test_election, does not exist.' in output.data)
Beispiel #23
0
    def test_auth_login(self):
        """ Test the auth_login function. """
        app = flask.Flask('fedora_elections')

        with app.test_request_context():
            flask.g.fas_user = FakeUser(['gitr2spec'])
            output = self.app.get('/login')
            self.assertEqual(output.status_code, 302)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                'https://iddev.fedorainfracloud.org/openidc/Authorization?',
                output_text)

            output = self.app.get('/login?next=http://localhost/')
            self.assertEqual(output.status_code, 302)
            output_text = output.get_data(as_text=True)
            self.assertIn(
                'https://iddev.fedorainfracloud.org/openidc/Authorization?',
                output_text)
Beispiel #24
0
    def test_vote_irc_process(self):
        """ Test the vote_irc function - the voting part. """
        output = self.app.get('/vote/test_election', follow_redirects=True)
        self.assertEqual(output.status_code, 200)
        self.assertTrue(
            '<title>OpenID transaction in progress</title>' in output.data
            or 'discoveryfailure' in output.data)

        self.setup_db()

        user = FakeUser(['packager'], username='******')
        with user_set(fedora_elections.APP, user):
            # Invalid candidate id - no csrf
            data = {
                'candidate': 1,
                'action': 'submit',
            }

            output = self.app.post('/vote/test_election7',
                                   data=data,
                                   follow_redirects=True)
            self.assertEqual(output.status_code, 200)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            # Valid input
            data = {
                'Toshio': '0',
                'Kevin': '1',
                'action': 'submit',
                'csrf_token': csrf_token,
            }

            output = self.app.post('/vote/test_election7',
                                   data=data,
                                   follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                'Your vote has been recorded.  Thank you!' in output.data)
            self.assertTrue('Open elections' in output.data)
    def test_vote_empty(self):
        """ Test the vote function without data. """
        output = self.app.get('/vote/test_election')
        self.assertEqual(output.status_code, 302)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '/login?next=http%3A%2F%2Flocalhost%2Fvote%2Ftest_election',
            output_text)

        user = FakeUser(['packager'], username='******')
        with user_set(fedora_elections.APP, user, oidc_id_token='foobar'):
            with patch(
                    'fedora_elections.OIDC.user_getfield',
                    MagicMock(return_value=['packager'])):
                output = self.app.get('/vote/test_election')
                self.assertEqual(output.status_code, 302)

                output = self.app.get('/vote/test_election', follow_redirects=True)
                output_text = output.get_data(as_text=True)
                self.assertTrue(
                    'The election, test_election, does not exist.'
                    in output_text)
    def test_editing_recurring_meeting_one_day_earlier(self):
        """ Test editing moving a meeting one day earlier in a recursion
        """
        # Setup info
        self.__setup_calendar()
        obj = model.Meeting(  # id:1
            meeting_name='test recurring',
            meeting_date=datetime(2014, 9, 1).date(),
            meeting_date_end=datetime(2014, 9, 1).date(),
            meeting_time_start=time(9, 0),
            meeting_time_stop=time(10, 0),
            meeting_information='This is a test meeting recurring',
            calendar_name='test_calendar',
            recursion_frequency=7,
            recursion_ends=datetime(2014, 10, 27).date(),
        )
        obj.add_manager(self.session, 'pingou')
        obj.save(self.session)
        self.session.commit()
        self.assertNotEqual(obj, None)

        # Before edition
        calendarobj = model.Calendar.by_id(self.session, 'test_calendar')
        meetings = fedocallib.get_by_date(self.session, calendarobj,
                                          datetime(2014, 9, 1).date(),
                                          datetime(2014, 10, 27).date())
        self.assertEqual(len(meetings), 9)
        ids = set([mtg.meeting_id for mtg in meetings])
        self.assertEqual(list(ids)[0], 1)
        dates = [str(mtg.meeting_date) for mtg in meetings]
        self.assertEqual(dates, [
            '2014-09-01',
            '2014-09-08',
            '2014-09-15',
            '2014-09-22',
            '2014-09-29',
            '2014-10-06',
            '2014-10-13',
            '2014-10-20',
            '2014-10-27',
        ])

        # Edit meeting in the middle to move it by one day
        meeting = model.Meeting.by_id(self.session, 1)
        fedocallib.edit_meeting(
            session=self.session,
            meeting=meeting,
            calendarobj=calendarobj,
            fas_user=FakeUser(groups=['fi-apprentice'], username='******'),
            meeting_name='test recurring',
            meeting_date=datetime(2014, 9, 14).date(),
            meeting_date_end=datetime(2014, 9, 14).date(),
            meeting_time_start=time(9, 0),
            meeting_time_stop=time(10, 0),
            comanager='pingou',
            meeting_information='This is a test meeting recurring',
            meeting_location=None,
            tzone='UTC',
            recursion_frequency=7,
            recursion_ends=datetime(2014, 10, 27).date(),
            remind_when=None,
            reminder_from=None,
            remind_who=None,
            full_day=False,
            edit_all_meeting=False,
            admin=False)

        # After edition
        calendarobj = model.Calendar.by_id(self.session, 'test_calendar')
        meetings = fedocallib.get_by_date(self.session, calendarobj,
                                          datetime(2014, 9, 1).date(),
                                          datetime(2014, 10, 27).date())
        #self.assertEqual(len(meetings), 9)
        ids = set([mtg.meeting_id for mtg in meetings])
        #self.assertEqual(list(ids), [1, 2, 3])
        dates = [str(mtg.meeting_date) for mtg in meetings]
        # One meeting moved by 1 day
        self.assertEqual(sorted(dates), [
            '2014-09-01',
            '2014-09-08',
            '2014-09-14',
            '2014-09-22',
            '2014-09-29',
            '2014-10-06',
            '2014-10-13',
            '2014-10-20',
            '2014-10-27',
        ])

        # Edit meeting in the middle to move it by 3 days (earlier)
        meeting = model.Meeting.by_id(self.session, 3)
        fedocallib.edit_meeting(
            session=self.session,
            meeting=meeting,
            calendarobj=calendarobj,
            fas_user=FakeUser(groups=['fi-apprentice'], username='******'),
            meeting_name='test recurring',
            meeting_date=datetime(2014, 9, 26).date(),
            meeting_date_end=datetime(2014, 9, 26).date(),
            meeting_time_start=time(9, 0),
            meeting_time_stop=time(10, 0),
            comanager='pingou',
            meeting_information='This is a test meeting recurring',
            meeting_location=None,
            tzone='UTC',
            recursion_frequency=7,
            recursion_ends=datetime(2014, 10, 27).date(),
            remind_when=None,
            reminder_from=None,
            remind_who=None,
            full_day=False,
            edit_all_meeting=False,
            admin=False)

        # After edition
        calendarobj = model.Calendar.by_id(self.session, 'test_calendar')
        meetings = fedocallib.get_by_date(self.session, calendarobj,
                                          datetime(2014, 9, 1).date(),
                                          datetime(2014, 10, 27).date())
        self.assertEqual(len(meetings), 9)
        ids = set([mtg.meeting_id for mtg in meetings])
        self.assertEqual(list(ids), [1, 2, 3, 4, 5])
        dates = [str(mtg.meeting_date) for mtg in meetings]
        # One meeting moved by 3 day
        self.assertEqual(sorted(dates), [
            '2014-09-01',
            '2014-09-08',
            '2014-09-14',
            '2014-09-22',
            '2014-09-26',
            '2014-10-06',
            '2014-10-13',
            '2014-10-20',
            '2014-10-27',
        ])
Beispiel #27
0
    def test_admin_edit_candidate(self):
        """ Test the admin_edit_candidate function. """
        user = FakeUser(
            fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'],
            username='******')

        self.setup_db()

        # Election does not exist
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test/candidates/1/edit')
            self.assertEqual(output.status_code, 404)

        # Candidate does not exist
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test_election/candidates/100/edit')
            self.assertEqual(output.status_code, 404)

        # Candidate not in election
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test_election/candidates/5/edit')
            self.assertEqual(output.status_code, 404)

        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test_election/candidates/1/edit')
            self.assertEqual(output.status_code, 200)

            self.assertTrue('Edit candidate' in output.data)
            self.assertTrue(
                'input class="form-control" id="name" name="name" type="text"'
                in output.data)

            data = {
                'name': 'Toshio Kuratomi',
                'url': 'https://fedoraproject.org/wiki/User:Toshio',
            }

            self.assertTrue('Edit candidate' in output.data)
            self.assertTrue(
                'input class="form-control" id="name" name="name" type="text"'
                in output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            data = {
                'name': '',
                'csrf_token': csrf_token,
            }

            output = self.app.post('/admin/test_election/candidates/1/edit',
                                   data=data)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('Edit candidate' in output.data)
            self.assertTrue(
                'input class="form-control" id="name" name="name" type="text"'
                in output.data)
            self.assertTrue(
                '<div class="form-control-feedback">This field is required.</div>'
                in output.data)

            # Check election before edit
            output = self.app.get('/admin/test_election/')
            self.assertEqual(output.status_code, 200)
            self.assertTrue(
                'Candidates <span class="label label-pill label-default">3</span>'
                in output.data)
            self.assertTrue(
                '<input class="form-control" id="seats_elected" name="seats_elected" type="text" value="1">'
                in output.data)
            self.assertTrue(
                '<div class="list-group-item">Toshio' in output.data)

            data = {
                'name': 'Toshio Kuratomi',
                'url': 'https://fedoraproject.org/wiki/User:Toshio',
                'csrf_token': csrf_token,
            }

            output = self.app.post('/admin/test_election/candidates/1/edit',
                                   data=data,
                                   follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('Candidate "Toshio Kuratomi" saved' in output.data)
            self.assertTrue(
                'Candidates <span class="label label-pill label-default">3</span>'
                in output.data)
            self.assertTrue(
                '<div class="list-group-item">Toshio' in output.data)
Beispiel #28
0
    def test_admin_delete_candidate(self):
        """ Test the admin_delete_candidate function. """
        user = FakeUser(
            fedora_elections.APP.config['FEDORA_ELECTIONS_ADMIN_GROUP'],
            username='******')

        self.setup_db()

        # Election does not exist
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test/candidates/1/delete')
            self.assertEqual(output.status_code, 404)

        # Candidate does not exist
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test_election/candidates/100/delete')
            self.assertEqual(output.status_code, 404)

        # Candidate not in election
        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test_election/candidates/5/delete')
            self.assertEqual(output.status_code, 404)

        with user_set(fedora_elections.APP, user):
            output = self.app.get('/admin/test_election/candidates/1/delete')
            self.assertEqual(output.status_code, 200)

            self.assertTrue('<h2>Delete candidate</h2>' in output.data)
            self.assertTrue(
                'p>Are you sure you want to delete candidate "Toshio"?</p' in
                output.data)

            output = self.app.post('/admin/test_election/candidates/1/delete')
            self.assertEqual(output.status_code, 200)
            self.assertTrue('<h2>Delete candidate</h2>' in output.data)
            self.assertTrue(
                'p>Are you sure you want to delete candidate "Toshio"?</p' in
                output.data)

            csrf_token = output.data.split(
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]

            # Try to delete while there are votes link to this candidates
            data = {
                'csrf_token': csrf_token,
            }

            output = self.app.post('/admin/test_election/candidates/1/delete',
                                   data=data,
                                   follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('<h2>test election shortdesc</h2>' in output.data)
            self.assertTrue('Could not delete this candidate. Is it '
                            'already part of an election?' in output.data)

            # Check election before edit
            output = self.app.get('/admin/test_election4/')
            self.assertTrue(
                'Candidates <span class="label label-pill label-default">2</span>'
                in output.data)
            self.assertTrue(
                '<input class="form-control" id="seats_elected" name="seats_elected" type="text" value="1">'
                in output.data)
            self.assertTrue(
                '<div class="list-group-item">Toshio' in output.data)
            self.assertTrue(
                'value="test election 4 shortdesc">' in output.data)

            # Delete one candidate
            data = {
                'csrf_token': csrf_token,
            }

            output = self.app.post(
                '/admin/test_election4/candidates/10/delete',
                data=data,
                follow_redirects=True)
            self.assertEqual(output.status_code, 200)
            self.assertTrue('Candidate "Toshio" deleted' in output.data)
            self.assertTrue(
                'value="test election 4 shortdesc">' in output.data)
            self.assertTrue(
                'Candidates <span class="label label-pill label-default">1</span>'
                in output.data)
            self.assertFalse(
                '<div class="list-group-item">Toshio' in output.data)
Beispiel #29
0
    def test_vote_range_process(self):
        """ Test the vote_range function - the voting part. """
        output = self.app.get('/vote/test_election')
        self.assertEqual(output.status_code, 302)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '/login?next=http%3A%2F%2Flocalhost%2Fvote%2Ftest_election',
            output_text)

        self.setup_db()

        user = FakeUser(['voters'], username='******')
        with user_set(fedora_elections.APP, user, oidc_id_token='foobar'):
            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['voters'])):
                # No csrf token provided
                data = {
                    'Toshio': 1,
                    'Kevin': 3,
                    'Ralph': 2,
                    'action': 'submit',
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 3)

                csrf_token = output_text.split(
                    'name="csrf_token" type="hidden" value="')[1].split(
                        '">')[0]

                # Invalid vote: invalid username
                data = {
                    'Toshio': 1,
                    'Kevin': 3,
                    'Ralph': 2,
                    'action': 'submit',
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 3)

                # Invalid vote: too low
                data = {
                    '4': -1,
                    '5': 0,
                    '6': 2,
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 1)

                # Invalid vote: 2 are too high
                data = {
                    '4': 5,
                    '5': 3,
                    '6': 2,
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3',
                                       data=data,
                                       follow_redirects=True)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 2)

                # Invalid vote: Not numeric
                data = {
                    '4': 'a',
                    '5': 0,
                    '6': 2,
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3',
                                       data=data,
                                       follow_redirects=True)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 1)

                # Valid input
                data = {
                    '4': 1,
                    '5': 0,
                    '6': 2,
                    'action': 'submit',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3',
                                       data=data,
                                       follow_redirects=True)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue(
                    'Your vote has been recorded.  Thank you!' in output_text)
                self.assertTrue('Open elections' in output_text)
Beispiel #30
0
    def test_vote_range(self):
        """ Test the vote_range function - the preview part. """
        output = self.app.get('/vote/test_election')
        self.assertEqual(output.status_code, 302)
        output_text = output.get_data(as_text=True)
        self.assertIn(
            '/login?next=http%3A%2F%2Flocalhost%2Fvote%2Ftest_election',
            output_text)

        self.setup_db()

        user = FakeUser(['voters'], username='******')
        with user_set(fedora_elections.APP, user, oidc_id_token='foobar'):
            with patch('fedora_elections.OIDC.user_getfield',
                       MagicMock(return_value=['voters'])):
                output = self.app.get('/vote/test_election3')
                output_text = output.get_data(as_text=True)
                self.assertTrue('test election 3 shortdesc' in output_text)
                self.assertTrue(
                    '<input type="hidden" name="action" value="preview" />' in
                    output_text)

                # Invalid candidate id
                data = {
                    'Toshio': 1,
                    'kevin': 3,
                    'Ralph': 2,
                    'action': 'preview',
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 3)

                csrf_token = output_text.split(
                    'name="csrf_token" type="hidden" value="')[1].split(
                        '">')[0]

                # Invalid candidate id
                data = {
                    'Toshio': 1,
                    'Kevin': 3,
                    'Ralph': 2,
                    'action': 'preview',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 3)

                # Invalid vote: too low
                data = {
                    '9': -1,
                    '6': 0,
                    '5': 2,
                    'action': 'preview',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue('test election 3 shortdesc' in output_text)
                self.assertTrue(
                    '<input type="hidden" name="action" value="preview" />' in
                    output_text)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 1)

                # Invalid vote: 2 are too high
                data = {
                    '9': 5,
                    '6': 3,
                    '5': 2,
                    'action': 'preview',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue('test election 3 shortdesc' in output_text)
                self.assertTrue(
                    '<input type="hidden" name="action" value="preview" />' in
                    output_text)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 2)

                # Invalid vote: Not numeric
                data = {
                    '9': 'a',
                    '6': 0,
                    '5': 2,
                    'action': 'preview',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue('test election 3 shortdesc' in output_text)
                self.assertTrue(
                    '<input type="hidden" name="action" value="preview" />' in
                    output_text)
                self.assertEqual(
                    output_text.count(
                        '<div class="form-control-feedback">Not a valid choice</div>'
                    ), 1)

                # Valid input
                data = {
                    '4': 1,
                    '6': 0,
                    '5': 2,
                    'action': 'preview',
                    'csrf_token': csrf_token,
                }

                output = self.app.post('/vote/test_election3', data=data)
                self.assertEqual(output.status_code, 200)
                output_text = output.get_data(as_text=True)
                self.assertTrue('test election 3 shortdesc' in output_text)
                self.assertTrue(
                    '<input type="hidden" name="action" value="submit" />' in
                    output_text)