class MembershipTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_signin(self): """ Ensure signin page is rendered """ assert 200 == self.client.go('/en/signin') assert '- Sign In</title>' in self.client.content def test_signup(self): """ """ assert 200 == self.client.go('/en/signup') assert '- Sign Up</title>' in self.client.content def test_signout(self): """ """ assert 302 == self.client.go('/en/signout')
class HelloworldTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_welcome(self): """ Ensure welcome page is rendered. """ assert 200 == self.client.get("/") assert "Hello" in self.client.content def test_welcome2(self): """ Ensure welcome2 page is rendered. """ assert 200 == self.client.get("/welcome2") assert "Hello" in self.client.content def test_now(self): """ Ensure now page is rendered. """ assert 200 == self.client.get("/now") assert "It is" in self.client.content def test_now2(self): """ Ensure welcome page is rendered. """ assert 200 == self.client.get("/now2") assert "It is" in self.client.content
class PublicTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_root(self): """ Ensure root page is rendered. """ assert 200 == self.client.get('/') assert '- Home</title>' in self.client.content def test_home(self): """ Ensure home page is rendered. """ assert 200 == self.client.get('/home') assert '- Home</title>' in self.client.content def test_static_files(self): """ Ensure static content is served. """ for static_file in [ '/favicon.ico', '/static/css/site.css', '/static/js/core.js', ]: assert 200 == self.client.get(static_file) def test_static_file_not_found(self): """ Ensure 404 status code for non existing static content. """ assert 302 == self.client.get('/static/css/unknown.css') assert '404' in self.client.headers['Location'][0] def test_static_file_forbidden(self): """ Ensure 403 status code for forbidden static content. """ assert 302 == self.client.get('/static/../templates/') assert '403' in self.client.headers['Location'][0]
class HelloTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_home(self): """ Ensure welcome page is rendered. """ assert 200 == self.client.get('/') assert 'Hello World!' == self.client.content def test_welcome(self): """ Ensure welcome page is rendered. """ assert 200 == self.client.get('/welcome') assert 'Hello World!' == self.client.content
class SignOutTestCase(unittest.TestCase, SignInMixin, SignOutMixin): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_signout_user(self): """ Ensure sigout is successful. """ self.signin('demo', 'P@ssw0rd') assert 200 == self.client.follow() self.signout()
class SignInAJAXTestCase(unittest.TestCase, SignInMixin): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_ajax_validation_errors(self): """ Ensure sigin page displays field validation errors on AJAX call. """ assert 200 == self.ajax_signin('', '') errors = self.client.json.errors assert 2 == len(errors) assert AUTH_COOKIE not in self.client.cookies def test_ajax_unknown_user(self): """ Ensure sigin page displays general error message on AJAX call. """ assert 200 == self.ajax_signin('test', 'password') errors = self.client.json.errors assert '__ERROR__' in errors assert AUTH_COOKIE not in self.client.cookies def test_ajax_valid_user(self): """ Ensure sigin is successful. """ assert 207 == self.ajax_signin('demo', 'P@ssw0rd') assert 200 == self.client.follow() assert AUTH_COOKIE in self.client.cookies assert XSRF_NAME not in self.client.cookies assert 'Welcome <b>demo' in self.client.content def test_ajax_xrsf_token_invalid(self): client = self.client assert 200 == client.get('/en/signin') page = SignInPage(client) client.cookies.clear() assert 207 == page.ajax_submit() assert 200 == client.follow() SignInPage(client)
class ErrorTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_error_400(self): """ Ensure bad request page is rendered. """ assert 400 == self.client.get('/error/400') assert 'Code 400' in self.client.content def test_error_403(self): """ Ensure forbidden page is rendered. """ assert 403 == self.client.get('/error/403') assert 'Code 403' in self.client.content def test_error_404(self): """ Ensure not found page is rendered. """ assert 404 == self.client.get('/error/404') assert 'Code 404' in self.client.content def test_route_not_found(self): """ Ensure not found page is rendered. """ self.client.get('/test-not-found') assert 404 == self.client.follow() assert 'Code 404' in self.client.content def test_error_500(self): """ Ensure internal error page is rendered. """ assert 500 == self.client.get('/error/500') assert 'Code 500' in self.client.content # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
class ErrorTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_error_400(self): """ Ensure bad request page is rendered. """ assert 400 == self.client.get("/error/400") assert "Code 400" in self.client.content def test_error_403(self): """ Ensure forbidden page is rendered. """ assert 403 == self.client.get("/error/403") assert "Code 403" in self.client.content def test_error_404(self): """ Ensure not found page is rendered. """ assert 404 == self.client.get("/error/404") assert "Code 404" in self.client.content def test_route_not_found(self): """ Ensure not found page is rendered. """ self.client.get("/test-not-found") assert 404 == self.client.follow() assert "Code 404" in self.client.content def test_error_500(self): """ Ensure internal error page is rendered. """ assert 500 == self.client.get("/error/500") assert "Code 500" in self.client.content
class PublicTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_root(self): """ Ensure root page is rendered. """ assert 200 == self.client.get('/') assert '- Home</title>' in self.client.content def test_home(self): """ Ensure home page is rendered. """ assert 200 == self.client.get('/en/home') assert '- Home</title>' in self.client.content def test_about(self): """ Ensure about page is rendered. """ assert 200 == self.client.get('/en/about') assert '- About</title>' in self.client.content def test_space_around_newline(self): """ space around newline in markup should not be removed """ self.client.get('/en/home') assert re.search('elit\.\s+Donec', self.client.content) assert re.search('enim,\s+quis', self.client.content) assert not re.search('"type', self.client.content) def test_extra_whitespace(self): for url in [ '/', '/en/home', '/en/about' ]: assert 200 == self.client.get(url) assert not extra_whitespace(self.client.content)
class PublicTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_root(self): """ Ensure root page is rendered. """ assert 200 == self.client.get("/") assert "- Home</title>" in self.client.content def test_home(self): """ Ensure home page is rendered. """ assert 200 == self.client.get("/home") assert "- Home</title>" in self.client.content def test_static_files(self): """ Ensure static content is served. """ for static_file in ["/favicon.ico", "/static/css/site.css", "/static/js/core.js"]: assert 200 == self.client.get(static_file) def test_static_file_not_found(self): """ Ensure 404 status code for non existing static content. """ assert 302 == self.client.get("/static/css/unknown.css") assert "404" in self.client.headers["Location"][0] def test_static_file_forbidden(self): """ Ensure 403 status code for forbidden static content. """ assert 302 == self.client.get("/static/../templates/") assert "403" in self.client.headers["Location"][0] def xtest_static_file_gzip(self): """ Ensure static files are compressed. """ self.client.get( "/static/css/site.css", environ={"SERVER_PROTOCOL": "HTTP/1.1", "HTTP_ACCEPT_ENCODING": "gzip, deflate"} ) assert "gzip" in self.client.headers["Content-Encoding"] def test_static_file_if_modified_since(self): """ Request static resource with If-Modified-Since header. """ assert 200 == self.client.get("/static/css/site.css") last_modified = self.client.headers["Last-Modified"][0] assert 304 == self.client.get("/static/css/site.css", environ={"HTTP_IF_MODIFIED_SINCE": last_modified}) def test_static_file_if_none_match(self): """ Request static resource with If-None-Match header. """ assert 200 == self.client.get("/static/css/site.css") etag = self.client.headers["ETag"][0] assert 304 == self.client.get("/static/css/site.css", environ={"HTTP_IF_NONE_MATCH": etag}) def test_head_static_file(self): """ Request static resource with HTTP HEAD. """ assert 200 == self.client.head("/static/css/site.css") assert 0 == len(self.client.content)
class SignUpTestCase(unittest.TestCase, SignInMixin, SignUpMixin, SignOutMixin): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_validation_errors(self): """ Ensure signup page displays field validation errors. """ errors = self.signup() assert 6 == len(errors) assert AUTH_COOKIE not in self.client.cookies assert 'class="error"' in self.client.content def test_already_registered(self): """ Ensure signup page displays general error message. """ errors = self.signup( username='******', display_name='Demo', email='*****@*****.**', date_of_birth='1984/11/14', password='******', confirm_password='******', answer='7') assert ['username'] == errors assert AUTH_COOKIE not in self.client.cookies def test_registration_succeed(self): """ Ensure if all supplied information is valid than user is registered and logged in. """ errors = self.signup( username='******', display_name='John Smith', email='*****@*****.**', date_of_birth='1987/2/7', password='******', confirm_password='******', answer='7') assert not errors assert 200 == self.client.follow() assert AUTH_COOKIE in self.client.cookies assert RESUBMISSION_NAME not in self.client.cookies assert 'Welcome <b>John Smith' in self.client.content def test_lockout_quota(self): """ Ensure signup quata is reached. """ from config import config mode = config.get('runtime', 'lockout') self.client.environ['REMOTE_ADDR'] = '192.168.10.101' if mode == 'ignore': return errors = self.signup( username='******', display_name='Joe Smith', email='*****@*****.**', date_of_birth='1980/4/5', password='******', confirm_password='******', answer='7') assert not errors assert 200 == self.client.follow() assert AUTH_COOKIE in self.client.cookies self.signout() errors = self.signup( username='******', display_name='Jassy Smith', email='*****@*****.**', date_of_birth='1982/7/17', password='******', confirm_password='******', answer='7') assert not errors assert 200 == self.client.follow() assert AUTH_COOKIE in self.client.cookies self.signout() self.signup( username='******', display_name='Jack Smith', email='*****@*****.**', date_of_birth='1981/3/18', password='******', confirm_password='******', answer='7') # after 2nd attempt the access is forbidden assert 403 == self.client.follow() def test_if_authenticated_redirect(self): """ If user is already authenticated redirect to default page. """ self.signin('demo', 'P@ssw0rd') assert 200 == self.client.follow() self.client.get('/en/signup') assert 200 == self.client.follow() def test_resubmission_token_invalid(self): client = self.client assert 200 == client.get('/en/signup') page = SignUpPage(client) client.cookies[RESUBMISSION_NAME] = '100' page.submit() SignUpPage(client)
class PublicTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_root(self): """ Ensure root page is rendered. """ assert 200 == self.client.get('/') assert '- Home</title>' in self.client.content def test_home(self): """ Ensure home page is rendered. """ assert 200 == self.client.get('/en/home') assert '- Home</title>' in self.client.content def test_static_files(self): """ Ensure static content is served. """ for static_file in [ '/favicon.ico', '/static/css/site.css', '/static/js/core.js', ]: assert 200 == self.client.get(static_file) def test_static_file_not_found(self): """ Ensure 404 status code for non existing static content. """ assert 302 == self.client.get('/static/css/unknown.css') assert '404' in self.client.headers['Location'][0] def test_static_file_forbidden(self): """ Ensure 403 status code for forbidden static content. """ assert 302 == self.client.get('/static/../templates/') assert '403' in self.client.headers['Location'][0] def xtest_static_file_gzip(self): """ Ensure static files are compressed. """ self.client.get('/static/css/site.css', environ={ 'SERVER_PROTOCOL': 'HTTP/1.1', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate' }) assert 'gzip' in self.client.headers['Content-Encoding'] def test_static_file_if_modified_since(self): """ Request static resource with If-Modified-Since header. """ assert 200 == self.client.get('/static/css/site.css') last_modified = self.client.headers['Last-Modified'][0] assert 304 == self.client.get('/static/css/site.css', environ={ 'HTTP_IF_MODIFIED_SINCE': last_modified }) def test_static_file_if_none_match(self): """ Request static resource with If-None-Match header. """ assert 200 == self.client.get('/static/css/site.css') etag = self.client.headers['ETag'][0] assert 304 == self.client.get('/static/css/site.css', environ={ 'HTTP_IF_NONE_MATCH': etag }) def test_head_static_file(self): """ Request static resource with HTTP HEAD. """ assert 200 == self.client.head('/static/css/site.css') assert 0 == len(self.client.content)
class ErrorTestCase(unittest.TestCase): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_error_400(self): """ Ensure bad request page is rendered. """ assert 400 == self.client.get('/en/error/400') assert 'Code 400' in self.client.content def test_error_403(self): """ Ensure forbidden page is rendered. """ assert 403 == self.client.get('/en/error/403') assert 'Code 403' in self.client.content def test_error_404(self): """ Ensure not found page is rendered. """ assert 404 == self.client.get('/en/error/404') assert 'Code 404' in self.client.content def test_route_not_found(self): """ Ensure not found page is rendered. """ self.client.get('/test-not-found') assert 404 == self.client.follow() assert 'Code 404' in self.client.content def test_error_500(self): """ Ensure internal error page is rendered. """ assert 500 == self.client.get('/en/error/500') assert 'Code 500' in self.client.content def test_space_around_newline(self): """ space around newline in markup should not be removed """ self.client.get('/en/error/404') r = re.compile('been\s+removed') assert r.search(self.client.content) def test_extra_whitespace(self): for url in [ '/en/error/400', '/en/error/403', '/en/error/404', '/en/error/500', ]: self.client.get(url) assert not extra_whitespace(self.client.content)
def setUp(self): self.client = WSGIClient(main)
class SignInTestCase(unittest.TestCase, SignInMixin): def setUp(self): self.client = WSGIClient(main) def tearDown(self): del self.client self.client = None def test_validation_errors(self): """ Ensure sigin page displays field validation errors. """ errors = self.signin('', '') assert 2 == len(errors) assert AUTH_COOKIE not in self.client.cookies assert 'class="error"' in self.client.content def test_unknown_user(self): """ Ensure sigin page displays general error message. """ errors = self.signin('test', 'password') assert not errors assert AUTH_COOKIE not in self.client.cookies assert 'class="error-message"' in self.client.content def test_lockout_guard(self): """ Ensure sigin lockout guard is reached. """ from config import config mode = config.get('runtime', 'lockout') self.client.environ['REMOTE_ADDR'] = '192.168.10.101' if mode == 'ignore': return self.client.environ['REMOTE_ADDR'] = '192.168.10.101' errors = self.signin('test', 'password') assert not errors assert AUTH_COOKIE not in self.client.cookies assert 'class="error-message"' in self.client.content self.signin('test', 'password') self.signin('test', 'password') assert 'class="error-message"' in self.client.content # after 3rd attempt the access is forbidden self.signin('test', 'password') assert 403 == self.client.follow() def test_valid_user(self): """ Ensure sigin is successful. """ self.signin('demo', 'P@ssw0rd') assert 200 == self.client.follow() assert AUTH_COOKIE in self.client.cookies assert XSRF_NAME not in self.client.cookies assert 'Welcome <b>demo' in self.client.content def test_if_authenticated_redirect(self): """ If user is already authenticated redirect to default page. """ self.signin('demo', 'P@ssw0rd') assert 200 == self.client.follow() self.client.get('/en/signin') assert 200 == self.client.follow() def test_xrsf_token_invalid(self): client = self.client assert 200 == client.get('/en/signin') page = SignInPage(client) client.cookies.clear() page.submit() assert 200 == client.follow() SignInPage(client)