def _has_permission(user, wsid, upa, perm, expected_wsid, public=False): wsc = create_autospec(Workspace, spec_set=True, instance=True) ws = WS(wsc) wsc.administer.assert_called_once_with({'command': 'listModRequests'}) retperms = {'perms': [{'a': 'w', 'b': 'r', 'c': 'a'}]} if public: retperms['perms'][0]['*'] = 'r' if wsid: wsc.administer.return_value = retperms else: wsc.administer.side_effect = [retperms, {'infos': [['objinfo goes here']]}] ws.has_permission(user, perm, wsid, upa) getperms = {'command': 'getPermissionsMass', 'params': {'workspaces': [{'id': expected_wsid}]}} if wsid: wsc.administer.assert_called_with(getperms) else: wsc.administer.assert_any_call(getperms) wsc.administer.assert_called_with({'command': 'getObjectInfo', 'params': {'objects': [{'ref': str(upa)}], 'ignoreErrors': 1}}) assert wsc.administer.call_count == 2 if wsid else 3
def _has_permission_fail_ws_exception(ws_exception, expected): wsc = create_autospec(Workspace, spec_set=True, instance=True) ws = WS(wsc) wsc.administer.assert_called_once_with({'command': 'listModRequests'}) wsc.administer.side_effect = ws_exception with raises(Exception) as got: ws.has_permission('foo', WorkspaceAccessType.READ, 22) assert_exception_correct(got.value, expected)
def test_has_permission_fail_on_get_info_server_error(): wsc = create_autospec(Workspace, spec_set=True, instance=True) ws = WS(wsc) wsc.administer.assert_called_once_with({'command': 'listModRequests'}) wsc.administer.side_effect = [ {'perms': [{'a': 'w', 'b': 'r', 'c': 'a'}]}, ServerError('JSONRPCError', -32500, 'Thanks Obama')] with raises(Exception) as got: ws.has_permission(UserID('b'), WorkspaceAccessType.READ, upa=UPA('67/8/90')) assert_exception_correct(got.value, ServerError('JSONRPCError', -32500, 'Thanks Obama'))
def test_has_permission_fail_no_object(): wsc = create_autospec(Workspace, spec_set=True, instance=True) ws = WS(wsc) wsc.administer.assert_called_once_with({'command': 'listModRequests'}) wsc.administer.side_effect = [ {'perms': [{'a': 'w', 'b': 'r', 'c': 'a'}]}, {'infos': [None]}] with raises(Exception) as got: ws.has_permission(UserID('b'), WorkspaceAccessType.READ, upa=UPA('67/8/90')) assert_exception_correct(got.value, NoSuchWorkspaceDataError('Object 67/8/90 does not exist'))