async def test_users_screen_on_body(users_screen): def target(item_): class MockTarget: class parent: item = item_ return MockTarget() event = Event('Mouse', 'click') event.target = target(None) await users_screen.on_body(event) assert users_screen.modal is None event.target = target({ 'id': '1', 'name': '', 'username': '', 'email': '', 'attributes': {} }) await users_screen.on_body(event) await asyncio.sleep(0) assert users_screen.modal is not None
async def test_restrictions_screen_on_body(restrictions_modal): def target(item_): class MockTarget: class parent: item = item_ return MockTarget() event = Event('Mouse', 'click') event.target = target(None) restrictions_modal.build() await restrictions_modal.on_body(event) assert restrictions_modal.modal is None event.target = target({ 'id': '', 'resource': '', 'privilege': '', 'active': '' }) await restrictions_modal.on_body(event) await asyncio.sleep(0) assert restrictions_modal.modal is not None
async def test_policies_modal_on_modal_done(policies_modal): policies_modal.build() event = Event('Custom', 'done', details={'result': 'default'}) await policies_modal.on_modal_done(event) assert policies_modal.modal is None assert len(policies_modal.body.data) == 1 event = Event('Custom', 'done', details={'result': 'restrictions'}) await policies_modal.on_modal_done(event) await asyncio.sleep(0) assert type(policies_modal.modal).__name__ == 'RestrictionsModal'
async def test_users_selection_modal_on_chosen_search(users_selection_modal): users_selection_modal.build() users_selection_modal.chosen_search.setup(content='value').connect() assert users_selection_modal.chosen_search.text == 'value' assert users_selection_modal.chosen_domain == [] await users_selection_modal.on_chosen_search( Event('Keyboard', 'keydown', key='')) await users_selection_modal.on_chosen_search( Event('Keyboard', 'keydown', key='\n')) await asyncio.sleep(0) assert users_selection_modal.chosen_domain == [ '|', ('name', 'ilike', '%value%'), ('email', 'ilike', '%value%')]
async def test_users_screen_on_modal_done(users_screen): event = Event('Custom', 'done', details={'result': 'roles'}) await users_screen.on_modal_done(event) assert type(users_screen.modal).__name__ == 'RankingsModal' event = Event('Custom', 'done', details={'result': 'credentials'}) await users_screen.on_modal_done(event) assert type(users_screen.modal).__name__ == 'CredentialsModal' event = Event('Custom', 'done', details={'result': 'other'}) await users_screen.on_modal_done(event) await asyncio.sleep(0) assert len(users_screen.body.data) == 2
async def test_restriction_details_modal_on_save(restriction_details_modal): event = Event('Mouse', 'click') given_restrictions = None async def create_restriction(self, restrictions): nonlocal given_restrictions given_restrictions = restrictions restriction_details_modal.build() restriction_details_modal.security_manager.create_restriction = MethodType( create_restriction, restriction_details_modal) await restriction_details_modal.on_save(event) await asyncio.sleep(0) assert given_restrictions == [{ 'id': '1', 'policy_id': '1', 'sequence': 0, 'name': 'Own Customers', 'target': 'customers', 'domain': '[["user_id", "=", ">>> user["id"]"]]' }]
async def test_dominions_screen_on_modal_done(dominions_screen): dominions_screen.dominion = {'id': '1', 'name': 'Proser'} event = Event('Custom', 'done', details={'result': 'other'}) await dominions_screen.on_modal_done(event) await asyncio.sleep(0) assert len(dominions_screen.body.data) == 1
async def test_provision_modal_on_save(tenant_provision_modal): event = Event('Mouse', 'click') given_tenants = None def mock_create_tenant(self, tenants): nonlocal given_tenants given_tenants = tenants tenant_provision_modal.build() tenant_provision_modal.tenant_supplier.create_tenant = MethodType( mock_create_tenant, tenant_provision_modal) await tenant_provision_modal.on_save(event) assert given_tenants['data'] == {'name': ''} class MockId: text = '123' tenant_provision_modal.id = MockId() await tenant_provision_modal.on_save(event) await asyncio.sleep(0) assert given_tenants['data'] == {'id': '123', 'name': ''}
async def test_users_selection_modal_on_choose(users_selection_modal): users_selection_modal.build() users_selection_modal.chosen_search.setup(content='value').connect() await users_selection_modal.on_choose(Event('Mouse', 'click')) assert users_selection_modal.available.data == [] item = {'id': '1', 'usename': 'Dummy'} users_selection_modal.focused = item users_selection_modal.available.data = [item] await users_selection_modal.on_choose(Event('Mouse', 'click')) await asyncio.sleep(0) assert users_selection_modal.available.data == [] assert users_selection_modal.chosen.data == [item]
async def test_roles_screen_on_modal_done(roles_screen): event = Event('Custom', 'done', details={'result': 'default'}) await roles_screen.on_modal_done(event) await asyncio.sleep(0) assert roles_screen.modal is None assert len(roles_screen.body.data) == 1
async def test_restrictions_modal_on_modal_done(restrictions_modal): event = Event('Custom', 'done', details={'result': 'default'}) restrictions_modal.build() await restrictions_modal.on_modal_done(event) await asyncio.sleep(0) assert restrictions_modal.modal is None assert len(restrictions_modal.body.data) == 1
async def test_users_screen_on_create(users_screen): event = Event('Mouse', 'click') await users_screen.on_create(event) await asyncio.sleep(0) assert users_screen.modal.user == { 'name': '', 'username': '', 'email': '', 'attributes': {} }
async def on_chosen_search(self, event: Event) -> None: if event.key == '\n': event.stop = True text = self.chosen_search.text.strip() self.chosen.offset = 0 self.chosen_domain = [] if not text else [ '|', ('name', 'ilike', f'%{text}%'), ('email', 'ilike', f'%{text}%')] await self.load() self.chosen_search.focus()
async def test_roles_screen_on_modal_done(roles_screen): roles_screen.role = {'id': '1', 'name': 'admin', 'dominion_id': '1', 'description': ''} event = Event('Custom', 'done', details={'result': 'policies'}) roles_screen.build() await roles_screen.load() await roles_screen.on_modal_done(event) assert type(roles_screen.modal).__name__ == 'PoliciesModal' event = Event('Custom', 'done', details={'result': 'other'}) await roles_screen.on_modal_done(event) assert len(roles_screen.body.data) == 1 event = Event('Custom', 'done', details={'result': 'users'}) await roles_screen.on_modal_done(event) await asyncio.sleep(0) assert type(roles_screen.modal).__name__ == 'UsersSelectionModal'
async def test_dominions_screen_on_body(dominions_screen): def target(item_): class MockTarget: class parent: item = item_ return MockTarget() event = Event('Mouse', 'click') event.target = target(None) await dominions_screen.on_body(event) assert dominions_screen.modal is None event.target = target({'name': ''}) await dominions_screen.on_body(event) await asyncio.sleep(0) assert dominions_screen.modal is not None
async def test_policies_modal_on_create(policies_modal): event = Event('Mouse', 'click') await policies_modal.on_create(event) await asyncio.sleep(0) assert policies_modal.modal.policy == { 'active': False, 'id': '', 'privilege': '', 'resource': '', 'role_id': '1' }
async def test_roles_screen_on_body(roles_screen): def target(item_): class MockTarget: class parent: item = item_ return MockTarget() event = Event('Mouse', 'click') event.target = target(None) await roles_screen.on_body(event) assert roles_screen.modal is None event.target = target({'id': '1', 'name': 'admin', 'dominion_id': '1', 'description': ''}) await roles_screen.on_body(event) await asyncio.sleep(0) assert roles_screen.modal is not None
async def test_roles_screen_on_cancel(roles_screen): event = Event('Mouse', 'click') given_result = None async def mock_done(self, result): nonlocal given_result given_result = result roles_screen.done = MethodType(mock_done, roles_screen) await roles_screen.on_cancel(event) assert given_result == {'result': 'cancelled'}
async def test_restrictions_modal_on_create(restrictions_modal): event = Event('Mouse', 'click') await restrictions_modal.on_create(event) await asyncio.sleep(0) assert restrictions_modal.modal.restriction == { 'id': '', 'policy_id': '1', 'name': '', 'sequence': 0, 'target': 'customers', 'domain': '' }
async def test_users_selection_modal_on_select(users_selection_modal): def target(item_): class MockTarget: class parent: item = item_ def focus(self): pass return MockTarget() users_selection_modal.connect() event = Event('Mouse', 'click') event.target = target({'id': '1', 'username': '******', 'email': '[email protected]'}) await users_selection_modal.on_select(event) await asyncio.sleep(0) assert users_selection_modal.focused == { 'id': '1', 'username': '******', 'email': '[email protected]'}
async def test_application_on_tenant_switch(application): given_tenant = None def mock_set_tenant(self, tenant): nonlocal given_tenant given_tenant = tenant application.session_manager.set_tenant = MethodType( mock_set_tenant, application.session_manager) event = Event('Mouse', 'click') await application.on_tenant_switch(event) await asyncio.sleep(0) assert given_tenant is None event = Event('Mouse', 'click', details={'name': 'Corporation X'}) await application.on_tenant_switch(event) await asyncio.sleep(0) assert given_tenant is not {}
async def test_users_selection_modal_on_cancel(users_selection_modal): event = Event('Mouse', 'click') given_result = None async def mock_done(self, result): nonlocal given_result given_result = result users_selection_modal.done = MethodType(mock_done, users_selection_modal) await users_selection_modal.on_cancel(event) assert given_result == {'result': 'cancelled'}
async def test_tenant_provision_modal_on_buttons(tenant_provision_modal): event = Event('Mouse', 'click') given_result = None async def mock_done(self, result): nonlocal given_result given_result = result tenant_provision_modal.done = MethodType(mock_done, tenant_provision_modal) await tenant_provision_modal.on_cancel(event) assert given_result == {'result': 'cancelled'}
async def test_application_on_menu_click(application): def target(item_): class MockTarget: class parent: item = item_ return MockTarget() event = Event('Mouse', 'click') application.build() event.target = target({'tag': 'users'}) await application.on_menu_click(event) assert type(application.body.children[0]).__name__ == 'UsersScreen' event.target = target({'tag': 'roles'}) await application.on_menu_click(event) assert type(application.body.children[0]).__name__ == 'RolesScreen' event.target = target({'tag': 'dominions'}) await application.on_menu_click(event) assert type(application.body.children[0]).__name__ == 'DominionsScreen' event.target = target({'tag': 'other'}) await application.on_menu_click(event) await asyncio.sleep(0) assert type(application.body.children[0]).__name__ == 'DominionsScreen'
async def test_users_screen_on_search(users_screen): focus_called = False class MockSearch: text = 'value' def focus(self): nonlocal focus_called focus_called = True users_screen.search = MockSearch() await users_screen.on_search(Event('Keyboard', 'keydown', key='value')) assert users_screen.domain == [] await users_screen.on_search(Event('Keyboard', 'keydown', key='\n')) await asyncio.sleep(0) assert focus_called is True assert users_screen.domain == [ '|', ('name', 'ilike', '%value%'), ('email', 'ilike', '%value%') ]
async def test_users_selection_modal_on_clear_all(users_selection_modal): users_selection_modal.build() users_selection_modal.chosen_search.setup(content='value').connect() item_1 = {'id': '1', 'usename': 'Dummy 1'} item_2 = {'id': '2', 'usename': 'Dummy 2'} users_selection_modal.chosen.data = [item_1, item_2] await users_selection_modal.on_clear_all(Event('Mouse', 'click')) await asyncio.sleep(0) assert users_selection_modal.chosen.data == [] assert users_selection_modal.available.data == [item_1, item_2]
async def test_dominions_modal_on_body(dominions_modal): given_details = None async def mock_done(self, details): nonlocal given_details given_details = details dominions_modal.connect() dominions_modal.done = MethodType(mock_done, dominions_modal) def target(item_): class MockTarget: class parent: item = item_ return MockTarget() event = Event('Mouse', 'click') event.target = target({'id': '1', 'name': 'Dominion'}) await dominions_modal.on_body(event) await asyncio.sleep(0) assert given_details == {'id': '1', 'name': 'Dominion'}
async def test_restriction_details_modal_on_buttons(restriction_details_modal): event = Event('Mouse', 'click') given_result = None async def mock_done(self, result): nonlocal given_result given_result = result restriction_details_modal.done = MethodType(mock_done, restriction_details_modal) await restriction_details_modal.on_cancel(event) assert given_result == {'result': 'cancelled'} await restriction_details_modal.on_delete(event) assert given_result == {'result': 'deleted'}
async def test_users_selection_modal_on_save(users_selection_modal): event = Event('Mouse', 'click') given_ranking_ids = None async def deassign_role(self, ranking_ids): nonlocal given_ranking_ids given_ranking_ids = ranking_ids users_selection_modal.build() users_selection_modal.management_manager.deassign_role = MethodType( deassign_role, users_selection_modal) await users_selection_modal.on_save(event) await asyncio.sleep(0) assert given_ranking_ids == []
async def test_dominions_details_modal_on_save(dominion_details_modal): event = Event('Mouse', 'click') given_dominions = None async def mock_update(self, dominions): nonlocal given_dominions given_dominions = dominions dominion_details_modal.build() dominion_details_modal.management_manager.create_dominion = MethodType( mock_update, dominion_details_modal) await dominion_details_modal.on_save(event) await asyncio.sleep(0) assert given_dominions == [{'id': '1', 'name': 'Proser'}]