def test_referral_segment(self, client):
        referral_segment = SegmentFactory(name='Referral')
        ReferralRuleFactory(regex_string="test.test", segment=referral_segment)

        client.get('/', **{'HTTP_REFERER': 'test.test'})

        assert client.session['segments'][0]['encoded_name'] == 'referral'
def test_create_segment_with_new_referral_rule():
    segment = SegmentFactory()

    segment.referral_rule = ReferralRuleFactory(regex_string='test.notest',
                                                segment=segment)

    assert segment.referral_rule.regex_string == 'test.notest'
def test_referral_rule_create():
    segment = SegmentFactory(name='Referral')
    referral_rule = ReferralRuleFactory(
        regex_string='test.test',
        segment=segment)

    assert referral_rule.regex_string == 'test.test'
def test_referral_segment(client, site):
    referral_segment = SegmentFactory(name='Referral')
    ReferralRuleFactory(regex_string="test.test", segment=referral_segment)

    response = client.get('/', **{'HTTP_REFERER': 'test.test'})
    assert response.status_code == 200

    assert client.session['segments'][0]['encoded_name'] == 'referral'
    def test_no_time_but_referral_segment(self, client):
        segment = SegmentFactory(name='Not both')
        TimeRuleFactory(start_time=datetime.time(8, 0, 0),
                        end_time=datetime.time(23, 0, 0),
                        segment=segment)
        ReferralRuleFactory(regex_string="test.test", segment=segment)

        client.get('/', **{'HTTP_REFERER': 'test.test'})

        assert len(client.session['segments']) == 0
    def test_time_and_referral_segment(self, client):
        segment = SegmentFactory(name='Both')
        TimeRuleFactory(start_time=datetime.time(8, 0, 0),
                        end_time=datetime.time(23, 0, 0),
                        segment=segment)
        ReferralRuleFactory(regex_string="test.test", segment=segment)

        client.get('/', **{'HTTP_REFERER': 'test.test'})

        assert client.session['segments'][0]['encoded_name'] == 'both'
def test_time_but_no_referral_segment(client, site):
    segment = SegmentFactory(name='Not both')
    TimeRuleFactory(start_time=datetime.time(8, 0, 0),
                    end_time=datetime.time(23, 0, 0),
                    segment=segment)
    ReferralRuleFactory(regex_string="test.test", segment=segment)

    response = client.get('/')
    assert response.status_code == 200

    assert len(client.session['segments']) == 0
    def test_multiple_segments_exist(self, client):
        first_segment = SegmentFactory(name='First')
        second_segment = SegmentFactory(name='Second')

        TimeRuleFactory(start_time=datetime.time(8, 0, 0),
                        end_time=datetime.time(23, 0, 0),
                        segment=first_segment)
        ReferralRuleFactory(regex_string="test.test", segment=first_segment)

        TimeRuleFactory(start_time=datetime.time(8, 0, 0),
                        end_time=datetime.time(23, 0, 0),
                        segment=second_segment)