-
Notifications
You must be signed in to change notification settings - Fork 15
Remove a user from a consultation #1037
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
13 commits
Select commit
Hold shift + click to select a range
e1efc5e
Add form, endpoint, tests and middleware intercept for removing a use…
e94e56b
Formatting
f4df69a
Change format of user_id
73ed222
Removed some 404 checks from getting the user object
e2a1e48
Change beforeEach to afterEach
55b7c88
Use routes for redirect after adding and removing a user from a consu…
d47103f
Remove trailing slash from support consultations redirect
22f59a6
Attempt to fix backend tests after url fix
3c93816
Changed from django var:val to regex url match
80ebc55
Changed from django var:val to regex url match
a150757
Remove unused import
4c596b4
Add IsAdminUser checks to adding and removing users from consultation…
6a02e65
Remove hasdashboardaccess permission class from add and remove users …
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
Some comments aren't visible on the classic Files Changed page.
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
127 changes: 127 additions & 0 deletions
127
frontend/src/components/screens/AddUserToConsultationForm.test.ts
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,127 @@ | ||
| import { describe, expect, it, vi, afterEach } from "vitest"; | ||
| import { render, screen, fireEvent } from "@testing-library/svelte"; | ||
| import AddUserToConsultationForm from "./AddUserToConsultationForm.svelte"; | ||
| import type { User } from "../../global/types"; | ||
|
|
||
| // Mock fetch | ||
| const mockFetch = vi.fn(); | ||
| global.fetch = mockFetch; | ||
|
|
||
| describe("AddUserToConsultationForm", () => { | ||
| const mockUsers: User[] = [ | ||
| { | ||
| id: 1, | ||
| email: "[email protected]", | ||
| is_staff: false, | ||
| has_dashboard_access: false, | ||
| created_at: "2023-01-01T00:00:00Z", | ||
| }, | ||
| { | ||
| id: 2, | ||
| email: "[email protected]", | ||
| is_staff: true, | ||
| has_dashboard_access: true, | ||
| created_at: "2023-01-01T00:00:00Z", | ||
| }, | ||
| ]; | ||
|
|
||
| const consultationId = "test-consultation-123"; | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("should render with users list", () => { | ||
| render(AddUserToConsultationForm, { | ||
| users: mockUsers, | ||
| consultationId, | ||
| }); | ||
|
|
||
| expect(screen.getByText("[email protected]")).toBeTruthy(); | ||
| expect(screen.getByText("[email protected]")).toBeTruthy(); | ||
| expect(screen.getByText("Add user(s)")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("should show error when no users selected", async () => { | ||
| render(AddUserToConsultationForm, { | ||
| users: mockUsers, | ||
| consultationId, | ||
| }); | ||
|
|
||
| const submitButton = screen.getByText("Add user(s)"); | ||
| await fireEvent.click(submitButton); | ||
|
|
||
| expect(screen.getByText("Please select a user to add")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("should submit selected users successfully", async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: () => | ||
| Promise.resolve({ | ||
| message: "Successfully added 1 users to consultation", | ||
| }), | ||
| }); | ||
|
|
||
| // Mock window.location.href | ||
| Object.defineProperty(window, "location", { | ||
| value: { href: "" }, | ||
| writable: true, | ||
| }); | ||
|
|
||
| render(AddUserToConsultationForm, { | ||
| users: mockUsers, | ||
| consultationId, | ||
| }); | ||
|
|
||
| // Select first user | ||
| const checkbox = screen.getByLabelText("[email protected]"); | ||
| await fireEvent.click(checkbox); | ||
|
|
||
| // Submit form | ||
| const submitButton = screen.getByText("Add user(s)"); | ||
| await fireEvent.click(submitButton); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `/api/consultations/${consultationId}/add-users/`, | ||
| expect.objectContaining({ | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ user_ids: ["1"] }), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("should handle API errors", async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 400, | ||
| }); | ||
|
|
||
| render(AddUserToConsultationForm, { | ||
| users: mockUsers, | ||
| consultationId, | ||
| }); | ||
|
|
||
| // Select first user | ||
| const checkbox = screen.getByLabelText("[email protected]"); | ||
| await fireEvent.click(checkbox); | ||
|
|
||
| // Submit form | ||
| const submitButton = screen.getByText("Add user(s)"); | ||
| await fireEvent.click(submitButton); | ||
|
|
||
| expect(screen.getByText("Error: 400")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("should handle empty users list", () => { | ||
| render(AddUserToConsultationForm, { | ||
| users: [], | ||
| consultationId, | ||
| }); | ||
|
|
||
| expect(screen.getByText("Add user(s)")).toBeTruthy(); | ||
| // Should not show any checkboxes | ||
| expect(screen.queryByRole("checkbox")).toBeNull(); | ||
| }); | ||
| }); | ||
66 changes: 66 additions & 0 deletions
66
frontend/src/components/screens/RemoveUserFromConsultationForm.svelte
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,66 @@ | ||
| <script lang="ts"> | ||
| import clsx from "clsx"; | ||
|
|
||
| import { slide } from "svelte/transition"; | ||
|
|
||
| import { | ||
| getApiRemoveUserFromConsultation, | ||
| getSupportConsultationDetails, | ||
| } from "../../global/routes"; | ||
|
|
||
| import Button from "../inputs/Button/Button.svelte"; | ||
| import Text from "../Text/Text.svelte"; | ||
| import type { ConsultationResponse, User } from "../../global/types"; | ||
|
|
||
| let sending: boolean = false; | ||
| let errors: Record<string, string> = {}; | ||
|
|
||
| export let consultation: ConsultationResponse; | ||
| export let user: User; | ||
252afh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const handleSubmit = async () => { | ||
| errors = {}; | ||
| sending = true; | ||
| try { | ||
| const response = await fetch( | ||
| getApiRemoveUserFromConsultation(consultation.id, user.id.toString()), | ||
| { | ||
| method: "DELETE", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Error: ${response.status}`); | ||
| } | ||
| window.location.href = getSupportConsultationDetails(consultation.id); | ||
| } catch (err: unknown) { | ||
| errors["general"] = | ||
| err instanceof Error ? err.message : "An unknown error occurred"; | ||
| } finally { | ||
| sending = false; | ||
| } | ||
| }; | ||
| </script> | ||
|
|
||
| <form class={clsx(["flex", "flex-col", "gap-4"])}> | ||
| {#if "general" in errors} | ||
| <small class="text-sm text-red-500" transition:slide={{ duration: 300 }}> | ||
| {errors.general} | ||
| </small> | ||
| {/if} | ||
| <Text | ||
| >Are you sure you want to remove <strong>{user.email}</strong> from | ||
| <strong>{consultation.title}</strong>?</Text | ||
| > | ||
| <Button | ||
| type="submit" | ||
| variant="primary" | ||
| handleClick={handleSubmit} | ||
| disabled={sending} | ||
| > | ||
| {sending ? "Removing..." : "Yes, remove them"} | ||
| </Button> | ||
| </form> | ||
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.