Repository navigation
Disable the server-level pgTimeTable Chain create menu when pg_timetable is not available. #10694 #10739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dpage
wants to merge
5
commits into
pgadmin-org:master
Choose a base branch
from
dpage:fix/issue-10694-pgt-chain-menu
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Disable the server-level pgTimeTable Chain create menu when pg_timetable is not available. #10694 #10739
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb530ce
Disable the server-level pgTimeTable Chain create menu when pg_timeta…
dpage 03bf45b
Read the pg_timetable access check result by column name, as the driv…
dpage 2452e66
Require schema USAGE in the pg_timetable access check, and refresh th…
dpage 9573104
Add docstrings to the new pg_timetable test and menu helper.
dpage a53bfee
Mention schema USAGE in the pg_timetable not-available error.
dpage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
web/pgadmin/browser/server_groups/servers/pg_timetable/tests/test_pgt_chain_not_available.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| ########################################################################## | ||
| # | ||
| # pgAdmin 4 - PostgreSQL Tools | ||
| # | ||
| # Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| # This software is released under the PostgreSQL Licence | ||
| # | ||
| ########################################################################## | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| from pgadmin.utils.route import BaseTestGenerator | ||
| from regression.python_test_utils import test_utils as utils | ||
| from . import utils as pgt_utils | ||
|
|
||
| NOT_AVAILABLE_MSG = ( | ||
| "The pg_timetable schema was not found in the maintenance database, " | ||
| "or the current role does not have USAGE on it and INSERT, SELECT " | ||
| "and UPDATE privileges on timetable.chain." | ||
| ) | ||
| ACCESS_CHECK = \ | ||
| 'pgadmin.browser.server_groups.servers.pg_timetable.has_pgt_chain_access' | ||
|
|
||
|
|
||
| class PgtChainNotAvailableTestCase(BaseTestGenerator): | ||
| """ | ||
| Test that the pgTimetable chain API returns a clear error, rather than a | ||
| raw SQL error and HTTP 500, when pg_timetable is not usable in the | ||
| server's maintenance database. | ||
| """ | ||
| scenarios = [ | ||
| ('Create chain: pg_timetable not available', dict( | ||
| api='create', mock_return=(True, False), | ||
| expected_data={'status_code': 400, | ||
| 'error_msg': NOT_AVAILABLE_MSG})), | ||
| ('List chains: pg_timetable not available', dict( | ||
| api='list', mock_return=(True, False), | ||
| expected_data={'status_code': 400, | ||
| 'error_msg': NOT_AVAILABLE_MSG})), | ||
| ('List chains: access check fails', dict( | ||
| api='list', mock_return=(False, 'Mocked Internal Server Error'), | ||
| expected_data={'status_code': 500, | ||
| 'error_msg': 'Mocked Internal Server Error'})), | ||
| ('List chains: pg_timetable not installed (unmocked)', dict( | ||
| api='list', mock_return=None, | ||
| expected_data={'status_code': 400, | ||
| 'error_msg': NOT_AVAILABLE_MSG})), | ||
| ] | ||
|
|
||
| def setUp(self): | ||
| """Connect to the server and set up the request data""" | ||
| super().setUp() | ||
| self.url = '/browser/pgt_chain/obj/' | ||
| self.data = {'chain_name': 'test_chain_not_available', 'live': False} | ||
|
|
||
| flag, msg = pgt_utils.is_valid_server_to_run_pgtimetable(self) | ||
| if not flag: | ||
| self.skipTest(msg) | ||
|
|
||
| if self.mock_return is None: | ||
| installed, _ = pgt_utils.is_pgtimetable_installed_on_server(self) | ||
| if installed: | ||
| self.skipTest('pg_timetable is installed on the server.') | ||
|
|
||
| def _call_api(self): | ||
| """Call the chain API selected by the scenario""" | ||
| if self.api == 'create': | ||
| return pgt_utils.api_create(self) | ||
| return pgt_utils.api_get(self, '') | ||
|
|
||
| def runTest(self): | ||
| """Check the error returned when pg_timetable is not available""" | ||
| if self.mock_return is None: | ||
| response = self._call_api() | ||
| else: | ||
| with patch(ACCESS_CHECK, return_value=self.mock_return): | ||
| response = self._call_api() | ||
|
|
||
| utils.assert_status_code(self, response) | ||
| utils.assert_error_message(self, response) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| ///////////////////////////////////////////////////////////// | ||
| // | ||
| // pgAdmin 4 - PostgreSQL Tools | ||
| // | ||
| // Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| // This software is released under the PostgreSQL Licence | ||
| // | ||
| ////////////////////////////////////////////////////////////// | ||
|
|
||
| jest.mock('../../../pgadmin/browser/server_groups/servers/pg_timetable/static/js/pgt_chain.ui', () => ({ | ||
| __esModule: true, default: jest.fn(), | ||
| })); | ||
| jest.mock('../../../pgadmin/browser/server_groups/servers/pg_timetable/tasks/static/js/pgt_chaintask.ui', () => ({ | ||
| getNodePgtChainTaskSchema: jest.fn(), | ||
| })); | ||
| jest.mock('../../../pgadmin/static/js/api_instance', () => ({ | ||
| __esModule: true, default: jest.fn(), | ||
| })); | ||
|
|
||
| const pgBrowser = { | ||
| Nodes: {}, | ||
| Node: {extend: (props) => props}, | ||
| Collection: {extend: (props) => props}, | ||
| Events: {on: jest.fn()}, | ||
| add_menus: jest.fn(), | ||
| enable_disable_menus: jest.fn(), | ||
| }; | ||
|
|
||
| // The node module registers itself through an AMD style define(); load it | ||
| // with a stub that hands the factory our fake browser. | ||
| global.define = (_name, _deps, factory) => { | ||
| factory((s) => s, () => '', pgBrowser); | ||
| }; | ||
| require('../../../pgadmin/browser/server_groups/servers/pg_timetable/static/js/pgt_chain'); | ||
| delete global.define; | ||
|
|
||
| describe('pgTimeTable Chain create menu on the server', () => { | ||
| const serverMenu = pgBrowser.add_menus.mock.calls[0][0].find( | ||
| (m) => m.name === 'create_pgt_chain_on_server' | ||
| ); | ||
| const addedHandler = pgBrowser.Events.on.mock.calls.find( | ||
| (c) => c[0] === 'pgadmin-browser:tree:added' | ||
| )[1]; | ||
|
|
||
| const setChildren = (types) => { | ||
| const children = types.map((t) => ({_type: t})); | ||
| pgBrowser.tree = { | ||
| children: jest.fn(() => children), | ||
| itemData: (child) => child, | ||
| }; | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| pgBrowser.enable_disable_menus.mockClear(); | ||
| }); | ||
|
|
||
| it('is enabled when the server has a pgTimeTable Chains collection', () => { | ||
| setChildren(['coll-database', 'coll-pgt_chain']); | ||
| expect(serverMenu.enable({connected: true}, {})).toBe(true); | ||
| }); | ||
|
|
||
| it('is disabled when the collection is not present', () => { | ||
| setChildren(['coll-database', 'coll-role']); | ||
| expect(serverMenu.enable({connected: true}, {})).toBe(false); | ||
| }); | ||
|
|
||
| it('is disabled when the server children are not loaded', () => { | ||
| pgBrowser.tree = {children: jest.fn(() => undefined), itemData: jest.fn()}; | ||
| expect(serverMenu.enable({connected: true}, {})).toBe(false); | ||
| }); | ||
|
|
||
| it('is disabled when the server is not connected', () => { | ||
| setChildren(['coll-pgt_chain']); | ||
| expect(serverMenu.enable({connected: false}, {})).toBe(false); | ||
| }); | ||
|
|
||
| it('refreshes the menus when the collection is added to the tree', () => { | ||
| addedHandler({}, {_type: 'coll-pgt_chain'}); | ||
| expect(pgBrowser.enable_disable_menus).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not refresh the menus for other nodes', () => { | ||
| addedHandler({}, {_type: 'coll-database'}); | ||
| addedHandler({}, undefined); | ||
| expect(pgBrowser.enable_disable_menus).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.