Ejemplo n.º 1
0
 def test_search_only_includes_articles(self):
     self.backend = get_search_backend('default')
     self.backend.reset_index()
     self.mk_articles(self.english_section, count=2)
     footer = FooterPage(title='Test Footer')
     self.footer_index.add_child(instance=footer)
     footer.save_revision().publish()
     self.backend.refresh_index()
     response = self.client.get(reverse('search'), {
         'q': 'Test'
     })
     results = response.context['results']
     for article in results:
         self.assertNotEqual(article.title, 'Test Footer')
Ejemplo n.º 2
0
 def test_search_only_includes_articles(self):
     self.backend = get_search_backend('default')
     self.backend.reset_index()
     self.mk_articles(self.english_section, count=2)
     footer = FooterPage(title='Test Footer')
     self.footer_index.add_child(instance=footer)
     footer.save_revision().publish()
     self.backend.refresh_index()
     response = self.client.get(reverse('search'), {
         'q': 'Test'
     })
     results = response.context['results']
     for article in results:
         self.assertNotEqual(article.title, 'Test Footer')
Ejemplo n.º 3
0
    def test_latest_rotation_no_start_or_end_date(self):
        """This test that if the weekdays and times are set for
        content rotation, that the content doesn't rotates with no dates set"""
        site = Site.objects.get(is_default_site=True)
        settings = SettingsProxy(site)
        site_settings = settings['core']['SiteSettings']
        site_settings.monday_rotation = True
        site_settings.tuesday_rotation = True
        site_settings.wednesday_rotation = True
        site_settings.thursday_rotation = True
        site_settings.friday_rotation = True
        site_settings.saturday_rotation = True
        site_settings.sunday_rotation = True
        site_settings.save()

        for i in range(5):
            self.footer = FooterPage(title='Footer Page %s',
                                     slug='footer-page-%s' % (i, ))
            self.footer_index.add_child(instance=self.footer)

        self.assertEquals(FooterPage.objects.live().count(), 5)
        self.assertEquals(self.main.latest_articles().count(), 0)

        self.mk_articles(self.yourmind_sub, count=10, featured_in_latest=True)
        self.mk_articles(self.yourmind_sub, count=10, featured_in_latest=False)
        self.assertEquals(self.main.latest_articles().count(), 10)
        first_article_old = self.main.latest_articles()[0].pk
        last_article_old = self.main.latest_articles()[9].pk
        rotate_content()
        self.assertEquals(first_article_old, self.main.latest_articles()[0].pk)
        self.assertEquals(last_article_old, self.main.latest_articles()[9].pk)
Ejemplo n.º 4
0
    def test_footer_importable(self):
        content = constants.ARTICLE_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(),
        )

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

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

        class_ = FooterPage

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

        self.check_article_and_footer_fields(page, content, record_keeper)
Ejemplo n.º 5
0
    def test_latest_rotation_no_time(self):
        """This test that if the date range and weekdays are set for
        content rotation, that the content doesn't rotates with no times set"""
        site = Site.objects.get(is_default_site=True)
        site_settings = SiteSettings.for_site(site)

        site_settings.monday_rotation = True
        site_settings.content_rotation_start_date = timezone.now()
        site_settings.content_rotation_end_date = timezone.now() + timedelta(
            days=1)
        site_settings.save()

        for i in range(5):
            self.footer = FooterPage(title='Footer Page %s',
                                     slug='footer-page-%s' % (i, ))
            self.footer_index.add_child(instance=self.footer)

        self.assertEqual(FooterPage.objects.live().count(), 5)
        self.assertEqual(self.main.latest_articles().count(), 0)

        self.mk_articles(self.yourmind_sub,
                         count=10,
                         featured_in_latest_start_date=timezone.now())
        promote_articles()
        self.mk_articles(self.yourmind_sub, count=10, featured_in_latest=False)
        self.assertEqual(self.main.latest_articles().count(), 10)
        first_article_old = self.main.latest_articles()[0].pk
        last_article_old = self.main.latest_articles()[9].pk
        rotate_content(0)
        self.assertEqual(first_article_old, self.main.latest_articles()[0].pk)
        self.assertEqual(last_article_old, self.main.latest_articles()[9].pk)
Ejemplo n.º 6
0
    def test_latest_rotation_on_multisite(self):
        """This test that if the date range, weekdays and times are set for
        content rotation, that the content rotates accordingly"""
        # sets the site settings
        site = self.main2.get_site()
        settings = SettingsProxy(site)
        site_settings = settings['core']['SiteSettings']

        site_settings.content_rotation_start_date = timezone.now()
        site_settings.content_rotation_end_date = timezone.now() + timedelta(
            days=1)
        time1 = str(timezone.now().time())[:8]
        time2 = str((timezone.now() + timedelta(minutes=1)).time())[:8]
        site_settings.time = dumps([{
            'type': 'time',
            'value': time1
        }, {
            'type': 'time',
            'value': time2
        }])
        site_settings.monday_rotation = True
        site_settings.save()

        # creates articles and pages, some set to feature in latest, others not
        for i in range(5):
            self.footer = FooterPage(title='Footer Page %s',
                                     slug='footer-page-%s' % (i, ))
            self.footer_index.add_child(instance=self.footer)

        self.assertEqual(FooterPage.objects.live().count(), 5)
        self.assertEqual(self.main.latest_articles().count(), 0)

        self.mk_articles(self.yourmind_sub22,
                         count=10,
                         featured_in_latest_start_date=timezone.now())
        promote_articles()
        self.mk_articles(self.yourmind_sub22,
                         count=10,
                         featured_in_latest=False)
        self.assertEqual(self.main2.latest_articles().count(), 10)
        # gets the first and last articles of the list before it rotates
        first_article_old = self.main2.latest_articles()[0].pk
        last_article_old = self.main2.latest_articles()[9].pk

        rotate_content(day=0)

        # checks to see that the number of latest articles has not increased
        self.assertEqual(self.main2.latest_articles().count(), 10)
        # checks to see the the old first articles is not still the first one
        self.assertNotEqual(first_article_old,
                            self.main2.latest_articles()[0].pk)
        # checks to see the old first article has moved up 2 places
        self.assertEqual(first_article_old, self.main2.latest_articles()[2].pk)
        # checks to see the the old last article is not still last
        self.assertNotEqual(last_article_old,
                            self.main2.latest_articles()[8].pk)

        featured_from_main1 = self.main2.latest_articles().descendant_of(
            self.main).count()
        self.assertEqual(featured_from_main1, 0)
Ejemplo n.º 7
0
    def test_footer_importable(self):
        content = constants.ARTICLE_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(),
        )

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

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

        class_ = FooterPage

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

        self.check_article_and_footer_fields(page, content, record_keeper)
Ejemplo n.º 8
0
    def setUp(self):
        self.main = self.mk_main(
            title='main1', slug='main1', path='00010002', url_path='/main1/')
        self.client = Client(HTTP_HOST=self.main.get_site().hostname)

        self.site_settings = SiteSettings.for_site(self.main.get_site())
        self.site_settings.local_ga_tracking_code = 'local_ga_tracking_code'
        self.site_settings.save()

        GemSettings.objects.create(
            site_id=self.main.get_site().id,
            bbm_ga_account_subdomain='bbm',
            bbm_ga_tracking_code='bbm_tracking_code',
        )
        self.yourmind = self.mk_section(
            SectionIndexPage.objects.child_of(self.main).first(),
            title='Your mind')

        self.article = self.mk_article(
            self.yourmind, title='article',
            subtitle='article with nav_tags',
            slug='article')
        self.article.save_revision().publish()
        self.article2 = self.mk_article(
            self.yourmind, title='article2',
            subtitle='artitle without nav_tags',
            slug='article2')
        self.article2.save_revision().publish()

        self.tag_index = TagIndexPage.objects.child_of(self.main).first()
        self.tag = Tag(title='Tag1')
        self.tag2 = Tag(title='Tag2')
        self.tag_index.add_child(instance=self.tag)
        self.tag.save_revision().publish()
        self.tag_index.add_child(instance=self.tag2)
        self.tag2.save_revision().publish()

        self.article.nav_tags.create(tag=self.tag)
        self.article.save_revision().publish()
        self.article.nav_tags.create(tag=self.tag2)
        self.article.save_revision().publish()
        # get footerpage
        self.footer_index = FooterIndexPage.objects.child_of(self.main).first()
        self.footer = FooterPage(title='Test Footer Page')
        self.footer_index.add_child(instance=self.footer)
        self.footer.save_revision().publish()
        self.response = self.client.get('/')
Ejemplo n.º 9
0
    def test_footer_pages(self):
        self.footer = FooterPage(title='Footer Page', slug='footer-page')
        self.footer_index.add_child(instance=self.footer)
        footer_french = self.mk_article_translation(
            self.footer, self.french, title='Footer Page in french')

        response = self.client.get('/')
        self.assertContains(response, 'Footer Page')
        self.assertContains(
            response, '<a href="/footer-pages/footer-page/">Footer Page</a>')
        self.assertNotContains(
            response,
            '<a href="/%s/">Footer Page in french</a>' % footer_french.slug)

        response = self.client.get('/sections/your-mind/your-mind-subsection/')
        self.assertContains(response, 'Footer Page')
        self.assertNotContains(response, 'Footer Page in french')
Ejemplo n.º 10
0
    def test_latest_rotation_no_valid_days(self):
        """This test that if the date range and times are set for
        content rotation, that it doesn't rotate without any weekdays set"""
        site = Site.objects.get(is_default_site=True)
        settings = SettingsProxy(site)
        site_settings = settings['core']['SiteSettings']
        site_settings.monday_rotation = True
        site_settings.content_rotation_start_date = timezone.now()
        site_settings.content_rotation_end_date = timezone.now() + timedelta(
            days=1)
        time1 = str(timezone.now().time())[:8]
        time2 = str((timezone.now() + timedelta(minutes=1)).time())[:8]
        site_settings.time = dumps([{
            'type': 'time',
            'value': time1
        }, {
            'type': 'time',
            'value': time2
        }])
        site_settings.save()

        for i in range(5):
            self.footer = FooterPage(title='Footer Page %s',
                                     slug='footer-page-%s' % (i, ))
            self.footer_index.add_child(instance=self.footer)

        self.assertEqual(FooterPage.objects.live().count(), 5)
        self.assertEqual(self.main.latest_articles().count(), 0)

        self.mk_articles(self.yourmind_sub,
                         count=10,
                         featured_in_latest_start_date=timezone.now())
        promote_articles()
        self.mk_articles(self.yourmind_sub, count=10, featured_in_latest=False)
        self.assertEqual(self.main.latest_articles().count(), 10)
        first_article_old = self.main.latest_articles()[0].pk
        last_article_old = self.main.latest_articles()[9].pk
        rotate_content(4)
        self.assertEqual(first_article_old, self.main.latest_articles()[0].pk)
        self.assertEqual(last_article_old, self.main.latest_articles()[9].pk)
Ejemplo n.º 11
0
    def handle(self, *args, **options):
        for main in Main.objects.all():
            print(
                "*" * 10,
                "Migrating Surveys in",
                main,
                "*" * 10,
            )
            surveys_index = SurveysIndexPage.objects.child_of(main).first()
            forms_index = FormsIndexPage.objects.descendant_of(main).filter(
                slug='surveys-indexpage').first()
            surveys_tc = TermsAndConditionsIndexPage.objects.child_of(
                surveys_index).first()

            # Copy the Survey Ts&Cs to Forms
            if surveys_tc:
                forms_tc = FormsTermsAndConditionsIndexPage.objects.filter(
                    slug='terms-conditions-indexpage').child_of(
                        forms_index).first()
                forms_index.add_child(instance=forms_tc)
                forms_tc.save_revision().publish()
                for footerpage in FooterPage.objects.child_of(surveys_tc):
                    footer_dict = {}
                    exclude = (
                        '_state',
                        'id',
                        'path',
                        'numchild',
                        'page_ptr_id',
                        'content_type_id',
                        'articlepage_ptr_id',
                        '_language_cache',
                    )
                    for item in footerpage.__dict__.items():
                        if item[0] not in exclude:
                            footer_dict[item[0]] = item[1]

                    footer_page = FooterPage(**footer_dict)
                    forms_tc.add_child(instance=footer_page)
                    if footerpage.status_string == 'draft' \
                            or footerpage.status_string == 'expired':
                        footer_page.specific.unpublish()
                    else:
                        footer_page.save_revision().publish()
            print("Copying of Survey Ts&Cs is Done")

            # Migrate Survey Page to Form Page
            translated_survey = {}
            for survey in MoloSurveyPage.objects.descendant_of(
                    surveys_index).exact_type(MoloSurveyPage):
                if survey.language.is_main_language and\
                     survey.translated_pages.first():
                    translated_survey[survey.slug] = \
                        survey.translated_pages.first().slug

                survey_dict = {}
                exclude = ('_state', 'id', 'path', 'numchild', 'page_ptr_id',
                           'content_type_id', 'landing_page_template',
                           '_language_cache')
                for item in survey.__dict__.items():
                    if item[0] not in exclude:
                        survey_dict[item[0]] = item[1]

                survey_id = survey.id
                survey_dict['slug'] = "form-" + survey_dict['slug']
                survey_dict['display_form_directly'] = survey_dict[
                    'display_survey_directly']
                del survey_dict['display_survey_directly']

                form = MoloFormPage(**survey_dict)
                forms_index.add_child(instance=form)
                if survey.status_string == 'draft'\
                        or survey.status_string == 'expired':
                    form.specific.unpublish()
                else:
                    form.save_revision().publish()

                if survey.terms_and_conditions.first():
                    form_tc = FooterPage.objects.child_of(forms_tc).filter(
                        title=survey.terms_and_conditions.first(
                        ).terms_and_conditions.title)
                    survey.terms_and_conditions.first().terms_and_conditions
                    FormTermsConditions.objects.create(
                        page=form, terms_and_conditions=form_tc.first())

                for form_field in MoloSurveyFormField.objects.filter(
                        page_id=survey_id):
                    form_field_dict = {}
                    for item in form_field.__dict__.items():
                        form_field_dict[item[0]] = item[1]
                    form_field_dict['page_id'] = form.id
                    del form_field_dict['_state']
                    del form_field_dict['id']

                    MoloFormField.objects.create(**form_field_dict)

                for submission in MoloSurveySubmission.objects.filter(
                        page_id=survey_id):
                    submission_dict = {}
                    for item in submission.__dict__.items():
                        submission_dict[item[0]] = item[1]

                    del submission_dict['_state']
                    del submission_dict['id']
                    submission_dict['submit_time'] = submission_dict[
                        'created_at']
                    del submission_dict['created_at']
                    submission_dict['page_id'] = form.id
                    form_submission = MoloFormSubmission.objects.create(
                        **submission_dict)
                    form_submission.submit_time =\
                        submission_dict['submit_time']
                    form_submission.save()

            for key in translated_survey:
                main_form = MoloFormPage.objects.descendant_of(
                    forms_index).get(slug="form-%s" % key).specific
                translated_form = MoloFormPage.objects.descendant_of(
                    forms_index).get(slug="form-%s" %
                                     translated_survey[key]).specific
                main_form.translated_pages.add(translated_form)
                translated_form.translated_pages.add(main_form)
            print("Migration of SurveyPage is Done")

            # Migrate Personalisable Survey to Form
            translated_survey = {}
            for personalisable_survey in \
                PersonalisableSurvey.objects.descendant_of(
                    surveys_index).exact_type(PersonalisableSurvey):
                if personalisable_survey.language.is_main_language and \
                     personalisable_survey.translated_pages.first():
                    translated_survey[personalisable_survey.slug] = \
                        personalisable_survey.translated_pages.first().slug

                personalisable_survey_dict = {}
                exclude = ('_state', 'id', 'content_type_id',
                           'molosurveypage_ptr_id', 'numchild',
                           'landing_page_template', 'page_ptr_id',
                           '_language_cache')
                for item in personalisable_survey.__dict__.items():
                    if item[0] not in exclude:
                        personalisable_survey_dict[item[0]] = item[1]

                personalisable_survey_id = personalisable_survey.id
                personalisable_survey_dict['slug'] = \
                    "form-" + personalisable_survey_dict['slug']
                personalisable_survey_dict['display_form_directly'] \
                    = personalisable_survey_dict[
                        'display_survey_directly']
                del personalisable_survey_dict['display_survey_directly']

                personalisable_form = PersonalisableForm(
                    **personalisable_survey_dict)
                forms_index.add_child(instance=personalisable_form)
                if personalisable_survey.status_string == 'draft'\
                        or personalisable_survey.status_string == 'expired':
                    personalisable_form.specific.unpublish()
                else:
                    personalisable_form.save_revision().publish()

                if personalisable_survey.terms_and_conditions.first():
                    form_tc = FooterPage.objects.child_of(forms_tc).filter(
                        title=personalisable_survey.terms_and_conditions.first(
                        ).terms_and_conditions.title)
                    personalisable_survey.terms_and_conditions.first()\
                        .terms_and_conditions
                    FormTermsConditions.objects.create(
                        page=personalisable_form,
                        terms_and_conditions=form_tc.first())

                for personalisable_form_field in \
                    PersonalisableSurveyFormField.objects.filter(
                        page_id=personalisable_survey_id):
                    personalisable_form_field_dict = {}

                    for item in personalisable_form_field.__dict__.items():
                        personalisable_form_field_dict[item[0]] = item[1]
                    personalisable_form_field_dict[
                        'page_id'] = PersonalisableForm.objects.filter(
                            id=personalisable_form.id).first().id
                    del personalisable_form_field_dict['_state']
                    del personalisable_form_field_dict['id']

                    PersonalisableFormField.objects.create(
                        **personalisable_form_field_dict)
                for submission in \
                    MoloSurveySubmission.objects.filter(
                        page_id=personalisable_survey_id):
                    submission_dict = {}
                    for item in submission.__dict__.items():
                        submission_dict[item[0]] = item[1]
                    del submission_dict['_state']
                    del submission_dict['id']
                    submission_dict['submit_time'] = \
                        submission_dict['created_at']
                    del submission_dict['created_at']
                    submission_dict['page_id'] = personalisable_form.id
                    form_submission = MoloFormSubmission.objects.create(
                        **submission_dict)
                    form_submission.submit_time =\
                        submission_dict['submit_time']
                    form_submission.save()
            for key in translated_survey:
                main_form = PersonalisableForm.objects.descendant_of(
                    forms_index).get(slug="form-%s" % key).specific
                translated_form = PersonalisableForm.objects.descendant_of(
                    forms_index).get(slug="form-%s" %
                                     translated_survey[key]).specific
                main_form.translated_pages.add(translated_form)
                translated_form.translated_pages.add(main_form)
            print("Migration of Personalisable Survey is Done")

        # Migrate Survey User Group to Form
        for segment_user_group in SegmentUserGroup.objects.all():
            segment_user_group_dict = {}
            for item in segment_user_group.__dict__.items():
                segment_user_group_dict[item[0]] = item[1]
            del segment_user_group_dict['_state']
            del segment_user_group_dict['id']
            segment_user_group_dict['name'] = \
                "form-" + segment_user_group_dict['name']

            FormsSegmentUserGroup.objects.create(**segment_user_group_dict)
        print("Migration of User Group is Done")

        # Migrate Survey Page View
        for view in MoloSurveyPageView.objects.all().iterator():
            view_dict = {}
            for item in view.__dict__.items():
                if item[0] not in ('id', '_state'):
                    view_dict[item[0]] = item[1]
            form_page_view = MoloFormPageView(**view_dict)
            form_page_view.save()
            form_page_view.visited_at = view_dict["visited_at"]
            form_page_view.save()
        print("Migration of PageView is Done")

        # Migrate Survey Rules to Form
        for segment in Segment.objects.all().iterator():
            submission_rules = \
                SurveySubmissionDataRule.objects.filter(
                    segment_id=segment.id).all()
            for submission_rule in submission_rules:
                rule_dict = {}
                for item in submission_rule.__dict__.items():
                    if item[0] not in ('id', '_state'):
                        rule_dict[item[0]] = item[1]
                survey_slug = MoloSurveyPage.objects.filter(
                    id=rule_dict["survey_id"]).first().slug
                form_id = MoloFormPage.objects.filter(slug="form-" +
                                                      survey_slug).first().id
                rule_dict["form_id"] = form_id
                del rule_dict["survey_id"]
                FormSubmissionDataRule.objects.create(**rule_dict)
                submission_rule.delete()
                segment.save()

            response_rules = SurveyResponseRule.objects.filter(
                segment_id=segment.id).all()
            for response_rule in response_rules:
                rule_dict = {}
                for item in response_rule.__dict__.items():
                    if item[0] not in ('id', '_state'):
                        rule_dict[item[0]] = item[1]
                survey_slug = MoloSurveyPage.objects.filter(
                    id=rule_dict["survey_id"]).first().slug
                form_id = MoloFormPage.objects.filter(slug="form-" +
                                                      survey_slug).first().id
                rule_dict["form_id"] = form_id
                del rule_dict["survey_id"]
                FormResponseRule.objects.create(**rule_dict)
                response_rule.delete()
                segment.save()

            article_rules = ArticleTagRule.objects.filter(
                segment_id=segment.id).all()
            for article_rule in article_rules:
                rule_dict = {}
                for item in article_rule.__dict__.items():
                    if item[0] not in ('id', '_state'):
                        rule_dict[item[0]] = item[1]
                FormsArticleTagRule.objects.create(**rule_dict)
                article_rule.delete()
                segment.save()

            combination_rules = CombinationRule.objects.filter(
                segment_id=segment.id).all()
            for combination_rule in combination_rules:
                rule_dict = {}
                for item in combination_rule.__dict__.items():
                    if item[0] not in ('id', '_state'):
                        rule_dict[item[0]] = item[1]
                FormCombinationRule.objects.create(**rule_dict)
                combination_rule.delete()
                segment.save()

            group_rules = GroupMembershipRule.objects.filter(
                segment_id=segment.id).all()
            for group_rule in group_rules:
                rule_dict = {}
                for item in group_rule.__dict__.items():
                    if item[0] not in ('id', '_state'):
                        rule_dict[item[0]] = item[1]
                survey_group = SegmentUserGroup.objects.filter(
                    id=rule_dict["group_id"]).first()
                form_group = FormsSegmentUserGroup.objects.filter(
                    name="form-" + survey_group.name).first()
                rule_dict["group_id"] = form_group.id
                FormGroupMembershipRule.objects.create(**rule_dict)
                group_rule.delete()
                segment.save()
        print("Migration of Survey Rules to Form is Done")
        print("*" * 10, "Migration is Done", "*" * 10)
Ejemplo n.º 12
0
class TestCustomGemMiddleware(TestCase, GemTestCaseMixin):

    submit_tracking_method = (
        "gem.middleware.GemMoloGoogleAnalyticsMiddleware.submit_tracking"
    )

    def setUp(self):
        self.main = self.mk_main(
            title='main1', slug='main1', path='00010002', url_path='/main1/')
        self.client = Client(HTTP_HOST=self.main.get_site().hostname)

        self.site_settings = SiteSettings.for_site(self.main.get_site())
        self.site_settings.local_ga_tracking_code = 'local_ga_tracking_code'
        self.site_settings.save()

        GemSettings.objects.create(
            site_id=self.main.get_site().id,
            bbm_ga_account_subdomain='bbm',
            bbm_ga_tracking_code='bbm_tracking_code',
        )
        self.yourmind = self.mk_section(
            SectionIndexPage.objects.child_of(self.main).first(),
            title='Your mind')

        self.article = self.mk_article(
            self.yourmind, title='article',
            subtitle='article with nav_tags',
            slug='article')
        self.article.save_revision().publish()
        self.article2 = self.mk_article(
            self.yourmind, title='article2',
            subtitle='artitle without nav_tags',
            slug='article2')
        self.article2.save_revision().publish()

        self.tag_index = TagIndexPage.objects.child_of(self.main).first()
        self.tag = Tag(title='Tag1')
        self.tag2 = Tag(title='Tag2')
        self.tag_index.add_child(instance=self.tag)
        self.tag.save_revision().publish()
        self.tag_index.add_child(instance=self.tag2)
        self.tag2.save_revision().publish()

        self.article.nav_tags.create(tag=self.tag)
        self.article.save_revision().publish()
        self.article.nav_tags.create(tag=self.tag2)
        self.article.save_revision().publish()
        # get footerpage
        self.footer_index = FooterIndexPage.objects.child_of(self.main).first()
        self.footer = FooterPage(title='Test Footer Page')
        self.footer_index.add_child(instance=self.footer)
        self.footer.save_revision().publish()
        self.response = self.client.get('/')

    @patch(submit_tracking_method)
    def test_submit_to_additional_ga_account(self, mock_submit_tracking):
        '''
        Given that bbm_ga_account_subdomain and bbm_ga_tracking_code
        are set in Gem Settings, and the URL contains the
        bbm_ga_account_subdomain, info should be sent to
        the additional GA account.
        '''

        request = RequestFactory().get(
            '/',
            HTTP_HOST='bbm.localhost',
            HTTP_X_DCMGUID="0000-000-01"
        )
        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.submit_to_local_account(
            request, self.response, self.site_settings)

        mock_submit_tracking.assert_called_once_with(
            'bbm_tracking_code',
            request, self.response, {"cd3": 'Visitor', 'cd1': "0000-000-01"})

    @patch(submit_tracking_method)
    def test_submit_to_bbm_analytics_if_cookie_set(self, mock_submit_tracking):
        '''
        Given that bbm_ga_account_subdomain and bbm_ga_tracking_code
        are set in Gem Settings, and the BBM cookie is set, info
        should be sent to the additional GA account.
        '''

        request = RequestFactory().get(
            '/',
            HTTP_HOST='localhost',
            HTTP_X_DCMGUID="0000-000-01"
        )
        request.COOKIES['bbm'] = 'true'
        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.submit_to_local_account(
            request, self.response, self.site_settings)

        mock_submit_tracking.assert_called_once_with(
            'bbm_tracking_code',
            request, self.response, {"cd3": 'Visitor', 'cd1': "0000-000-01"})

    @patch(submit_tracking_method)
    def test_submit_to_local_ga_account(self, mock_submit_tracking):
        '''
        Given that bbm_ga_account_subdomain and bbm_ga_tracking_code
        are set in Gem Settings, and the URL does not contain the
        bbm_ga_account_subdomain, info should be sent to
        the local GA account, not the additional GA account.
        '''

        request = RequestFactory().get(
            '/',
            HTTP_HOST='localhost',
            HTTP_X_DCMGUID="0000-000-01",)
        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.submit_to_local_account(
            request, self.response, self.site_settings)

        mock_submit_tracking.assert_called_once_with(
            'local_ga_tracking_code',
            request,
            self.response,
            {"cd3": 'Visitor', 'cd1': "0000-000-01"}
        )

    @patch(submit_tracking_method)
    def test_submit_to_local_ga_account__custom_params(self,
                                                       mock_submit_tracking):
        '''
        Given that bbm_ga_account_subdomain and bbm_ga_tracking_code
        are set in Gem Settings, and the URL does not contain the
        bbm_ga_account_subdomain, info should be sent to
        the local GA account, not the additional GA account.
        '''
        # create a user with a profile
        self.user = User.objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******')
        self.client.login(username='******', password='******')
        self.user.profile.gender = 'f'

        self.user.profile.save()

        request = RequestFactory().get(
            '/',
            HTTP_HOST='localhost',
            HTTP_X_DCMGUID="0000-000-01",
        )
        request.site = self.main.get_site()
        request.user = self.user
        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.submit_to_local_account(
            request, self.response, self.site_settings)
        cd1 = middleware.get_visitor_id(request)
        mock_submit_tracking.assert_called_once_with(
            'local_ga_tracking_code',
            request,
            self.response,
            {"cd3": 'Registered', 'cd2': self.user.profile.uuid, 'cd1': cd1})

    @patch(submit_tracking_method)
    def test_submit_to_local_ga__ignored_info(self, mock_submit_tracking):
        '''
        Paths in GOOGLE_ANALYTICS_IGNORE_PATH should not invoke
        a call to google analytics tracking
        '''

        request = RequestFactory().get(
            '/profiles/password-reset/',
            HTTP_HOST='localhost',
        )
        request.site = self.main.get_site()
        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.process_response(
            request, self.response)
        # test that the tracking method is not called with senstive info
        mock_submit_tracking.assert_not_called()

        request = RequestFactory().get(
            '/profiles/reset-success/',
            HTTP_HOST='localhost',
        )
        middleware.process_response(
            request, self.response)
        mock_submit_tracking.assert_not_called()

        request = RequestFactory().get(
            '/profiles/reset-password/',
            HTTP_HOST='localhost',
        )
        middleware.process_response(
            request, self.response)
        mock_submit_tracking.assert_not_called()

    @patch(submit_tracking_method)
    def test_submit_to_local_ga__sensitive_info(self, mock_submit_tracking):
        '''
        Paths with sensitive information should not
        be tracked on google analytics
        '''

        request = RequestFactory().get(
            '/search/?q=user%40user.com',
            HTTP_HOST='localhost',
        )
        request.site = self.main.get_site()
        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.process_response(
            request, self.response)
        mock_submit_tracking.assert_not_called()

    @patch(submit_tracking_method)
    def test_submit_to_local_ga_valid_info(self, mock_submit_tracking):

        request = RequestFactory().get(
            '/search/?q=whatislife',
            HTTP_HOST='localhost',
            HTTP_X_DCMGUID="0000-000-01"
        )
        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.process_response(
            request, self.response)
        # a normal response should activate GA tracking
        mock_submit_tracking.assert_called_once_with(
            'local_ga_tracking_code',
            request, self.response,
            {"cd3": 'Visitor', 'cd1': "0000-000-01"})

    @patch(submit_tracking_method)
    def test_submit_to_local_ga_articlepage_title(
            self, mock_submit_tracking):
        """requests for article with tags should
        make a submit tracking with a cd6 value in the
        custom params containing all the article tags"""

        request = RequestFactory().get(
            '/sections-main1-1/{}/{}/'.format(
                self.yourmind.slug,
                self.article2.slug),
            HTTP_HOST='localhost',
            HTTP_X_DCMGUID="0000-000-01"
        )
        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.process_response(
            request, self.response)
        # a normal response should activate GA tracking
        mock_submit_tracking.assert_called_once_with(
            'local_ga_tracking_code',
            request, self.response,
            {'cd5': self.article2.title,
                "cd3": 'Visitor',
                'cd1': "0000-000-01",
             })

    @patch(submit_tracking_method)
    def test_submit_to_local_ga_translated_articlepage_title(
            self, mock_submit_tracking):
        """requests for article with tags should
        make a submit tracking with a cd6 value in the
        custom params containing all the article tags"""

        french = SiteLanguageRelation.objects.create(
            language_setting=Languages.for_site(Site.objects.first()),
            locale='fr',
            is_active=True)
        french_article = self.mk_article_translation(self.article2, french)
        french_article.title = "french translation of article"
        french_article.save_revision().publish()
        request = RequestFactory().get(
            '/sections-main1-1/{}/{}/'.format(
                self.yourmind.slug,
                french_article.slug),
            HTTP_HOST='localhost',
            HTTP_X_DCMGUID="0000-000-01"
        )
        request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = french.locale
        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.process_response(
            request, self.response)
        # check that the tilte of the article in the main languge is used
        mock_submit_tracking.assert_called_once_with(
            'local_ga_tracking_code',
            request, self.response,
            {'cd5': self.article2.title,
                "cd3": 'Visitor',
                'cd1': "0000-000-01",
             })

    @patch(submit_tracking_method)
    def test_submit_to_local_ga_articlepage_with_tags(
            self, mock_submit_tracking):
        """requests for article with tags should
        make a submit tracking with a cd6 value in the
        custom params containing all the article tags"""

        request = RequestFactory().get(
            '/sections-main1-1/{}/{}/'.format(
                self.yourmind.slug,
                self.article.slug),
            HTTP_HOST='localhost',
            HTTP_X_DCMGUID="0000-000-01"
        )
        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.process_response(
            request, self.response)
        # a normal response should activate GA tracking
        nav_tags = ArticlePageTags.objects.all()
        tags = [nav_tag.tag.title for nav_tag in nav_tags]
        mock_submit_tracking.assert_called_once_with(
            'local_ga_tracking_code',
            request, self.response,
            {'cd5': self.article.title,
                "cd3": 'Visitor', 'cd1': "0000-000-01",
                'cd6': "|".join(tags)}
        )

    @patch(submit_tracking_method)
    def test_submit_to_local_ga_articlepage_no_tags(
            self, mock_submit_tracking):
        '''request for articles with not tags
        should not have a cd6 value in
        the custom params'''

        request = RequestFactory().get(
            '/sections-main1-1/{}/{}/'.format(
                self.yourmind.slug,
                self.article2.slug),
            HTTP_X_DCMGUID="0000-000-01"
        )

        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.process_response(
            request, self.response)
        # a normal response should activate GA tracking
        mock_submit_tracking.assert_called_once_with(
            'local_ga_tracking_code',
            request, self.response,
            {'cd5': self.article2.title,
                "cd3": 'Visitor',
                'cd1': "0000-000-01"})

    @patch(submit_tracking_method)
    def test_submit_to_local_ga__footerpage__no_tags(
            self, mock_submit_tracking):
        '''request for articles with not tags
        should not have a cd6 value in
        the custom params'''

        request = RequestFactory().get(
            '/footers-main1-1/{}/'.format(
                self.footer.slug),
            HTTP_X_DCMGUID="0000-000-01"
        )

        request.site = self.main.get_site()

        middleware = GemMoloGoogleAnalyticsMiddleware()
        middleware.process_response(
            request, self.response)
        # a normal response should activate GA tracking
        mock_submit_tracking.assert_called_once_with(
            'local_ga_tracking_code',
            request, self.response,
            {'cd5': self.footer.title,
                "cd3": 'Visitor', 'cd1': "0000-000-01"})
Ejemplo n.º 13
0
    def test_tag_nav_data_does_not_pull_in_footer_pages(self):
        self.mk_articles(parent=self.yourmind, count=2)
        footer = FooterPage(title='Test Footer Page')
        self.footer_index.add_child(instance=footer)
        footer.save_revision().publish()
        footer2 = FooterPage(title='Test Footer Page 2')
        self.footer_index.add_child(instance=footer2)
        footer2.save_revision().publish()
        footer3 = FooterPage(title='Test Footer Page 3')
        self.footer_index.add_child(instance=footer3)
        footer3.save_revision().publish()
        footer_pks = [footer.pk, footer2.pk, footer3.pk]

        response = self.client.get('/')
        data = response.context['tag_nav_data']
        hoempage_articles = []
        for section, section_list in data['sections']:
            homepage_articles = list(chain(hoempage_articles, section_list))
        for tag, tag_list in data['tags_list']:
            homepage_articles = list(chain(homepage_articles, tag_list))
        homepage_articles = list(chain(
            homepage_articles, data['latest_articles']))
        for article in homepage_articles:
            self.assertFalse(article.pk in footer_pks)