Best Python code snippet using Kiwi_python
test_permissions.py
Source:test_permissions.py
1from django.urls import reverse2from saleor.account.models import User3def test_staff_can_access_product_details(4 staff_client, staff_user, permission_manage_products, product):5 assert not staff_user.has_perm('product.manage_products')6 url = reverse('dashboard:product-details', kwargs={'pk': product.pk})7 response = staff_client.get(url)8 assert response.status_code == 3029 staff_user.user_permissions.add(permission_manage_products)10 staff_user = User.objects.get(pk=staff_user.pk)11 assert staff_user.has_perm('product.manage_products')12 response = staff_client.post(url)13 assert response.status_code == 20014def test_staff_can_access_product_toggle_is_published(15 staff_client, staff_user, permission_manage_products, product):16 assert not staff_user.has_perm('product.manage_products')17 url = reverse('dashboard:product-publish', kwargs={'pk': product.pk})18 response = staff_client.post(url)19 assert response.status_code == 30220 staff_user.user_permissions.add(permission_manage_products)21 staff_user = User.objects.get(pk=staff_user.pk)22 assert staff_user.has_perm('product.manage_products')23 response = staff_client.post(url)24 assert response.status_code == 20025def test_staff_can_access_product_select_type(26 staff_client, staff_user, permission_manage_products):27 assert not staff_user.has_perm('product.manage_products')28 url = reverse('dashboard:product-add-select-type')29 response = staff_client.get(url)30 assert response.status_code == 30231 staff_user.user_permissions.add(permission_manage_products)32 staff_user = User.objects.get(pk=staff_user.pk)33 assert staff_user.has_perm('product.manage_products')34 response = staff_client.post(url)35 assert response.status_code == 20036def test_staff_can_access_product_create(37 staff_client, staff_user, permission_manage_products, product_type):38 assert not staff_user.has_perm('product.manage_products')39 url = reverse('dashboard:product-add', kwargs={'type_pk': product_type.pk})40 response = staff_client.get(url)41 assert response.status_code == 30242 staff_user.user_permissions.add(permission_manage_products)43 staff_user = User.objects.get(pk=staff_user.pk)44 assert staff_user.has_perm('product.manage_products')45 response = staff_client.post(url)46 assert response.status_code == 20047def test_staff_can_access_product_edit(48 staff_client, staff_user, permission_manage_products, product):49 assert not staff_user.has_perm('product.manage_products')50 url = reverse('dashboard:product-update', kwargs={'pk': product.pk})51 response = staff_client.get(url)52 assert response.status_code == 30253 staff_user.user_permissions.add(permission_manage_products)54 staff_user = User.objects.get(pk=staff_user.pk)55 assert staff_user.has_perm('product.manage_products')56 response = staff_client.post(url)57 assert response.status_code == 20058def test_staff_can_access_product_delete(59 staff_client, staff_user, permission_manage_products, product):60 assert not staff_user.has_perm('product.manage_products')61 url = reverse('dashboard:product-delete', kwargs={'pk': product.pk})62 response = staff_client.get(url)63 assert response.status_code == 30264 staff_user.user_permissions.add(permission_manage_products)65 staff_user = User.objects.get(pk=staff_user.pk)66 assert staff_user.has_perm('product.manage_products')67 response = staff_client.get(url)68 assert response.status_code == 20069def test_staff_can_view_product_list(70 staff_client, staff_user, permission_manage_products):71 assert not staff_user.has_perm('product.manage_products')72 response = staff_client.get(reverse('dashboard:product-list'))73 assert response.status_code == 30274 staff_user.user_permissions.add(permission_manage_products)75 staff_user = User.objects.get(pk=staff_user.pk)76 assert staff_user.has_perm('product.manage_products')77 response = staff_client.get(reverse('dashboard:product-list'))78 assert response.status_code == 20079def test_staff_can_view_category_list(80 staff_client, staff_user, permission_manage_products):81 assert not staff_user.has_perm('product.manage_products')82 response = staff_client.get(reverse('dashboard:category-list'))83 assert response.status_code == 30284 staff_user.user_permissions.add(permission_manage_products)85 staff_user = User.objects.get(pk=staff_user.pk)86 assert staff_user.has_perm('product.manage_products')87 response = staff_client.get(reverse('dashboard:category-list'))88 assert response.status_code == 20089def test_staff_can_view_category_add_root(90 staff_client, staff_user, permission_manage_products):91 assert not staff_user.has_perm('product.manage_products')92 response = staff_client.get(reverse('dashboard:category-add'))93 assert response.status_code == 30294 staff_user.user_permissions.add(permission_manage_products)95 staff_user = User.objects.get(pk=staff_user.pk)96 assert staff_user.has_perm('product.manage_products')97 response = staff_client.get(reverse('dashboard:category-add'))98 assert response.status_code == 20099def test_staff_can_view_category_add_subcategory(100 staff_client, staff_user, permission_manage_products, category):101 assert not staff_user.has_perm('product.manage_products')102 response = staff_client.get(103 reverse('dashboard:category-add', args=[category.pk]))104 assert response.status_code == 302105 staff_user.user_permissions.add(permission_manage_products)106 staff_user = User.objects.get(pk=staff_user.pk)107 assert staff_user.has_perm('product.manage_products')108 response = staff_client.get(109 reverse('dashboard:category-add', args=[category.pk]))110 assert response.status_code == 200111def test_staff_can_view_category_edit(112 staff_client, staff_user, permission_manage_products, category):113 assert not staff_user.has_perm('product.manage_products')114 response = staff_client.get(115 reverse('dashboard:category-edit', args=[category.pk]))116 assert response.status_code == 302117 staff_user.user_permissions.add(permission_manage_products)118 staff_user = User.objects.get(pk=staff_user.pk)119 assert staff_user.has_perm('product.manage_products')120 response = staff_client.get(121 reverse('dashboard:category-edit', args=[category.pk]))122 assert response.status_code == 200123def test_staff_can_view_category_delete(124 staff_client, staff_user, permission_manage_products, category):125 assert not staff_user.has_perm('product.manage_products')126 response = staff_client.get(127 reverse('dashboard:category-delete', args=[category.pk]))128 assert response.status_code == 302129 staff_user.user_permissions.add(permission_manage_products)130 staff_user = User.objects.get(pk=staff_user.pk)131 assert staff_user.has_perm('product.manage_products')132 response = staff_client.get(133 reverse('dashboard:category-delete', args=[category.pk]))134 assert response.status_code == 200135def test_staff_can_view_sale_list(136 staff_client, staff_user, permission_manage_discounts):137 assert not staff_user.has_perm('discount.manage_discounts')138 response = staff_client.get(reverse('dashboard:sale-list'))139 assert response.status_code == 302140 staff_user.user_permissions.add(permission_manage_discounts)141 staff_user = User.objects.get(pk=staff_user.pk)142 assert staff_user.has_perm('discount.manage_discounts')143 response = staff_client.get(reverse('dashboard:sale-list'))144 assert response.status_code == 200145def test_staff_can_view_sale_update(146 staff_client, staff_user, permission_manage_discounts, sale):147 assert not staff_user.has_perm('discount.manage_discounts')148 response = staff_client.get(149 reverse('dashboard:sale-update', args=[sale.pk]))150 assert response.status_code == 302151 staff_user.user_permissions.add(permission_manage_discounts)152 staff_user = User.objects.get(pk=staff_user.pk)153 assert staff_user.has_perm('discount.manage_discounts')154 response = staff_client.get(155 reverse('dashboard:sale-update', args=[sale.pk]))156 assert response.status_code == 200157def test_staff_can_view_sale_add(158 staff_client, staff_user, permission_manage_discounts, sale):159 assert not staff_user.has_perm('discount.manage_discounts')160 response = staff_client.get(reverse('dashboard:sale-add'))161 assert response.status_code == 302162 staff_user.user_permissions.add(permission_manage_discounts)163 staff_user = User.objects.get(pk=staff_user.pk)164 assert staff_user.has_perm('discount.manage_discounts')165 response = staff_client.get(reverse('dashboard:sale-add'))166 assert response.status_code == 200167def test_staff_can_view_sale_delete(168 staff_client, staff_user, permission_manage_discounts, sale):169 assert not staff_user.has_perm('discount.manage_discounts')170 response = staff_client.get(171 reverse('dashboard:sale-delete', args=[sale.pk]))172 assert response.status_code == 302173 staff_user.user_permissions.add(permission_manage_discounts)174 staff_user = User.objects.get(pk=staff_user.pk)175 assert staff_user.has_perm('discount.manage_discounts')176 response = staff_client.get(177 reverse('dashboard:sale-delete', args=[sale.pk]))178 assert response.status_code == 200179def test_staff_can_view_voucher_list(180 staff_client, staff_user, permission_manage_discounts):181 assert not staff_user.has_perm('discount.manage_discounts')182 response = staff_client.get(reverse('dashboard:voucher-list'))183 assert response.status_code == 302184 staff_user.user_permissions.add(permission_manage_discounts)185 staff_user = User.objects.get(pk=staff_user.pk)186 assert staff_user.has_perm('discount.manage_discounts')187 response = staff_client.get(reverse('dashboard:voucher-list'))188 assert response.status_code == 200189def test_staff_can_view_voucher_update(190 staff_client, staff_user, permission_manage_discounts, voucher):191 assert not staff_user.has_perm('discount.manage_discounts')192 response = staff_client.get(193 reverse('dashboard:voucher-update', args=[voucher.pk]))194 assert response.status_code == 302195 staff_user.user_permissions.add(permission_manage_discounts)196 staff_user = User.objects.get(pk=staff_user.pk)197 assert staff_user.has_perm('discount.manage_discounts')198 response = staff_client.get(199 reverse('dashboard:voucher-update', args=[voucher.pk]))200 assert response.status_code == 200201def test_staff_can_view_voucher_add(202 staff_client, staff_user, permission_manage_discounts):203 assert not staff_user.has_perm('discount.manage_discounts')204 response = staff_client.get(reverse('dashboard:voucher-add'))205 assert response.status_code == 302206 staff_user.user_permissions.add(permission_manage_discounts)207 staff_user = User.objects.get(pk=staff_user.pk)208 assert staff_user.has_perm('discount.manage_discounts')209 response = staff_client.get(reverse('dashboard:voucher-add'))210 assert response.status_code == 200211def test_staff_can_view_voucher_delete(212 staff_client, staff_user, permission_manage_discounts, voucher):213 assert not staff_user.has_perm('discount.manage_discounts')214 response = staff_client.get(215 reverse('dashboard:voucher-delete', args=[voucher.pk]))216 assert response.status_code == 302217 staff_user.user_permissions.add(permission_manage_discounts)218 staff_user = User.objects.get(pk=staff_user.pk)219 assert staff_user.has_perm('discount.manage_discounts')220 response = staff_client.get(221 reverse('dashboard:voucher-delete', args=[voucher.pk]))222 assert response.status_code == 200223def test_staff_can_view_order_list(224 staff_client, staff_user, permission_manage_orders):225 assert not staff_user.has_perm('order.manage_orders')226 response = staff_client.get(reverse('dashboard:orders'))227 assert response.status_code == 302228 staff_user.user_permissions.add(permission_manage_orders)229 staff_user = User.objects.get(pk=staff_user.pk)230 assert staff_user.has_perm('order.manage_orders')231 response = staff_client.get(reverse('dashboard:orders'))232 assert response.status_code == 200233def test_staff_can_view_order_details(234 staff_client, staff_user, permission_manage_orders, order_with_lines):235 assert not staff_user.has_perm('order.manage_orders')236 response = staff_client.get(237 reverse('dashboard:order-details', args=[order_with_lines.pk]))238 assert response.status_code == 302239 staff_user.user_permissions.add(permission_manage_orders)240 staff_user = User.objects.get(pk=staff_user.pk)241 assert staff_user.has_perm('order.manage_orders')242 response = staff_client.get(243 reverse('dashboard:order-details', args=[order_with_lines.pk]))244 assert response.status_code == 200245def test_staff_can_view_order_add_note(246 staff_client, staff_user, permission_manage_orders, order):247 assert not staff_user.has_perm('order.manage_orders')248 response = staff_client.get(249 reverse('dashboard:order-add-note', args=[order.pk]))250 assert response.status_code == 302251 staff_user.user_permissions.add(permission_manage_orders)252 staff_user = User.objects.get(pk=staff_user.pk)253 assert staff_user.has_perm('order.manage_orders')254 response = staff_client.get(255 reverse('dashboard:order-add-note', args=[order.pk]))256 assert response.status_code == 200257def test_staff_can_view_cancel_order(258 staff_client, staff_user, permission_manage_orders, order):259 assert not staff_user.has_perm('order.manage_orders')260 response = staff_client.get(261 reverse('dashboard:order-cancel', args=[order.pk]))262 assert response.status_code == 302263 staff_user.user_permissions.add(permission_manage_orders)264 staff_user = User.objects.get(pk=staff_user.pk)265 assert staff_user.has_perm('order.manage_orders')266 response = staff_client.get(267 reverse('dashboard:order-cancel', args=[order.pk]))268 assert response.status_code == 200269def test_staff_can_view_billing_address_edit(270 staff_client, staff_user, permission_manage_orders, order):271 assert not staff_user.has_perm('order.manage_orders')272 response = staff_client.get(273 reverse('dashboard:address-edit', args=[order.pk, 'billing']))274 assert response.status_code == 302275 staff_user.user_permissions.add(permission_manage_orders)276 staff_user = User.objects.get(pk=staff_user.pk)277 assert staff_user.has_perm('order.manage_orders')278 response = staff_client.get(279 reverse('dashboard:address-edit', args=[order.pk, 'billing']))280 assert response.status_code == 200281def test_staff_can_view_customers_list(282 staff_client, staff_user, permission_manage_users):283 assert not staff_user.has_perm('account.manage_users')284 response = staff_client.get(reverse('dashboard:customers'))285 assert response.status_code == 302286 staff_user.user_permissions.add(permission_manage_users)287 staff_user = User.objects.get(pk=staff_user.pk)288 assert staff_user.has_perm('account.manage_users')289 response = staff_client.get(reverse('dashboard:customers'))290 assert response.status_code == 200291def test_staff_can_view_customer_details(292 staff_client, staff_user, permission_manage_users, customer_user,293 order_with_lines):294 assert not staff_user.has_perm('account.manage_users')295 response = staff_client.get(296 reverse('dashboard:customer-details', args=[customer_user.pk]))297 assert response.status_code == 302298 staff_user.user_permissions.add(permission_manage_users)299 staff_user = User.objects.get(pk=staff_user.pk)300 assert staff_user.has_perm('account.manage_users')301 response = staff_client.get(302 reverse('dashboard:customer-details', args=[customer_user.pk]))303 assert response.status_code == 200304 response = staff_client.get(305 reverse('dashboard:order-details', args=[order_with_lines.pk]))306 assert response.status_code == 302307def test_staff_can_view_staff_members_list(308 staff_client, staff_user, permission_manage_staff):309 assert not staff_user.has_perm('account.manage_staff')310 response = staff_client.get(reverse('dashboard:staff-list'))311 assert response.status_code == 302312 staff_user.user_permissions.add(permission_manage_staff)313 staff_user = User.objects.get(pk=staff_user.pk)314 assert staff_user.has_perm('account.manage_staff')315 response = staff_client.get(reverse('dashboard:staff-list'))316 assert response.status_code == 200317def test_staff_can_view_detail_create_and_delete_staff_members(318 staff_client, staff_user, permission_manage_staff):319 assert not staff_user.has_perm('account.manage_staff')320 response = staff_client.get(reverse('dashboard:staff-create'))321 assert response.status_code == 302322 response = staff_client.get(323 reverse('dashboard:staff-delete', args=[staff_user.pk]))324 assert response.status_code == 302325 response = staff_client.get(326 reverse('dashboard:staff-details', args=[staff_user.pk]))327 assert response.status_code == 302328 staff_user.user_permissions.add(permission_manage_staff)329 staff_user = User.objects.get(pk=staff_user.pk)330 assert staff_user.has_perm('account.manage_staff')331 response = staff_client.get(reverse('dashboard:staff-create'))332 assert response.status_code == 200333 response = staff_client.get(334 reverse('dashboard:staff-delete', args=[staff_user.pk]))335 assert response.status_code == 200336 response = staff_client.get(337 reverse('dashboard:staff-details', args=[staff_user.pk]))338 assert response.status_code == 200339def test_staff_with_permissions_can_view_product_types_list(340 staff_client, staff_user, permission_manage_products):341 assert not staff_user.has_perm('product.manage_products')342 response = staff_client.get(reverse('dashboard:product-type-list'))343 assert response.status_code == 302344 staff_user.user_permissions.add(permission_manage_products)345 staff_user = User.objects.get(pk=staff_user.pk)346 assert staff_user.has_perm('product.manage_products')347 response = staff_client.get(reverse('dashboard:product-type-list'))348 assert response.status_code == 200349def test_staff_with_permissions_can_edit_add_and_delete_product_types_list(350 staff_client, staff_user, permission_manage_products, product_type):351 assert not staff_user.has_perm('product.manage_products')352 response = staff_client.get(353 reverse('dashboard:product-type-update', args=[product_type.pk]))354 assert response.status_code == 302355 response = staff_client.get(356 reverse('dashboard:product-type-delete', args=[product_type.pk]))357 assert response.status_code == 302358 response = staff_client.get(reverse('dashboard:product-type-add'))359 assert response.status_code == 302360 staff_user.user_permissions.add(permission_manage_products)361 staff_user = User.objects.get(pk=staff_user.pk)362 assert staff_user.has_perm('product.manage_products')363 response = staff_client.get(364 reverse('dashboard:product-type-update', args=[product_type.pk]))365 assert response.status_code == 200366 response = staff_client.get(367 reverse('dashboard:product-type-delete', args=[product_type.pk]))368 assert response.status_code == 200369 response = staff_client.get(reverse('dashboard:product-type-add'))370 assert response.status_code == 200371def test_staff_can_access_variant_details(372 staff_client, staff_user, permission_manage_products, product):373 product_type = product.product_type374 product_type.has_variants = True375 product_type.save()376 variant = product.variants.get()377 assert not staff_user.has_perm('product.manage_products')378 url = reverse(379 'dashboard:variant-details',380 kwargs={381 'product_pk': product.pk,382 'variant_pk': variant.pk})383 response = staff_client.get(url)384 assert response.status_code == 302385 staff_user.user_permissions.add(permission_manage_products)386 staff_user = User.objects.get(pk=staff_user.pk)387 assert staff_user.has_perm('product.manage_products')388 response = staff_client.get(url)389 assert response.status_code == 200390def test_staff_can_access_variant_create(391 staff_client, staff_user, permission_manage_products, product):392 assert not staff_user.has_perm('product.manage_products')393 url = reverse('dashboard:variant-add', kwargs={'product_pk': product.pk})394 response = staff_client.get(url)395 assert response.status_code == 302396 staff_user.user_permissions.add(permission_manage_products)397 staff_user = User.objects.get(pk=staff_user.pk)398 assert staff_user.has_perm('product.manage_products')399 response = staff_client.get(url)400 assert response.status_code == 200401def test_staff_can_access_variant_edit(402 staff_client, staff_user, permission_manage_products, product):403 variant = product.variants.get()404 assert not staff_user.has_perm('product.manage_products')405 url = reverse(406 'dashboard:variant-update',407 kwargs={408 'product_pk': product.pk,409 'variant_pk': variant.pk})410 response = staff_client.get(url)411 assert response.status_code == 302412 staff_user.user_permissions.add(permission_manage_products)413 staff_user = User.objects.get(pk=staff_user.pk)414 assert staff_user.has_perm('product.manage_products')415 response = staff_client.get(url)416 assert response.status_code == 200417def test_staff_can_access_variant_delete(418 staff_client, staff_user, permission_manage_products, product):419 variant = product.variants.get()420 assert not staff_user.has_perm('product.manage_products')421 url = reverse(422 'dashboard:variant-delete',423 kwargs={424 'product_pk': product.pk,425 'variant_pk': variant.pk})426 response = staff_client.get(url)427 assert response.status_code == 302428 staff_user.user_permissions.add(permission_manage_products)429 staff_user = User.objects.get(pk=staff_user.pk)430 assert staff_user.has_perm('product.manage_products')431 response = staff_client.get(url)432 assert response.status_code == 200433def test_staff_can_access_variant_images(434 staff_client, staff_user, permission_manage_products, product):435 variant = product.variants.get()436 assert not staff_user.has_perm('product.manage_products')437 url = reverse(438 'dashboard:variant-images',439 kwargs={440 'product_pk': product.pk,441 'variant_pk': variant.pk})442 response = staff_client.get(url)443 assert response.status_code == 302444 staff_user.user_permissions.add(permission_manage_products)445 staff_user = User.objects.get(pk=staff_user.pk)446 assert staff_user.has_perm('product.manage_products')447 response = staff_client.get(url)448 assert response.status_code == 200449def test_staff_can_access_product_image_list(450 staff_client, staff_user, permission_manage_products, product):451 assert not staff_user.has_perm('product.manage_products')452 url = reverse(453 'dashboard:product-image-list', kwargs={'product_pk': product.pk})454 response = staff_client.get(url)455 assert response.status_code == 302456 staff_user.user_permissions.add(permission_manage_products)457 staff_user = User.objects.get(pk=staff_user.pk)458 assert staff_user.has_perm('product.manage_products')459 response = staff_client.get(url)460 assert response.status_code == 200461def test_staff_can_access_product_image_add(462 staff_client, staff_user, permission_manage_products, product):463 assert not staff_user.has_perm('product.manage_products')464 url = reverse(465 'dashboard:product-image-add', kwargs={'product_pk': product.pk})466 response = staff_client.get(url)467 assert response.status_code == 302468 staff_user.user_permissions.add(permission_manage_products)469 staff_user = User.objects.get(pk=staff_user.pk)470 assert staff_user.has_perm('product.manage_products')471 response = staff_client.get(url)472 assert response.status_code == 200473def test_staff_can_access_product_image_update(474 staff_client, staff_user, permission_manage_products, product_with_image):475 product_image = product_with_image.images.get()476 assert not staff_user.has_perm('product.manage_products')477 url = reverse(478 'dashboard:product-image-update',479 kwargs={480 'product_pk': product_with_image.pk,481 'img_pk': product_image.pk})482 response = staff_client.get(url)483 assert response.status_code == 302484 staff_user.user_permissions.add(permission_manage_products)485 staff_user = User.objects.get(pk=staff_user.pk)486 assert staff_user.has_perm('product.manage_products')487 response = staff_client.get(url)488 assert response.status_code == 200489def test_staff_can_access_product_image_delete(490 staff_client, staff_user, permission_manage_products, product_with_image):491 product_image = product_with_image.images.get()492 assert not staff_user.has_perm('product.manage_products')493 url = reverse(494 'dashboard:product-image-delete',495 kwargs={496 'product_pk': product_with_image.pk,497 'img_pk': product_image.pk})498 response = staff_client.get(url)499 assert response.status_code == 302500 staff_user.user_permissions.add(permission_manage_products)501 staff_user = User.objects.get(pk=staff_user.pk)502 assert staff_user.has_perm('product.manage_products')503 response = staff_client.get(url)504 assert response.status_code == 200505def test_staff_with_permissions_can_view_products_attributes_list(506 staff_client, staff_user, permission_manage_products, color_attribute):507 assert not staff_user.has_perm('product.manage_products')508 response = staff_client.get(reverse('dashboard:attributes'))509 assert response.status_code == 302510 response = staff_client.get(511 reverse(512 'dashboard:attribute-details', args=[color_attribute.pk]))513 assert response.status_code == 302514 staff_user.user_permissions.add(permission_manage_products)515 staff_user = User.objects.get(pk=staff_user.pk)516 assert staff_user.has_perm('product.manage_products')517 response = staff_client.get(reverse('dashboard:attributes'))518 assert response.status_code == 200519 response = staff_client.get(520 reverse(521 'dashboard:attribute-details', args=[color_attribute.pk]))522 assert response.status_code == 200523def test_staff_with_permissions_can_update_add_and_delete_products_attributes(524 staff_client, staff_user, permission_manage_products, color_attribute):525 assert not staff_user.has_perm('product.manage_products')526 response = staff_client.get(527 reverse(528 'dashboard:attribute-update', args=[color_attribute.pk]))529 assert response.status_code == 302530 response = staff_client.get(531 reverse(532 'dashboard:attribute-delete', args=[color_attribute.pk]))533 assert response.status_code == 302534 response = staff_client.get(reverse('dashboard:attribute-add'))535 assert response.status_code == 302536 staff_user.user_permissions.add(permission_manage_products)537 staff_user = User.objects.get(pk=staff_user.pk)538 assert staff_user.has_perm('product.manage_products')539 response = staff_client.get(540 reverse(541 'dashboard:attribute-update', args=[color_attribute.pk]))542 assert response.status_code == 200543 response = staff_client.get(544 reverse(545 'dashboard:attribute-delete', args=[color_attribute.pk]))546 assert response.status_code == 200547 response = staff_client.get(reverse('dashboard:attribute-add'))548 assert response.status_code == 200549def test_staff_can_access_attribute_create(550 staff_client, staff_user, permission_manage_products, color_attribute):551 assert not staff_user.has_perm('product.manage_products')552 url = reverse(553 'dashboard:attribute-value-add',554 kwargs={'attribute_pk': color_attribute.pk})555 response = staff_client.get(url)556 assert response.status_code == 302557 staff_user.user_permissions.add(permission_manage_products)558 staff_user = User.objects.get(pk=staff_user.pk)559 assert staff_user.has_perm('product.manage_products')560 response = staff_client.get(url)561 assert response.status_code == 200562def test_staff_can_access_attribute_edit(563 staff_client, staff_user, permission_manage_products, color_attribute):564 value = color_attribute.values.first()565 assert not staff_user.has_perm('product.manage_products')566 url = reverse(567 'dashboard:attribute-value-update',568 kwargs={569 'attribute_pk': color_attribute.pk,570 'value_pk': value.pk})571 response = staff_client.get(url)572 assert response.status_code == 302573 staff_user.user_permissions.add(permission_manage_products)574 staff_user = User.objects.get(pk=staff_user.pk)575 assert staff_user.has_perm('product.manage_products')576 response = staff_client.get(url)577 assert response.status_code == 200578def test_staff_can_access_attribute_delete(579 staff_client, staff_user, permission_manage_products, color_attribute):580 value = color_attribute.values.first()581 assert not staff_user.has_perm('product.manage_products')582 url = reverse(583 'dashboard:attribute-value-delete',584 kwargs={585 'attribute_pk': color_attribute.pk,586 'value_pk': value.pk})587 response = staff_client.get(url)588 assert response.status_code == 302589 staff_user.user_permissions.add(permission_manage_products)590 staff_user = User.objects.get(pk=staff_user.pk)591 assert staff_user.has_perm('product.manage_products')592 response = staff_client.get(url)593 assert response.status_code == 200594def test_staff_with_permissions_can_view_shipping_methods_and_details(595 staff_client, staff_user, permission_manage_shipping, shipping_zone):596 assert not staff_user.has_perm('shipping.manage_shipping')597 response = staff_client.get(reverse('dashboard:shipping-zone-list'))598 assert response.status_code == 302599 response = staff_client.get(600 reverse(601 'dashboard:shipping-zone-details', args=[shipping_zone.pk]))602 assert response.status_code == 302603 staff_user.user_permissions.add(permission_manage_shipping)604 staff_user = User.objects.get(pk=staff_user.pk)605 assert staff_user.has_perm('shipping.manage_shipping')606 response = staff_client.get(reverse('dashboard:shipping-zone-list'))607 assert response.status_code == 200608 response = staff_client.get(609 reverse(610 'dashboard:shipping-zone-details', args=[shipping_zone.pk]))611 assert response.status_code == 200612def test_staff_with_permissions_can_update_add_and_delete_shipping_zone(613 staff_client, staff_user, permission_manage_shipping, shipping_zone):614 assert not staff_user.has_perm('shipping.manage_shipping')615 response = staff_client.get(616 reverse('dashboard:shipping-zone-update', args=[shipping_zone.pk]))617 assert response.status_code == 302618 response = staff_client.get(619 reverse('dashboard:shipping-zone-delete', args=[shipping_zone.pk]))620 assert response.status_code == 302621 response = staff_client.get(reverse('dashboard:shipping-zone-add'))622 assert response.status_code == 302623 staff_user.user_permissions.add(permission_manage_shipping)624 staff_user = User.objects.get(pk=staff_user.pk)625 assert staff_user.has_perm('shipping.manage_shipping')626 response = staff_client.get(627 reverse('dashboard:shipping-zone-update', args=[shipping_zone.pk]))628 assert response.status_code == 200629 response = staff_client.get(630 reverse('dashboard:shipping-zone-delete', args=[shipping_zone.pk]))631 assert response.status_code == 200632 response = staff_client.get(reverse('dashboard:shipping-zone-add'))633 assert response.status_code == 200634def test_staff_with_permissions_can_edit_customer(635 staff_client, customer_user, staff_user, permission_manage_users):636 assert customer_user.email == 'test@example.com'637 response = staff_client.get(638 reverse('dashboard:customer-update', args=[customer_user.pk]))639 assert response.status_code == 302640 staff_user.user_permissions.add(permission_manage_users)641 staff_user = User.objects.get(pk=staff_user.pk)642 assert staff_user.has_perm('account.manage_users')643 response = staff_client.get(644 reverse('dashboard:customer-update', args=[customer_user.pk]))645 assert response.status_code == 200646 url = reverse('dashboard:customer-update', args=[customer_user.pk])647 data = {'email': 'newemail@example.com', 'is_active': True}648 staff_client.post(url, data)649 customer_user = User.objects.get(pk=customer_user.pk)650 assert customer_user.email == 'newemail@example.com'651 assert customer_user.is_active652def test_staff_with_permissions_can_add_customer(653 staff_client, staff_user, permission_manage_users):654 response = staff_client.get(reverse('dashboard:customer-create'))655 assert response.status_code == 302656 staff_user.user_permissions.add(permission_manage_users)657 staff_user = User.objects.get(pk=staff_user.pk)658 assert staff_user.has_perm('account.manage_users')659 response = staff_client.get(reverse('dashboard:customer-create'))660 assert response.status_code == 200661 url = reverse('dashboard:customer-create')662 data = {'email': 'newcustomer@example.com', 'is_active': True}663 staff_client.post(url, data)664 customer = User.objects.get(email='newcustomer@example.com')665 assert customer.is_active666def test_staff_can_view_and_edit_site_settings(667 staff_client, staff_user, site_settings, permission_manage_settings):668 assert not staff_user.has_perm('site.manage_settings')669 response = staff_client.get(670 reverse('dashboard:site-update', args=[site_settings.pk]))671 assert response.status_code == 302672 staff_user.user_permissions.add(permission_manage_settings)673 staff_user = User.objects.get(pk=staff_user.pk)674 assert staff_user.has_perm('site.manage_settings')675 response = staff_client.get(676 reverse('dashboard:site-update', args=[site_settings.pk]))677 assert response.status_code == 200678def test_staff_can_view_and_edit_taxes_settings(679 staff_client, staff_user, site_settings, permission_manage_settings):680 assert not staff_user.has_perm('site.manage_settings')681 url = reverse('dashboard:configure-taxes')682 response = staff_client.get(url)683 assert response.status_code == 302684 staff_user.user_permissions.add(permission_manage_settings)685 staff_user = User.objects.get(pk=staff_user.pk)686 assert staff_user.has_perm('site.manage_settings')687 response = staff_client.get(url)688def test_staff_can_view_menus_and_details(689 staff_client, staff_user, permission_manage_menus, menu_item):690 menu_list_url = reverse('dashboard:menu-list')691 menu_details_url = reverse(692 'dashboard:menu-details', args=[menu_item.menu.pk])693 menu_item_details_url = reverse(694 'dashboard:menu-item-details', args=[menu_item.menu.pk, menu_item.pk])695 assert not staff_user.has_perm('menu.manage_menus')696 response = staff_client.get(menu_list_url)697 assert response.status_code == 302698 response = staff_client.get(menu_details_url)699 assert response.status_code == 302700 response = staff_client.get(menu_item_details_url)701 assert response.status_code == 302702 staff_user.user_permissions.add(permission_manage_menus)703 staff_user = User.objects.get(pk=staff_user.pk)704 assert staff_user.has_perm('menu.manage_menus')705 response = staff_client.get(menu_list_url)706 assert response.status_code == 200707 response = staff_client.get(menu_details_url)708 assert response.status_code == 200709 response = staff_client.get(menu_item_details_url)710 assert response.status_code == 200711def test_staff_can_manage_menuss(712 staff_client, staff_user, permission_manage_menus, menu_item):713 menu_add_url = reverse('dashboard:menu-add')714 menu_edit_url = reverse('dashboard:menu-edit', args=[menu_item.menu.pk])715 menu_delete_url = reverse(716 'dashboard:menu-delete', args=[menu_item.menu.pk])717 assert not staff_user.has_perm('menu.manage_menus')718 response = staff_client.get(menu_add_url)719 assert response.status_code == 302720 response = staff_client.get(menu_edit_url)721 assert response.status_code == 302722 response = staff_client.get(menu_delete_url)723 assert response.status_code == 302724 staff_user.user_permissions.add(permission_manage_menus)725 staff_user = User.objects.get(pk=staff_user.pk)726 assert staff_user.has_perm('menu.manage_menus')727 response = staff_client.get(menu_add_url)728 assert response.status_code == 200729 response = staff_client.get(menu_edit_url)730 assert response.status_code == 200731 response = staff_client.get(menu_delete_url)732 assert response.status_code == 200733def test_staff_can_manage_menus_items(734 staff_client, staff_user, permission_manage_menus, menu_item):735 menu_item_add_url = reverse(736 'dashboard:menu-item-add', args=[menu_item.menu.pk, menu_item.pk])737 menu_item_edit_url = reverse(738 'dashboard:menu-item-edit', args=[menu_item.menu.pk, menu_item.pk])739 menu_item_delete_url = reverse(740 'dashboard:menu-item-delete', args=[menu_item.menu.pk, menu_item.pk])741 assert not staff_user.has_perm('menu.manage_menus')742 response = staff_client.get(menu_item_add_url)743 assert response.status_code == 302744 response = staff_client.get(menu_item_edit_url)745 assert response.status_code == 302746 response = staff_client.get(menu_item_delete_url)747 assert response.status_code == 302748 staff_user.user_permissions.add(permission_manage_menus)749 staff_user = User.objects.get(pk=staff_user.pk)750 assert staff_user.has_perm('menu.manage_menus')751 response = staff_client.get(menu_item_add_url)752 assert response.status_code == 200753 response = staff_client.get(menu_item_edit_url)754 assert response.status_code == 200755 response = staff_client.get(menu_item_delete_url)756 assert response.status_code == 200757def test_staff_can_remove_user(staff_client, staff_user, permission_manage_users):758 url = reverse('dashboard:customer-delete', args=[staff_user.pk])759 response = staff_client.get(url)760 assert response.status_code == 302761 staff_user.user_permissions.add(permission_manage_users)762 staff_user = User.objects.get(pk=staff_user.pk)763 response = staff_client.get(url)764 assert response.status_code == 200
utils.py
Source:utils.py
...18 except:19 return False20# FUNCION PARA OBTENER LOS CUADROS TENIENDO EN CUENTA LOS PERMISOS DE USUARIOS.21def getCuadros(user):22 if user.has_perm('usuario.administrador') or user.has_perm('usuario.oficinaCentral'):23 query = Cuadro.objects.all().order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')24 return query25 elif user.has_perm('usuario.uas'):26 query = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS').order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')27 return query28 elif user.has_perm('usuario.21'):29 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=21).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')30 return query31 elif user.has_perm('usuario.22'):32 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=22).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')33 return query34 elif user.has_perm('usuario.23'):35 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=23).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')36 return query37 elif user.has_perm('usuario.24'):38 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=24).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')39 return query40 elif user.has_perm('usuario.25'):41 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=25).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')42 return query43 elif user.has_perm('usuario.26'):44 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=26).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')45 return query46 elif user.has_perm('usuario.27'):47 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=27).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')48 return query49 elif user.has_perm('usuario.28'):50 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=28).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')51 return query52 elif user.has_perm('usuario.29'):53 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=29).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')54 return query55 elif user.has_perm('usuario.30'):56 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=30).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')57 return query58 elif user.has_perm('usuario.31'):59 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=31).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')60 return query61 elif user.has_perm('usuario.32'):62 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=32).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')63 return query64 elif user.has_perm('usuario.33'):65 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=33).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')66 return query67 elif user.has_perm('usuario.34'):68 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=34).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')69 return query70 elif user.has_perm('usuario.35'):71 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=35).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')72 return query73 elif user.has_perm('usuario.40'):74 query = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=40).order_by('fk_cargo__fk_clasificador_cargo_cuadro__codigo')75 return query76''' FUNCIONES AUXILIARES PARA OBTENER LA DATA DE LOS GRAFICOS SEGUN EL USUARIO EN SESION'''77# FUNCIONES PARA OBTENER LA DATA A MOSTRAR EN EL GRAFICO DE CANTIDAD DE CARGOS POR GENERO78def cantidadPorGenero(user):79 if user.has_perm('usuario.administrador'):80 data = []81 try:82 cantMasculinos = Cuadro.objects.filter(sexo='M').count()83 cantFemeninos = Cuadro.objects.filter(sexo='F').count()84 data.append(cantMasculinos)85 data.append(cantFemeninos)86 except:87 pass88 return data89 elif user.has_perm('usuario.oficinaCentral'):90 data = []91 try:92 cantMasculinos = Cuadro.objects.filter(sexo='M', fk_cargo__nivel_subordinacion='OC').count()93 cantFemeninos = Cuadro.objects.filter(sexo='F', fk_cargo__nivel_subordinacion='OC').count()94 data.append(cantMasculinos)95 data.append(cantFemeninos)96 except:97 pass98 return data99 elif user.has_perm('usuario.uas'):100 data = []101 try:102 cantMasculinos = Cuadro.objects.filter(sexo='M', fk_cargo__nivel_subordinacion='UAS').count()103 cantFemeninos = Cuadro.objects.filter(sexo='F', fk_cargo__nivel_subordinacion='UAS').count()104 data.append(cantMasculinos)105 data.append(cantFemeninos)106 except:107 pass108 return data109 elif user.has_perm('usuario.21'):110 return getQuerysetDeCantidadPorGenero(21)111 elif user.has_perm('usuario.22'):112 return getQuerysetDeCantidadPorGenero(22)113 elif user.has_perm('usuario.23'):114 return getQuerysetDeCantidadPorGenero(23)115 elif user.has_perm('usuario.24'):116 return getQuerysetDeCantidadPorGenero(24)117 elif user.has_perm('usuario.25'):118 return getQuerysetDeCantidadPorGenero(25)119 elif user.has_perm('usuario.26'):120 return getQuerysetDeCantidadPorGenero(26)121 elif user.has_perm('usuario.27'):122 return getQuerysetDeCantidadPorGenero(27)123 elif user.has_perm('usuario.28'):124 return getQuerysetDeCantidadPorGenero(28)125 elif user.has_perm('usuario.29'):126 return getQuerysetDeCantidadPorGenero(29)127 elif user.has_perm('usuario.30'):128 return getQuerysetDeCantidadPorGenero(30)129 elif user.has_perm('usuario.31'):130 return getQuerysetDeCantidadPorGenero(31)131 elif user.has_perm('usuario.32'):132 return getQuerysetDeCantidadPorGenero(32)133 elif user.has_perm('usuario.33'):134 return getQuerysetDeCantidadPorGenero(33)135 elif user.has_perm('usuario.34'):136 return getQuerysetDeCantidadPorGenero(34)137 elif user.has_perm('usuario.35'):138 return getQuerysetDeCantidadPorGenero(35)139 elif user.has_perm('usuario.40'):140 return getQuerysetDeCantidadPorGenero(40)141def getQuerysetDeCantidadPorGenero(codigoDPA):142 data = []143 try:144 cantMasculinos = Cuadro.objects.filter(sexo='M', fk_cargo__provincia__codigo__exact=codigoDPA).count()145 cantFemeninos = Cuadro.objects.filter(sexo='F', fk_cargo__provincia__codigo__exact=codigoDPA).count()146 data.append(cantMasculinos)147 data.append(cantFemeninos)148 except:149 pass150 return data151# FUNCIONES PARA OBTENER LA DATA A MOSTRAR EN EL GRAFICO DE CANTIDAD DE CUADROS POR CATEGORIA152def cantidadPorCategoria(user):153 if user.has_perm('usuario.administrador'):154 data = []155 try:156 cantDirectivoSuerior = Cuadro.objects.filter(categoria__exact='DS').count()157 cantDirectivoIntermedio = Cuadro.objects.filter(categoria__exact='DI').count()158 cantEjecutivo = Cuadro.objects.filter(categoria__exact='E').count()159 data.append(cantDirectivoSuerior)160 data.append(cantDirectivoIntermedio)161 data.append(cantEjecutivo)162 except:163 pass164 return data165 elif user.has_perm('usuario.oficinaCentral'):166 data = []167 try:168 cantDirectivoSuerior = Cuadro.objects.filter(categoria__exact='DS', fk_cargo__nivel_subordinacion='OC').count()169 cantDirectivoIntermedio = Cuadro.objects.filter(categoria__exact='DI', fk_cargo__nivel_subordinacion='OC').count()170 cantEjecutivo = Cuadro.objects.filter(categoria__exact='E', fk_cargo__nivel_subordinacion='OC').count()171 data.append(cantDirectivoSuerior)172 data.append(cantDirectivoIntermedio)173 data.append(cantEjecutivo)174 except:175 pass176 return data177 elif user.has_perm('usuario.uas'):178 data = []179 try:180 cantDirectivoSuerior = Cuadro.objects.filter(categoria__exact='DS', fk_cargo__nivel_subordinacion='UAS').count()181 cantDirectivoIntermedio = Cuadro.objects.filter(categoria__exact='DI', fk_cargo__nivel_subordinacion='UAS').count()182 cantEjecutivo = Cuadro.objects.filter(categoria__exact='E', fk_cargo__nivel_subordinacion='UAS').count()183 data.append(cantDirectivoSuerior)184 data.append(cantDirectivoIntermedio)185 data.append(cantEjecutivo)186 except:187 pass188 return data189 elif user.has_perm('usuario.21'):190 return getQuerysetDeCantidadPorCategoria(21)191 elif user.has_perm('usuario.22'):192 return getQuerysetDeCantidadPorCategoria(22)193 elif user.has_perm('usuario.23'):194 return getQuerysetDeCantidadPorCategoria(23)195 elif user.has_perm('usuario.24'):196 return getQuerysetDeCantidadPorCategoria(24)197 elif user.has_perm('usuario.25'):198 return getQuerysetDeCantidadPorCategoria(25)199 elif user.has_perm('usuario.26'):200 return getQuerysetDeCantidadPorCategoria(26)201 elif user.has_perm('usuario.27'):202 return getQuerysetDeCantidadPorCategoria(27)203 elif user.has_perm('usuario.28'):204 return getQuerysetDeCantidadPorCategoria(28)205 elif user.has_perm('usuario.29'):206 return getQuerysetDeCantidadPorCategoria(29)207 elif user.has_perm('usuario.30'):208 return getQuerysetDeCantidadPorCategoria(30)209 elif user.has_perm('usuario.31'):210 return getQuerysetDeCantidadPorCategoria(31)211 elif user.has_perm('usuario.32'):212 return getQuerysetDeCantidadPorCategoria(32)213 elif user.has_perm('usuario.33'):214 return getQuerysetDeCantidadPorCategoria(33)215 elif user.has_perm('usuario.34'):216 return getQuerysetDeCantidadPorCategoria(34)217 elif user.has_perm('usuario.35'):218 return getQuerysetDeCantidadPorCategoria(35)219 elif user.has_perm('usuario.40'):220 return getQuerysetDeCantidadPorCategoria(40)221def getQuerysetDeCantidadPorCategoria(codigoDPA):222 data = []223 try:224 cantDirectivoSuerior = Cuadro.objects.filter(categoria__exact='DS', fk_cargo__provincia__codigo__exact=codigoDPA).count()225 cantDirectivoIntermedio = Cuadro.objects.filter(categoria__exact='DI', fk_cargo__provincia__codigo__exact=codigoDPA).count()226 cantEjecutivo = Cuadro.objects.filter(categoria__exact='E', fk_cargo__provincia__codigo__exact=codigoDPA).count()227 data.append(cantDirectivoSuerior)228 data.append(cantDirectivoIntermedio)229 data.append(cantEjecutivo)230 except:231 pass232 return data233# FUNCIONES PARA OBTENER LA DATA A MOSTRAR EN EL GRAFICO DE CANTIDAD DE CUADROS SEGUN LA MILITANCIA234def cantidadSegunMilitancia(user):235 if user.has_perm('usuario.administrador'):236 data = []237 try:238 cantMilitantesPCC = Cuadro.objects.filter(militancia='PCC').count()239 cantMilitantesUJC = Cuadro.objects.filter(militancia='UJC').count()240 cantNoMilitantes = Cuadro.objects.filter(militancia=None).count()241 data.append(cantMilitantesPCC)242 data.append(cantMilitantesUJC)243 data.append(cantNoMilitantes)244 except:245 pass246 return data247 elif user.has_perm('usuario.oficinaCentral'):248 data = []249 try:250 cantMilitantesPCC = Cuadro.objects.filter(militancia='PCC', fk_cargo__nivel_subordinacion='OC').count()251 cantMilitantesUJC = Cuadro.objects.filter(militancia='UJC', fk_cargo__nivel_subordinacion='OC').count()252 cantNoMilitantes = Cuadro.objects.filter(militancia=None, fk_cargo__nivel_subordinacion='OC').count()253 data.append(cantMilitantesPCC)254 data.append(cantMilitantesUJC)255 data.append(cantNoMilitantes)256 except:257 pass258 return data259 elif user.has_perm('usuario.uas'):260 data = []261 try:262 cantMilitantesPCC = Cuadro.objects.filter(militancia='PCC', fk_cargo__nivel_subordinacion='UAS').count()263 cantMilitantesUJC = Cuadro.objects.filter(militancia='UJC', fk_cargo__nivel_subordinacion='UAS').count()264 cantNoMilitantes = Cuadro.objects.filter(militancia=None, fk_cargo__nivel_subordinacion='UAS').count()265 data.append(cantMilitantesPCC)266 data.append(cantMilitantesUJC)267 data.append(cantNoMilitantes)268 except:269 pass270 return data271 elif user.has_perm('usuario.21'):272 return getQuerysetDePorcentajeSegunMilitancia(21)273 elif user.has_perm('usuario.22'):274 return getQuerysetDePorcentajeSegunMilitancia(22)275 elif user.has_perm('usuario.23'):276 return getQuerysetDePorcentajeSegunMilitancia(23)277 elif user.has_perm('usuario.24'):278 return getQuerysetDePorcentajeSegunMilitancia(24)279 elif user.has_perm('usuario.25'):280 return getQuerysetDePorcentajeSegunMilitancia(25)281 elif user.has_perm('usuario.26'):282 return getQuerysetDePorcentajeSegunMilitancia(26)283 elif user.has_perm('usuario.27'):284 return getQuerysetDePorcentajeSegunMilitancia(27)285 elif user.has_perm('usuario.28'):286 return getQuerysetDePorcentajeSegunMilitancia(28)287 elif user.has_perm('usuario.29'):288 return getQuerysetDePorcentajeSegunMilitancia(29)289 elif user.has_perm('usuario.30'):290 return getQuerysetDePorcentajeSegunMilitancia(30)291 elif user.has_perm('usuario.31'):292 return getQuerysetDePorcentajeSegunMilitancia(31)293 elif user.has_perm('usuario.32'):294 return getQuerysetDePorcentajeSegunMilitancia(32)295 elif user.has_perm('usuario.33'):296 return getQuerysetDePorcentajeSegunMilitancia(33)297 elif user.has_perm('usuario.34'):298 return getQuerysetDePorcentajeSegunMilitancia(34)299 elif user.has_perm('usuario.35'):300 return getQuerysetDePorcentajeSegunMilitancia(35)301 elif user.has_perm('usuario.40'):302 return getQuerysetDePorcentajeSegunMilitancia(40)303def getQuerysetDePorcentajeSegunMilitancia(codigoDPA):304 data = []305 try:306 cantMilitantesPCC = Cuadro.objects.filter(militancia='PCC', fk_cargo__provincia__codigo__exact=codigoDPA).count()307 cantMilitantesUJC = Cuadro.objects.filter(militancia='UJC', fk_cargo__provincia__codigo__exact=codigoDPA).count()308 cantNoMilitantes = Cuadro.objects.filter(militancia=None, fk_cargo__provincia__codigo__exact=codigoDPA).count()309 data.append(cantMilitantesPCC)310 data.append(cantMilitantesUJC)311 data.append(cantNoMilitantes)312 except:313 pass314 return data315# FUNCIONES PARA OBTENER LA DATA A MOSTRAR EN EL GRAFICO DE CANTIDAD DE CARGOS POR COLOR316def cantidadPorColor(user):317 if user.has_perm('usuario.administrador'):318 data = []319 try:320 cantBlancos = Cuadro.objects.filter(color='B').count()321 cantMestizos = Cuadro.objects.filter(color='M').count()322 cantNegros = Cuadro.objects.filter(color='N').count()323 data.append(cantBlancos)324 data.append(cantMestizos)325 data.append(cantNegros)326 except:327 pass328 return data329 elif user.has_perm('usuario.oficinaCentral'):330 data = []331 try:332 cantBlancos = Cuadro.objects.filter(color='B', fk_cargo__nivel_subordinacion='OC').count()333 cantMestizos = Cuadro.objects.filter(color='M', fk_cargo__nivel_subordinacion='OC').count()334 cantNegros = Cuadro.objects.filter(color='N', fk_cargo__nivel_subordinacion='OC').count()335 data.append(cantBlancos)336 data.append(cantMestizos)337 data.append(cantNegros)338 except:339 pass340 return data341 elif user.has_perm('usuario.uas'):342 data = []343 try:344 cantBlancos = Cuadro.objects.filter(color='B', fk_cargo__nivel_subordinacion='UAS').count()345 cantMestizos = Cuadro.objects.filter(color='M', fk_cargo__nivel_subordinacion='UAS').count()346 cantNegros = Cuadro.objects.filter(color='N', fk_cargo__nivel_subordinacion='UAS').count()347 data.append(cantBlancos)348 data.append(cantMestizos)349 data.append(cantNegros)350 except:351 pass352 return data353 elif user.has_perm('usuario.21'):354 return getQuerysetDeCantidadPorColor(21)355 elif user.has_perm('usuario.22'):356 return getQuerysetDeCantidadPorColor(22)357 elif user.has_perm('usuario.23'):358 return getQuerysetDeCantidadPorColor(23)359 elif user.has_perm('usuario.24'):360 return getQuerysetDeCantidadPorColor(24)361 elif user.has_perm('usuario.25'):362 return getQuerysetDeCantidadPorColor(25)363 elif user.has_perm('usuario.26'):364 return getQuerysetDeCantidadPorColor(26)365 elif user.has_perm('usuario.27'):366 return getQuerysetDeCantidadPorColor(27)367 elif user.has_perm('usuario.28'):368 return getQuerysetDeCantidadPorColor(28)369 elif user.has_perm('usuario.29'):370 return getQuerysetDeCantidadPorColor(29)371 elif user.has_perm('usuario.30'):372 return getQuerysetDeCantidadPorColor(30)373 elif user.has_perm('usuario.31'):374 return getQuerysetDeCantidadPorColor(31)375 elif user.has_perm('usuario.32'):376 return getQuerysetDeCantidadPorColor(32)377 elif user.has_perm('usuario.33'):378 return getQuerysetDeCantidadPorColor(33)379 elif user.has_perm('usuario.34'):380 return getQuerysetDeCantidadPorColor(34)381 elif user.has_perm('usuario.35'):382 return getQuerysetDeCantidadPorColor(35)383 elif user.has_perm('usuario.40'):384 return getQuerysetDeCantidadPorColor(40)385def getQuerysetDeCantidadPorColor(codigoDPA):386 data = []387 try:388 cantBlancos = Cuadro.objects.filter(color='B', fk_cargo__provincia__codigo__exact=codigoDPA).count()389 cantMestizos = Cuadro.objects.filter(color='M', fk_cargo__provincia__codigo__exact=codigoDPA).count()390 cantNegros = Cuadro.objects.filter(color='N', fk_cargo__provincia__codigo__exact=codigoDPA).count()391 data.append(cantBlancos)392 data.append(cantMestizos)393 data.append(cantNegros)394 except:395 pass396 return data397# FUNCIONES PARA OBTENER LA DATA A MOSTRAR EN EL GRAFICO DE CANTIDAD DE CARGOS POR EDAD398def cantidadPorEdad(user):399 if user.has_perm('usuario.administrador'):400 data = []401 try:402 cuadrosMenorA40 = Cuadro.objects.filter(edad__lte=40).count()403 cuadrosDe41A50 = Cuadro.objects.filter(edad__range=(41, 50)).count()404 cuadrosDe51A60 = Cuadro.objects.filter(edad__range=(51, 60)).count()405 cuadrosDe61A70 = Cuadro.objects.filter(edad__range=(61, 70)).count()406 cuadrosMayorA70 = Cuadro.objects.filter(edad__gte=70).count()407 data.append(cuadrosMenorA40)408 data.append(cuadrosDe41A50)409 data.append(cuadrosDe51A60)410 data.append(cuadrosDe61A70)411 data.append(cuadrosMayorA70)412 except:413 pass414 return data415 elif user.has_perm('usuario.oficinaCentral'):416 data = []417 try:418 cuadrosMenorA40 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', edad__lte=40).count()419 cuadrosDe41A50 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', edad__range=(41, 50)).count()420 cuadrosDe51A60 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', edad__range=(51, 60)).count()421 cuadrosDe61A70 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', edad__range=(61, 70)).count()422 cuadrosMayorA70 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', edad__gte=70).count()423 data.append(cuadrosMenorA40)424 data.append(cuadrosDe41A50)425 data.append(cuadrosDe51A60)426 data.append(cuadrosDe61A70)427 data.append(cuadrosMayorA70)428 except:429 pass430 return data431 elif user.has_perm('usuario.uas'):432 data = []433 try:434 cuadrosMenorA40 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', edad__lte=40).count()435 cuadrosDe41A50 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', edad__range=(41, 50)).count()436 cuadrosDe51A60 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', edad__range=(51, 60)).count()437 cuadrosDe61A70 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', edad__range=(61, 70)).count()438 cuadrosMayorA70 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', edad__gte=70).count()439 data.append(cuadrosMenorA40)440 data.append(cuadrosDe41A50)441 data.append(cuadrosDe51A60)442 data.append(cuadrosDe61A70)443 data.append(cuadrosMayorA70)444 except:445 pass446 return data447 elif user.has_perm('usuario.21'):448 return getQuerysetDeCantidadPorEdad(21)449 elif user.has_perm('usuario.22'):450 return getQuerysetDeCantidadPorEdad(22)451 elif user.has_perm('usuario.23'):452 return getQuerysetDeCantidadPorEdad(23)453 elif user.has_perm('usuario.24'):454 return getQuerysetDeCantidadPorEdad(24)455 elif user.has_perm('usuario.25'):456 return getQuerysetDeCantidadPorEdad(25)457 elif user.has_perm('usuario.26'):458 return getQuerysetDeCantidadPorEdad(26)459 elif user.has_perm('usuario.27'):460 return getQuerysetDeCantidadPorEdad(27)461 elif user.has_perm('usuario.28'):462 return getQuerysetDeCantidadPorEdad(28)463 elif user.has_perm('usuario.29'):464 return getQuerysetDeCantidadPorEdad(29)465 elif user.has_perm('usuario.30'):466 return getQuerysetDeCantidadPorEdad(30)467 elif user.has_perm('usuario.31'):468 return getQuerysetDeCantidadPorEdad(31)469 elif user.has_perm('usuario.32'):470 return getQuerysetDeCantidadPorEdad(32)471 elif user.has_perm('usuario.33'):472 return getQuerysetDeCantidadPorEdad(33)473 elif user.has_perm('usuario.34'):474 return getQuerysetDeCantidadPorEdad(34)475 elif user.has_perm('usuario.35'):476 return getQuerysetDeCantidadPorEdad(35)477 elif user.has_perm('usuario.40'):478 return getQuerysetDeCantidadPorEdad(40)479def getQuerysetDeCantidadPorEdad(codigoDPA):480 data = []481 try:482 cuadrosMenorA40 = Cuadro.objects.filter(edad__lte=40, fk_cargo__provincia__codigo__exact=codigoDPA).count()483 cuadrosDe41A50 = Cuadro.objects.filter(edad__range=(41, 50), fk_cargo__provincia__codigo__exact=codigoDPA).count()484 cuadrosDe51A60 = Cuadro.objects.filter(edad__range=(51, 60), fk_cargo__provincia__codigo__exact=codigoDPA).count()485 cuadrosDe61A70 = Cuadro.objects.filter(edad__range=(61, 70), fk_cargo__provincia__codigo__exact=codigoDPA).count()486 cuadrosMayorA70 = Cuadro.objects.filter(edad__gte=70, fk_cargo__provincia__codigo__exact=codigoDPA).count()487 data.append(cuadrosMenorA40)488 data.append(cuadrosDe41A50)489 data.append(cuadrosDe51A60)490 data.append(cuadrosDe61A70)491 data.append(cuadrosMayorA70)492 except:493 pass494 return data495# FUNCIONES PARA OBTENER LA DATA A MOSTRAR EN EL GRAFICO DE CANTIDAD DE CUADROS POR TIEMPO496def cantidadPorTiempo(user):497 if user.has_perm('usuario.administrador'):498 data = []499 try:500 cuadrosMenorA1 = Cuadro.objects.filter(tiempo_en_cargo__lt=1).count()501 cuadrosDe1A2 = Cuadro.objects.filter(tiempo_en_cargo__range=(1, 2)).count()502 cuadrosDe3A5 = Cuadro.objects.filter(tiempo_en_cargo__range=(3, 5)).count()503 cuadrosDe6A10 = Cuadro.objects.filter(tiempo_en_cargo__range=(6, 10)).count()504 cuadrosMayorA10 = Cuadro.objects.filter(tiempo_en_cargo__gt=10).count()505 data.append(cuadrosMenorA1)506 data.append(cuadrosDe1A2)507 data.append(cuadrosDe3A5)508 data.append(cuadrosDe6A10)509 data.append(cuadrosMayorA10)510 except:511 pass512 return data513 elif user.has_perm('usuario.oficinaCentral'):514 data = []515 try:516 cuadrosMenorA1 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', tiempo_en_cargo__lt=1).count()517 cuadrosDe1A2 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', tiempo_en_cargo__range=(1, 2)).count()518 cuadrosDe3A5 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', tiempo_en_cargo__range=(3, 5)).count()519 cuadrosDe6A10 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', tiempo_en_cargo__range=(6, 10)).count()520 cuadrosMayorA10 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', tiempo_en_cargo__gt=10).count()521 data.append(cuadrosMenorA1)522 data.append(cuadrosDe1A2)523 data.append(cuadrosDe3A5)524 data.append(cuadrosDe6A10)525 data.append(cuadrosMayorA10)526 except:527 pass528 return data529 elif user.has_perm('usuario.uas'):530 data = []531 try:532 cuadrosMenorA1 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', tiempo_en_cargo__lt=1).count()533 cuadrosDe1A2 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', tiempo_en_cargo__range=(1, 2)).count()534 cuadrosDe3A5 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', tiempo_en_cargo__range=(3, 5)).count()535 cuadrosDe6A10 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', tiempo_en_cargo__range=(6, 10)).count()536 cuadrosMayorA10 = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', tiempo_en_cargo__gt=10).count()537 data.append(cuadrosMenorA1)538 data.append(cuadrosDe1A2)539 data.append(cuadrosDe3A5)540 data.append(cuadrosDe6A10)541 data.append(cuadrosMayorA10)542 except:543 pass544 return data545 elif user.has_perm('usuario.21'):546 return getQuerysetDeCantidadPorTiempo(21)547 elif user.has_perm('usuario.22'):548 return getQuerysetDeCantidadPorTiempo(22)549 elif user.has_perm('usuario.23'):550 return getQuerysetDeCantidadPorTiempo(23)551 elif user.has_perm('usuario.24'):552 return getQuerysetDeCantidadPorTiempo(24)553 elif user.has_perm('usuario.25'):554 return getQuerysetDeCantidadPorTiempo(25)555 elif user.has_perm('usuario.26'):556 return getQuerysetDeCantidadPorTiempo(26)557 elif user.has_perm('usuario.27'):558 return getQuerysetDeCantidadPorTiempo(27)559 elif user.has_perm('usuario.28'):560 return getQuerysetDeCantidadPorTiempo(28)561 elif user.has_perm('usuario.29'):562 return getQuerysetDeCantidadPorTiempo(29)563 elif user.has_perm('usuario.30'):564 return getQuerysetDeCantidadPorTiempo(30)565 elif user.has_perm('usuario.31'):566 return getQuerysetDeCantidadPorTiempo(31)567 elif user.has_perm('usuario.32'):568 return getQuerysetDeCantidadPorTiempo(32)569 elif user.has_perm('usuario.33'):570 return getQuerysetDeCantidadPorTiempo(33)571 elif user.has_perm('usuario.34'):572 return getQuerysetDeCantidadPorTiempo(34)573 elif user.has_perm('usuario.35'):574 return getQuerysetDeCantidadPorTiempo(35)575 elif user.has_perm('usuario.40'):576 return getQuerysetDeCantidadPorTiempo(40)577def getQuerysetDeCantidadPorTiempo(codigoDPA):578 data = []579 try:580 cuadrosMenorA1 = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, tiempo_en_cargo__lt=1).count()581 cuadrosDe1A2 = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, tiempo_en_cargo__range=(1, 2)).count()582 cuadrosDe3A5 = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, tiempo_en_cargo__range=(3, 5)).count()583 cuadrosDe6A10 = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, tiempo_en_cargo__range=(6, 10)).count()584 cuadrosMayorA10 = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, tiempo_en_cargo__gt=10).count()585 data.append(cuadrosMenorA1)586 data.append(cuadrosDe1A2)587 data.append(cuadrosDe3A5)588 data.append(cuadrosDe6A10)589 data.append(cuadrosMayorA10)590 except:591 pass592 return data593# FUNCIONES PARA OBTENER LA DATA A MOSTRAR EN EL GRAFICO DE CANTIDAD DE CUADROS POR ESCOLARIDAD594def cantidadPorEscolaridad(user):595 if user.has_perm('usuario.administrador'):596 data = []597 try:598 cant9no = Cuadro.objects.filter(escolaridad__exact='9noGrado').count()599 cant12mo = Cuadro.objects.filter(escolaridad__exact='12moGrado').count()600 cantTecnicoMedio = Cuadro.objects.filter(escolaridad__exact='Tec.Med.').count()601 cantUniversitarios = Cuadro.objects.filter(escolaridad__exact='Universitario').count()602 data.append(cant9no)603 data.append(cant12mo)604 data.append(cantTecnicoMedio)605 data.append(cantUniversitarios)606 except:607 pass608 return data609 elif user.has_perm('usuario.oficinaCentral'):610 data = []611 try:612 cant9no = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', escolaridad__exact='9noGrado').count()613 cant12mo = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', escolaridad__exact='12moGrado').count()614 cantTecnicoMedio = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', escolaridad__exact='Tec.Med.').count()615 cantUniversitarios = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='OC', escolaridad__exact='Universitario').count()616 data.append(cant9no)617 data.append(cant12mo)618 data.append(cantTecnicoMedio)619 data.append(cantUniversitarios)620 except:621 pass622 return data623 elif user.has_perm('usuario.uas'):624 data = []625 try:626 cant9no = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', escolaridad__exact='9noGrado').count()627 cant12mo = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', escolaridad__exact='12moGrado').count()628 cantTecnicoMedio = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', escolaridad__exact='Tec.Med.').count()629 cantUniversitarios = Cuadro.objects.filter(fk_cargo__nivel_subordinacion='UAS', escolaridad__exact='Universitario').count()630 data.append(cant9no)631 data.append(cant12mo)632 data.append(cantTecnicoMedio)633 data.append(cantUniversitarios)634 except:635 pass636 return data637 elif user.has_perm('usuario.21'):638 return getQuerysetDeCantidadPorEscolaridad(21)639 elif user.has_perm('usuario.22'):640 return getQuerysetDeCantidadPorEscolaridad(22)641 elif user.has_perm('usuario.23'):642 return getQuerysetDeCantidadPorEscolaridad(23)643 elif user.has_perm('usuario.24'):644 return getQuerysetDeCantidadPorEscolaridad(24)645 elif user.has_perm('usuario.25'):646 return getQuerysetDeCantidadPorEscolaridad(25)647 elif user.has_perm('usuario.26'):648 return getQuerysetDeCantidadPorEscolaridad(26)649 elif user.has_perm('usuario.27'):650 return getQuerysetDeCantidadPorEscolaridad(27)651 elif user.has_perm('usuario.28'):652 return getQuerysetDeCantidadPorEscolaridad(28)653 elif user.has_perm('usuario.29'):654 return getQuerysetDeCantidadPorEscolaridad(29)655 elif user.has_perm('usuario.30'):656 return getQuerysetDeCantidadPorEscolaridad(30)657 elif user.has_perm('usuario.31'):658 return getQuerysetDeCantidadPorEscolaridad(31)659 elif user.has_perm('usuario.32'):660 return getQuerysetDeCantidadPorEscolaridad(32)661 elif user.has_perm('usuario.33'):662 return getQuerysetDeCantidadPorEscolaridad(33)663 elif user.has_perm('usuario.34'):664 return getQuerysetDeCantidadPorEscolaridad(34)665 elif user.has_perm('usuario.35'):666 return getQuerysetDeCantidadPorEscolaridad(35)667 elif user.has_perm('usuario.40'):668 return getQuerysetDeCantidadPorEscolaridad(40)669def getQuerysetDeCantidadPorEscolaridad(codigoDPA):670 data = []671 try:672 cant9no = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, escolaridad__exact='9noGrado').count()673 cant12mo = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, escolaridad__exact='12moGrado').count()674 cantTecnicoMedio = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, escolaridad__exact='Tec.Med.').count()675 cantUniversitarios = Cuadro.objects.filter(fk_cargo__provincia__codigo__exact=codigoDPA, escolaridad__exact='Universitario').count()676 data.append(cant9no)677 data.append(cant12mo)678 data.append(cantTecnicoMedio)679 data.append(cantUniversitarios)680 except:681 pass...
test_policy.py
Source:test_policy.py
...53 def test_model_policy(self):54 p = Policy.objects.create(name="MyPolicy", role=self.model_role())55 p.user.add(self.user)56 self.assertNotIn(self.add_host_perm, self.user.user_permissions.all())57 self.assertFalse(self.user.has_perm("cm.add_host"))58 self.clear_perm_cache(self.user)59 p.apply()60 self.assertIn(self.add_host_perm, self.user.user_permissions.all())61 self.assertTrue(self.user.has_perm("cm.add_host"))62 self.clear_perm_cache(self.user)63 p.apply()64 self.assertTrue(self.user.has_perm("cm.add_host"))65 def test_model_policy4group(self):66 group = Group.objects.create(name="group")67 group.user_set.add(self.user)68 p = Policy.objects.create(name="MyPolicy", role=self.model_role())69 p.group.add(group)70 self.assertNotIn(self.add_host_perm, group.permissions.all())71 self.assertFalse(self.user.has_perm("cm.add_host"))72 self.clear_perm_cache(self.user)73 p.apply()74 self.assertIn(self.add_host_perm, group.permissions.all())75 self.assertTrue(self.user.has_perm("cm.add_host"))76 self.clear_perm_cache(self.user)77 p.apply()78 self.assertTrue(self.user.has_perm("cm.add_host"))79 def test_object_policy(self):80 cluster2 = Cluster.objects.create(name="Cluster_2", prototype=self.clp)81 p = Policy.objects.create(name="MyPolicy", role=self.object_role_view_perm_cluster())82 p.user.add(self.user)83 self.assertFalse(self.user.has_perm("cm.view_cluster", self.cluster))84 self.assertFalse(self.user.has_perm("cm.view_cluster", cluster2))85 p.add_object(self.cluster)86 p.apply()87 self.assertTrue(self.user.has_perm("cm.view_cluster", self.cluster))88 self.assertFalse(self.user.has_perm("cm.view_cluster", cluster2))89 def test_object_policy_remove_user(self):90 cluster2 = Cluster.objects.create(name="Cluster_2", prototype=self.clp)91 p = Policy.objects.create(name="MyPolicy", role=self.object_role())92 p.user.add(self.user)93 p.add_object(self.cluster)94 self.assertFalse(self.user.has_perm("cm.view_cluster", self.cluster))95 p.apply()96 self.assertTrue(self.user.has_perm("cm.view_cluster", self.cluster))97 self.assertFalse(self.user.has_perm("cm.view_cluster", cluster2))98 p.user.remove(self.user)99 p.apply()100 self.assertFalse(self.user.has_perm("cm.view_cluster", self.cluster))101 self.assertFalse(self.user.has_perm("cm.view_cluster", cluster2))102 def test_object_policy4group(self):103 cluster2 = Cluster.objects.create(name="Cluster_2", prototype=self.clp)104 group = Group.objects.create(name="group")105 group.user_set.add(self.user)106 p = Policy.objects.create(name="MyPolicy", role=self.object_role())107 p.group.add(group)108 p.add_object(self.cluster)109 self.assertFalse(self.user.has_perm("cm.view_cluster", self.cluster))110 p.apply()111 self.assertTrue(self.user.has_perm("cm.view_cluster", self.cluster))112 self.assertFalse(self.user.has_perm("cm.view_cluster", cluster2))113 def test_parent_policy4cluster(self):114 p = Policy.objects.create(role=self.object_role_custom_perm_cluster_service_component())115 p.user.add(self.user)116 p.add_object(self.cluster)117 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))118 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))119 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))120 self.assertFalse(121 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)122 )123 self.assertFalse(124 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)125 )126 p.apply()127 self.assertTrue(self.user.has_perm("cm.change_config_of_cluster", self.cluster))128 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))129 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))130 self.assertTrue(131 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)132 )133 self.assertTrue(134 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)135 )136 def test_parent_policy4service(self):137 p = Policy.objects.create(role=self.object_role_custom_perm_cluster_service_component())138 p.user.add(self.user)139 p.add_object(self.service_1)140 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))141 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))142 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))143 self.assertFalse(144 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)145 )146 self.assertFalse(147 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)148 )149 p.apply()150 self.assertTrue(self.user.has_perm("cm.view_cluster", self.cluster))151 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))152 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))153 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))154 self.assertTrue(155 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)156 )157 self.assertFalse(158 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)159 )160 def test_parent_policy4service2(self):161 p = Policy.objects.create(role=self.object_role_custom_perm_cluster_service_component())162 p.user.add(self.user)163 p.add_object(self.service_2)164 self.assertFalse(self.user.has_perm("cm.view_cluster", self.cluster))165 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))166 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))167 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))168 self.assertFalse(169 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)170 )171 self.assertFalse(172 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)173 )174 p.apply()175 self.assertTrue(self.user.has_perm("cm.view_cluster", self.cluster))176 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))177 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))178 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))179 self.assertFalse(180 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)181 )182 self.assertTrue(183 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)184 )185 def test_parent_policy4component(self):186 p = Policy.objects.create(role=self.object_role_custom_perm_cluster_service_component())187 p.user.add(self.user)188 p.add_object(self.component_11)189 self.assertFalse(self.user.has_perm("cm.view_cluster", self.cluster))190 self.assertFalse(self.user.has_perm("cm.view_clusterobject", self.service_1))191 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))192 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))193 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))194 self.assertFalse(195 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)196 )197 self.assertFalse(198 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)199 )200 p.apply()201 self.assertTrue(self.user.has_perm("cm.view_cluster", self.cluster))202 self.assertTrue(self.user.has_perm("cm.view_clusterobject", self.service_1))203 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))204 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))205 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))206 self.assertTrue(207 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)208 )209 self.assertFalse(210 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)211 )212 def test_parent_policy4host_in_cluster(self):213 provider, host1, host2 = self.get_hosts_and_provider()214 host3 = Host.objects.create(provider=provider, prototype=self.hp, fqdn="host_3")215 api.add_host_to_cluster(self.cluster, host1)216 api.add_host_to_cluster(self.cluster, host2)217 p = Policy.objects.create(role=self.object_role_custom_perm_cluster_host())218 p.user.add(self.user)219 p.add_object(self.cluster)220 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))221 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host1))222 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))223 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host3))224 p.apply()225 self.assertTrue(self.user.has_perm("cm.change_config_of_cluster", self.cluster))226 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host1))227 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host2))228 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host3))229 def test_parent_policy4host_in_service(self):230 _, host1, host2 = self.get_hosts_and_provider()231 api.add_host_to_cluster(self.cluster, host1)232 api.add_host_to_cluster(self.cluster, host2)233 api.add_hc(234 self.cluster,235 [236 {237 "service_id": self.service_1.id,238 "component_id": self.component_11.id,239 "host_id": host1.id,240 },241 {242 "service_id": self.service_2.id,243 "component_id": self.component_21.id,244 "host_id": host2.id,245 },246 ],247 )248 p = Policy.objects.create(249 role=self.object_role_custom_perm_cluster_service_component_host()250 )251 p.user.add(self.user)252 p.add_object(self.service_1)253 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))254 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))255 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))256 self.assertFalse(257 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)258 )259 self.assertFalse(260 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)261 )262 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host1))263 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))264 p.apply()265 self.assertFalse(self.user.has_perm("cm.change_confing_of_cluster", self.cluster))266 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))267 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))268 self.assertTrue(269 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)270 )271 self.assertFalse(272 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)273 )274 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host1))275 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))276 def test_parent_policy4host_in_component(self):277 provider, host1, host2 = self.get_hosts_and_provider()278 host3 = Host.objects.create(provider=provider, prototype=self.hp, fqdn="host_3")279 api.add_host_to_cluster(self.cluster, host1)280 api.add_host_to_cluster(self.cluster, host2)281 api.add_host_to_cluster(self.cluster, host3)282 api.add_hc(283 self.cluster,284 [285 {286 "service_id": self.service_2.id,287 "component_id": self.component_21.id,288 "host_id": host1.id,289 },290 {291 "service_id": self.service_2.id,292 "component_id": self.component_21.id,293 "host_id": host2.id,294 },295 {296 "service_id": self.service_1.id,297 "component_id": self.component_11.id,298 "host_id": host3.id,299 },300 ],301 )302 p = Policy.objects.create(303 role=self.object_role_custom_perm_cluster_service_component_host()304 )305 p.user.add(self.user)306 p.add_object(self.component_21)307 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))308 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))309 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))310 self.assertFalse(311 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)312 )313 self.assertFalse(314 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)315 )316 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host1))317 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))318 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host3))319 p.apply()320 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))321 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))322 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))323 self.assertFalse(324 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)325 )326 self.assertTrue(327 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_21)328 )329 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host1))330 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host2))331 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host3))332 def test_parent_policy4provider(self):333 provider, host1, host2 = self.get_hosts_and_provider()334 p = Policy.objects.create(role=self.object_role_custom_perm_provider_host())335 p.user.add(self.user)336 p.add_object(provider)337 self.assertFalse(self.user.has_perm("cm.change_config_of_hostprovider", provider))338 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host1))339 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))340 p.apply()341 self.assertTrue(self.user.has_perm("cm.change_config_of_hostprovider", provider))342 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host1))343 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host2))344 def test_simple_parent_policy(self):345 p = Policy.objects.create(role=self.model_role_view_cluster_service_component_perm())346 p.user.add(self.user)347 self.assertFalse(self.user.has_perm("cm.view_cluster"))348 self.assertFalse(self.user.has_perm("cm.view_clusterobject"))349 self.assertFalse(self.user.has_perm("cm.view_servicecomponent"))350 self.clear_perm_cache(self.user)351 p.apply()352 self.assertTrue(self.user.has_perm("cm.view_cluster"))353 self.assertTrue(self.user.has_perm("cm.view_clusterobject"))354 self.assertTrue(self.user.has_perm("cm.view_servicecomponent"))355 def test_add_service(self):356 sp_3 = Prototype.obj.create(bundle=self.bundle_1, type="service", name="service_3")357 p = Policy.objects.create(role=self.object_role_custom_perm_cluster_service())358 p.user.add(self.user)359 p.add_object(self.cluster)360 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))361 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))362 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))363 p.apply()364 self.assertTrue(self.user.has_perm("cm.change_config_of_cluster", self.cluster))365 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))366 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_2))367 service3 = api.add_service_to_cluster(self.cluster, sp_3)368 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", service3))369 def test_add_host(self):370 _, host1, host2 = self.get_hosts_and_provider()371 api.add_host_to_cluster(self.cluster, host1)372 api.add_hc(373 self.cluster,374 [375 {376 "service_id": self.service_1.id,377 "component_id": self.component_11.id,378 "host_id": host1.id,379 }380 ],381 )382 p = Policy.objects.create(383 role=self.object_role_custom_perm_cluster_service_component_host()384 )385 p.user.add(self.user)386 p.add_object(self.cluster)387 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))388 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))389 self.assertFalse(390 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)391 )392 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host1))393 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))394 p.apply()395 self.assertTrue(self.user.has_perm("cm.change_config_of_cluster", self.cluster))396 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))397 self.assertTrue(398 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)399 )400 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host1))401 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))402 api.add_host_to_cluster(self.cluster, host2)403 self.assertTrue(self.user.has_perm("cm.change_config_of_cluster", self.cluster))404 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))405 self.assertTrue(406 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)407 )408 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host1))409 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host2))410 def test_add_hc(self):411 _, host1, host2 = self.get_hosts_and_provider()412 api.add_host_to_cluster(self.cluster, host1)413 api.add_hc(414 self.cluster,415 [416 {417 "service_id": self.service_1.id,418 "component_id": self.component_11.id,419 "host_id": host1.id,420 }421 ],422 )423 p = Policy.objects.create(role=self.object_role_custom_perm_service_component_host())424 p.user.add(self.user)425 p.add_object(self.service_1)426 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))427 self.assertFalse(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))428 self.assertFalse(429 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)430 )431 self.assertFalse(432 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_12)433 )434 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host1))435 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))436 p.apply()437 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))438 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))439 self.assertTrue(440 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)441 )442 self.assertTrue(443 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_12)444 )445 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host1))446 self.assertFalse(self.user.has_perm("cm.change_config_of_host", host2))447 api.add_host_to_cluster(self.cluster, host2)448 api.add_hc(449 self.cluster,450 [451 {452 "service_id": self.service_1.id,453 "component_id": self.component_11.id,454 "host_id": host1.id,455 },456 {457 "service_id": self.service_1.id,458 "component_id": self.component_12.id,459 "host_id": host2.id,460 },461 ],462 )463 self.assertFalse(self.user.has_perm("cm.change_config_of_cluster", self.cluster))464 self.assertTrue(self.user.has_perm("cm.change_config_of_clusterobject", self.service_1))465 self.assertTrue(466 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_11)467 )468 self.assertTrue(469 self.user.has_perm("cm.change_config_of_servicecomponent", self.component_12)470 )471 self.assertTrue(self.user.has_perm("cm.change_config_of_host", host1))...
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!!