Skip to content
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

refactor(bulk_select): Fix bulk select tagging issues for users #31631

Merged
merged 23 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0702ac1
(refactor)Fix saved query access for SQL Lab users
LevisNgigi Dec 12, 2024
9125aba
Merge remote-tracking branch 'upstream/master'
LevisNgigi Dec 14, 2024
2bb93c9
Fix: Update SavedQuery filter to resolve access issues and failing tests
LevisNgigi Dec 14, 2024
45794f1
Fix: Update SavedQuery filter to resolve access issues and failing tests
LevisNgigi Dec 14, 2024
6717bb3
Fix: Update SavedQuery filter to resolve access issues and failing tests
LevisNgigi Dec 14, 2024
5732857
Fix: Update SavedQuery filter to resolve access issues and failing tests
LevisNgigi Dec 14, 2024
fcd21cd
refactor: modify savedquery filter logic
LevisNgigi Dec 15, 2024
072d145
refactor: modify savedquery filter logic
LevisNgigi Dec 15, 2024
5d2665f
refactor: modify savedquery filter logic
LevisNgigi Dec 15, 2024
7001353
fix:failing tests
LevisNgigi Dec 18, 2024
7647217
fix:failing tests
LevisNgigi Dec 18, 2024
79084f1
Fix tagging issue for users
LevisNgigi Dec 27, 2024
a56a7ab
Fix tagging issue for users
LevisNgigi Dec 27, 2024
8ac94eb
Fix failing unit test
LevisNgigi Dec 27, 2024
b83a238
Fix failing unit test
LevisNgigi Dec 27, 2024
5031def
fix(tests): add tests for bulk tagging
LevisNgigi Jan 7, 2025
72f2fa5
fix(tests): add tests for bulk tagging
LevisNgigi Jan 7, 2025
4a4f96f
refactor:fix bug that created new tag
LevisNgigi Jan 9, 2025
b7c870f
refactor:change logic for tag ownership check
LevisNgigi Jan 10, 2025
0b11ea8
refactor:change logic for tag ownership check
LevisNgigi Jan 10, 2025
6330af9
refactor:change logic for tag ownership check
LevisNgigi Jan 10, 2025
83358c6
trigger GA run
LevisNgigi Jan 10, 2025
5376c9f
trigger GA run
LevisNgigi Jan 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions superset-frontend/src/features/tags/BulkTagModal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* 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. 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 {
render,
screen,
fireEvent,
waitFor,
} from 'spec/helpers/testing-library';
import fetchMock from 'fetch-mock';
import BulkTagModal from './BulkTagModal';

const mockedProps = {
onHide: jest.fn(),
refreshData: jest.fn(),
addSuccessToast: jest.fn(),
addDangerToast: jest.fn(),
show: true,
selected: [
{ original: { id: 1, name: 'Dashboard 1' } },
{ original: { id: 2, name: 'Dashboard 2' } },
],
resourceName: 'dashboard',
};

describe('BulkTagModal', () => {
afterEach(() => {
fetchMock.reset();
jest.clearAllMocks();
});

test('should render', () => {
const { container } = render(<BulkTagModal {...mockedProps} />);
expect(container).toBeInTheDocument();
});

test('renders the correct title and message', () => {
render(<BulkTagModal {...mockedProps} />);
expect(
screen.getByText(/you are adding tags to 2 dashboards/i),
).toBeInTheDocument();
expect(screen.getByText('Bulk tag')).toBeInTheDocument();
});

test('renders tags input field', async () => {
render(<BulkTagModal {...mockedProps} />);
const tagsInput = await screen.findByRole('combobox', { name: /tags/i });
expect(tagsInput).toBeInTheDocument();
});

test('calls onHide when the Cancel button is clicked', () => {
render(<BulkTagModal {...mockedProps} />);
const cancelButton = screen.getByText('Cancel');
fireEvent.click(cancelButton);
expect(mockedProps.onHide).toHaveBeenCalled();
});

test('submits the selected tags and shows success toast', async () => {
fetchMock.post('glob:*/api/v1/tag/bulk_create', {
result: {
objects_tagged: [1, 2],
objects_skipped: [],
},
});

render(<BulkTagModal {...mockedProps} />);

const tagsInput = await screen.findByRole('combobox', { name: /tags/i });
fireEvent.change(tagsInput, { target: { value: 'Test Tag' } });
fireEvent.keyDown(tagsInput, { key: 'Enter', code: 'Enter' });

fireEvent.click(screen.getByText('Save'));

await waitFor(() => {
expect(mockedProps.addSuccessToast).toHaveBeenCalledWith(
'Tagged 2 dashboards',
);
});

expect(mockedProps.refreshData).toHaveBeenCalled();
expect(mockedProps.onHide).toHaveBeenCalled();
});

test('handles API errors gracefully', async () => {
fetchMock.post('glob:*/api/v1/tag/bulk_create', 500);

render(<BulkTagModal {...mockedProps} />);

const tagsInput = await screen.findByRole('combobox', { name: /tags/i });
fireEvent.change(tagsInput, { target: { value: 'Test Tag' } });
fireEvent.keyDown(tagsInput, { key: 'Enter', code: 'Enter' });

fireEvent.click(screen.getByText('Save'));

await waitFor(() => {
expect(mockedProps.addDangerToast).toHaveBeenCalledWith(
'Failed to tag items',
);
});
});
});
2 changes: 1 addition & 1 deletion superset-frontend/src/features/tags/BulkTagModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const BulkTagModal: FC<BulkTagModalProps> = ({
endpoint: `/api/v1/tag/bulk_create`,
jsonPayload: {
tags: tags.map(tag => ({
name: tag.value,
name: tag.label,
objects_to_tag: selected.map(item => [
resourceName,
+item.original.id,
Expand Down
37 changes: 21 additions & 16 deletions superset/commands/tag/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,32 @@
def validate(self) -> None:
exceptions = []
objects_to_tag = set(self._properties.get("objects_to_tag", []))

for obj_type, obj_id in objects_to_tag:
object_type = to_object_type(obj_type)

# Validate object type
for obj_type, obj_id in objects_to_tag:
object_type = to_object_type(obj_type)

if not object_type:
exceptions.append(
TagInvalidError(f"invalid object type {object_type}")
)
try:
if model := to_object_model(object_type, obj_id): # type: ignore
security_manager.raise_for_ownership(model)
except SupersetSecurityException:
# skip the object if the user doesn't have access
self._skipped_tagged_objects.add((obj_type, obj_id))
if not object_type:
exceptions.append(TagInvalidError(f"invalid object type {object_type}"))
continue

Check warning on line 98 in superset/commands/tag/create.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/tag/create.py#L97-L98

Added lines #L97 - L98 were not covered by tests

self._properties["objects_to_tag"] = (
set(objects_to_tag) - self._skipped_tagged_objects
)
try:
if model := to_object_model(object_type, obj_id):
try:
security_manager.raise_for_ownership(model)
except SupersetSecurityException:
if (
not model.created_by
or model.created_by != security_manager.current_user
):
# skip the object if the user doesn't have access
self._skipped_tagged_objects.add((obj_type, obj_id))
except Exception as e:
exceptions.append(TagInvalidError(str(e)))

Check warning on line 112 in superset/commands/tag/create.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/tag/create.py#L111-L112

Added lines #L111 - L112 were not covered by tests

self._properties["objects_to_tag"] = (
set(objects_to_tag) - self._skipped_tagged_objects
)

if exceptions:
raise TagInvalidError(exceptions=exceptions)
Loading