Best Python code snippet using tempest_python
test_tenants.py
Source:test_tenants.py
...32 def test_tenant_list_delete(self):33 # Create several tenants and delete them34 tenants = []35 for _ in xrange(3):36 resp, tenant = self.client.create_tenant(rand_name('tenant-new'))37 self.data.tenants.append(tenant)38 tenants.append(tenant)39 tenant_ids = map(lambda x: x['id'], tenants)40 resp, body = self.client.list_tenants()41 self.assertTrue(resp['status'].startswith('2'))42 found = [tenant for tenant in body if tenant['id'] in tenant_ids]43 self.assertEqual(len(found), len(tenants), 'Tenants not created')44 for tenant in tenants:45 resp, body = self.client.delete_tenant(tenant['id'])46 self.assertTrue(resp['status'].startswith('2'))47 self.data.tenants.remove(tenant)48 resp, body = self.client.list_tenants()49 found = [tenant for tenant in body if tenant['id'] in tenant_ids]50 self.assertFalse(any(found), 'Tenants failed to delete')51 @attr(type='negative')52 def test_tenant_delete_by_unauthorized_user(self):53 # Non-admin user should not be able to delete a tenant54 tenant_name = rand_name('tenant-')55 resp, tenant = self.client.create_tenant(tenant_name)56 self.data.tenants.append(tenant)57 self.assertRaises(exceptions.Unauthorized,58 self.non_admin_client.delete_tenant, tenant['id'])59 @attr(type='negative')60 def test_tenant_delete_request_without_token(self):61 # Request to delete a tenant without a valid token should fail62 tenant_name = rand_name('tenant-')63 resp, tenant = self.client.create_tenant(tenant_name)64 self.data.tenants.append(tenant)65 token = self.client.get_auth()66 self.client.delete_token(token)67 self.assertRaises(exceptions.Unauthorized, self.client.delete_tenant,68 tenant['id'])69 self.client.clear_auth()70 @attr(type='negative')71 def test_delete_non_existent_tenant(self):72 # Attempt to delete a non existent tenant should fail73 self.assertRaises(exceptions.NotFound, self.client.delete_tenant,74 'junk_tenant_123456abc')75 def test_tenant_create_with_description(self):76 # Create tenant with a description77 tenant_name = rand_name('tenant-')78 tenant_desc = rand_name('desc-')79 resp, body = self.client.create_tenant(tenant_name,80 description=tenant_desc)81 tenant = body82 self.data.tenants.append(tenant)83 st1 = resp['status']84 tenant_id = body['id']85 desc1 = body['description']86 self.assertTrue(st1.startswith('2'))87 self.assertEqual(desc1, tenant_desc, 'Description should have '88 'been sent in response for create')89 resp, body = self.client.get_tenant(tenant_id)90 desc2 = body['description']91 self.assertEqual(desc2, tenant_desc, 'Description does not appear'92 'to be set')93 self.client.delete_tenant(tenant_id)94 self.data.tenants.remove(tenant)95 def test_tenant_create_enabled(self):96 # Create a tenant that is enabled97 tenant_name = rand_name('tenant-')98 resp, body = self.client.create_tenant(tenant_name, enabled=True)99 tenant = body100 self.data.tenants.append(tenant)101 tenant_id = body['id']102 st1 = resp['status']103 en1 = body['enabled']104 self.assertTrue(st1.startswith('2'))105 self.assertTrue(en1, 'Enable should be True in response')106 resp, body = self.client.get_tenant(tenant_id)107 en2 = body['enabled']108 self.assertTrue(en2, 'Enable should be True in lookup')109 self.client.delete_tenant(tenant_id)110 self.data.tenants.remove(tenant)111 def test_tenant_create_not_enabled(self):112 # Create a tenant that is not enabled113 tenant_name = rand_name('tenant-')114 resp, body = self.client.create_tenant(tenant_name, enabled=False)115 tenant = body116 self.data.tenants.append(tenant)117 tenant_id = body['id']118 st1 = resp['status']119 en1 = body['enabled']120 self.assertTrue(st1.startswith('2'))121 self.assertEqual('false', str(en1).lower(),122 'Enable should be False in response')123 resp, body = self.client.get_tenant(tenant_id)124 en2 = body['enabled']125 self.assertEqual('false', str(en2).lower(),126 'Enable should be False in lookup')127 self.client.delete_tenant(tenant_id)128 self.data.tenants.remove(tenant)129 @attr(type='negative')130 def test_tenant_create_duplicate(self):131 # Tenant names should be unique132 tenant_name = rand_name('tenant-dup-')133 resp, body = self.client.create_tenant(tenant_name)134 tenant = body135 self.data.tenants.append(tenant)136 tenant1_id = body.get('id')137 self.addCleanup(self.client.delete_tenant, tenant1_id)138 self.addCleanup(self.data.tenants.remove, tenant)139 self.assertRaises(exceptions.Duplicate, self.client.create_tenant,140 tenant_name)141 @attr(type='negative')142 def test_create_tenant_by_unauthorized_user(self):143 # Non-admin user should not be authorized to create a tenant144 tenant_name = rand_name('tenant-')145 self.assertRaises(exceptions.Unauthorized,146 self.non_admin_client.create_tenant, tenant_name)147 @attr(type='negative')148 def test_create_tenant_request_without_token(self):149 # Create tenant request without a token should not be authorized150 tenant_name = rand_name('tenant-')151 token = self.client.get_auth()152 self.client.delete_token(token)153 self.assertRaises(exceptions.Unauthorized, self.client.create_tenant,154 tenant_name)155 self.client.clear_auth()156 @attr(type='negative')157 def test_create_tenant_with_empty_name(self):158 # Tenant name should not be empty159 self.assertRaises(exceptions.BadRequest, self.client.create_tenant,160 name='')161 def test_create_tenants_name_length_over_64(self):162 # Tenant name length should not be greater than 64 characters163 tenant_name = 'a' * 65164 self.assertRaises(exceptions.BadRequest, self.client.create_tenant,165 tenant_name)166 def test_tenant_update_name(self):167 # Update name attribute of a tenant168 t_name1 = rand_name('tenant-')169 resp, body = self.client.create_tenant(t_name1)170 tenant = body171 self.data.tenants.append(tenant)172 t_id = body['id']173 resp1_name = body['name']174 t_name2 = rand_name('tenant2-')175 resp, body = self.client.update_tenant(t_id, name=t_name2)176 st2 = resp['status']177 resp2_name = body['name']178 self.assertTrue(st2.startswith('2'))179 self.assertNotEqual(resp1_name, resp2_name)180 resp, body = self.client.get_tenant(t_id)181 resp3_name = body['name']182 self.assertNotEqual(resp1_name, resp3_name)183 self.assertEqual(t_name1, resp1_name)184 self.assertEqual(resp2_name, resp3_name)185 self.client.delete_tenant(t_id)186 self.data.tenants.remove(tenant)187 def test_tenant_update_desc(self):188 # Update description attribute of a tenant189 t_name = rand_name('tenant-')190 t_desc = rand_name('desc-')191 resp, body = self.client.create_tenant(t_name, description=t_desc)192 tenant = body193 self.data.tenants.append(tenant)194 t_id = body['id']195 resp1_desc = body['description']196 t_desc2 = rand_name('desc2-')197 resp, body = self.client.update_tenant(t_id, description=t_desc2)198 st2 = resp['status']199 resp2_desc = body['description']200 self.assertTrue(st2.startswith('2'))201 self.assertNotEqual(resp1_desc, resp2_desc)202 resp, body = self.client.get_tenant(t_id)203 resp3_desc = body['description']204 self.assertNotEqual(resp1_desc, resp3_desc)205 self.assertEqual(t_desc, resp1_desc)206 self.assertEqual(resp2_desc, resp3_desc)207 self.client.delete_tenant(t_id)208 self.data.tenants.remove(tenant)209 def test_tenant_update_enable(self):210 # Update the enabled attribute of a tenant211 t_name = rand_name('tenant-')212 t_en = False213 resp, body = self.client.create_tenant(t_name, enabled=t_en)214 tenant = body215 self.data.tenants.append(tenant)216 t_id = body['id']217 resp1_en = body['enabled']218 t_en2 = True219 resp, body = self.client.update_tenant(t_id, enabled=t_en2)220 st2 = resp['status']221 resp2_en = body['enabled']222 self.assertTrue(st2.startswith('2'))223 self.assertNotEqual(resp1_en, resp2_en)224 resp, body = self.client.get_tenant(t_id)225 resp3_en = body['enabled']226 self.assertNotEqual(resp1_en, resp3_en)227 self.assertEqual('false', str(resp1_en).lower())...
events.py
Source:events.py
1import datetime2from enum import Enum3from json import JSONEncoder4from typing import List5import pydantic6class OperationTypeEnum(str, Enum):7 create_tenant = 'create_tenant'8 assign_tenant = 'assign_tenant'9 delete_tenant = 'delete_tenant'10class DetailTypeEnum(str, Enum):11 # Create tenant events12 create_tenant = 'create_tenant'13 create_tenant_done = 'create_tenant_done'14 create_tenant_infrastructure = 'create_tenant_infrastructure'15 create_tenant_infrastructure_done = 'create_tenant_infrastructure_done'16 preprovisiong_license = 'preprovisiong_license'17 preprovisiong_license_done = 'preprovisiong_license_done'18 # Assign tenant events19 assign_tenant = 'assign_tenant'20 assign_tenant_infrastructure = 'assign_tenant_infrastructure'21 assign_tenant_infrastructure_done = 'assign_tenant_infrastructure_done'22 create_user = 'create_user'23 create_user_done = 'create_user_done'24 update_license = 'update_license'25 update_license_done = 'update_license_done'26 # Delete tenant events27 delete_tenant = 'delete_tenant'28 delete_tenant_infrastructure_done = 'delete_tenant_infrastructure_done'29 delete_license_done = 'delete_tenant_license_done'30class BaseEvent(pydantic.BaseModel):31 # header32 uuid: str33 correlation_id: str34 source: str35 detail_type: str36 timestamp: datetime.datetime37 operation_uuid: str38 operation_type: str39 termination_table: List[str]40class TenantEvent(BaseEvent):41 # body42 tenant_id: str43class EventEncoder(JSONEncoder):44 # Override the default method45 def default(self, obj):46 if isinstance(obj, (datetime.date, datetime.datetime)):...
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!!