def test_segment_template_block(rf, site): SegmentFactory(name='test', persistent=True) request = rf.get('/') request.session['segments'] = [{ "encoded_name": 'test', "id": 1, "timestamp": int(time.time()), "persistent": True }] content = render_template(""" {% load wagtail_personalisation_tags %} {% segment name='test' %}Test{% endsegment %} """, request=request).strip() assert content == "Test" content = render_template(""" {% load wagtail_personalisation_tags %} {% segment name='test2' %}Test{% endsegment %} """, request=request).strip() assert content == "" with pytest.raises(TemplateSyntaxError): content = render_template(""" {% load wagtail_personalisation_tags %} {% segment wrongname='test2' %}Test{% endsegment %} """, request=request).strip()
def test_get_from_cache_when_twitter_api_fails(self, logging_mock): exception_message = 'Too many requests' HTTPretty.register_uri( HTTPretty.GET, self.api_url, responses=[ HTTPretty.Response(body=get_json('jeresig.json'), status=200, content_encoding='identity'), HTTPretty.Response(body=exception_message, status=429, content_encoding='identity'), ]) # it should be ok by now output, context = render_template( """{% get_tweets for "jresig" as tweets %}""") cache_key = get_user_cache_key(asvar='tweets', username='******') expect(cache.get(cache_key)).should.have.length_of(1) expect(context['tweets'][0]['text']).to.equal( "This is not John Resig - you should be following @jeresig instead!!!" ) # when twitter api fails, should use cache output2, context2 = render_template( """{% get_tweets for "jresig" as tweets %}""") expect(cache.get(cache_key)).should.have.length_of(1) expect(context2['tweets'][0]['text']).to.equal( "This is not John Resig - you should be following @jeresig instead!!!" ) logging_mock.assert_called_with(self.logger_name) expect(logging_mock.return_value.error.call_args[0][0]).should.contain( exception_message)
def render(self, template, json_mocks): if type(json_mocks) is not list: json_mocks = [json_mocks] responses = [HTTPretty.Response(get_json(_)) for _ in json_mocks] HTTPretty.register_uri(HTTPretty.GET, self.api_url, responses=responses, content_type='application/json') return render_template(template=template)
def test_exception_is_not_propagated_but_logged(self, logging_mock): exception_message = 'Capacity Error' HTTPretty.register_uri(HTTPretty.GET, self.api_url, body=exception_message, status=503, content_encoding='identity') output, context = render_template("""{% get_tweets for "twitter" as tweets %}""") expect(output).should.be.empty expect(context['tweets']).should.be.empty logging_mock.assert_called_with(self.logger_name) expect(logging_mock.return_value.error.call_args[0][0]).should.contain(exception_message)
def test_get_from_cache_when_twitter_api_fails(self, logging_mock): exception_message = 'Too many requests' HTTPretty.register_uri(HTTPretty.GET, self.api_url, responses=[ HTTPretty.Response(body=get_json('jeresig.json'), status=200, content_encoding='identity'), HTTPretty.Response(body=exception_message, status=429, content_encoding='identity'), ]) # it should be ok by now output, context = render_template("""{% get_tweets for "jresig" as tweets %}""") cache_key = get_user_cache_key(asvar='tweets', username='******') expect(cache.get(cache_key)).should.have.length_of(1) expect(context['tweets'][0]['text']).to.equal("This is not John Resig - you should be following @jeresig instead!!!") # when twitter api fails, should use cache output2, context2 = render_template("""{% get_tweets for "jresig" as tweets %}""") expect(cache.get(cache_key)).should.have.length_of(1) expect(context2['tweets'][0]['text']).to.equal("This is not John Resig - you should be following @jeresig instead!!!") logging_mock.assert_called_with(self.logger_name) expect(logging_mock.return_value.error.call_args[0][0]).should.contain(exception_message)
def test_exception_is_not_propagated_but_logged(self, logging_mock): exception_message = 'Capacity Error' HTTPretty.register_uri(HTTPretty.GET, self.api_url, body=exception_message, status=503, content_encoding='identity') output, context = render_template( """{% get_tweets for "twitter" as tweets %}""") expect(output).should.be.empty expect(context['tweets']).should.be.empty logging_mock.assert_called_with(self.logger_name) expect(logging_mock.return_value.error.call_args[0][0]).should.contain( exception_message)
def test_error_summary(): """Verify an error summary is displayed correctly.""" template = """ {% load crispy_forms_tags %} {% if form.helper.form_show_errors and form.errors %} {% include 'gds/layout/error_summary.html' %} {% endif %} <div class="govuk-body"> {% crispy form %} </div> """ form = TextInputForm(data={"name": ""}) form.add_error(None, "Non-field error") page = render_template(template, form=form) assert parse_html(page) == parse_contents(RESULT_DIR, "error_summary.html")