def test_update_web_apps_list(self): self.assertEqual([], UserRole.by_domain(self.linked_domain)) report_mapping = {'master_report_id': 'linked_report_id'} with patch('corehq.apps.linked_domain.updates.get_static_report_mapping', return_value=report_mapping): update_user_roles(self.domain_link) roles = UserRole.by_domain(self.linked_domain) self.assertEqual(1, len(roles)) self.assertEqual(roles[0].permissions.view_web_apps_list, [self.linked_app._id]) self.assertEqual(roles[0].permissions.view_report_list, [get_ucr_class_name('linked_report_id')])
def test_match_names(self): self.assertEqual([], UserRole.objects.get_by_domain(self.linked_domain)) # create role in linked domain with the same name but no 'upstream_id' UserRole.create(self.linked_domain, 'other_test', Permissions(edit_web_users=True, view_locations=True)) update_user_roles(self.domain_link) roles = { r.name: r for r in UserRole.objects.get_by_domain(self.linked_domain) } self.assertEqual(2, len(roles)) self.assertTrue(roles['other_test'].permissions.edit_web_users) self.assertEqual(roles['other_test'].upstream_id, self.other_role.get_id)
def test_update_report_list(self): self.assertEqual([], UserRole.by_domain(self.linked_domain)) report_mapping = {'master_report_id': 'linked_report_id'} with patch( 'corehq.apps.linked_domain.updates.get_static_report_mapping', return_value=report_mapping): update_user_roles(self.domain_link) roles = {r.name: r for r in UserRole.by_domain(self.linked_domain)} self.assertEqual(2, len(roles)) self.assertEqual(roles['test'].permissions.view_report_list, [get_ucr_class_name('linked_report_id')]) self.assertTrue(roles['test'].is_non_admin_editable) self.assertTrue(roles['other_test'].permissions.edit_web_users) self.assertEqual(roles['other_test'].assignable_by, [roles['test'].get_id])
def test_match_ids(self): self.assertEqual([], UserRole.by_domain(self.linked_domain)) role = UserRole(domain=self.linked_domain, name='id_test', permissions=Permissions( edit_web_users=False, view_locations=True, ), assignable_by=[self.role.get_id], upstream_id=self.other_role.get_id) role.save() update_user_roles(self.domain_link) roles = {r.name: r for r in UserRole.by_domain(self.linked_domain)} self.assertEqual(2, len(roles)) self.assertIsNotNone(roles.get('other_test')) self.assertTrue(roles['other_test'].permissions.edit_web_users) self.assertEqual(roles['other_test'].upstream_id, self.other_role.get_id)
def test_match_ids(self): self.assertEqual([], UserRole.objects.get_by_domain(self.linked_domain)) # create role in linked domain with upstream_id and name not matching upstream name UserRole.create(self.linked_domain, 'id_test', Permissions(edit_web_users=False, view_locations=True), upstream_id=self.other_role.get_id) update_user_roles(self.domain_link) roles = { r.name: r for r in UserRole.objects.get_by_domain(self.linked_domain) } self.assertEqual(2, len(roles), roles.keys()) self.assertIsNotNone(roles.get('other_test')) self.assertTrue(roles['other_test'].permissions.edit_web_users) self.assertEqual(roles['other_test'].upstream_id, self.other_role.get_id)
def test_update_remote(self, remote_get_user_roles): remote_permissions = Permissions( edit_data=False, edit_reports=True, view_report_list=['corehq.reports.static_report']) # sync with existing local role remote_role1 = self._make_remote_role_json( _id=self.upstream_role1_id, name="test", permissions=remote_permissions.to_json(), ) # create new role remote_role_other = self._make_remote_role_json( _id=uuid.uuid4().hex, name="another", permissions=Permissions().to_json(), assignable_by=[self.upstream_role1_id]) remote_get_user_roles.return_value = [ _clean_json(role) for role in [remote_role1, remote_role_other] ] update_user_roles(self.domain_link) roles = { r.name: r for r in UserRole.objects.get_by_domain(self.linked_domain) } self.assertEqual(3, len(roles)) self.assertEqual(roles['test'].permissions, remote_permissions) self.assertEqual(roles['test'].is_non_admin_editable, False) self.assertEqual(roles['another'].assignable_by, [self.role1.get_id]) self.assertEqual(roles['another'].permissions, Permissions()) self.assertEqual(roles['other_test'].assignable_by, [self.role1.get_id])