Best Python code snippet using play_requests_python
test_users.py
Source:test_users.py
1import urlparse2import json3import httplib24from keystoneclient.v2_0 import users5from tests import utils6class UserTests(utils.TestCase):7 def setUp(self):8 super(UserTests, self).setUp()9 self.TEST_REQUEST_HEADERS = {'X-Auth-Token': 'aToken',10 'User-Agent': 'python-keystoneclient'}11 self.TEST_POST_HEADERS = {'Content-Type': 'application/json',12 'X-Auth-Token': 'aToken',13 'User-Agent': 'python-keystoneclient'}14 self.TEST_USERS = {15 "users": {16 "values": [17 {18 "email": "None",19 "enabled": True,20 "id": 1,21 "name": "admin"22 },23 {24 "email": "None",25 "enabled": True,26 "id": 2,27 "name": "demo"28 },29 ]30 }31 }32 def test_create(self):33 req_body = {"user": {"name": "gabriel",34 "password": "test",35 "tenantId": 2,36 "email": "test@example.com",37 "enabled": True}}38 resp_body = {"user": {"name": "gabriel",39 "enabled": True,40 "tenantId": 2,41 "id": 3,42 "password": "test",43 "email": "test@example.com"}}44 resp = httplib2.Response({45 "status": 200,46 "body": json.dumps(resp_body),47 })48 httplib2.Http.request(urlparse.urljoin(self.TEST_URL, 'v2.0/users'),49 'POST',50 body=json.dumps(req_body),51 headers=self.TEST_POST_HEADERS) \52 .AndReturn((resp, resp['body']))53 self.mox.ReplayAll()54 user = self.client.users.create(req_body['user']['name'],55 req_body['user']['password'],56 req_body['user']['email'],57 tenant_id=req_body['user']['tenantId'],58 enabled=req_body['user']['enabled'])59 self.assertTrue(isinstance(user, users.User))60 self.assertEqual(user.id, 3)61 self.assertEqual(user.name, "gabriel")62 self.assertEqual(user.email, "test@example.com")63 def test_delete(self):64 resp = httplib2.Response({65 "status": 200,66 "body": ""67 })68 httplib2.Http.request(urlparse.urljoin(self.TEST_URL, 'v2.0/users/1'),69 'DELETE',70 headers=self.TEST_REQUEST_HEADERS) \71 .AndReturn((resp, resp['body']))72 self.mox.ReplayAll()73 self.client.users.delete(1)74 def test_get(self):75 resp = httplib2.Response({76 "status": 200,77 "body": json.dumps({'user': self.TEST_USERS['users']['values'][0]})78 })79 httplib2.Http.request(urlparse.urljoin(self.TEST_URL,80 'v2.0/users/1?fresh=1234'),81 'GET',82 headers=self.TEST_REQUEST_HEADERS) \83 .AndReturn((resp, resp['body']))84 self.mox.ReplayAll()85 u = self.client.users.get(1)86 self.assertTrue(isinstance(u, users.User))87 self.assertEqual(u.id, 1)88 self.assertEqual(u.name, 'admin')89 def test_list(self):90 resp = httplib2.Response({91 "status": 200,92 "body": json.dumps(self.TEST_USERS),93 })94 httplib2.Http.request(urlparse.urljoin(self.TEST_URL,95 'v2.0/users?fresh=1234'),96 'GET',97 headers=self.TEST_REQUEST_HEADERS) \98 .AndReturn((resp, resp['body']))99 self.mox.ReplayAll()100 user_list = self.client.users.list()101 [self.assertTrue(isinstance(u, users.User)) for u in user_list]102 def test_update(self):103 req_1 = {"user": {"password": "swordfish", "id": 2}}104 req_2 = {"user": {"id": 2, "email": "gabriel@example.com"}}105 req_3 = {"user": {"tenantId": 1, "id": 2}}106 req_4 = {"user": {"enabled": False, "id": 2}}107 # Keystone basically echoes these back... including the password :-/108 resp_1 = httplib2.Response({"status": 200, "body": json.dumps(req_1)})109 resp_2 = httplib2.Response({"status": 200, "body": json.dumps(req_2)})110 resp_3 = httplib2.Response({"status": 200, "body": json.dumps(req_3)})111 resp_4 = httplib2.Response({"status": 200, "body": json.dumps(req_3)})112 httplib2.Http.request(urlparse.urljoin(self.TEST_URL,113 'v2.0/users/2/password'),114 'PUT',115 body=json.dumps(req_1),116 headers=self.TEST_POST_HEADERS) \117 .AndReturn((resp_1, resp_1['body']))118 httplib2.Http.request(urlparse.urljoin(self.TEST_URL, 'v2.0/users/2'),119 'PUT',120 body=json.dumps(req_2),121 headers=self.TEST_POST_HEADERS) \122 .AndReturn((resp_2, resp_2['body']))123 httplib2.Http.request(urlparse.urljoin(self.TEST_URL,124 'v2.0/users/2/tenant'),125 'PUT',126 body=json.dumps(req_3),127 headers=self.TEST_POST_HEADERS) \128 .AndReturn((resp_3, resp_3['body']))129 httplib2.Http.request(urlparse.urljoin(self.TEST_URL,130 'v2.0/users/2/enabled'),131 'PUT',132 body=json.dumps(req_4),133 headers=self.TEST_POST_HEADERS) \134 .AndReturn((resp_4, resp_4['body']))135 self.mox.ReplayAll()136 user = self.client.users.update_password(2, 'swordfish')137 user = self.client.users.update_email(2, 'gabriel@example.com')138 user = self.client.users.update_tenant(2, 1)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!