本文整理汇总了Python中werkzeug.test.EnvironBuilder方法的典型用法代码示例。如果您正苦于以下问题:Python test.EnvironBuilder方法的具体用法?Python test.EnvironBuilder怎么用?Python test.EnvironBuilder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.test
的用法示例。
在下文中一共展示了test.EnvironBuilder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_19_papertoken_count
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_19_papertoken_count(self):
g.logged_in_user = {"username": "admin1",
"realm": "",
"role": "admin"}
builder = EnvironBuilder(method='POST',
headers={})
env = builder.get_environ()
# Set the remote address so that we can filter for it
env["REMOTE_ADDR"] = "10.0.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
set_policy(name="paperpol",
scope=SCOPE.ENROLL,
action="{0!s}=10".format(PAPERACTION.PAPERTOKEN_COUNT))
g.policy_object = PolicyClass()
# request, that matches the policy
req.all_data = {}
req.User = User()
papertoken_count(req)
# Check if the papertoken count is set
self.assertEqual(req.all_data.get("papertoken_count"), "10")
# finally delete policy
delete_policy("paperpol")
示例2: test_08_check_conditions_serial
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_08_check_conditions_serial(self):
uhandler = UserNotificationEventHandler()
# check a serial with regexp
builder = EnvironBuilder(method='POST',
data={'user': "cornelius@realm1"},
headers={})
env = builder.get_environ()
req = Request(env)
req.all_data = {"user": "cornelius@realm1",
"serial": "OATH123456"}
req.User = User("cornelius", "realm1")
resp = Response()
resp.data = """{"result": {"value": true}}"""
r = uhandler.check_condition(
{"g": {},
"handler_def": {"conditions": {"serial": "^OATH.*"}},
"request": req,
"response": resp
}
)
# Serial matches the regexp
self.assertEqual(r, True)
示例3: test_02_loggingevent_default
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_02_loggingevent_default(self, capture):
# simple logging event with default values
g = FakeFlaskG()
g.audit_object = FakeAudit()
env = EnvironBuilder(method='POST', headers={}, path='/auth').get_environ()
req = Request(env)
req.all_data = {}
resp = Response(response="""{"result": {"value": true}}""")
options = {
"g": g,
"request": req,
"response": resp,
"handler_def": {
}
}
log_handler = LoggingEventHandler()
res = log_handler.do("logging", options=options)
self.assertTrue(res)
capture.check(
('pi-eventlogger', 'INFO', 'event=/auth triggered')
)
示例4: test_extracting_filestream_from_request
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_extracting_filestream_from_request(self):
# GIVEN a file to upload.
with tempfile.NamedTemporaryFile('w+b') as f:
# GIVEN the file's contents.
content = b'foo'
f.write(content)
f.seek(0)
# GIVEN a POST request containing the file to upload.
builder = EnvironBuilder(method='POST', data={'file': f})
request = Request(builder.get_environ())
# WHEN we process the request.
filestream = util.file_from_request(request)
# THEN the file-stream should correspond to the provided file.
self.assertEqual(content, filestream.read())
示例5: open
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def open(self, *args, **kwargs):
as_tuple = kwargs.pop('as_tuple', False)
buffered = kwargs.pop('buffered', False)
follow_redirects = kwargs.pop('follow_redirects', False)
if (
not kwargs and len(args) == 1
and isinstance(args[0], (EnvironBuilder, dict))
):
environ = self.environ_base.copy()
if isinstance(args[0], EnvironBuilder):
environ.update(args[0].get_environ())
else:
environ.update(args[0])
environ['flask._preserve_context'] = self.preserve_context
else:
kwargs.setdefault('environ_overrides', {}) \
['flask._preserve_context'] = self.preserve_context
kwargs.setdefault('environ_base', self.environ_base)
builder = make_test_environ_builder(
self.application, *args, **kwargs
)
try:
environ = builder.get_environ()
finally:
builder.close()
return Client.open(
self, environ,
as_tuple=as_tuple,
buffered=buffered,
follow_redirects=follow_redirects
)
示例6: make_test_environ_builder
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):
"""Creates a new test builder with some application defaults thrown in."""
http_host = app.config.get('SERVER_NAME')
app_root = app.config.get('APPLICATION_ROOT')
if base_url is None:
url = url_parse(path)
base_url = 'http://%s/' % (url.netloc or http_host or 'localhost')
if app_root:
base_url += app_root.lstrip('/')
if url.netloc:
path = url.path
if url.query:
path += '?' + url.query
return EnvironBuilder(path, base_url, *args, **kwargs)
示例7: from_values
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def from_values(cls, *args, **kwargs):
"""Create a new request object based on the values provided. If
environ is given missing values are filled from there. This method is
useful for small scripts when you need to simulate a request from an URL.
Do not use this method for unittesting, there is a full featured client
object (:class:`Client`) that allows to create multipart requests,
support for cookies etc.
This accepts the same options as the
:class:`~werkzeug.test.EnvironBuilder`.
.. versionchanged:: 0.5
This method now accepts the same arguments as
:class:`~werkzeug.test.EnvironBuilder`. Because of this the
`environ` parameter is now called `environ_overrides`.
:return: request object
"""
from werkzeug.test import EnvironBuilder
charset = kwargs.pop('charset', cls.charset)
kwargs['charset'] = charset
builder = EnvironBuilder(*args, **kwargs)
try:
return builder.get_request(cls)
finally:
builder.close()
示例8: test_10_api_endpoint
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_10_api_endpoint(self):
fixed = "ebedeeefegeheiej"
otpkey = "cc17a4d77eaed96e9d14b5c87a02e718"
uid = "000000000000"
otps = ["ebedeeefegeheiejtjtrutblehenfjljrirgdihrfuetljtt",
"ebedeeefegeheiejlekvlrlkrcluvctenlnnjfknrhgtjned",
"ebedeeefegeheiejktudedbktcnbuntrhdueikggtrugckij",
"ebedeeefegeheiejjvjncbnffdrvjcvrbgdfufjgndfetieu",
"ebedeeefegeheiejdruibhvlvktcgfjiruhltketifnitbuk"
]
token = init_token({"type": "yubikey",
"otpkey": otpkey,
"otplen": len(otps[0]),
"yubikey.prefix": fixed,
"serial": "UBAM12345678_1"})
builder = EnvironBuilder(method='GET',
headers={})
env = builder.get_environ()
# Set the remote address so that we can filter for it
env["REMOTE_ADDR"] = "10.0.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
nonce = "random nonce"
apiid = "hallo"
apikey = "1YMEbMZijD3DzL21UfKGnOOI13c="
set_privacyidea_config("yubikey.apiid.{0!s}".format(apiid), apikey)
req.all_data = {'id': apiid,
"otp": otps[0],
"nonce": nonce}
text_type, result = YubikeyTokenClass.api_endpoint(req, g)
self.assertEqual(text_type, "plain")
self.assertTrue("status=OK" in result, result)
self.assertTrue("nonce={0!s}".format(nonce) in result, result)
示例9: test_11_strip_whitespace
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_11_strip_whitespace(self):
fixed = "ebedeeefegeheiej"
# The backend automatically strips whitespace from the OTP key
otpkey = "cc 17 a4 d7 7e ae d9 6e 9d 14 b5 c8 7a 02 e7 18"
uid = "000000000000"
otps = ["ebedeeefegeheiejtjtrutblehenfjljrirgdihrfuetljtt",
"ebedeeefegeheiejlekvlrlkrcluvctenlnnjfknrhgtjned",
"ebedeeefegeheiejktudedbktcnbuntrhdueikggtrugckij",
"ebedeeefegeheiejjvjncbnffdrvjcvrbgdfufjgndfetieu",
"ebedeeefegeheiejdruibhvlvktcgfjiruhltketifnitbuk"
]
token = init_token({"type": "yubikey",
"otpkey": otpkey,
"otplen": len(otps[0]),
"yubikey.prefix": fixed,
"serial": "UBAM12345678_1"})
builder = EnvironBuilder(method='GET',
headers={})
env = builder.get_environ()
# Set the remote address so that we can filter for it
env["REMOTE_ADDR"] = "10.0.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
nonce = "random nonce"
apiid = "hallo"
apikey = "1YMEbMZijD3DzL21UfKGnOOI13c="
set_privacyidea_config("yubikey.apiid.{0!s}".format(apiid), apikey)
req.all_data = {'id': apiid,
"otp": otps[0],
"nonce": nonce}
text_type, result = YubikeyTokenClass.api_endpoint(req, g)
self.assertEqual(text_type, "plain")
self.assertTrue("status=OK" in result, result)
self.assertTrue("nonce={0!s}".format(nonce) in result, result)
示例10: test_01a_admin_realms
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_01a_admin_realms(self):
admin1 = {"username": "admin1",
"role": "admin",
"realm": "realm1"}
admin2 = {"username": "admin1",
"role": "admin",
"realm": "realm2"}
set_policy(name="pol",
scope=SCOPE.ADMIN,
action="*", adminrealm="realm1")
g.policy_object = PolicyClass()
builder = EnvironBuilder(method='POST',
data={'serial': "OATH123456"},
headers={})
env = builder.get_environ()
# Set the remote address so that we can filter for it
env["REMOTE_ADDR"] = "10.0.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
req.User = User()
req.all_data = {}
# admin1 is allowed to do everything
g.logged_in_user = admin1
r = check_base_action(req, action="delete")
self.assertTrue(r)
# admin2 is not allowed.
g.logged_in_user = admin2
self.assertRaises(PolicyError, check_base_action, req, action="delete")
delete_policy("pol")
示例11: test_03_check_token_upload
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_03_check_token_upload(self):
g.logged_in_user = {"username": "admin1",
"realm": "",
"role": "admin"}
builder = EnvironBuilder(method='POST',
data={'serial': "OATH123456"},
headers={})
env = builder.get_environ()
# Set the remote address so that we can filter for it
env["REMOTE_ADDR"] = "10.0.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
req.all_data = {"filename": "token.xml"}
req.User = User()
# Set a policy, that does allow the action
set_policy(name="pol1",
scope=SCOPE.ADMIN,
action="enrollTOTP, enrollHOTP, {0!s}".format(ACTION.IMPORT),
client="10.0.0.0/8")
g.policy_object = PolicyClass()
# Try to import tokens
r = check_token_upload(req)
self.assertTrue(r)
# The admin can not upload from another IP address
# An exception is raised
env["REMOTE_ADDR"] = "192.168.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
req.all_data = {"filename": "token.xml"}
req.User = User()
self.assertRaises(PolicyError,
check_token_upload, req)
# finally delete policy
delete_policy("pol1")
示例12: test_08_encrypt_pin
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_08_encrypt_pin(self):
g.logged_in_user = {"username": "admin1",
"realm": "",
"role": "admin"}
builder = EnvironBuilder(method='POST',
data={'serial': "OATH123456"},
headers={})
env = builder.get_environ()
# Set the remote address so that we can filter for it
env["REMOTE_ADDR"] = "10.0.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
# Set a policy that defines the PIN to be encrypted
set_policy(name="pol1",
scope=SCOPE.ENROLL,
action=ACTION.ENCRYPTPIN)
g.policy_object = PolicyClass()
# request, that matches the policy
req.all_data = {
"user": "cornelius",
"realm": "home"}
encrypt_pin(req)
# Check, if the tokenlabel was added
self.assertEqual(req.all_data.get("encryptpin"), "True")
# finally delete policy
delete_policy("pol1")
示例13: test_08b_enroll_pin_user
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_08b_enroll_pin_user(self):
g.logged_in_user = {"username": "cornelius",
"realm": self.realm1,
"role": "user"}
builder = EnvironBuilder(method='POST',
data={'serial': "OATH123456"},
headers={})
env = builder.get_environ()
# Set the remote address so that we can filter for it
env["REMOTE_ADDR"] = "10.0.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
# Set a policy that defines the PIN to be encrypted
set_policy(name="pol1",
scope=SCOPE.USER,
action="enrollHOTP, enrollpin")
g.policy_object = PolicyClass()
# request, that matches the policy
req.all_data = {"pin": "test",
"user": "cornelius",
"realm": self.realm1}
req.User = User("cornelius", self.realm1)
enroll_pin(req)
# Check, if the PIN was removed
self.assertEqual(req.all_data.get("pin"), "test")
# finally delete policy
delete_policy("pol1")
示例14: test_10_check_external
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_10_check_external(self):
g.logged_in_user = {"username": "user1",
"realm": "",
"role": "user"}
builder = EnvironBuilder(method='POST',
data={'serial': "OATH123456"},
headers={})
env = builder.get_environ()
req = Request(env)
g.policy_object = PolicyClass()
req.all_data = {
"user": "cornelius",
"realm": "home"}
# Check success on no definition
r = check_external(req)
self.assertTrue(r)
# Check success with external function
current_app.config["PI_INIT_CHECK_HOOK"] = \
"privacyidea.api.lib.prepolicy.mock_success"
r = check_external(req)
self.assertTrue(r)
# Check exception with external function
current_app.config["PI_INIT_CHECK_HOOK"] = \
"privacyidea.api.lib.prepolicy.mock_fail"
self.assertRaises(Exception, check_external, req)
示例15: test_14_required_email
# 需要导入模块: from werkzeug import test [as 别名]
# 或者: from werkzeug.test import EnvironBuilder [as 别名]
def test_14_required_email(self):
g.logged_in_user = {"username": "admin1",
"realm": "",
"role": "admin"}
builder = EnvironBuilder(method='POST',
data={'serial': "OATH123456"},
headers={})
env = builder.get_environ()
# Set the remote address so that we can filter for it
env["REMOTE_ADDR"] = "10.0.0.1"
g.client_ip = env["REMOTE_ADDR"]
req = Request(env)
# Set a mangle policy to change the username
# and only use the last 4 characters of the username
set_policy(name="email1",
scope=SCOPE.REGISTER,
action="{0!s}=/.*@mydomain\..*".format(ACTION.REQUIREDEMAIL))
g.policy_object = PolicyClass()
# request, that matches the policy
req.all_data = {"email": "user@mydomain.net"}
# This emails is allowed
r = required_email(req)
self.assertTrue(r)
# This email is not allowed
req.all_data = {"email": "user@otherdomain.net"}
# This emails is allowed
self.assertRaises(RegistrationError, required_email, req)
delete_policy("email1")
g.policy_object = PolicyClass()
# Without a policy, this email can register
req.all_data = {"email": "user@otherdomain.net"}
# This emails is allowed
r = required_email(req)
self.assertTrue(r)