Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bad_domain_fails(webapp):
"""Test the analyse page checks domain validity."""
with pytest.raises(webtest.app.AppError) as err:
webapp.get('/analyse/3234jskdnfsdf7y34')
assert '400 BAD REQUEST' in str(err)
def test_bad_domains_fail(webapp, monkeypatch):
"""Test the email views check domain validity."""
monkeypatch.setenv('feature.emails', 'true')
with pytest.raises(webtest.app.AppError) as err:
webapp.get('/email/subscribe/3234jskdnfsdf7y34')
assert '400 BAD REQUEST' in err.value.message
with pytest.raises(webtest.app.AppError) as err:
webapp.post('/email/pending_verify/3234jskdnfsdf7y34')
assert '400 BAD REQUEST' in err.value.message
def test_all_limit_negative(self):
self.assertRaises(webtest.app.AppError,
self.get_json,
'/meters/instance?limit=-2')
elif resp is not None:
return resp
else:
raise LookupError('No response found')
def close(self):
"""Close selenium and the WSGI server if needed"""
if self.server:
self.server.shutdown()
if 'SELENIUM_KEEP_OPEN' not in os.environ:
self.browser.stop()
if 'SELENIUM_PID' in os.environ:
os.kill(int(os.environ['SELENIUM_PID']), signal.SIGTERM)
class TestResponse(testapp.TestResponse):
def follow(self, status=None, **kw):
"""If this request is a redirect, follow that redirect. It
is an error if this is not a redirect response. Returns
another response object.
"""
if not (self.status_int >= 300 and self.status_int < 400):
raise ValueError(
'You can only follow 301 and 302. Not %s' % self.status_int)
if len(self.responses):
resp = self.responses[0]
if not kw.get('expect_errors', False):
self.app._check_status(status, resp)
if not status:
self.app._check_errors(resp)
return self.responses.pop(0)
header = _basic_auth_str(*auth)
elif auth_type == 'jwt':
token = auth[0] if isinstance(auth, (tuple, list)) else auth
header = ' '.join(['Bearer', token])
else:
raise ValueError('Auth type not supported: {0!r}'.format(auth_type))
if PY2:
auth_header = binary_type(header)
else:
auth_header = header
headers['Authorization'] = auth_header
return headers
class TestRequest(webtest.app.TestRequest):
ResponseClass = TestResponse
class TestApp(webtest.TestApp):
"""A modified webtest.TestApp with useful features such as
requests-style authentication and auto_follow.
Example: ::
>>> from my_wsgi_app import application
>>> from webtest_plus import TestApp
>>> app = TestApp(application)
>>> app.get("/protected/", auth=("admin", "passw0rd"))
>>> app.status_code
200
"""
def test_cookie_policy(self):
from six.moves import http_cookiejar
def cookie_app(environ, start_response):
status = to_bytes("200 OK")
body = 'Cookie.'
headers = [
('Content-Type', 'text/plain'),
('Content-Length', str(len(body))),
('Set-Cookie',
'spam=eggs; secure; Domain=.example.org;'),
]
start_response(status, headers)
return [to_bytes(body)]
policy = webtest.app.CookiePolicy()
flags = (
policy.DomainStrictNoDots |
policy.DomainRFC2965Match |
policy.DomainStrictNonDomain)
policy.strict_ns_domain |= flags
cookiejar = http_cookiejar.CookieJar(policy=policy)
app = webtest.TestApp(
cookie_app,
cookiejar=cookiejar,
extra_environ={'HTTP_HOST': 'example.org'})
res = app.get('/')
res = app.get('/')
self.assertFalse(app.cookies,
'Response should not have set cookies')
self.assertNotIn('HTTP_COOKIE', res.request.environ)
self.assertEqual(dict(res.request.cookies), {})
def blank(cls, path, *args, **kwargs):
if prefix:
path = f"/{prefix}{path}"
return webtest.app.TestRequest.blank(path, *args, **kwargs)
def blank(cls, path, *args, **kwargs):
if prefix:
path = '/%s%s' % (prefix, path)
return webtest.app.TestRequest.blank(path, *args, **kwargs)
def test_new_feed(self):
"""Tests the registration of a new feed - currently disabled."""
repository = dnstwister.repository
# We need a domain to get the feed for.
domain = u'www.\u0454xample.com'
# A feed is registered by trying to load it (and it not already being
# registered).
with pytest.raises(webtest.app.AppError) as err:
res = self.app.get('/atom/{}'.format(tools.encode_domain(domain)))
assert '404 NOT FOUND' in err.value.message
assert 'New RSS feed generation currently disabled.' in err.value.message
def get_request_class(prefix):
class PrefixedRequestClass(webtest.app.TestRequest):
@classmethod
def blank(cls, path, *args, **kwargs):
if prefix:
path = f"/{prefix}{path}"
return webtest.app.TestRequest.blank(path, *args, **kwargs)
return PrefixedRequestClass