def setUp(self):
        self.mk_main()
        self.request = RequestFactory().get('/')
        self.request.user = self.login()
        session_middleware = SessionMiddleware()
        session_middleware.process_request(self.request)
        self.request.session.save()

        self.section = SectionPage(title='test section')
        self.section_index.add_child(instance=self.section)

        self.important_tag = Tag(title='important tag')
        self.another_tag = Tag(title='other tag')
        self.tags = [self.important_tag, self.another_tag]
        for tag in self.tags:
            self.tag_index.add_child(instance=tag)
            tag.save_revision()

        self.page = ArticlePage(title='test article')
        self.section.add_child(instance=self.page)
        self.page.save_revision()

        for tag in self.tags:
            ArticlePageTags.objects.create(
                tag=tag,
                page=self.page,
            )

        self.adapter = PersistentFormsSegmentsAdapter(self.request)
Esempio n. 2
0
    def setUp(self):
        self.mk_main()
        self.request = RequestFactory().get('/')
        self.request.user = self.login()
        session_middleware = SessionMiddleware()
        session_middleware.process_request(self.request)
        self.request.session.save()

        self.section = SectionPage(title='test section')
        self.section_index.add_child(instance=self.section)

        self.important_tag = Tag(title='important tag')
        self.another_tag = Tag(title='other tag')
        self.tags = [self.important_tag, self.another_tag]
        for tag in self.tags:
            self.tag_index.add_child(instance=tag)
            tag.save_revision()

        self.page = ArticlePage(title='test article')
        self.section.add_child(instance=self.page)
        self.page.save_revision()

        for tag in self.tags:
            ArticlePageTags.objects.create(
                tag=tag,
                page=self.page,
            )

        self.adapter = PersistentSurveysSegmentsAdapter(self.request)
Esempio n. 3
0
 def mk_sections(self, parent, count=2, **kwargs):
     sections = []
     for i in range(count):
         data = {}
         data.update({
             'title': 'Test Section %s' % (i, ),
         })
         data.update(kwargs)
         data.update({
             'slug': generate_slug(data['title']),
         })
         section = SectionPage(**data)
         parent.add_child(instance=section)
         section.save_revision().publish()
         sections.append(section)
     return sections
Esempio n. 4
0
 def mk_sections(self, parent, count=2, **kwargs):
     sections = []
     for i in range(count):
         data = {}
         data.update({
             'title': 'Test Section %s' % (i, ),
         })
         data.update(kwargs)
         data.update({
             'slug': generate_slug(data['title']),
         })
         section = SectionPage(**data)
         parent.add_child(instance=section)
         section.save_revision().publish()
         sections.append(section)
     return sections
    def setUp(self):
        # Fabricate a request with a logged-in user
        # so we can use it to test the segment rule
        self.mk_main()
        self.request_factory = RequestFactory()
        self.request = self.request_factory.get('/')
        self.request.user = get_user_model().objects.create_user(
            username='******', email='*****@*****.**', password='******')
        middleware = SessionMiddleware()
        middleware.process_request(self.request)
        self.request.session.save()

        self.section = SectionPage(title='test section')
        self.section_index.add_child(instance=self.section)

        self.tag = Tag(title='test')
        self.tag_index.add_child(instance=self.tag)
        self.tag.save_revision()

        self.article = self.add_article(title='test article', tags=[self.tag])

        self.adapter = get_segment_adapter(self.request)
Esempio n. 6
0
    def test_section_importable(self):
        content = constants.SECTION_PAGE_RESPONSE
        content_copy = dict(content)

        # Validate Assumptions
        #   The images have already been imported
        #   The record keeper has mapped the relationship

        foreign_image_id = content["image"]["id"]
        image = Image.objects.create(
            title=content["image"]["title"],
            file=get_test_image_file(),
        )

        record_keeper = importers.RecordKeeper()
        record_keeper.record_image_relation(foreign_image_id, image.id)

        class_ = SectionPage

        page = SectionPage.create_page(content_copy,
                                       class_,
                                       record_keeper=record_keeper)

        self.assertEqual(page.title, content["title"])
        self.assertEqual(page.description, content["description"])
        self.assertEqual(page.extra_style_hints, content["extra_style_hints"])
        self.assertEqual(page.monday_rotation, content["monday_rotation"])
        self.assertEqual(page.tuesday_rotation, content["tuesday_rotation"])
        self.assertEqual(page.wednesday_rotation,
                         content["wednesday_rotation"])
        self.assertEqual(page.thursday_rotation, content["thursday_rotation"])
        self.assertEqual(page.friday_rotation, content["friday_rotation"])
        self.assertEqual(page.saturday_rotation, content["saturday_rotation"])
        self.assertEqual(page.sunday_rotation, content["sunday_rotation"])
        self.assertEqual(page.content_rotation_start_date,
                         content["content_rotation_start_date"])
        self.assertEqual(page.content_rotation_end_date,
                         content["content_rotation_end_date"])

        # NESTED FIELDS
        self.assertTrue(hasattr(page.time, "stream_data"))
        self.assertEqual(page.time.stream_data, content["time"])

        # Check that image has been added
        self.assertTrue(page.image)
        self.assertEqual(page.image.title, content["image"]["title"])
Esempio n. 7
0
    def test_section_page_question(self):
        section = SectionPage(
            title='section', slug='section', extra_style_hints='purple')
        self.main.add_child(instance=section)
        section.save_revision().publish()

        question = Question(title='is this a test')
        section.add_child(instance=question)
        question.save_revision().publish()
        # make a vote
        client = Client()
        client.login(username='******', password='******')
        response = client.get('/')
        self.assertContains(response, 'section')
        response = self.client.get(
            '/section/')
        self.assertContains(response, "is this a test")
        self.assertEquals(section.get_effective_extra_style_hints(), 'purple')
        self.assertEquals(question.get_effective_extra_style_hints(), 'purple')
Esempio n. 8
0
class TestPersistentSurveysSegmentsAdapter(TestCase, MoloTestCaseMixin):
    def setUp(self):
        self.mk_main()
        self.request = RequestFactory().get('/')
        self.request.user = self.login()
        session_middleware = SessionMiddleware()
        session_middleware.process_request(self.request)
        self.request.session.save()

        self.section = SectionPage(title='test section')
        self.section_index.add_child(instance=self.section)

        self.important_tag = Tag(title='important tag')
        self.another_tag = Tag(title='other tag')
        self.tags = [self.important_tag, self.another_tag]
        for tag in self.tags:
            self.tag_index.add_child(instance=tag)
            tag.save_revision()

        self.page = ArticlePage(title='test article')
        self.section.add_child(instance=self.page)
        self.page.save_revision()

        for tag in self.tags:
            ArticlePageTags.objects.create(
                tag=tag,
                page=self.page,
            )

        self.adapter = PersistentSurveysSegmentsAdapter(self.request)

    def test_no_exception_raised_if_user_not_set(self):
        del self.request.user
        try:
            self.adapter.add_page_visit(self.page)
        except AttributeError as e:
            self.fail('add_page_visit() raised AttributeError: {0}'.format(e))

    def test_no_pageview_stored_if_not_articlepage(self):
        self.adapter.add_page_visit(self.section)
        self.assertEqual(MoloSurveyPageView.objects.all().count(), 0)

    def test_no_pageview_stored_for_anonymous_user(self):
        self.request.user = AnonymousUser()
        self.adapter.add_page_visit(self.page)
        self.assertEqual(MoloSurveyPageView.objects.all().count(), 0)

    def test_pageview_stored(self):
        self.adapter.add_page_visit(self.page)

        pageviews = MoloSurveyPageView.objects.all()

        self.assertEqual(pageviews.count(), 1)

        self.assertEqual(pageviews[0].user, self.request.user)
        self.assertEqual(pageviews[0].page, self.page)

    def test_get_tag_count_zero_if_no_user(self):
        del self.request.user

        with self.assertNumQueries(0):
            count = self.adapter.get_tag_count(self.important_tag)

        self.assertEqual(count, 0)

    def test_get_tag_count_zero_if_anonymous_user(self):
        self.request.user = AnonymousUser()

        with self.assertNumQueries(0):
            count = self.adapter.get_tag_count(self.important_tag)

        self.assertEqual(count, 0)

    def test_get_tag_count_only_counts_current_user(self):
        another_user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******',
        )
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloSurveyPageView.objects.create(
            user=another_user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_tag_count_only_counts_specified_tag(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_tag_count_uses_date_from_if_provided(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(
            self.important_tag,
            date_from=make_aware(datetime(2099, 12, 31)),
        ), 0)

    def test_get_tag_count_uses_date_to_if_provided(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(
            self.important_tag,
            date_to=make_aware(datetime(2000, 1, 1)),
        ), 0)

    def test_get_tag_count_groups_by_unique_article(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_visit_count_zero_no_request_user(self):
        del self.request.user
        self.assertEqual(self.adapter.get_visit_count(self.page), 0)

    def test_get_visit_count_zero_for_anonymous_user(self):
        self.request.user = AnonymousUser()
        self.assertEqual(self.adapter.get_visit_count(self.page), 0)

    def test_get_visit_count_zero_if_page_not_provided(self):
        self.assertEqual(self.adapter.get_visit_count(), 0)

    def test_get_visit_counts_pageviews_for_user_and_page(self):
        MoloSurveyPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_visit_count(self.page), 1)
class TestArticleTagRuleSegmentation(TestCase, MoloTestCaseMixin):
    def setUp(self):
        # Fabricate a request with a logged-in user
        # so we can use it to test the segment rule
        self.mk_main()
        self.request_factory = RequestFactory()
        self.request = self.request_factory.get('/')
        self.request.user = get_user_model().objects.create_user(
            username='******', email='*****@*****.**', password='******')
        middleware = SessionMiddleware()
        middleware.process_request(self.request)
        self.request.session.save()

        self.section = SectionPage(title='test section')
        self.section_index.add_child(instance=self.section)

        self.tag = Tag(title='test')
        self.tag_index.add_child(instance=self.tag)
        self.tag.save_revision()

        self.article = self.add_article(title='test article', tags=[self.tag])

        self.adapter = get_segment_adapter(self.request)

    def add_article(self, title, tags):
        new_article = ArticlePage(title=title)
        self.section.add_child(instance=new_article)
        new_article.save_revision()
        for tag in tags:
            ArticlePageTags.objects.create(
                tag=tag,
                page=new_article,
            )
        return new_article

    def test_article_tag_rule_is_static(self):
        rule = ArticleTagRule(tag=self.tag, count=1)
        self.assertTrue(rule.static)

    def test_user_visits_page_with_tag(self):
        rule = ArticleTagRule(
            operator=ArticleTagRule.EQUALS,
            tag=self.tag,
            count=1,
        )

        self.adapter.add_page_visit(self.article)

        self.assertTrue(rule.test_user(self.request))

    def test_user_tag_with_no_visits(self):
        rule = ArticleTagRule(tag=self.tag, count=1)

        self.assertFalse(rule.test_user(self.request))

    def test_user_visits_page_twice_tag_not_duplicated(self):
        rule = ArticleTagRule(
            operator=ArticleTagRule.EQUALS,
            tag=self.tag,
            count=1,
        )

        self.adapter.add_page_visit(self.article)
        self.adapter.add_page_visit(self.article)

        self.assertTrue(rule.test_user(self.request))

    def test_user_visits_page_after_cutoff(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=1,
            date_to=timezone.make_aware(datetime.datetime.now() -
                                        datetime.timedelta(days=1)),
        )

        self.adapter.add_page_visit(self.article)
        self.adapter.add_page_visit(self.article)

        self.assertFalse(rule.test_user(self.request))

    def test_user_visits_two_different_pages_same_tag(self):
        rule = ArticleTagRule(
            operator=ArticleTagRule.EQUALS,
            tag=self.tag,
            count=2,
        )
        new_article = self.add_article(title='new article', tags=[self.tag])

        self.adapter.add_page_visit(self.article)
        self.adapter.add_page_visit(new_article)

        self.assertTrue(rule.test_user(self.request))

    def test_user_passes_less_than(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=2,
            operator=ArticleTagRule.LESS_THAN,
        )
        self.adapter.add_page_visit(self.article)
        self.assertTrue(rule.test_user(self.request))

    def test_user_fails_less_than(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=1,
            operator=ArticleTagRule.LESS_THAN,
        )
        self.adapter.add_page_visit(self.article)
        self.assertFalse(rule.test_user(self.request))

    def test_user_fails_greater_than(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=1,
            operator=ArticleTagRule.GREATER_THAN,
        )
        self.adapter.add_page_visit(self.article)
        self.assertFalse(rule.test_user(self.request))

    def test_user_passes_greater_than(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=0,
            operator=ArticleTagRule.GREATER_THAN,
        )
        self.adapter.add_page_visit(self.article)
        self.assertTrue(rule.test_user(self.request))

    def test_dates_are_in_order(self):
        rule = ArticleTagRule(tag=self.tag,
                              count=1,
                              date_from=datetime.datetime.now(),
                              date_to=datetime.datetime.now() -
                              datetime.timedelta(days=1))
        with self.assertRaises(ValidationError):
            rule.clean()

    def test_count_more_than_article_error(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=2,
        )
        with self.assertRaises(ValidationError):
            rule.clean()

    def test_visting_non_tagged_page_isnt_error(self):
        self.adapter.add_page_visit(self.main)
        self.assertFalse(self.request.session['tag_count'])

    def test_call_test_user_on_invalid_rule_fails(self):
        rule = ArticleTagRule()
        self.adapter.add_page_visit(self.article)
        self.assertFalse(rule.test_user(None, self.request.user))

    def test_call_test_user_without_request(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=0,
            operator=ArticleTagRule.GREATER_THAN,
        )
        self.adapter.add_page_visit(self.article)
        self.assertTrue(rule.test_user(None, self.request.user))

    def test_call_test_user_without_user_or_request(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=0,
            operator=ArticleTagRule.GREATER_THAN,
        )
        self.adapter.add_page_visit(self.article)
        self.assertFalse(rule.test_user(None))

    def test_get_column_header(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=0,
            operator=ArticleTagRule.GREATER_THAN,
        )
        self.assertEqual(rule.get_column_header(), 'Article Tag = test')

    def test_get_user_info_returns_true(self):
        rule = ArticleTagRule(
            tag=self.tag,
            count=0,
            operator=ArticleTagRule.GREATER_THAN,
        )
        self.adapter.add_page_visit(self.article)
        self.assertEqual(rule.get_user_info_string(self.request.user), '1')
class TestPersistentFormsSegmentsAdapter(TestCase, MoloTestCaseMixin):
    def setUp(self):
        self.mk_main()
        self.request = RequestFactory().get('/')
        self.request.user = self.login()
        session_middleware = SessionMiddleware()
        session_middleware.process_request(self.request)
        self.request.session.save()

        self.section = SectionPage(title='test section')
        self.section_index.add_child(instance=self.section)

        self.important_tag = Tag(title='important tag')
        self.another_tag = Tag(title='other tag')
        self.tags = [self.important_tag, self.another_tag]
        for tag in self.tags:
            self.tag_index.add_child(instance=tag)
            tag.save_revision()

        self.page = ArticlePage(title='test article')
        self.section.add_child(instance=self.page)
        self.page.save_revision()

        for tag in self.tags:
            ArticlePageTags.objects.create(
                tag=tag,
                page=self.page,
            )

        self.adapter = PersistentFormsSegmentsAdapter(self.request)

    def test_no_exception_raised_if_user_not_set(self):
        del self.request.user
        try:
            self.adapter.add_page_visit(self.page)
        except AttributeError as e:
            self.fail('add_page_visit() raised AttributeError: {0}'.format(e))

    def test_no_pageview_stored_if_not_articlepage(self):
        self.adapter.add_page_visit(self.section)
        self.assertEqual(MoloFormPageView.objects.all().count(), 0)

    def test_no_pageview_stored_for_anonymous_user(self):
        self.request.user = AnonymousUser()
        self.adapter.add_page_visit(self.page)
        self.assertEqual(MoloFormPageView.objects.all().count(), 0)

    def test_pageview_stored(self):
        self.adapter.add_page_visit(self.page)

        pageviews = MoloFormPageView.objects.all()

        self.assertEqual(pageviews.count(), 1)

        self.assertEqual(pageviews[0].user, self.request.user)
        self.assertEqual(pageviews[0].page, self.page)

    def test_get_tag_count_zero_if_no_user(self):
        del self.request.user

        with self.assertNumQueries(0):
            count = self.adapter.get_tag_count(self.important_tag)

        self.assertEqual(count, 0)

    def test_get_tag_count_zero_if_anonymous_user(self):
        self.request.user = AnonymousUser()

        with self.assertNumQueries(0):
            count = self.adapter.get_tag_count(self.important_tag)

        self.assertEqual(count, 0)

    def test_get_tag_count_only_counts_current_user(self):
        another_user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******',
        )
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloFormPageView.objects.create(
            user=another_user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_tag_count_only_counts_specified_tag(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_tag_count_uses_date_from_if_provided(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(
            self.adapter.get_tag_count(
                self.important_tag,
                date_from=make_aware(datetime(2099, 12, 31)),
            ), 0)

    def test_get_tag_count_uses_date_to_if_provided(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(
            self.adapter.get_tag_count(
                self.important_tag,
                date_to=make_aware(datetime(2000, 1, 1)),
            ), 0)

    def test_get_tag_count_groups_by_unique_article(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_tag_count(self.important_tag), 1)

    def test_get_visit_count_zero_no_request_user(self):
        del self.request.user
        self.assertEqual(self.adapter.get_visit_count(self.page), 0)

    def test_get_visit_count_zero_for_anonymous_user(self):
        self.request.user = AnonymousUser()
        self.assertEqual(self.adapter.get_visit_count(self.page), 0)

    def test_get_visit_count_zero_if_page_not_provided(self):
        self.assertEqual(self.adapter.get_visit_count(), 0)

    def test_get_visit_counts_pageviews_for_user_and_page(self):
        MoloFormPageView.objects.create(
            user=self.request.user,
            page=self.page,
        )
        self.assertEqual(self.adapter.get_visit_count(self.page), 1)
Esempio n. 11
0
    def test_section_importable(self):
        content = constants.SECTION_PAGE_RESPONSE
        content_copy = dict(content)

        # Validate Assumptions
        #   The images have already been imported
        #   The record keeper has mapped the relationship

        foreign_image_id = content["image"]["id"]
        image = Image.objects.create(
            title=content["image"]["title"],
            file=get_test_image_file(),
        )

        record_keeper = importers.RecordKeeper()
        record_keeper.record_image_relation(foreign_image_id, image.id)

        class_ = SectionPage

        page = SectionPage.create_page(
            content_copy, class_, record_keeper=record_keeper)

        self.assertEqual(page.title, content["title"])
        self.assertEqual(page.description, content["description"])
        self.assertEqual(page.extra_style_hints,
                         content["extra_style_hints"])
        self.assertEqual(page.commenting_state, content["commenting_state"])
        self.assertEqual(page.monday_rotation, content["monday_rotation"])
        self.assertEqual(page.tuesday_rotation, content["tuesday_rotation"])
        self.assertEqual(page.wednesday_rotation,
                         content["wednesday_rotation"])
        self.assertEqual(page.thursday_rotation,
                         content["thursday_rotation"])
        self.assertEqual(page.friday_rotation, content["friday_rotation"])
        self.assertEqual(page.saturday_rotation,
                         content["saturday_rotation"])
        self.assertEqual(page.sunday_rotation, content["sunday_rotation"])

        self.assertEqual(page.commenting_open_time,
                         content["commenting_open_time"])
        self.assertEqual(page.commenting_close_time,
                         content["commenting_close_time"])
        self.assertEqual(page.content_rotation_start_date,
                         content["content_rotation_start_date"])
        self.assertEqual(page.content_rotation_end_date,
                         content["content_rotation_end_date"])

        # NESTED FIELDS
        self.assertTrue(hasattr(page.time, "stream_data"))
        self.assertEqual(page.time.stream_data, content["time"])

        # Check that image has been added
        self.assertTrue(page.image)
        self.assertEqual(page.image.title, content["image"]["title"])

        # Check that foreign relationships have been created
        self.assertTrue(
            content["id"] in
            record_keeper.foreign_to_many_foreign_map["section_tags"])
        self.assertEqual(
            (record_keeper.foreign_to_many_foreign_map["section_tags"]
                [content["id"]]),
            [content["section_tags"][0]["tag"]["id"],
             content["section_tags"][1]["tag"]["id"]])