Best Python code snippet using playwright-python
test_permissions.py
Source:test_permissions.py
...8def p(permission_string):9 app_label, codename = permission_string.split('.', 1)10 return Permission.objects.get(content_type__app_label=app_label,11 codename=codename)12def grant_permissions(perms):13 def decorator(fn):14 @wraps(fn)15 def method(self):16 self.user.user_permissions.add(*[p(perm) for perm in perms])17 self.user.save()18 return fn(self)19 return method20 return decorator21class WithNewsIndexTestCase(TestCase):22 def setUp(self):23 super(WithNewsIndexTestCase, self).setUp()24 root_page = Page.objects.get(pk=2)25 self.index = NewsIndex(26 title='News', slug='news')27 root_page.add_child(instance=self.index)28class WithNewsItemTestCase(WithNewsIndexTestCase):29 def setUp(self):30 super(WithNewsItemTestCase, self).setUp()31 self.newsitem = NewsItem.objects.create(32 newsindex=self.index, title='News item')33class PermissionTestCase(TestCase, WagtailTestUtils):34 def setUp(self):35 super(PermissionTestCase, self).setUp()36 # Create a group with permission to edit pages37 # Required to enable page searching38 self.group = Group.objects.create(name='Test group')39 GroupPagePermission.objects.create(40 group=self.group, page=Page.objects.get(pk=1),41 permission_type='add')42 self.user = self.create_test_user()43 self.client.login(username='test@email.com', password='password')44 def create_test_user(self):45 """46 Create a normal boring user, not a super user. This user has no news47 related permissions by default.48 """49 user = User.objects.create_user(50 username='test@email.com', password='password')51 user.groups.add(self.group)52 user.user_permissions.add(p('wagtailadmin.access_admin'))53 user.save()54 return user55 def assertStatusCode(self, url, status_code, msg=None):56 response = self.client.get(url)57 self.assertEqual(response.status_code, status_code, msg=msg)58class TestChooseNewsIndex(PermissionTestCase):59 @grant_permissions(['app.add_newsitem', 'app.change_newsitem'])60 def test_chooser_multiple_choices(self):61 """62 Test the chooser when there are multiple valid choices, and some63 missing due to lack of permissions.64 """65 root_page = Page.objects.get(pk=2)66 news1 = root_page.add_child(instance=NewsIndex(67 title='Normal News 1', slug='news-1'))68 news2 = root_page.add_child(instance=NewsIndex(69 title='Normal News 2', slug='news-2'))70 secondary_news = root_page.add_child(instance=SecondaryNewsIndex(71 title='Secondary News', slug='secondary-news'))72 response = self.client.get(reverse('wagtailnews:choose'))73 self.assertContains(response, news1.title)74 self.assertContains(response, news2.title)75 self.assertNotContains(response, secondary_news.title)76 @grant_permissions(['app.add_newsitem', 'app.change_newsitem'])77 def test_chooser_one_choice(self):78 """79 Test the chooser when there is a single valid choice, and some80 missing due to lack of permissions.81 """82 root_page = Page.objects.get(pk=2)83 news = root_page.add_child(instance=NewsIndex(84 title='News', slug='news'))85 root_page.add_child(instance=SecondaryNewsIndex(86 title='Secondary News', slug='secondary-news'))87 response = self.client.get(reverse('wagtailnews:choose'))88 self.assertRedirects(response, reverse('wagtailnews:index', kwargs={89 'pk': news.pk}))90 def test_chooser_no_perms(self):91 """92 Test the chooser when there are no valid choices.93 """94 root_page = Page.objects.get(pk=2)95 root_page.add_child(instance=NewsIndex(96 title='News', slug='news'))97 root_page.add_child(instance=SecondaryNewsIndex(98 title='Secondary News', slug='secondary-news'))99 response = self.client.get(reverse('wagtailnews:choose'))100 self.assertEqual(response.status_code, 403)101 @grant_permissions(['app.add_newsitem', 'app.change_newsitem'])102 def test_chooser_has_perms_no_news(self):103 """104 Test the chooser when there are no news items, but the user has105 relevant permissions.106 """107 response = self.client.get(reverse('wagtailnews:choose'))108 self.assertEqual(response.status_code, 200)109class TestNewsIndex(WithNewsIndexTestCase, PermissionTestCase):110 def setUp(self):111 super(TestNewsIndex, self).setUp()112 self.url = reverse('wagtailnews:index', kwargs={'pk': self.index.pk})113 @grant_permissions(['app.add_newsitem', 'app.change_newsitem'])114 def test_news_index_has_perm(self):115 """116 Check the user is allowed to access the news index list117 """118 self.assertStatusCode(self.url, 200)119 def test_news_index_no_perm(self):120 """121 Check the user is denied access to the news index list122 """123 self.assertStatusCode(self.url, 403)124class TestCreateNewsItem(WithNewsIndexTestCase, PermissionTestCase):125 def setUp(self):126 super(TestCreateNewsItem, self).setUp()127 self.url = reverse('wagtailnews:create', kwargs={'pk': self.index.pk})128 @grant_permissions(['app.add_newsitem', 'app.change_newsitem'])129 def test_has_permission(self):130 """ Test users can create NewsItems """131 self.assertStatusCode(self.url, 200)132 @grant_permissions(['app.add_newsitem'])133 def test_only_add_perm(self):134 """ Users need both add and edit. Add is not sufficient """135 self.assertStatusCode(self.url, 403)136 @grant_permissions(['app.change_newsitem'])137 def test_only_edit_perm(self):138 """ Users need both add and edit. Edit is not sufficient """139 self.assertStatusCode(self.url, 403)140 def test_no_permission(self):141 """ Test user can not create without permission """142 self.assertStatusCode(self.url, 403)143 @grant_permissions(['app.add_newsitem', 'app.change_newsitem'])144 def test_add_button_appears(self):145 """Test that the add button appears"""146 response = self.client.get(reverse('wagtailnews:index', kwargs={147 'pk': self.index.pk}))148 self.assertContains(response, self.url)149 @grant_permissions(['app.change_newsitem'])150 def test_no_add_button_appears(self):151 """Test that the add button does not appear"""152 response = self.client.get(reverse('wagtailnews:index', kwargs={153 'pk': self.index.pk}))154 self.assertNotContains(response, self.url)155class TestEditNewsItem(WithNewsItemTestCase, PermissionTestCase):156 def setUp(self):157 super(TestEditNewsItem, self).setUp()158 self.url = reverse('wagtailnews:edit', kwargs={159 'pk': self.index.pk, 'newsitem_pk': self.newsitem.pk})160 @grant_permissions(['app.change_newsitem'])161 def test_has_permission(self):162 """ Test users can create NewsItems """163 self.assertStatusCode(self.url, 200)164 def test_no_permission(self):165 """ Test user can not edit without permission """166 self.assertStatusCode(self.url, 403)167 @grant_permissions(['app.change_newsitem'])168 def test_edit_button_appears(self):169 """Test that the edit button appears"""170 response = self.client.get(reverse('wagtailnews:index', kwargs={171 'pk': self.index.pk}))172 self.assertContains(response, self.url)173 @grant_permissions(['app.delete_newsitem'])174 def test_no_edit_button_appears(self):175 """Test that the edit button does not appear"""176 response = self.client.get(reverse('wagtailnews:index', kwargs={177 'pk': self.index.pk}))178 self.assertNotContains(response, self.url)179class TestUnpublishNewsItem(WithNewsItemTestCase, PermissionTestCase):180 def setUp(self):181 super(TestUnpublishNewsItem, self).setUp()182 self.url = reverse('wagtailnews:unpublish', kwargs={183 'pk': self.index.pk, 'newsitem_pk': self.newsitem.pk})184 @grant_permissions(['app.change_newsitem'])185 def test_has_permission(self):186 """ Test users can unpublish NewsItems """187 self.assertStatusCode(self.url, 200)188 def test_no_permission(self):189 """ Test user can not unpublish without permission """190 self.assertStatusCode(self.url, 403)191 @grant_permissions(['app.change_newsitem'])192 def test_unpublish_button_appears(self):193 """Test that the unpublish button appears"""194 response = self.client.get(reverse('wagtailnews:index', kwargs={195 'pk': self.index.pk}))196 self.assertContains(response, self.url)197 @grant_permissions(['app.delete_newsitem'])198 def test_no_unpublish_button_appears(self):199 """Test that the unpublish button does not appear"""200 response = self.client.get(reverse('wagtailnews:index', kwargs={201 'pk': self.index.pk}))202 self.assertNotContains(response, self.url)203class TestDeleteNewsItem(WithNewsItemTestCase, PermissionTestCase):204 def setUp(self):205 super(TestDeleteNewsItem, self).setUp()206 self.url = reverse('wagtailnews:delete', kwargs={207 'pk': self.index.pk, 'newsitem_pk': self.newsitem.pk})208 @grant_permissions(['app.delete_newsitem'])209 def test_has_permission(self):210 """ Test users can delete NewsItems """211 self.assertStatusCode(self.url, 200)212 def test_no_permission(self):213 """ Test user can not delete without permission """214 self.assertStatusCode(self.url, 403)215 @grant_permissions(['app.delete_newsitem'])216 def test_delete_button_appears_index(self):217 """Test that the delete button appears on the index page"""218 response = self.client.get(reverse('wagtailnews:index', kwargs={219 'pk': self.index.pk}))220 self.assertContains(response, self.url)221 @grant_permissions(['app.change_newsitem'])222 def test_no_delete_button_appears_index(self):223 """Test that the delete button does not appear on the index page"""224 response = self.client.get(reverse('wagtailnews:index', kwargs={225 'pk': self.index.pk}))226 self.assertNotContains(response, self.url)227 @grant_permissions(['app.change_newsitem', 'app.delete_newsitem'])228 def test_delete_button_appears_edit(self):229 """Test that the delete button appears on the edit page"""230 response = self.client.get(reverse('wagtailnews:edit', kwargs={231 'pk': self.index.pk, 'newsitem_pk': self.newsitem.pk}))232 self.assertContains(response, self.url)233 @grant_permissions(['app.change_newsitem'])234 def test_no_delete_button_appears_edit(self):235 """Test that the delete button does not appear on the edit page"""236 response = self.client.get(reverse('wagtailnews:edit', kwargs={237 'pk': self.index.pk, 'newsitem_pk': self.newsitem.pk}))238 self.assertNotContains(response, self.url)239class TestSearchNewsItem(WithNewsItemTestCase, PermissionTestCase):240 def setUp(self):241 super(TestSearchNewsItem, self).setUp()242 self.url = reverse('wagtailnews:search')243 self.search_url = reverse('wagtailadmin_pages:search') + '?q=hello'244 @grant_permissions(['app.change_newsitem'])245 def test_has_permission(self):246 self.assertStatusCode(self.url, 200)247 def test_no_permission(self):248 self.assertStatusCode(self.url, 403)249 @grant_permissions(['app.add_newsitem', 'app.change_newsitem'])250 def test_search_area_appears_permission(self):251 response = self.client.get(self.search_url)252 self.assertContains(response, self.url)253 def test_search_area_hidden_no_permission(self):254 response = self.client.get(self.search_url)255 self.assertNotContains(response, 'News')...
test_grant_permissions.py
Source:test_grant_permissions.py
1import functions.grant_permissions2from functions.grant_permissions import grant_download_permissions, permissions_worker3from functions.settings import GOOGLE_WORKER_TOPIC4import pytest5from unittest.mock import MagicMock, call6def test_grant_download_permissions(monkeypatch):7 user_email_list = ["foo@bar.com", "user@test.com", "cidc@foo.bar"]8 full_email_dict = {9 None: {"bar": [user_email_list[0]]},10 "foo": {None: [user_email_list[1]], "bar": [user_email_list[2]]},11 "biz": {"wes": [user_email_list[2]]},12 }13 def mock_get_user_emails(trial_id: str, upload_type: str, session):14 return {15 trial: {16 upload: users17 for upload, users in upload_dict.items()18 if upload is None or upload == upload_type19 }20 for trial, upload_dict in full_email_dict.items()21 if trial is None or trial == trial_id22 }23 monkeypatch.setattr(24 functions.grant_permissions.Permissions,25 "get_user_emails_for_trial_upload",26 mock_get_user_emails,27 )28 mock_blob_name_list = MagicMock()29 # need more than 100 to test chunking30 mock_blob_name_list.return_value = [f"blob{n}" for n in range(100 + 50)]31 monkeypatch.setattr(32 functions.grant_permissions, "get_blob_names", mock_blob_name_list33 )34 mock_encode_and_publish = MagicMock()35 monkeypatch.setattr(36 functions.grant_permissions, "_encode_and_publish", mock_encode_and_publish37 )38 # no matching does nothing at all, just logging39 mock_extract_data = MagicMock()40 mock_extract_data.return_value = "{}"41 monkeypatch.setattr(42 functions.grant_permissions, "extract_pubsub_data", mock_extract_data43 )44 with pytest.raises(45 Exception, match="trial_id and upload_type must both be provided, you provided:"46 ):47 grant_download_permissions({}, None)48 # incomplete/incorrect matching does nothing at all, just logging49 mock_extract_data = MagicMock()50 mock_extract_data.return_value = str(51 {"trial_id": "foo", "user_email_list": ["baz"]}52 )53 monkeypatch.setattr(54 functions.grant_permissions, "extract_pubsub_data", mock_extract_data55 )56 with pytest.raises(57 Exception, match="trial_id and upload_type must both be provided, you provided:"58 ):59 grant_download_permissions({}, None)60 # with data response, calls61 mock_extract_data = MagicMock()62 mock_extract_data.return_value = str({"trial_id": "foo", "upload_type": "bar"})63 monkeypatch.setattr(64 functions.grant_permissions, "extract_pubsub_data", mock_extract_data65 )66 grant_download_permissions({}, None)67 assert mock_blob_name_list.call_count == 368 # (None, bar), (foo, None), (foo, bar) all match69 # Note no (biz, wes) as that doesn't match70 for _, kwargs in mock_blob_name_list.call_args_list:71 assert kwargs["trial_id"] in (None, "foo")72 assert kwargs["upload_type"] in (None, "bar")73 assert mock_encode_and_publish.call_count == 674 assert mock_encode_and_publish.call_args_list == [75 call(76 str(77 {78 "_fn": "permissions_worker",79 "user_email_list": user_email_list[:1],80 "blob_name_list": mock_blob_name_list.return_value[:100],81 "revoke": False,82 "is_group": False,83 }84 ),85 GOOGLE_WORKER_TOPIC,86 ),87 call(88 str(89 {90 "_fn": "permissions_worker",91 "user_email_list": user_email_list[:1],92 "blob_name_list": mock_blob_name_list.return_value[100:],93 "revoke": False,94 "is_group": False,95 }96 ),97 GOOGLE_WORKER_TOPIC,98 ),99 call(100 str(101 {102 "_fn": "permissions_worker",103 "user_email_list": user_email_list[1:2],104 "blob_name_list": mock_blob_name_list.return_value[:100],105 "revoke": False,106 "is_group": False,107 }108 ),109 GOOGLE_WORKER_TOPIC,110 ),111 call(112 str(113 {114 "_fn": "permissions_worker",115 "user_email_list": user_email_list[1:2],116 "blob_name_list": mock_blob_name_list.return_value[100:],117 "revoke": False,118 "is_group": False,119 }120 ),121 GOOGLE_WORKER_TOPIC,122 ),123 call(124 str(125 {126 "_fn": "permissions_worker",127 "user_email_list": user_email_list[-1:],128 "blob_name_list": mock_blob_name_list.return_value[:100],129 "revoke": False,130 "is_group": False,131 }132 ),133 GOOGLE_WORKER_TOPIC,134 ),135 call(136 str(137 {138 "_fn": "permissions_worker",139 "user_email_list": user_email_list[-1:],140 "blob_name_list": mock_blob_name_list.return_value[100:],141 "revoke": False,142 "is_group": False,143 }144 ),145 GOOGLE_WORKER_TOPIC,146 ),147 ]148 # with revoke: True, passing revoke: True149 # passing user_email_list doesn't get the Permissions or users150 mock_encode_and_publish.reset_mock() # we're checking this151 mock_blob_name_list.reset_mock()152 def no_call(self):153 assert False154 monkeypatch.setattr(155 functions.grant_permissions.Permissions,156 "get_user_emails_for_trial_upload",157 lambda *args: no_call(),158 )159 mock_extract_data.return_value = str(160 {161 "trial_id": "foo",162 "upload_type": "bar",163 "user_email_list": user_email_list,164 "revoke": True,165 }166 )167 grant_download_permissions({}, None)168 assert mock_blob_name_list.call_count == 1169 _, kwargs = mock_blob_name_list.call_args170 assert kwargs["trial_id"] == "foo"171 assert kwargs["upload_type"] == "bar"172 assert mock_encode_and_publish.call_count == 2173 assert mock_encode_and_publish.call_args_list == [174 call(175 str(176 {177 "_fn": "permissions_worker",178 "user_email_list": user_email_list,179 "blob_name_list": mock_blob_name_list.return_value[:100],180 "revoke": True,181 "is_group": False,182 }183 ),184 GOOGLE_WORKER_TOPIC,185 ),186 call(187 str(188 {189 "_fn": "permissions_worker",190 "user_email_list": user_email_list,191 "blob_name_list": mock_blob_name_list.return_value[100:],192 "revoke": True,193 "is_group": False,194 }195 ),196 GOOGLE_WORKER_TOPIC,197 ),198 ]199def test_permissions_worker(monkeypatch):200 user_email_list = ["foo@bar.com", "user@test.com"]201 blob_name_list = [f"blob{n}" for n in range(100)]202 with pytest.raises(203 ValueError, match="user_email_list and blob_name_list must both be provided"204 ):205 permissions_worker()206 mock_grant, mock_revoke = MagicMock(), MagicMock()207 monkeypatch.setattr(208 functions.grant_permissions, "grant_download_access_to_blob_names", mock_grant209 )210 monkeypatch.setattr(211 functions.grant_permissions,212 "revoke_download_access_from_blob_names",213 mock_revoke,214 )215 permissions_worker(216 user_email_list=user_email_list,217 blob_name_list=blob_name_list,218 revoke=False,219 is_group=False,220 )221 mock_grant.assert_called_with(222 user_email_list=user_email_list, blob_name_list=blob_name_list, is_group=False223 )224 mock_revoke.assert_not_called()225 mock_grant.reset_mock()226 permissions_worker(227 user_email_list=user_email_list,228 blob_name_list=blob_name_list,229 revoke=True,230 is_group=False,231 )232 mock_grant.assert_not_called()233 mock_revoke.assert_called_with(234 user_email_list=user_email_list, blob_name_list=blob_name_list, is_group=False...
tox_hook.py
Source:tox_hook.py
...4import shutil5import subprocess6class CopyFileToSolutionException(Exception):7 pass8def grant_permissions(path: Path) -> None:9 subprocess.check_output(10 ["icacls", f"{path}", "/grant", "Everyone:F", "/t"],11 stderr=subprocess.STDOUT,12 )13def tfs_command(path: Path, command: str, recursive: bool = False) -> None:14 subprocess.check_output(15 [16 f"{tf_exe_path}",17 command,18 f"{path}",19 f"{'/recursive' if recursive else ''}",20 ],21 stderr=subprocess.STDOUT,22 )23composapy_root_dir = Path(__file__).parent24composable_analytics_product_dir = composapy_root_dir.parent.parent.joinpath("Product")25datalabservice_static_dir = composable_analytics_product_dir.joinpath(26 "CompAnalytics.DataLabService", "static"27)28tf_exe_path = Path(dotenv_values(".local.env").get("TF_EXE_PATH"))29print("Copying composapy-readme.ipynb...")30notebook_src = composapy_root_dir.joinpath("composapy-readme.ipynb")31notebook_dest = datalabservice_static_dir.joinpath(notebook_src.name)32try:33 shutil.copy(notebook_src, notebook_dest)34except Exception:35 raise CopyFileToSolutionException(36 f"Failed to copy composapy-readme.ipynb from {notebook_src} to {notebook_dest}."37 )38grant_permissions(notebook_dest)39print("Copying tests...")40tests_src = composapy_root_dir.joinpath("tests")41tests_dest = composable_analytics_product_dir.joinpath(42 "UnitTests", "TestData", "composapy"43)44try:45 shutil.copytree(tests_src, tests_dest, dirs_exist_ok=True)46except Exception:47 raise CopyFileToSolutionException(48 f"Failed to copy tests from {tests_src} to {tests_dest}."49 )50grant_permissions(tests_dest)51print("Copying wheel...")52wheel_src = sorted(composapy_root_dir.joinpath(".tox", "dist").glob("*.whl"))[0]53wheel_dest = datalabservice_static_dir.joinpath("wheels")54try:55 old_wheel = sorted(wheel_dest.glob("composapy-*.whl"))[0]56 os.remove(Path(old_wheel))57except IndexError:58 print(59 "Could not find old version of composapy... updating with newly built composapy wheel."60 )61try:62 shutil.copy(wheel_src, wheel_dest)63 grant_permissions(wheel_dest)64except Exception:65 raise CopyFileToSolutionException(66 f"Failed to copy wheel from {wheel_src} to {wheel_dest}."67 )68tfs_command(datalabservice_static_dir.joinpath("*"), "add", recursive=True) # static/*69tfs_command(tests_dest.joinpath("test_*.py"), "add") # tests/test_*.py70tfs_command(tests_dest.joinpath("conftest.py"), "add") # tests/conftest.py71tfs_command(tests_dest.joinpath("__init__.py"), "add") # tests/__init__.py72tfs_command(tests_dest.joinpath(".test.env"), "add") # tests/.test.env73tfs_command(74 tests_dest.joinpath("TestFiles"), "add", recursive=True75) # tests/TestFiles/*76## cleanup unwanted directory77tfs_command(tests_dest.joinpath("TestFiles", ".pytest_cache"), "undo", recursive=True)
3_grant_data_product_access.py
Source:3_grant_data_product_access.py
1import logging2import warnings3import sys4import os5import inspect67two_up = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))))8sys.path.insert(0, two_up)910import test.test_utils as test_utils11from data_mesh_util.lib.constants import *12import argparse13from data_mesh_util import DataMeshProducer as dmp14from data_mesh_util.lib.SubscriberTracker import *1516warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning)171819class Step3():20 '''21 Class to test the functionality of a data producer. Should be run using credentials for a principal who can assume22 the DataMeshAdminProducer role in the data mesh. Requires environment variables:2324 AWS_REGION25 AWS_ACCESS_KEY_ID26 AWS_SECRET_ACCESS_KEY27 AWS_SESSION_TOKEN (Optional)28 '''29 _region, _clients, _account_ids, _creds = test_utils.load_client_info_from_file()3031 _mgr = dmp.DataMeshProducer(data_mesh_account_id=_account_ids.get(MESH),32 log_level=logging.DEBUG,33 region_name=_region,34 use_credentials=_creds.get(PRODUCER))3536 def setUp(self) -> None:37 warnings.filterwarnings("ignore", category=ResourceWarning)3839 def grant_access_request(self, subscription_id: str, grant_permissions: list, approval_notes: str):40 # approve access from the producer41 approval = self._mgr.approve_access_request(42 request_id=subscription_id,43 grant_permissions=grant_permissions,44 grantable_permissions=grant_permissions,45 decision_notes=approval_notes46 )4748 return approval495051if __name__ == "__main__":52 parser = argparse.ArgumentParser()53 parser.add_argument('--subscription_id', dest='subscription_id', required=True)54 parser.add_argument('--grant_permissions', nargs="+", dest='grant_permissions', required=True)55 parser.add_argument('--approval_notes', dest='approval_notes', required=True)5657 args = parser.parse_args()58 print(Step3().grant_access_request(subscription_id=args.subscription_id, grant_permissions=args.grant_permissions,
...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!