How to use test_response_headers method in Httmock

Best Python code snippet using httmock_python

test_auth.py

Source: test_auth.py Github

copy

Full Screen

1import copy2import json3import requests4from keystoneclient import exceptions5from keystoneclient.v3 import client6from tests.v3 import utils7class AuthenticateAgainstKeystoneTests(utils.TestCase):8 def setUp(self):9 super(AuthenticateAgainstKeystoneTests, self).setUp()10 self.TEST_RESPONSE_DICT = {11 "token": {12 "methods": [13 "token",14 "password"15 ],16 "expires_at": "2020-01-01T00:00:10.000123Z",17 "project": {18 "domain": {19 "id": self.TEST_DOMAIN_ID,20 "name": self.TEST_DOMAIN_NAME21 },22 "id": self.TEST_TENANT_ID,23 "name": self.TEST_TENANT_NAME24 },25 "user": {26 "domain": {27 "id": self.TEST_DOMAIN_ID,28 "name": self.TEST_DOMAIN_NAME29 },30 "id": self.TEST_USER,31 "name": self.TEST_USER32 },33 "issued_at": "2013-05-29T16:55:21.468960Z",34 "catalog": self.TEST_SERVICE_CATALOG35 },36 }37 self.TEST_REQUEST_BODY = {38 "auth": {39 "identity": {40 "methods": ["password"],41 "password": {42 "user": {43 "domain": {44 "name": self.TEST_DOMAIN_NAME45 },46 "name": self.TEST_USER,47 "password": self.TEST_TOKEN48 }49 }50 },51 "scope": {52 "project": {53 "id": self.TEST_TENANT_ID54 },55 }56 }57 }58 self.TEST_REQUEST_HEADERS = {59 'Content-Type': 'application/​json',60 'User-Agent': 'python-keystoneclient'61 }62 self.TEST_RESPONSE_HEADERS = {63 'X-Subject-Token': self.TEST_TOKEN64 }65 def test_authenticate_success(self):66 TEST_TOKEN = "abcdef"67 self.TEST_RESPONSE_HEADERS['X-Subject-Token'] = TEST_TOKEN68 ident = self.TEST_REQUEST_BODY['auth']['identity']69 del ident['password']['user']['domain']70 del ident['password']['user']['name']71 ident['password']['user']['id'] = self.TEST_USER72 resp = utils.TestResponse({73 "status_code": 200,74 "text": json.dumps(self.TEST_RESPONSE_DICT),75 "headers": self.TEST_RESPONSE_HEADERS,76 })77 kwargs = copy.copy(self.TEST_REQUEST_BASE)78 kwargs['headers'] = self.TEST_REQUEST_HEADERS79 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)80 requests.request('POST',81 self.TEST_URL + "/​auth/​tokens",82 **kwargs).AndReturn((resp))83 self.mox.ReplayAll()84 cs = client.Client(user_id=self.TEST_USER,85 password=self.TEST_TOKEN,86 project_id=self.TEST_TENANT_ID,87 auth_url=self.TEST_URL)88 self.assertEqual(cs.auth_token, TEST_TOKEN)89 def test_authenticate_failure(self):90 ident = self.TEST_REQUEST_BODY['auth']['identity']91 ident['password']['user']['password'] = 'bad_key'92 resp = utils.TestResponse({93 "status_code": 401,94 "text": json.dumps({95 "unauthorized": {96 "message": "Unauthorized",97 "code": "401",98 },99 }),100 })101 kwargs = copy.copy(self.TEST_REQUEST_BASE)102 kwargs['headers'] = self.TEST_REQUEST_HEADERS103 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)104 requests.request('POST',105 self.TEST_URL + "/​auth/​tokens",106 **kwargs).AndReturn((resp))107 self.mox.ReplayAll()108 # Workaround for issue with assertRaises on python2.6109 # where with assertRaises(exceptions.Unauthorized): doesn't work110 # right111 def client_create_wrapper():112 client.Client(user_domain_name=self.TEST_DOMAIN_NAME,113 username=self.TEST_USER,114 password="bad_key",115 project_id=self.TEST_TENANT_ID,116 auth_url=self.TEST_URL)117 self.assertRaises(exceptions.Unauthorized, client_create_wrapper)118 def test_auth_redirect(self):119 correct_response = json.dumps(self.TEST_RESPONSE_DICT, sort_keys=True)120 dict_responses = [121 {122 "headers": {123 'location': self.TEST_ADMIN_URL + "/​auth/​tokens",124 'X-Subject-Token': self.TEST_TOKEN,125 },126 "status_code": 305,127 "text": "Use proxy",128 },129 {130 "headers": {'X-Subject-Token': self.TEST_TOKEN},131 "status_code": 200,132 "text": correct_response,133 },134 ]135 responses = [(utils.TestResponse(resp))136 for resp in dict_responses]137 kwargs = copy.copy(self.TEST_REQUEST_BASE)138 kwargs['headers'] = self.TEST_REQUEST_HEADERS139 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)140 requests.request('POST',141 self.TEST_URL + "/​auth/​tokens",142 **kwargs).AndReturn(responses[0])143 kwargs = copy.copy(self.TEST_REQUEST_BASE)144 kwargs['headers'] = self.TEST_REQUEST_HEADERS145 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)146 requests.request('POST',147 self.TEST_ADMIN_URL + "/​auth/​tokens",148 **kwargs).AndReturn(responses[1])149 self.mox.ReplayAll()150 cs = client.Client(user_domain_name=self.TEST_DOMAIN_NAME,151 username=self.TEST_USER,152 password=self.TEST_TOKEN,153 project_id=self.TEST_TENANT_ID,154 auth_url=self.TEST_URL)155 self.assertEqual(cs.management_url,156 self.TEST_RESPONSE_DICT["token"]["catalog"][3]157 ['endpoints'][2]["url"])158 self.assertEqual(cs.auth_token,159 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])160 def test_authenticate_success_domain_username_password_scoped(self):161 resp = utils.TestResponse({162 "status_code": 200,163 "text": json.dumps(self.TEST_RESPONSE_DICT),164 "headers": self.TEST_RESPONSE_HEADERS,165 })166 kwargs = copy.copy(self.TEST_REQUEST_BASE)167 kwargs['headers'] = self.TEST_REQUEST_HEADERS168 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)169 requests.request('POST',170 self.TEST_URL + "/​auth/​tokens",171 **kwargs).AndReturn((resp))172 self.mox.ReplayAll()173 cs = client.Client(user_domain_name=self.TEST_DOMAIN_NAME,174 username=self.TEST_USER,175 password=self.TEST_TOKEN,176 project_id=self.TEST_TENANT_ID,177 auth_url=self.TEST_URL)178 self.assertEqual(cs.management_url,179 self.TEST_RESPONSE_DICT["token"]["catalog"][3]180 ['endpoints'][2]["url"])181 self.assertEqual(cs.auth_token,182 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])183 def test_authenticate_success_userid_password_domain_scoped(self):184 ident = self.TEST_REQUEST_BODY['auth']['identity']185 del ident['password']['user']['domain']186 del ident['password']['user']['name']187 ident['password']['user']['id'] = self.TEST_USER188 scope = self.TEST_REQUEST_BODY['auth']['scope']189 del scope['project']190 scope['domain'] = {}191 scope['domain']['id'] = self.TEST_DOMAIN_ID192 token = self.TEST_RESPONSE_DICT['token']193 del token['project']194 token['domain'] = {}195 token['domain']['id'] = self.TEST_DOMAIN_ID196 token['domain']['name'] = self.TEST_DOMAIN_NAME197 resp = utils.TestResponse({198 "status_code": 200,199 "text": json.dumps(self.TEST_RESPONSE_DICT),200 "headers": self.TEST_RESPONSE_HEADERS,201 })202 kwargs = copy.copy(self.TEST_REQUEST_BASE)203 kwargs['headers'] = self.TEST_REQUEST_HEADERS204 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)205 requests.request('POST',206 self.TEST_URL + "/​auth/​tokens",207 **kwargs).AndReturn((resp))208 self.mox.ReplayAll()209 cs = client.Client(user_id=self.TEST_USER,210 password=self.TEST_TOKEN,211 domain_id=self.TEST_DOMAIN_ID,212 auth_url=self.TEST_URL)213 self.assertEqual(cs.auth_domain_id,214 self.TEST_DOMAIN_ID)215 self.assertEqual(cs.management_url,216 self.TEST_RESPONSE_DICT["token"]["catalog"][3]217 ['endpoints'][2]["url"])218 self.assertEqual(cs.auth_token,219 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])220 def test_authenticate_success_userid_password_project_scoped(self):221 ident = self.TEST_REQUEST_BODY['auth']['identity']222 del ident['password']['user']['domain']223 del ident['password']['user']['name']224 ident['password']['user']['id'] = self.TEST_USER225 resp = utils.TestResponse({226 "status_code": 200,227 "text": json.dumps(self.TEST_RESPONSE_DICT),228 "headers": self.TEST_RESPONSE_HEADERS,229 })230 kwargs = copy.copy(self.TEST_REQUEST_BASE)231 kwargs['headers'] = self.TEST_REQUEST_HEADERS232 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)233 requests.request('POST',234 self.TEST_URL + "/​auth/​tokens",235 **kwargs).AndReturn((resp))236 self.mox.ReplayAll()237 cs = client.Client(user_id=self.TEST_USER,238 password=self.TEST_TOKEN,239 project_id=self.TEST_TENANT_ID,240 auth_url=self.TEST_URL)241 self.assertEqual(cs.auth_tenant_id,242 self.TEST_TENANT_ID)243 self.assertEqual(cs.management_url,244 self.TEST_RESPONSE_DICT["token"]["catalog"][3]245 ['endpoints'][2]["url"])246 self.assertEqual(cs.auth_token,247 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])248 def test_authenticate_success_password_unscoped(self):249 del self.TEST_RESPONSE_DICT['token']['catalog']250 del self.TEST_REQUEST_BODY['auth']['scope']251 resp = utils.TestResponse({252 "status_code": 200,253 "text": json.dumps(self.TEST_RESPONSE_DICT),254 "headers": self.TEST_RESPONSE_HEADERS,255 })256 kwargs = copy.copy(self.TEST_REQUEST_BASE)257 kwargs['headers'] = self.TEST_REQUEST_HEADERS258 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)259 requests.request('POST',260 self.TEST_URL + "/​auth/​tokens",261 **kwargs).AndReturn((resp))262 self.mox.ReplayAll()263 cs = client.Client(user_domain_name=self.TEST_DOMAIN_NAME,264 username=self.TEST_USER,265 password=self.TEST_TOKEN,266 auth_url=self.TEST_URL)267 self.assertEqual(cs.auth_token,268 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])269 self.assertFalse('catalog' in cs.service_catalog.catalog)270 def test_authenticate_success_token_domain_scoped(self):271 ident = self.TEST_REQUEST_BODY['auth']['identity']272 del ident['password']273 ident['methods'] = ['token']274 ident['token'] = {}275 ident['token']['id'] = self.TEST_TOKEN276 scope = self.TEST_REQUEST_BODY['auth']['scope']277 del scope['project']278 scope['domain'] = {}279 scope['domain']['id'] = self.TEST_DOMAIN_ID280 token = self.TEST_RESPONSE_DICT['token']281 del token['project']282 token['domain'] = {}283 token['domain']['id'] = self.TEST_DOMAIN_ID284 token['domain']['name'] = self.TEST_DOMAIN_NAME285 self.TEST_REQUEST_HEADERS['X-Auth-Token'] = self.TEST_TOKEN286 resp = utils.TestResponse({287 "status_code": 200,288 "text": json.dumps(self.TEST_RESPONSE_DICT),289 "headers": self.TEST_RESPONSE_HEADERS,290 })291 kwargs = copy.copy(self.TEST_REQUEST_BASE)292 kwargs['headers'] = self.TEST_REQUEST_HEADERS293 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)294 requests.request('POST',295 self.TEST_URL + "/​auth/​tokens",296 **kwargs).AndReturn((resp))297 self.mox.ReplayAll()298 cs = client.Client(token=self.TEST_TOKEN,299 domain_id=self.TEST_DOMAIN_ID,300 auth_url=self.TEST_URL)301 self.assertEqual(cs.auth_domain_id,302 self.TEST_DOMAIN_ID)303 self.assertEqual(cs.management_url,304 self.TEST_RESPONSE_DICT["token"]["catalog"][3]305 ['endpoints'][2]["url"])306 self.assertEqual(cs.auth_token,307 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])308 def test_authenticate_success_token_project_scoped(self):309 ident = self.TEST_REQUEST_BODY['auth']['identity']310 del ident['password']311 ident['methods'] = ['token']312 ident['token'] = {}313 ident['token']['id'] = self.TEST_TOKEN314 self.TEST_REQUEST_HEADERS['X-Auth-Token'] = self.TEST_TOKEN315 resp = utils.TestResponse({316 "status_code": 200,317 "text": json.dumps(self.TEST_RESPONSE_DICT),318 "headers": self.TEST_RESPONSE_HEADERS,319 })320 kwargs = copy.copy(self.TEST_REQUEST_BASE)321 kwargs['headers'] = self.TEST_REQUEST_HEADERS322 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)323 requests.request('POST',324 self.TEST_URL + "/​auth/​tokens",325 **kwargs).AndReturn((resp))326 self.mox.ReplayAll()327 cs = client.Client(token=self.TEST_TOKEN,328 project_id=self.TEST_TENANT_ID,329 auth_url=self.TEST_URL)330 self.assertEqual(cs.auth_tenant_id,331 self.TEST_TENANT_ID)332 self.assertEqual(cs.management_url,333 self.TEST_RESPONSE_DICT["token"]["catalog"][3]334 ['endpoints'][2]["url"])335 self.assertEqual(cs.auth_token,336 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])337 def test_authenticate_success_token_unscoped(self):338 ident = self.TEST_REQUEST_BODY['auth']['identity']339 del ident['password']340 ident['methods'] = ['token']341 ident['token'] = {}342 ident['token']['id'] = self.TEST_TOKEN343 del self.TEST_REQUEST_BODY['auth']['scope']344 del self.TEST_RESPONSE_DICT['token']['catalog']345 self.TEST_REQUEST_HEADERS['X-Auth-Token'] = self.TEST_TOKEN346 resp = utils.TestResponse({347 "status_code": 200,348 "text": json.dumps(self.TEST_RESPONSE_DICT),349 "headers": self.TEST_RESPONSE_HEADERS,350 })351 kwargs = copy.copy(self.TEST_REQUEST_BASE)352 kwargs['headers'] = self.TEST_REQUEST_HEADERS353 kwargs['data'] = json.dumps(self.TEST_REQUEST_BODY, sort_keys=True)354 requests.request('POST',355 self.TEST_URL + "/​auth/​tokens",356 **kwargs).AndReturn((resp))357 self.mox.ReplayAll()358 cs = client.Client(token=self.TEST_TOKEN,359 auth_url=self.TEST_URL)360 self.assertEqual(cs.auth_token,361 self.TEST_RESPONSE_HEADERS["X-Subject-Token"])...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

How To Write End-To-End Tests Using Cypress App Actions

When I started writing tests with Cypress, I was always going to use the user interface to interact and change the application’s state when running tests.

How to Recognize and Hire Top QA / DevOps Engineers

With the rising demand for new services and technologies in the IT, manufacturing, healthcare, and financial sector, QA/ DevOps engineering has become the most important part of software companies. Below is a list of some characteristics to look for when interviewing a potential candidate.

Test Optimization for Continuous Integration

“Test frequently and early.” If you’ve been following my testing agenda, you’re probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. I’ve encountered several teams who have a lot of automated tests but don’t use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.

How To Get Started With Cypress Debugging

One of the most important tasks of a software developer is not just writing code fast; it is the ability to find what causes errors and bugs whenever you encounter one and the ability to solve them quickly.

Options for Manual Test Case Development & Management

The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Httmock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful