Пример #1
0
 def test_append_empty_forms_only_when_no_initial_data_is_provided(self):
     # Setup
     poll = TopicPollFactory.create(topic=self.topic)
     option_1 = TopicPollOptionFactory.create(poll=poll)
     option_2 = TopicPollOptionFactory.create(poll=poll)
     form_data_1 = {
         'form-0-id': option_1.pk,
         'form-0-text': faker.text(max_nb_chars=100),
         'form-1-id': option_2.pk,
         'form-1-text': option_2.text,
         'form-INITIAL_FORMS': 2,
         'form-TOTAL_FORMS': 2,
         'form-MAX_NUM_FORMS': 1000,
     }
     self.topic.refresh_from_db()
     # Run
     form_1 = TopicPollOptionFormset(
         data=form_data_1,
         topic=self.topic)
     form_2 = TopicPollOptionFormset(topic=self.topic)  # poll already created
     form_3 = TopicPollOptionFormset(topic=self.alt_topic)  # no poll
     # Check
     assert form_1.total_form_count() == 2
     assert form_2.total_form_count() == 2
     assert form_3.total_form_count() == 2
Пример #2
0
 def test_can_update_options_associated_with_an_existing_poll(self):
     # Setup
     poll = TopicPollFactory.create(topic=self.topic)
     option_1 = TopicPollOptionFactory.create(poll=poll)
     option_2 = TopicPollOptionFactory.create(poll=poll)
     form_data = {
         'form-0-id': option_1.pk,
         'form-0-text': faker.text(max_nb_chars=100),
         'form-1-id': option_2.pk,
         'form-1-text': option_2.text,
         'form-INITIAL_FORMS': 2,
         'form-TOTAL_FORMS': 2,
         'form-MAX_NUM_FORMS': 1000,
     }
     self.topic.refresh_from_db()
     # Run
     form = TopicPollOptionFormset(
         data=form_data,
         topic=self.topic)
     valid = form.is_valid()
     # Check
     assert valid
     form.save()
     option_1.refresh_from_db()
     assert option_1.text == form_data['form-0-text']
Пример #3
0
    def test_can_if_an_anonymous_user_has_already_voted_in_a_poll(self):
        # Setup
        def get_rendered(poll, user):
            request = self.request_factory.get('/')
            request.user = user
            t = Template(self.loadstatement +
                         '{% if poll|has_been_completed_by:request.user %}'
                         'HAS_VOTED{% else %}HAS_NOT_VOTED{% endif %}')
            c = Context({'poll': poll, 'request': request})
            rendered = t.render(c)

            return rendered

        u2 = AnonymousUser()
        u2.forum_key = 'dummy'
        u3 = AnonymousUser()
        poll_option_1 = TopicPollOptionFactory.create(poll=self.poll_1)
        TopicPollOptionFactory.create(poll=self.poll_1)
        TopicPollVoteFactory.create(poll_option=poll_option_1,
                                    anonymous_key='dummy')

        # Run & check
        assert get_rendered(self.poll_1, u2) == 'HAS_VOTED'
        assert get_rendered(self.poll_2, u2) == 'HAS_NOT_VOTED'
        assert get_rendered(self.poll_2, u3) == 'HAS_NOT_VOTED'
        assert get_rendered(self.poll_2, u3) == 'HAS_NOT_VOTED'
Пример #4
0
    def test_cannot_be_associated_with_a_user_and_a_session_key_at_the_same_time_to_be_created(self):  # noqa
        # Setup
        poll = TopicPollFactory.create(topic=self.topic, max_options=2)
        option_1 = TopicPollOptionFactory.create(poll=poll)
        TopicPollOptionFactory.create(poll=poll)

        # Run & check
        with pytest.raises(ValidationError):
            vote = TopicPollVoteFactory.build(
                poll_option=option_1, voter=self.u1, anonymous_key='ertyue')
            vote.clean()
Пример #5
0
    def test_cannot_be_associated_with_a_user_and_a_session_key_at_the_same_time_to_be_created(self):  # noqa
        # Setup
        poll = TopicPollFactory.create(topic=self.topic, max_options=2)
        option_1 = TopicPollOptionFactory.create(poll=poll)
        TopicPollOptionFactory.create(poll=poll)

        # Run & check
        with pytest.raises(ValidationError):
            vote = TopicPollVoteFactory.build(
                poll_option=option_1, voter=self.u1, anonymous_key='ertyue')
            vote.clean()
Пример #6
0
    def test_must_be_associated_with_a_user_or_a_session_key_to_be_created(self):
        # Setup
        poll = TopicPollFactory.create(topic=self.topic, max_options=2)
        option_1 = TopicPollOptionFactory.create(poll=poll)
        TopicPollOptionFactory.create(poll=poll)

        # Run & check
        with pytest.raises(ValidationError):
            vote = TopicPollVoteFactory.build(
                poll_option=option_1, voter=None, anonymous_key=None)
            vote.clean()
Пример #7
0
    def test_must_be_associated_with_a_user_or_a_session_key_to_be_created(self):
        # Setup
        poll = TopicPollFactory.create(topic=self.topic, max_options=2)
        option_1 = TopicPollOptionFactory.create(poll=poll)
        TopicPollOptionFactory.create(poll=poll)

        # Run & check
        with pytest.raises(ValidationError):
            vote = TopicPollVoteFactory.build(
                poll_option=option_1, voter=None, anonymous_key=None)
            vote.clean()
Пример #8
0
 def test_can_return_a_list_of_all_related_votes(self):
     # Setup
     u2 = UserFactory.create()
     poll = TopicPollFactory.create(topic=self.topic, max_options=2)
     option_1 = TopicPollOptionFactory.create(poll=poll)
     option_2 = TopicPollOptionFactory.create(poll=poll)
     # Run
     vote_1 = TopicPollVoteFactory.create(poll_option=option_1, voter=self.u1)
     vote_2 = TopicPollVoteFactory.create(poll_option=option_2, voter=self.u1)
     vote_3 = TopicPollVoteFactory.create(poll_option=option_2, voter=u2)
     # Check
     assert set(poll.votes) == set([vote_1, vote_2, vote_3])
Пример #9
0
 def test_knows_its_percentage(self):
     # Setup
     u2 = UserFactory.create()
     poll = TopicPollFactory.create(topic=self.topic, max_options=2)
     option_1 = TopicPollOptionFactory.create(poll=poll)
     option_2 = TopicPollOptionFactory.create(poll=poll)
     TopicPollVoteFactory.create(poll_option=option_1, voter=self.u1)
     TopicPollVoteFactory.create(poll_option=option_2, voter=self.u1)
     TopicPollVoteFactory.create(poll_option=option_1, voter=u2)
     TopicPollVoteFactory.create(poll_option=option_2, voter=u2)
     # Run & check
     assert option_1.percentage == 50
     assert option_2.percentage == 50
Пример #10
0
 def test_inserts_the_topic_poll_in_the_context_if_applicable(self):
     # Setup
     poll = TopicPollFactory.create(topic=self.topic)
     option_1 = TopicPollOptionFactory.create(poll=poll)
     option_2 = TopicPollOptionFactory.create(poll=poll)
     correct_url = reverse(
         'forum_moderation:queued_post', kwargs={'pk': self.post.pk})
     # Run
     response = self.client.get(correct_url, follow=True)
     # Check
     assert response.status_code == 200
     assert response.context['poll'] == poll
     assert set(response.context['poll_options']) == set({option_1, option_2, })
Пример #11
0
 def test_cannot_validate_votes_for_too_many_options(self):
     # Setup
     self.poll.max_options = 3
     self.poll.save()
     option_3 = TopicPollOptionFactory.create(poll=self.poll)
     option_4 = TopicPollOptionFactory.create(poll=self.poll)
     form_data = {
         'options': [self.option_1.pk, self.option_2.pk, option_3.pk, option_4.pk, ]
     }
     # Run
     form = TopicPollVoteForm(
         data=form_data,
         poll=self.poll)
     valid = form.is_valid()
     # Check
     assert not valid
Пример #12
0
 def test_inserts_the_topic_poll_in_the_context_if_applicable(self):
     # Setup
     poll = TopicPollFactory.create(topic=self.topic)
     option_1 = TopicPollOptionFactory.create(poll=poll)
     option_2 = TopicPollOptionFactory.create(poll=poll)
     correct_url = reverse('forum_moderation:queued_post',
                           kwargs={'pk': self.post.pk})
     # Run
     response = self.client.get(correct_url, follow=True)
     # Check
     assert response.status_code == 200
     assert response.context['poll'] == poll
     assert set(response.context['poll_options']) == set({
         option_1,
         option_2,
     })
Пример #13
0
    def test_can_tell_if_an_authenticated_user_has_already_voted_in_a_poll(self):
        # Setup
        def get_rendered(poll, user):
            request = self.request_factory.get('/')
            request.user = user
            t = Template(self.loadstatement + '{% if poll|has_been_completed_by:request.user %}HAS_VOTED{% else %}HAS_NOT_VOTED{% endif %}')
            c = Context({'poll': poll, 'request': request})
            rendered = t.render(c)

            return rendered

        # Setup
        poll_option_1 = TopicPollOptionFactory.create(poll=self.poll_1)
        TopicPollOptionFactory.create(poll=self.poll_1)
        TopicPollVoteFactory.create(poll_option=poll_option_1, voter=self.u1)

        # Run & check
        assert get_rendered(self.poll_1, self.u1) == 'HAS_VOTED'
        assert get_rendered(self.poll_2, self.u1) == 'HAS_NOT_VOTED'
Пример #14
0
    def test_can_tell_if_an_authenticated_user_has_already_voted_in_a_poll(self):
        # Setup
        def get_rendered(poll, user):
            request = self.request_factory.get('/')
            request.user = user
            t = Template(
                self.loadstatement + '{% if poll|has_been_completed_by:request.user %}HAS_VOTED'
                                     '{% else %}HAS_NOT_VOTED{% endif %}')
            c = Context({'poll': poll, 'request': request})
            rendered = t.render(c)

            return rendered

        # Setup
        poll_option_1 = TopicPollOptionFactory.create(poll=self.poll_1)
        TopicPollOptionFactory.create(poll=self.poll_1)
        TopicPollVoteFactory.create(poll_option=poll_option_1, voter=self.u1)

        # Run & check
        assert get_rendered(self.poll_1, self.u1) == 'HAS_VOTED'
        assert get_rendered(self.poll_2, self.u1) == 'HAS_NOT_VOTED'
Пример #15
0
    def setup(self):
        # Permission handler
        self.perm_handler = PermissionHandler()

        # Set up a top-level forum
        self.top_level_forum = create_forum()

        # Set up a topic and some posts
        self.topic = create_topic(forum=self.top_level_forum, poster=self.user)
        self.post = PostFactory.create(topic=self.topic, poster=self.user)

        # Creates a poll and two options
        self.poll = TopicPollFactory.create(topic=self.topic)
        self.option_1 = TopicPollOptionFactory.create(poll=self.poll)
        self.option_2 = TopicPollOptionFactory.create(poll=self.poll)

        # Mark the forum as read
        ForumReadTrackFactory.create(forum=self.top_level_forum, user=self.user)

        # Assign some permissions
        assign_perm('can_read_forum', self.user, self.top_level_forum)
        assign_perm('can_vote_in_polls', self.user, self.top_level_forum)
Пример #16
0
    def setup(self):
        # Permission handler
        self.perm_handler = PermissionHandler()

        # Create a basic user
        self.user = UserFactory.create()

        # Set up a top-level forum
        self.top_level_forum = create_forum()

        # Set up a topic and some posts
        self.topic = create_topic(forum=self.top_level_forum, poster=self.user)
        self.post = PostFactory.create(topic=self.topic, poster=self.user)

        # Creates a poll and two options
        self.poll = TopicPollFactory.create(topic=self.topic)
        self.option_1 = TopicPollOptionFactory.create(poll=self.poll)
        self.option_2 = TopicPollOptionFactory.create(poll=self.poll)

        # Assign some permissions
        assign_perm('can_read_forum', self.user, self.top_level_forum)
        assign_perm('can_start_new_topics', self.user, self.top_level_forum)
Пример #17
0
    def setup(self):
        # Permission handler
        self.perm_handler = PermissionHandler()

        # Create a basic user
        self.user = UserFactory.create()

        # Set up a top-level forum
        self.top_level_forum = create_forum()

        # Set up a topic and some posts
        self.topic = create_topic(forum=self.top_level_forum, poster=self.user)
        self.post = PostFactory.create(topic=self.topic, poster=self.user)

        # Creates a poll and two options
        self.poll = TopicPollFactory.create(topic=self.topic)
        self.option_1 = TopicPollOptionFactory.create(poll=self.poll)
        self.option_2 = TopicPollOptionFactory.create(poll=self.poll)

        # Assign some permissions
        assign_perm('can_read_forum', self.user, self.top_level_forum)
        assign_perm('can_start_new_topics', self.user, self.top_level_forum)
Пример #18
0
    def test_can_if_an_anonymous_user_has_already_voted_in_a_poll(self):
        # Setup
        def get_rendered(poll, user):
            request = self.request_factory.get('/')
            request.user = user
            t = Template(self.loadstatement + '{% if poll|has_been_completed_by:request.user %}HAS_VOTED{% else %}HAS_NOT_VOTED{% endif %}')
            c = Context({'poll': poll, 'request': request})
            rendered = t.render(c)

            return rendered

        u2 = AnonymousUser()
        u2.forum_key = 'dummy'
        u3 = AnonymousUser()
        poll_option_1 = TopicPollOptionFactory.create(poll=self.poll_1)
        TopicPollOptionFactory.create(poll=self.poll_1)
        TopicPollVoteFactory.create(poll_option=poll_option_1, anonymous_key='dummy')

        # Run & check
        assert get_rendered(self.poll_1, u2) == 'HAS_VOTED'
        assert get_rendered(self.poll_2, u2) == 'HAS_NOT_VOTED'
        assert get_rendered(self.poll_2, u3) == 'HAS_NOT_VOTED'
        assert get_rendered(self.poll_2, u3) == 'HAS_NOT_VOTED'
Пример #19
0
    def test_knows_if_a_user_can_vote_again_in_a_poll(self):
        # Setup
        poll_1 = TopicPollFactory.create(topic=self.forum_1_topic, user_changes=True)
        poll_option_1 = TopicPollOptionFactory.create(poll=poll_1)
        TopicPollOptionFactory.create(poll=poll_1)

        poll_2 = TopicPollFactory.create(topic=self.forum_3_topic)
        poll_option_2 = TopicPollOptionFactory.create(poll=poll_2)
        TopicPollOptionFactory.create(poll=poll_2)

        TopicPollVoteFactory.create(poll_option=poll_option_1, voter=self.u1)
        TopicPollVoteFactory.create(poll_option=poll_option_2, voter=self.u1)

        assign_perm('can_vote_in_polls', self.u1, self.forum_1)
        assign_perm('can_vote_in_polls', self.u1, self.forum_3)
        # Run & check
        assert self.perm_handler.can_vote_in_poll(poll_1, self.u1)
        assert not self.perm_handler.can_vote_in_poll(poll_2, self.u1)
Пример #20
0
    def test_knows_if_a_user_can_vote_again_in_a_poll(self):
        # Setup
        poll_1 = TopicPollFactory.create(topic=self.forum_1_topic, user_changes=True)
        poll_option_1 = TopicPollOptionFactory.create(poll=poll_1)
        TopicPollOptionFactory.create(poll=poll_1)

        poll_2 = TopicPollFactory.create(topic=self.forum_3_topic)
        poll_option_2 = TopicPollOptionFactory.create(poll=poll_2)
        TopicPollOptionFactory.create(poll=poll_2)

        TopicPollVoteFactory.create(poll_option=poll_option_1, voter=self.u1)
        TopicPollVoteFactory.create(poll_option=poll_option_2, voter=self.u1)

        assign_perm('can_vote_in_polls', self.u1, self.forum_1)
        assign_perm('can_vote_in_polls', self.u1, self.forum_3)
        # Run & check
        assert self.perm_handler.can_vote_in_poll(poll_1, self.u1)
        assert not self.perm_handler.can_vote_in_poll(poll_2, self.u1)
Пример #21
0
    def test_knows_if_an_anonymous_user_can_vote_again_in_a_poll(self):
        # Setup
        u3 = AnonymousUser()
        u3.forum_key = 1
        poll_1 = TopicPollFactory.create(topic=self.forum_1_topic, user_changes=True)
        poll_option_1 = TopicPollOptionFactory.create(poll=poll_1)
        TopicPollOptionFactory.create(poll=poll_1)

        poll_2 = TopicPollFactory.create(topic=self.forum_3_topic)
        poll_option_2 = TopicPollOptionFactory.create(poll=poll_2)
        TopicPollOptionFactory.create(poll=poll_2)

        TopicPollVoteFactory.create(poll_option=poll_option_1, anonymous_key=1)
        TopicPollVoteFactory.create(poll_option=poll_option_2, anonymous_key=1)

        assign_perm('can_vote_in_polls', u3, self.forum_1)
        assign_perm('can_vote_in_polls', u3, self.forum_3)
        # Run & check
        assert self.perm_handler.can_vote_in_poll(poll_1, u3)
        assert not self.perm_handler.can_vote_in_poll(poll_2, u3)
Пример #22
0
    def test_knows_if_an_anonymous_user_can_vote_again_in_a_poll(self):
        # Setup
        u3 = AnonymousUser()
        u3.forum_key = 1
        poll_1 = TopicPollFactory.create(topic=self.forum_1_topic, user_changes=True)
        poll_option_1 = TopicPollOptionFactory.create(poll=poll_1)
        TopicPollOptionFactory.create(poll=poll_1)

        poll_2 = TopicPollFactory.create(topic=self.forum_3_topic)
        poll_option_2 = TopicPollOptionFactory.create(poll=poll_2)
        TopicPollOptionFactory.create(poll=poll_2)

        TopicPollVoteFactory.create(poll_option=poll_option_1, anonymous_key=1)
        TopicPollVoteFactory.create(poll_option=poll_option_2, anonymous_key=1)

        assign_perm('can_vote_in_polls', u3, self.forum_1)
        assign_perm('can_vote_in_polls', u3, self.forum_3)
        # Run & check
        assert self.perm_handler.can_vote_in_poll(poll_1, u3)
        assert not self.perm_handler.can_vote_in_poll(poll_2, u3)