]> gitweb.fperrin.net Git - djsite.git/blob - quotes/conftest.py
26ac8efe0b44e9db997681542358e62e7c125349
[djsite.git] / quotes / conftest.py
1 import pytest
2
3 import lxml.etree
4 import html5lib
5 import urlparse
6
7 class ValidatingClient(object):
8     def __init__(self, client):
9         self.client = client
10
11     def request(self, url, method, exp_status=200, params={}):
12         if not url.startswith('/quotes/'):
13             url = '/quotes/' + url
14
15         if method == 'get':
16             response = self.client.get(url)
17         elif method == 'post':
18             response = self.client.post(url, params)
19         else:
20             raise RuntimeError('Unknown method %s for %s' % (method, url))
21         assert response.status_code == exp_status
22         if response.status_code != 200:
23             return None
24         assert response.charset == 'utf-8'
25         document = response.content.decode(response.charset)
26         print 'For url %s got page:\n%s' % (url, document)
27         lxml.etree.fromstring(document.replace('<br>', '<br/>'))
28
29         parser = html5lib.HTMLParser(strict=True)
30         parser.parse(document)
31
32         assert '<script>' not in document
33         return document
34
35     def getPage(self, url, exp_status=200):
36         return self.request(url, 'get', exp_status=exp_status)
37
38     def postPage(self, url, params, exp_status=200):
39         return self.request(url, 'post', params=params, exp_status=exp_status)
40
41     def checkAllLinks(self, url, document):
42         tree = lxml.etree.fromstring(document)
43         links = tree.xpath("//a")
44         for link in links:
45             href = link.attrib['href']
46             if href == '#':
47                 continue
48             fullurl = urlparse.urljoin(url, href)
49             print "asking for fullurl=", fullurl
50             assert self.getPage(fullurl)
51
52 @pytest.fixture
53 def c(client):
54     return ValidatingClient(client)
55
56 @pytest.fixture
57 def c_adm(admin_client):
58     return ValidatingClient(admin_client)