-
Notifications
You must be signed in to change notification settings - Fork 16.8k
feat(SIP-85): OAuth2 for databases #27631
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
adf177c
feat(SIP-85): OAuth2 for databases
betodealmeida 38e2bf8
Add more tests
betodealmeida d0a3fb3
Add KV lock for refreshing tokens
betodealmeida 1c3f841
Make lock generic
betodealmeida 9a4aa20
Refactor JWT encode/decode
betodealmeida 66ec9cb
Use DAO, Marshmallow schema, and cascade deletes
betodealmeida ee41e59
Bump shillelagh
betodealmeida d61018e
Fix typo
betodealmeida 3f484bb
Improve docstring
betodealmeida 421d5f7
Add dep for test
betodealmeida 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
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
170 changes: 170 additions & 0 deletions
170
superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.test.tsx
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,170 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import * as reduxHooks from 'react-redux'; | ||
| import { Provider } from 'react-redux'; | ||
| import { createStore } from 'redux'; | ||
| import { render, fireEvent, waitFor } from '@testing-library/react'; | ||
| import '@testing-library/jest-dom'; | ||
| import { ThemeProvider, supersetTheme } from '@superset-ui/core'; | ||
| import OAuth2RedirectMessage from 'src/components/ErrorMessage/OAuth2RedirectMessage'; | ||
| import { | ||
| ErrorLevel, | ||
| ErrorSource, | ||
| ErrorTypeEnum, | ||
| } from 'src/components/ErrorMessage/types'; | ||
| import { reRunQuery } from 'src/SqlLab/actions/sqlLab'; | ||
| import { triggerQuery } from 'src/components/Chart/chartAction'; | ||
| import { onRefresh } from 'src/dashboard/actions/dashboardState'; | ||
|
|
||
| // Mock the Redux store | ||
| const mockStore = createStore(() => ({ | ||
| sqlLab: { | ||
| queries: { 'query-id': { sql: 'SELECT * FROM table' } }, | ||
| queryEditors: [{ id: 'editor-id', latestQueryId: 'query-id' }], | ||
| tabHistory: ['editor-id'], | ||
| }, | ||
| explore: { | ||
| slice: { slice_id: 123 }, | ||
| }, | ||
| charts: { '1': {}, '2': {} }, | ||
| dashboardInfo: { id: 'dashboard-id' }, | ||
| })); | ||
|
|
||
| // Mock actions | ||
| jest.mock('src/SqlLab/actions/sqlLab', () => ({ | ||
| reRunQuery: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('src/components/Chart/chartAction', () => ({ | ||
| triggerQuery: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('src/dashboard/actions/dashboardState', () => ({ | ||
| onRefresh: jest.fn(), | ||
| })); | ||
|
|
||
| // Mock useDispatch | ||
| const mockDispatch = jest.fn(); | ||
| jest.spyOn(reduxHooks, 'useDispatch').mockReturnValue(mockDispatch); | ||
|
|
||
| // Mock global window functions | ||
| const mockOpen = jest.spyOn(window, 'open').mockImplementation(() => null); | ||
| const mockAddEventListener = jest.spyOn(window, 'addEventListener'); | ||
| const mockRemoveEventListener = jest.spyOn(window, 'removeEventListener'); | ||
|
|
||
| // Mock window.postMessage | ||
| const originalPostMessage = window.postMessage; | ||
|
|
||
| beforeEach(() => { | ||
| window.postMessage = jest.fn(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| window.postMessage = originalPostMessage; | ||
| }); | ||
|
|
||
| function simulateMessageEvent(data: any, origin: string) { | ||
| const messageEvent = new MessageEvent('message', { data, origin }); | ||
| window.dispatchEvent(messageEvent); | ||
| } | ||
|
|
||
| const defaultProps = { | ||
| error: { | ||
| error_type: ErrorTypeEnum.OAUTH2_REDIRECT, | ||
| message: "You don't have permission to access the data.", | ||
| extra: { | ||
| url: 'https://example.com', | ||
| tab_id: 'tabId', | ||
| redirect_uri: 'https://redirect.example.com', | ||
| }, | ||
| level: 'warning' as ErrorLevel, | ||
| }, | ||
| source: 'sqllab' as ErrorSource, | ||
| }; | ||
|
|
||
| const setup = (overrides = {}) => ( | ||
| <ThemeProvider theme={supersetTheme}> | ||
| <Provider store={mockStore}> | ||
| <OAuth2RedirectMessage {...defaultProps} {...overrides} />; | ||
| </Provider> | ||
| </ThemeProvider> | ||
| ); | ||
|
|
||
| describe('OAuth2RedirectMessage Component', () => { | ||
| it('renders without crashing and displays the correct initial UI elements', () => { | ||
| const { getByText } = render(setup()); | ||
|
|
||
| expect(getByText(/Authorization needed/i)).toBeInTheDocument(); | ||
| expect(getByText(/provide authorization/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('opens a new window with the correct URL when the link is clicked', () => { | ||
| const { getByText } = render(setup()); | ||
|
|
||
| const linkElement = getByText(/provide authorization/i); | ||
| fireEvent.click(linkElement); | ||
|
|
||
| expect(mockOpen).toHaveBeenCalledWith('https://example.com', '_blank'); | ||
| }); | ||
|
|
||
| it('cleans up the message event listener on unmount', () => { | ||
| const { unmount } = render(setup()); | ||
|
|
||
| expect(mockAddEventListener).toHaveBeenCalled(); | ||
| unmount(); | ||
| expect(mockRemoveEventListener).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('dispatches reRunQuery action when a message with correct tab ID is received for SQL Lab', async () => { | ||
| render(setup()); | ||
|
|
||
| simulateMessageEvent({ tabId: 'tabId' }, 'https://redirect.example.com'); | ||
|
|
||
| await waitFor(() => { | ||
| expect(reRunQuery).toHaveBeenCalledWith({ sql: 'SELECT * FROM table' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('dispatches triggerQuery action for explore source upon receiving a correct message', async () => { | ||
| render(setup({ source: 'explore' })); | ||
|
|
||
| simulateMessageEvent({ tabId: 'tabId' }, 'https://redirect.example.com'); | ||
|
|
||
| await waitFor(() => { | ||
| expect(triggerQuery).toHaveBeenCalledWith(true, 123); | ||
| }); | ||
| }); | ||
|
|
||
| it('dispatches onRefresh action for dashboard source upon receiving a correct message', async () => { | ||
| render(setup({ source: 'dashboard' })); | ||
|
|
||
| simulateMessageEvent({ tabId: 'tabId' }, 'https://redirect.example.com'); | ||
|
|
||
| await waitFor(() => { | ||
| expect(onRefresh).toHaveBeenCalledWith( | ||
| ['1', '2'], | ||
| true, | ||
| 0, | ||
| 'dashboard-id', | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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.