How to use organization method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

test_organizations.py

Source:test_organizations.py Github

copy

Full Screen

...104 get(reverse('api:organization_inventories_list', kwargs={'pk': organization.id}), user=rando, expect=403)105 106@pytest.mark.django_db107@mock.patch('awx.api.views.feature_enabled', lambda feature: True)108def test_create_organization(post, admin, alice):109 new_org = {110 'name': 'new org',111 'description': 'my description'112 }113 res = post(reverse('api:organization_list'), new_org, user=admin, expect=201)114 assert res.data['name'] == new_org['name']115 res = post(reverse('api:organization_list'), new_org, user=admin, expect=400)116@pytest.mark.django_db117@mock.patch('awx.api.views.feature_enabled', lambda feature: True)118def test_create_organization_xfail(post, alice):119 new_org = {120 'name': 'new org',121 'description': 'my description'122 }123 post(reverse('api:organization_list'), new_org, user=alice, expect=403)124@pytest.mark.django_db125def test_add_user_to_organization(post, organization, alice, bob):126 organization.admin_role.members.add(alice)127 post(reverse('api:organization_users_list', kwargs={'pk': organization.id}), {'id': bob.id}, user=alice, expect=204)128 assert bob in organization.member_role129 post(reverse('api:organization_users_list', kwargs={'pk': organization.id}), {'id': bob.id, 'disassociate': True} , user=alice, expect=204)130 assert bob not in organization.member_role131@pytest.mark.django_db132def test_add_user_to_organization_xfail(post, organization, alice, bob):133 organization.member_role.members.add(alice)134 post(reverse('api:organization_users_list', kwargs={'pk': organization.id}), {'id': bob.id}, user=alice, expect=403)135@pytest.mark.django_db136def test_add_admin_to_organization(post, organization, alice, bob):137 organization.admin_role.members.add(alice)138 post(reverse('api:organization_admins_list', kwargs={'pk': organization.id}), {'id': bob.id}, user=alice, expect=204)139 assert bob in organization.admin_role140 assert bob in organization.member_role141 post(reverse('api:organization_admins_list', kwargs={'pk': organization.id}), {'id': bob.id, 'disassociate': True} , user=alice, expect=204)142 assert bob not in organization.admin_role143 assert bob not in organization.member_role144@pytest.mark.django_db145def test_add_admin_to_organization_xfail(post, organization, alice, bob):146 organization.member_role.members.add(alice)147 post(reverse('api:organization_admins_list', kwargs={'pk': organization.id}), {'id': bob.id}, user=alice, expect=403)148@pytest.mark.django_db149def test_update_organization(get, put, organization, alice, bob):150 organization.admin_role.members.add(alice)151 data = get(reverse('api:organization_detail', kwargs={'pk': organization.id}), user=alice, expect=200).data152 data['description'] = 'hi'153 put(reverse('api:organization_detail', kwargs={'pk': organization.id}), data, user=alice, expect=200)154 organization.refresh_from_db()155 assert organization.description == 'hi'156 data['description'] = 'bye'157 put(reverse('api:organization_detail', kwargs={'pk': organization.id}), data, user=bob, expect=403)158@pytest.mark.django_db159def test_update_organization_max_hosts(get, put, organization, admin, alice, bob):160 # Admin users can get and update max_hosts161 data = get(reverse('api:organization_detail', kwargs={'pk': organization.id}), user=admin, expect=200).data162 assert organization.max_hosts == 0163 data['max_hosts'] = 3164 put(reverse('api:organization_detail', kwargs={'pk': organization.id}), data, user=admin, expect=200)165 organization.refresh_from_db()166 assert organization.max_hosts == 3167 # Organization admins can get the data and can update other fields, but not max_hosts168 organization.admin_role.members.add(alice)169 data = get(reverse('api:organization_detail', kwargs={'pk': organization.id}), user=alice, expect=200).data170 data['max_hosts'] = 5171 put(reverse('api:organization_detail', kwargs={'pk': organization.id}), data, user=alice, expect=400)172 organization.refresh_from_db()173 assert organization.max_hosts == 3174 # Ordinary users shouldn't be able to update either.175 put(reverse('api:organization_detail', kwargs={'pk': organization.id}), data, user=bob, expect=403)176 organization.refresh_from_db()177 assert organization.max_hosts == 3178@pytest.mark.django_db179@mock.patch('awx.main.access.BaseAccess.check_license', lambda *a, **kw: True)180def test_delete_organization(delete, organization, admin):181 delete(reverse('api:organization_detail', kwargs={'pk': organization.id}), user=admin, expect=204)182@pytest.mark.django_db183@mock.patch('awx.main.access.BaseAccess.check_license', lambda *a, **kw: True)184def test_delete_organization2(delete, organization, alice):185 organization.admin_role.members.add(alice)186 delete(reverse('api:organization_detail', kwargs={'pk': organization.id}), user=alice, expect=204)187@pytest.mark.django_db188@mock.patch('awx.main.access.BaseAccess.check_license', lambda *a, **kw: True)189def test_delete_organization_xfail1(delete, organization, alice):190 organization.member_role.members.add(alice)191 delete(reverse('api:organization_detail', kwargs={'pk': organization.id}), user=alice, expect=403)192@pytest.mark.django_db193@mock.patch('awx.main.access.BaseAccess.check_license', lambda *a, **kw: True)194def test_delete_organization_xfail2(delete, organization):...

Full Screen

Full Screen

test_organization_counts.py

Source:test_organization_counts.py Github

copy

Full Screen

...51 'inventories': 0,52 'teams': 053}54@pytest.fixture55def resourced_organization(organization_resource_creator):56 return organization_resource_creator(**COUNTS_PRIMES)57@pytest.mark.django_db58def test_org_counts_detail_admin(resourced_organization, user, get):59 # Check that all types of resources are counted by a superuser60 external_admin = user('admin', True)61 response = get(reverse('api:organization_detail',62 kwargs={'pk': resourced_organization.pk}), external_admin)63 assert response.status_code == 20064 counts = response.data['summary_fields']['related_field_counts']65 assert counts == COUNTS_PRIMES66@pytest.mark.django_db67def test_org_counts_detail_member(resourced_organization, user, get):68 # Check that a non-admin org member can only see users / admin in detail view69 member_user = resourced_organization.member_role.members.get(username='org-member 1')...

Full Screen

Full Screen

test_rbac_core.py

Source:test_rbac_core.py Github

copy

Full Screen

1import pytest2from awx.main.models import (3 Role,4 Organization,5 Project,6)7from awx.main.fields import update_role_parentage_for_instance8@pytest.mark.django_db9def test_auto_inheritance_by_children(organization, alice):10 A = Role.objects.create()11 B = Role.objects.create()12 A.members.add(alice)13 assert alice not in organization.admin_role14 assert Organization.accessible_objects(alice, 'admin_role').count() == 015 A.children.add(B)16 assert alice not in organization.admin_role17 assert Organization.accessible_objects(alice, 'admin_role').count() == 018 A.children.add(organization.admin_role)19 assert alice in organization.admin_role20 assert Organization.accessible_objects(alice, 'admin_role').count() == 121 A.children.remove(organization.admin_role)22 assert alice not in organization.admin_role23 B.children.add(organization.admin_role)24 assert alice in organization.admin_role25 B.children.remove(organization.admin_role)26 assert alice not in organization.admin_role27 assert Organization.accessible_objects(alice, 'admin_role').count() == 028 # We've had the case where our pre/post save init handlers in our field descriptors29 # end up creating a ton of role objects because of various not-so-obvious issues30 assert Role.objects.count() < 5031@pytest.mark.django_db32def test_auto_inheritance_by_parents(organization, alice):33 A = Role.objects.create()34 B = Role.objects.create()35 A.members.add(alice)36 assert alice not in organization.admin_role37 B.parents.add(A)38 assert alice not in organization.admin_role39 organization.admin_role.parents.add(A)40 assert alice in organization.admin_role41 organization.admin_role.parents.remove(A)42 assert alice not in organization.admin_role43 organization.admin_role.parents.add(B)44 assert alice in organization.admin_role45 organization.admin_role.parents.remove(B)46 assert alice not in organization.admin_role47@pytest.mark.django_db48def test_accessible_objects(organization, alice, bob):49 A = Role.objects.create()50 A.members.add(alice)51 B = Role.objects.create()52 B.members.add(alice)53 B.members.add(bob)54 assert Organization.accessible_objects(alice, 'admin_role').count() == 055 assert Organization.accessible_objects(bob, 'admin_role').count() == 056 A.children.add(organization.admin_role)57 assert Organization.accessible_objects(alice, 'admin_role').count() == 158 assert Organization.accessible_objects(bob, 'admin_role').count() == 059@pytest.mark.django_db60def test_team_symantics(organization, team, alice):61 assert alice not in organization.auditor_role62 team.member_role.children.add(organization.auditor_role)63 assert alice not in organization.auditor_role64 team.member_role.members.add(alice)65 assert alice in organization.auditor_role66 team.member_role.members.remove(alice)67 assert alice not in organization.auditor_role68@pytest.mark.django_db69def test_auto_field_adjustments(organization, inventory, team, alice):70 'Ensures the auto role reparenting is working correctly through non m2m fields'71 org2 = Organization.objects.create(name='Org 2', description='org 2')72 org2.admin_role.members.add(alice)73 assert alice not in inventory.admin_role74 inventory.organization = org275 inventory.save()76 assert alice in inventory.admin_role77 inventory.organization = organization78 inventory.save()79 assert alice not in inventory.admin_role80 #assert False81@pytest.mark.django_db82def test_implicit_deletes(alice):83 'Ensures implicit resources and roles delete themselves'84 delorg = Organization.objects.create(name='test-org')85 child = Role.objects.create()86 child.parents.add(delorg.admin_role)87 delorg.admin_role.members.add(alice)88 admin_role_id = delorg.admin_role.id89 auditor_role_id = delorg.auditor_role.id90 assert child.ancestors.count() > 191 assert Role.objects.filter(id=admin_role_id).count() == 192 assert Role.objects.filter(id=auditor_role_id).count() == 193 n_alice_roles = alice.roles.count()94 n_system_admin_children = Role.singleton('system_administrator').children.count()95 delorg.delete()96 assert Role.objects.filter(id=admin_role_id).count() == 097 assert Role.objects.filter(id=auditor_role_id).count() == 098 assert alice.roles.count() == (n_alice_roles - 1)99 assert Role.singleton('system_administrator').children.count() == (n_system_admin_children - 1)100 assert child.ancestors.count() == 1101 assert child.ancestors.all()[0] == child102@pytest.mark.django_db103def test_content_object(user):104 'Ensure our content_object stuf seems to be working'105 org = Organization.objects.create(name='test-org')106 assert org.admin_role.content_object.id == org.id107@pytest.mark.django_db108def test_hierarchy_rebuilding_multi_path():109 'Tests a subdtle cases around role hierarchy rebuilding when you have multiple paths to the same role of different length'110 X = Role.objects.create()111 A = Role.objects.create()112 B = Role.objects.create()113 C = Role.objects.create()114 D = Role.objects.create()115 A.children.add(B)116 A.children.add(D)117 B.children.add(C)118 C.children.add(D)119 assert A.is_ancestor_of(D)120 assert X.is_ancestor_of(D) is False121 X.children.add(A)122 assert X.is_ancestor_of(D) is True123 X.children.remove(A)124 # This can be the stickler, the rebuilder needs to ensure that D's role125 # hierarchy is built after both A and C are updated.126 assert X.is_ancestor_of(D) is False127@pytest.mark.django_db128def test_auto_parenting():129 org1 = Organization.objects.create(name='org1')130 org2 = Organization.objects.create(name='org2')131 prj1 = Project.objects.create(name='prj1')132 prj2 = Project.objects.create(name='prj2')133 assert org1.admin_role.is_ancestor_of(prj1.admin_role) is False134 assert org1.admin_role.is_ancestor_of(prj2.admin_role) is False135 assert org2.admin_role.is_ancestor_of(prj1.admin_role) is False136 assert org2.admin_role.is_ancestor_of(prj2.admin_role) is False137 prj1.organization = org1138 prj1.save()139 assert org1.admin_role.is_ancestor_of(prj1.admin_role)140 assert org1.admin_role.is_ancestor_of(prj2.admin_role) is False141 assert org2.admin_role.is_ancestor_of(prj1.admin_role) is False142 assert org2.admin_role.is_ancestor_of(prj2.admin_role) is False143 prj2.organization = org1144 prj2.save()145 assert org1.admin_role.is_ancestor_of(prj1.admin_role)146 assert org1.admin_role.is_ancestor_of(prj2.admin_role)147 assert org2.admin_role.is_ancestor_of(prj1.admin_role) is False148 assert org2.admin_role.is_ancestor_of(prj2.admin_role) is False149 prj1.organization = org2150 prj1.save()151 assert org1.admin_role.is_ancestor_of(prj1.admin_role) is False152 assert org1.admin_role.is_ancestor_of(prj2.admin_role)153 assert org2.admin_role.is_ancestor_of(prj1.admin_role)154 assert org2.admin_role.is_ancestor_of(prj2.admin_role) is False155 prj2.organization = org2156 prj2.save()157 assert org1.admin_role.is_ancestor_of(prj1.admin_role) is False158 assert org1.admin_role.is_ancestor_of(prj2.admin_role) is False159 assert org2.admin_role.is_ancestor_of(prj1.admin_role)160 assert org2.admin_role.is_ancestor_of(prj2.admin_role)161@pytest.mark.django_db162def test_update_parents_keeps_teams(team, project):163 project.update_role.parents.add(team.member_role)164 assert team.member_role in project.update_role # test prep sanity check165 update_role_parentage_for_instance(project)...

Full Screen

Full Screen

views.py

Source:views.py Github

copy

Full Screen

1from django.utils.translation import ugettext_lazy as _2from rest_framework import mixins3from rest_framework.exceptions import PermissionDenied4from rest_framework.generics import get_object_or_4045from rest_framework.permissions import IsAuthenticated6from rest_framework.viewsets import GenericViewSet7from bothub.api.v2.metadata import Metadata8from bothub.authentication.models import User9from bothub.common.models import Organization, OrganizationAuthorization10from .filters import OrganizationAuthorizationFilter11from .permissions import (12 OrganizationAdminManagerAuthorization,13 OrganizationHasPermission,14)15from .serializers import (16 OrganizationSeralizer,17 OrganizationAuthorizationSerializer,18 OrganizationAuthorizationRoleSerializer,19)20class OrganizationViewSet(21 mixins.ListModelMixin,22 mixins.RetrieveModelMixin,23 mixins.CreateModelMixin,24 mixins.UpdateModelMixin,25 mixins.DestroyModelMixin,26 GenericViewSet,27):28 queryset = Organization.objects.all()29 serializer_class = OrganizationSeralizer30 permission_classes = [IsAuthenticated, OrganizationHasPermission]31 lookup_field = "nickname"32 metadata_class = Metadata33 def get_queryset(self, *args, **kwargs):34 if getattr(self, "swagger_fake_view", False):35 # queryset just for schema generation metadata36 return Organization.objects.none()37 auth = (38 OrganizationAuthorization.objects.exclude(role=0)39 .filter(user=self.request.user)40 .values("organization")41 )42 return self.queryset.filter(repository_owner__in=auth)43class OrganizationProfileViewSet(mixins.RetrieveModelMixin, GenericViewSet):44 """45 Get organization profile46 """47 serializer_class = OrganizationSeralizer48 queryset = Organization.objects49 lookup_field = "nickname"50class OrganizationAuthorizationViewSet(51 mixins.ListModelMixin,52 mixins.UpdateModelMixin,53 mixins.DestroyModelMixin,54 GenericViewSet,55):56 queryset = OrganizationAuthorization.objects.exclude(57 role=OrganizationAuthorization.ROLE_NOT_SETTED58 )59 serializer_class = OrganizationAuthorizationSerializer60 filter_class = OrganizationAuthorizationFilter61 permission_classes = [IsAuthenticated]62 lookup_fields = ["organization_nickname", "user__nickname"]63 def get_object(self):64 organization = get_object_or_404(65 Organization, nickname=self.kwargs.get("organization_nickname")66 )67 user = get_object_or_404(User, nickname=self.kwargs.get("user__nickname"))68 obj = organization.get_organization_authorization(user)69 self.check_object_permissions(self.request, obj)70 return obj71 def update(self, *args, **kwargs):72 self.filter_class = None73 self.serializer_class = OrganizationAuthorizationRoleSerializer74 self.permission_classes = [75 IsAuthenticated,76 OrganizationAdminManagerAuthorization,77 ]78 response = super().update(*args, **kwargs)79 self.get_object()80 return response81 def destroy(self, request, *args, **kwargs):82 self.filter_class = None83 self.serializer_class = OrganizationAuthorizationRoleSerializer84 self.permission_classes = [85 IsAuthenticated,86 OrganizationAdminManagerAuthorization,87 ]88 return super().destroy(request, *args, **kwargs)89 def perform_destroy(self, instance):90 user = self.kwargs.get("user__nickname")91 if self.request.user.nickname == user:92 raise PermissionDenied(_("You cannot delete your own user."))...

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