How to use _create_alias method in localstack

Best Python code snippet using localstack_python

test_user_limits.py

Source:test_user_limits.py Github

copy

Full Screen

...56 values.update(kwargs)57 return self.ajax_post(58 reverse("admin:account_add"), values, status59 )60 def _create_alias(self, email, rcpt="user@test.com", status=200):61 values = {62 "address": email, "recipients": rcpt, "enabled": True63 }64 return self.ajax_post(65 reverse("admin:alias_add"), values, status66 )67 def _create_domain(self, name, status=200, withtpl=False, **kwargs):68 values = {69 "name": name, "quota": 100, "default_mailbox_quota": 10,70 "create_dom_admin": False, "with_mailbox": True,71 "create_aliases": False, "stepid": "step3", "type": "domain"72 }73 if withtpl:74 values.update({75 "create_dom_admin": True,76 "dom_admin_username": "admin",77 "create_aliases": True78 })79 values.update(kwargs)80 response = self.ajax_post(81 reverse("admin:domain_add"), values, status82 )83 return response84 def _domain_alias_operation(self, optype, domain, name, status=200):85 dom = Domain.objects.get(name=domain)86 values = {87 "name": dom.name, "quota": dom.quota, "enabled": dom.enabled,88 "type": "domain",89 "default_mailbox_quota": dom.default_mailbox_quota,90 }91 aliases = [alias.name for alias in dom.domainalias_set.all()]92 if optype == "add":93 aliases.append(name)94 else:95 aliases.remove(name)96 for cpt, alias in enumerate(aliases):97 fname = "aliases" if not cpt else "aliases_%d" % cpt98 values[fname] = alias99 self.ajax_post(100 reverse("admin:domain_change", args=[dom.id]),101 values, status102 )103 def _check_limit(self, name, curvalue, maxvalue):104 limit = self.user.userobjectlimit_set.get(name=name)105 self.assertEqual(limit.current_value, curvalue)106 self.assertEqual(limit.max_value, maxvalue)107class DomainAdminTestCase(ResourceTestCase):108 @classmethod109 def setUpTestData(cls): # NOQA:N802110 """Create test data."""111 super(DomainAdminTestCase, cls).setUpTestData()112 cls.user = User.objects.get(username="admin@test.com")113 cls.user.userobjectlimit_set.filter(114 name__in=["mailboxes", "mailbox_aliases"]).update(max_value=2)115 def setUp(self):116 """Test initialization."""117 super(DomainAdminTestCase, self).setUp()118 self.client.force_login(self.user)119 def test_mailboxes_limit(self):120 self._create_account("tester1@test.com")121 self._check_limit("mailboxes", 1, 2)122 self._create_account("tester2@test.com")123 self._check_limit("mailboxes", 2, 2)124 self._create_account("tester3@test.com", status=403)125 self._check_limit("mailboxes", 2, 2)126 self.ajax_post(127 reverse("admin:account_delete",128 args=[User.objects.get(username="tester2@test.com").id]),129 {}130 )131 self._check_limit("mailboxes", 1, 2)132 def test_aliases_limit(self):133 self._create_alias("alias1@test.com")134 self._check_limit("mailbox_aliases", 1, 2)135 self._create_alias("alias2@test.com")136 self._check_limit("mailbox_aliases", 2, 2)137 self._create_alias("alias3@test.com", status=403)138 self._check_limit("mailbox_aliases", 2, 2)139 # Set unlimited value140 self.user.userobjectlimit_set.filter(name="mailbox_aliases").update(141 max_value=-1)142 self._create_alias("alias3@test.com")143 self._check_limit("mailbox_aliases", 3, -1)144 self.ajax_post(145 reverse("admin:alias_delete") + "?selection=%d"146 % Alias.objects.get(address="alias2@test.com").id,147 {}148 )149 self._check_limit("mailbox_aliases", 2, -1)150 def test_aliases_limit_through_account_form(self):151 user = User.objects.get(username="user@test.com")152 values = {153 "username": user.username, "role": user.role,154 "is_active": user.is_active, "email": user.email, "quota_act": True,155 "aliases": "alias1@test.com", "aliases_1": "alias2@test.com",156 "language": "en"157 }158 self.ajax_post(159 reverse("admin:account_change", args=[user.id]),160 values161 )162 Alias.objects.get(address="alias1@test.com")163 self._check_limit("mailbox_aliases", 2, 2)164class ResellerTestCase(ResourceTestCase):165 @classmethod166 def setUpTestData(cls): # NOQA:N802167 """Create test data."""168 super(ResellerTestCase, cls).setUpTestData()169 cls.localconfig.parameters.set_value("deflt_user_quota_limit", 1000)170 cls.localconfig.save()171 cls.user = UserFactory(172 username="reseller", groups=("Resellers",)173 )174 def setUp(self):175 """Test initialization."""176 super(ResellerTestCase, self).setUp()177 self.client.force_login(self.user)178 def test_domains_limit(self):179 response = self.client.get(reverse("admin:domain_list"))180 self.assertContains(response, "Domains (0%)")181 self.assertContains(response, "Domain aliases (0%)")182 self._create_domain("domain1.tld")183 self._check_limit("domains", 1, 2)184 self._create_domain("domain2.tld")185 self._check_limit("domains", 2, 2)186 self._create_domain("domain3.tld", 403)187 self._check_limit("domains", 2, 2)188 self.ajax_post(189 reverse("admin:domain_delete",190 args=[Domain.objects.get(name="domain2.tld").id]),191 {}192 )193 self._check_limit("domains", 1, 2)194 def test_domain_aliases_limit(self):195 self._create_domain("pouet.com")196 self._domain_alias_operation("add", "pouet.com", "domain-alias1.tld")197 self._check_limit("domain_aliases", 1, 2)198 self._domain_alias_operation("add", "pouet.com", "domain-alias2.tld")199 self._check_limit("domain_aliases", 2, 2)200 self._domain_alias_operation(201 "add", "pouet.com", "domain-alias3.tld", 403202 )203 self._check_limit("domain_aliases", 2, 2)204 self._domain_alias_operation(205 "delete", "pouet.com", "domain-alias2.tld")206 self._check_limit("domain_aliases", 1, 2)207 def test_domain_admins_limit(self):208 response = self.client.get(reverse("admin:identity_list"))209 self.assertContains(response, "Domain admins (0%)")210 self.assertContains(response, "Mailboxes (0%)")211 self.assertContains(response, "Mailbox aliases (0%)")212 self._create_domain("domain.tld")213 self._create_account("admin1@domain.tld", role="DomainAdmins")214 self._check_limit("domain_admins", 1, 2)215 self._create_account("admin2@domain.tld", role="DomainAdmins")216 self._check_limit("domain_admins", 2, 2)217 resp = self._create_account(218 "admin3@domain.tld",219 role="DomainAdmins",220 status=400)221 self.assertEqual(222 resp["form_errors"]["role"][0],223 "Select a valid choice. DomainAdmins is not one of the available "224 "choices."225 )226 self._check_limit("domain_admins", 2, 2)227 self.user.userobjectlimit_set.filter(228 name="mailboxes").update(max_value=3)229 self._create_account("user1@domain.tld")230 user = User.objects.get(username="user1@domain.tld")231 values = {232 "username": user.username, "role": "DomainAdmins",233 "quota_act": True, "is_active": user.is_active,234 "email": user.email, "language": "en"235 }236 resp = self.ajax_post(237 reverse("admin:account_change", args=[user.id]),238 values, status=400239 )240 self.assertEqual(241 resp["form_errors"]["role"][0],242 "Select a valid choice. DomainAdmins is not one of the available "243 "choices."244 )245 self._check_limit("domain_admins", 2, 2)246 def test_domain_admin_resource_are_empty(self):247 self._create_domain("domain.tld")248 self._create_account("admin1@domain.tld", role="DomainAdmins")249 domadmin = User.objects.get(username="admin1@domain.tld")250 for l in ["mailboxes", "mailbox_aliases"]:251 self.assertEqual(252 domadmin.userobjectlimit_set.get(name=l).max_value, 0253 )254 def test_domain_admins_limit_from_domain_tpl(self):255 self.user.userobjectlimit_set.filter(256 name="domains").update(max_value=3)257 self._create_domain("domain1.tld", withtpl=True)258 self._create_domain("domain2.tld", withtpl=True)259 self._check_limit("domain_admins", 2, 2)260 self._check_limit("domains", 2, 3)261 self._create_domain("domain3.tld", status=200, withtpl=True)262 self._check_limit("domain_admins", 2, 2)263 self._check_limit("domains", 3, 3)264 def test_quota(self):265 """Check quota resource."""266 self._create_domain("domain1.tld", withtpl=True, quota=1000)267 response = self._create_domain(268 "domain2.tld", status=403, withtpl=True, quota=1000)269 self.assertEqual(response, "Quota: limit reached")270 dom1 = Domain.objects.get(name="domain1.tld")271 url = reverse("admin:domain_change", args=[dom1.pk])272 values = {273 "name": dom1.name, "type": dom1.type, "enabled": dom1.enabled,274 "default_mailbox_quota": dom1.default_mailbox_quota,275 "quota": 500276 }277 self.ajax_post(url, values)278 def test_quota_constraints(self):279 """Check reseller can't define unlimited quota."""280 response = self._create_domain("domain1.tld", 400, quota=0)281 self.assertEqual(282 response["form_errors"]["quota"][0],283 "You can't define an unlimited quota.")284 response = self._create_domain(285 "domain1.tld", 400, default_mailbox_quota=0)286 self.assertEqual(287 response["form_errors"]["default_mailbox_quota"][0],288 "You can't define an unlimited quota.")289 self.user.userobjectlimit_set.filter(name="quota").update(max_value=0)290 response = self._create_domain("domain2.tld", quota=0)291 def test_quota_propagation(self):292 """Check that quota is applied everywhere."""293 # Try to assign an unlimited quota to an admin294 # See https://github.com/modoboa/modoboa/issues/1223295 self._create_domain("domain1.tld")296 self._create_account(297 "admin@domain1.tld", role="DomainAdmins",298 quota_act=False, quota=0, status=400)299 self._create_domain("domain2.tld", withtpl=True)300 admin = User.objects.get(username="admin@domain2.tld")301 values = {302 "username": admin.username, "role": admin.role,303 "is_active": admin.is_active, "email": admin.email,304 "mailboxes_limit": 1, "mailbox_aliases_limit": 2,305 "language": "en", "quota_act": False, "quota": 0306 }307 self.ajax_post(308 reverse("admin:account_change", args=[admin.pk]),309 values,310 400311 )312 def test_reseller_deletes_domain(self):313 """Check if all resources are restored after the deletion."""314 self._create_domain("domain.tld", withtpl=True)315 dom = Domain.objects.get(name="domain.tld")316 self.ajax_post(317 reverse("admin:domain_delete", args=[dom.id]),318 {}319 )320 self._check_limit("domains", 0, 2)321 self._check_limit("domain_admins", 1, 2)322 self._check_limit("mailboxes", 0, 2)323 self._check_limit("mailbox_aliases", 0, 2)324 def test_sadmin_removes_ownership(self):325 self._create_domain("domain.tld", withtpl=True)326 dom = Domain.objects.get(name="domain.tld")327 self.client.logout()328 self.client.login(username="admin", password="password")329 self.ajax_get(330 "{0}?domid={1}&daid={2}".format(331 reverse("admin:permission_remove"),332 dom.id, self.user.id333 ), {}334 )335 self._check_limit("domains", 0, 2)336 self._check_limit("domain_admins", 0, 2)337 self._check_limit("mailboxes", 0, 2)338 self._check_limit("mailbox_aliases", 0, 2)339 def test_allocate_from_pool(self):340 self._create_domain("domain.tld")341 self._create_account("admin1@domain.tld", role="DomainAdmins")342 user = User.objects.get(username="admin1@domain.tld")343 # Give 1 mailbox and 2 aliases to the admin -> should work344 values = {345 "username": user.username, "role": user.role, "quota_act": True,346 "is_active": user.is_active, "email": user.email,347 "mailboxes_limit": 1, "mailbox_aliases_limit": 2,348 "language": "en"349 }350 self.ajax_post(351 reverse("admin:account_change", args=[user.id]),352 values353 )354 self._check_limit("mailboxes", 1, 1)355 self._check_limit("mailbox_aliases", 0, 0)356 # Delete the admin -> resources should go back to the357 # reseller's pool358 self.ajax_post(359 reverse("admin:account_delete", args=[user.id]),360 {}361 )362 self._check_limit("mailboxes", 0, 2)363 self._check_limit("mailbox_aliases", 0, 2)364 def test_restore_resources(self):365 """Give resource to a domain admin and restore them."""366 self._create_domain("domain.tld")367 dom = Domain.objects.get(name="domain.tld")368 self._create_account("admin1@domain.tld", role="DomainAdmins")369 user = User.objects.get(username="admin1@domain.tld")370 values = {371 "username": user.username, "role": user.role, "quota_act": True,372 "is_active": user.is_active, "email": user.email,373 "mailboxes_limit": 1, "mailbox_aliases_limit": 2,374 "language": "en"375 }376 self.ajax_post(377 reverse("admin:account_change", args=[user.id]),378 values379 )380 dom.add_admin(user)381 self.client.logout()382 self.client.login(username="admin1@domain.tld", password="Toto1234")383 self._create_account("user1@domain.tld")384 self._create_alias("alias1@domain.tld", "user1@domain.tld")385 self._create_alias("alias2@domain.tld", "user1@domain.tld")386 self.client.logout()387 self.client.login(username="reseller", password="toto")388 # Delete the admin -> resources should go back to the389 # reseller's pool390 self.ajax_post(391 reverse("admin:account_delete", args=[user.id]),392 {}393 )394 self._check_limit("mailboxes", 1, 2)395 self._check_limit("mailbox_aliases", 2, 2)396 def test_change_role(self):397 self._create_domain("domain.tld")398 self._create_account("admin1@domain.tld", role="DomainAdmins")399 user = User.objects.get(username="admin1@domain.tld")...

Full Screen

Full Screen

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 localstack 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