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 _get_user_workspaces_fail(user, expected): wsc = create_autospec(Workspace, spec_set=True, instance=True) ws = WS(wsc) wsc.administer.assert_called_once_with({'command': 'listModRequests'}) with raises(Exception) as got: ws.get_user_workspaces(user) assert_exception_correct(got.value, expected)
def test_get_user_workspaces_anonymous(): wsc = create_autospec(Workspace, spec_set=True, instance=True) ws = WS(wsc) wsc.administer.assert_called_once_with({'command': 'listModRequests'}) wsc.list_workspace_ids.return_value = {'workspaces': [], 'pub': [6, 7]} assert ws.get_user_workspaces(None) == [6, 7] wsc.list_workspace_ids.assert_called_once_with({'onlyGlobal': 1})
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'))
def _get_user_workspaces_anonymous_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.list_workspace_ids.side_effect = ws_exception wsc.administer.side_effect = ws_exception with raises(Exception) as got: ws.get_user_workspaces(None) assert_exception_correct(got.value, expected)
def _get_user_workspaces(workspaces, pub, expected): wsc = create_autospec(Workspace, spec_set=True, instance=True) ws = WS(wsc) wsc.administer.assert_called_once_with({'command': 'listModRequests'}) wsc.administer.return_value = {'workspaces': workspaces, 'pub': pub} assert ws.get_user_workspaces(UserID('usera')) == expected wsc.administer.assert_called_with({'command': 'listWorkspaceIDs', 'user': '******', 'params': {'perm': 'r', 'excludeGlobal': 0}}) assert wsc.administer.call_count == 2
def _init_fail(wsc, expected): with raises(Exception) as got: WS(wsc) assert_exception_correct(got.value, expected)