def step_impl(context):
d = context.bad_login
assert d['source']['username'] != 'ValidUser'
httpretty.enable()
httpretty.register_uri(
httpretty.POST,
context.good_build_url,
body='{"errors":[{"context":null,"message":"Authentication failed. Please check your credentials and try again.","exceptionName":"com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException"}]}',
status=401
)
from scripts.bitbucket import post_result, err
result = post_result(context.good_build_url, d['source']['username'], d['source']['password'], False, good_status_dict(), True)
assert result.status_code == 401
httpretty.disable()
httpretty.reset()
python类enable()的实例源码
def step_impl(context):
d = context.good_login
assert context.good_login['source']['username'] == 'ValidUser'
httpretty.enable()
httpretty.register_uri(
httpretty.POST,
context.bad_build_url,
body='{"errors":[{"context":null,"message":"You are not permitted to access this resource","exceptionName":"com.atlassian.bitbucket.AuthorisationException"}]}',
status=403
)
from scripts.bitbucket import post_result, err
result = post_result(context.bad_build_url, d['source']['username'], d['source']['password'], False, good_status_dict(), True)
assert result.status_code == 403
httpretty.disable()
httpretty.reset()
def step_impl(context):
d = context.good_login
assert context.good_login['source']['username'] == 'ValidUser'
assert 'somethingicantaccess' not in context.good_build_url
httpretty.enable()
httpretty.register_uri(
httpretty.POST,
context.good_build_url,
status=204
)
from scripts.bitbucket import post_result, err
result = post_result(context.good_build_url, d['source']['username'], d['source']['password'], False, good_status_dict(), True)
assert result.status_code == 204
context.goodresult = result
def setUp(self):
with open('mock_data/account_info.json') as data_file:
raw_account_info = data_file.read()
self.account_info = json.loads(raw_account_info)
with open('mock_data/' + self.installation_file_name) as data_file:
raw_installation_data = data_file.read()
self.installation_info = json.loads(raw_installation_data)
with open('mock_data/location_1_status.json') as data_file:
raw_location_1_status = data_file.read()
self.location_1_status = json.loads(raw_location_1_status)
with open('mock_data/location_2_status.json') as data_file:
raw_location_2_status = data_file.read()
self.location_2_status = json.loads(raw_location_2_status)
httpretty.enable()
httpretty.register_uri(httpretty.POST, "https://tccna.honeywell.com/Auth/OAuth/Token",
body='{"access_token": "TEST_TOKEN"}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "https://tccna.honeywell.com/WebAPI/emea/api/v1/userAccount",
body=raw_account_info,
content_type="application/json")
httpretty.register_uri(httpretty.GET,
"https://tccna.honeywell.com/WebAPI/emea/api/v1/location/installationInfo?userId=%s&includeTemperatureControlSystems=True" %
self.account_info["userId"],
body=raw_installation_data,
content_type="application/json")
httpretty.register_uri(httpretty.GET,
"https://tccna.honeywell.com/WebAPI/emea/api/v1/location/1/status?includeTemperatureControlSystems=True",
body=raw_location_1_status)
httpretty.register_uri(httpretty.GET,
"https://tccna.honeywell.com/WebAPI/emea/api/v1/location/2/status?includeTemperatureControlSystems=True",
body=raw_location_2_status)
def setUp(self):
"""About tests have no database component"""
httpretty.enable() # enable HTTPretty so that it will monkey patch the socket module
httpretty.register_uri(httpretty.GET, "http://yipit.com/",
body="Find the best daily deals")
def start_mocking_http(self):
self.host = "fake-eventstore.com"
self.port = 12345
self.client = Client(self.host, self.port, "admin", "changeit", no_ssl=True)
httpretty.enable()
def stop_http_server(context):
context.server.stop()
httpretty.enable()
def start_tcp_server(context):
context.tcp_port = get_free_tcp_port()
context.server = TCPServer(context.tcp_port)
context.server.start()
context.client = TCPClient(context.tcp_port)
httpretty.enable()
def stop_tcp_server(context):
context.server.stop()
context.client.close()
httpretty.enable()
def test_httpretty_bypasses_when_disabled(context):
"httpretty should bypass all requests by disabling it"
httpretty.register_uri(
httpretty.GET, "http://localhost:{0}/go-for-bubbles/".format(context.http_port),
body="glub glub")
httpretty.disable()
fd = urllib2.urlopen('http://localhost:{0}/go-for-bubbles/'.format(context.http_port))
got1 = fd.read()
fd.close()
expect(got1).to.equal(
b'. o O 0 O o . o O 0 O o . o O 0 O o . o O 0 O o . o O 0 O o .')
fd = urllib2.urlopen('http://localhost:{0}/come-again/'.format(context.http_port))
got2 = fd.read()
fd.close()
expect(got2).to.equal(b'<- HELLO WORLD ->')
httpretty.enable()
fd = urllib2.urlopen('http://localhost:{0}/go-for-bubbles/'.format(context.http_port))
got3 = fd.read()
fd.close()
expect(got3).to.equal(b'glub glub')
core.POTENTIAL_HTTP_PORTS.remove(context.http_port)
def setUp(self):
httpretty.enable()
def swagger_stub(swagger_files_url):
"""Fixture to stub a microservice from swagger files.
To use this fixture you need to define a swagger fixture named
swagger_files_url with the path to your swagger files, and the url to stub.
Then just add this fixture to your tests and your request pointing to the
urls in swagger_files_url will be managed by the stub.
Example:
@pytest.fixture
def swagger_files_url():
return [('tests/swagger.yaml', 'http://localhost:8000')]
"""
httpretty.enable()
for i in swagger_files_url: # Get all given swagger files and url
base_url = i[1]
s = SwaggerParser(i[0])
swagger_url[base_url] = s
# Register all urls
httpretty.register_uri(
httpretty.GET, re.compile(base_url + r'/.*'),
body=get_data_from_request)
httpretty.register_uri(
httpretty.POST, re.compile(base_url + r'/.*'),
body=get_data_from_request)
httpretty.register_uri(
httpretty.PUT, re.compile(base_url + r'/.*'),
body=get_data_from_request)
httpretty.register_uri(
httpretty.PATCH, re.compile(base_url + r'/.*'),
body=get_data_from_request)
httpretty.register_uri(
httpretty.DELETE, re.compile(base_url + r'/.*'),
body=get_data_from_request)
memory[base_url] = StubMemory(s)
yield memory[base_url]
# Close httpretty
httpretty.disable()
httpretty.reset()
def mock_server(ctx):
httpretty.enable()
httpretty.register_uri(httpretty.POST,"%slogin" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header":{"code" : "200"},"response" : {"result" : [ { "session" : { "sessionToken":"abcdefg" }}]}}',
content_type="application/json")
httpretty.register_uri(httpretty.POST,"%ssubmissions" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.DELETE, "%slogout" % (ctx.obj['SETTINGS']['apiUrl']),
content_type="application/json")
httpretty.register_uri(httpretty.POST, "%ssubmissions/12345/studies" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"6789" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.PUT, "%sstudies/6789?action=VALIDATE" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%sstudies/test_alias?idType=ALIAS" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%ssamples/sample_alias?idType=ALIAS&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%sdatasets/dataset_alias?idType=ALIAS&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%spolicies/policy_alias?idType=ALIAS&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%ssamples?status=SUBMITTED&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%sstudies?status=SUBMITTED&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%sstudies?status=SUBMITTED&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%spolicies?status=SUBMITTED&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.GET, "%sdatasets?status=SUBMITTED&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")
httpretty.register_uri(httpretty.DELETE, "%ssamples?status=SUBMITTED&skip=0&limit=0" % (ctx.obj['SETTINGS']['apiUrl']),
body='{"header" : {"code" : "200"}, "response" : {"result" : [{ "id":"12345" }]}}',
content_type="application/json")