|
| 1 | +import { test, expect } from "@chromatic-com/playwright"; |
| 2 | +import type { Page } from "@playwright/test"; |
| 3 | +import { loginAs, loginAsRandomUser } from "../utils/auth"; |
| 4 | + |
| 5 | +test.use({ storageState: "admin_auth.json" }); |
| 6 | + |
| 7 | +const SLACK_CLIENT_ID = process.env.SLACK_CLIENT_ID; |
| 8 | +const SLACK_CLIENT_SECRET = process.env.SLACK_CLIENT_SECRET; |
| 9 | + |
| 10 | +async function createFederatedSlackConnector(page: Page) { |
| 11 | + // Navigate to add connector page |
| 12 | + await page.goto("http://localhost:3000/admin/add-connector"); |
| 13 | + await page.waitForLoadState("networkidle"); |
| 14 | + |
| 15 | + // Click on Slack connector tile (specifically the one with "Logo Slack" text, not "Slack Bots") |
| 16 | + await page.getByRole("link", { name: "Logo Slack" }).first().click(); |
| 17 | + await page.waitForLoadState("networkidle"); |
| 18 | + |
| 19 | + if (!SLACK_CLIENT_ID || !SLACK_CLIENT_SECRET) { |
| 20 | + throw new Error("SLACK_CLIENT_ID and SLACK_CLIENT_SECRET must be set"); |
| 21 | + } |
| 22 | + |
| 23 | + // Fill in the client ID and client secret |
| 24 | + await page.getByLabel(/client id/i).fill(SLACK_CLIENT_ID); |
| 25 | + await page.getByLabel(/client secret/i).fill(SLACK_CLIENT_SECRET); |
| 26 | + |
| 27 | + // Submit the form to create or update the federated connector |
| 28 | + const createOrUpdateButton = await page.getByRole("button", { |
| 29 | + name: /create|update/i, |
| 30 | + }); |
| 31 | + await createOrUpdateButton.click(); |
| 32 | + |
| 33 | + // Wait for success message or redirect |
| 34 | + await page.waitForTimeout(2000); |
| 35 | +} |
| 36 | + |
| 37 | +async function navigateToUserSettings(page: Page) { |
| 38 | + // Wait for any existing modals to close |
| 39 | + await page.waitForTimeout(1000); |
| 40 | + |
| 41 | + // Wait for potential modal backdrop to disappear |
| 42 | + await page |
| 43 | + .waitForSelector(".fixed.inset-0.bg-neutral-950\\/50", { |
| 44 | + state: "detached", |
| 45 | + timeout: 5000, |
| 46 | + }) |
| 47 | + .catch(() => {}); |
| 48 | + |
| 49 | + // Click on user dropdown/settings button |
| 50 | + await page.locator("#onyx-user-dropdown").click(); |
| 51 | + |
| 52 | + // Click on settings option |
| 53 | + await page.getByText("User Settings").click(); |
| 54 | + |
| 55 | + // Wait for settings modal to appear |
| 56 | + await expect(page.locator("h2", { hasText: "User Settings" })).toBeVisible(); |
| 57 | +} |
| 58 | + |
| 59 | +async function openConnectorsTab(page: Page) { |
| 60 | + // Click on the Connectors tab in user settings |
| 61 | + await page.getByRole("button", { name: "Connectors" }).click(); |
| 62 | + |
| 63 | + // Wait for connectors section to be visible |
| 64 | + // Allow multiple instances of "Connected Services" to be visible |
| 65 | + const connectedServicesLocators = page.getByText("Connected Services"); |
| 66 | + await expect(connectedServicesLocators.first()).toBeVisible(); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Cleanup function to delete the federated Slack connector from the admin panel |
| 71 | + * This ensures test isolation by removing any test data created during the test |
| 72 | + */ |
| 73 | +async function deleteFederatedSlackConnector(page: Page) { |
| 74 | + // Navigate to admin indexing status page |
| 75 | + await page.goto("http://localhost:3000/admin/indexing/status"); |
| 76 | + await page.waitForLoadState("networkidle"); |
| 77 | + |
| 78 | + // Expand the Slack section first (summary row toggles open on click) |
| 79 | + const slackSummaryRow = page.locator("tr").filter({ |
| 80 | + has: page.locator("text=/^\s*Slack\s*$/i"), |
| 81 | + }); |
| 82 | + if ((await slackSummaryRow.count()) > 0) { |
| 83 | + await slackSummaryRow.first().click(); |
| 84 | + // Wait a moment for rows to render |
| 85 | + await page.waitForTimeout(500); |
| 86 | + } |
| 87 | + |
| 88 | + // Look for the Slack federated connector row inside the expanded section |
| 89 | + // The federated connectors have a "Federated Access" badge |
| 90 | + const slackRow = page.locator("tr", { hasText: /federated access/i }); |
| 91 | + |
| 92 | + // Check if the connector exists |
| 93 | + const rowCount = await slackRow.count(); |
| 94 | + if (rowCount === 0) { |
| 95 | + // No federated Slack connector found, nothing to delete |
| 96 | + console.log("No federated Slack connector found to delete"); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + // Click on the row to navigate to the detail page |
| 101 | + await slackRow.first().click(); |
| 102 | + await page.waitForLoadState("networkidle"); |
| 103 | + |
| 104 | + // Look for and click the delete button |
| 105 | + // Open the Manage menu and click Delete |
| 106 | + const manageButton = page.getByRole("button", { name: /manage/i }); |
| 107 | + await manageButton |
| 108 | + .waitFor({ state: "visible", timeout: 5000 }) |
| 109 | + .catch(() => {}); |
| 110 | + if (!(await manageButton.isVisible().catch(() => false))) { |
| 111 | + console.log("Manage button not visible; skipping delete"); |
| 112 | + return; |
| 113 | + } |
| 114 | + await manageButton.click(); |
| 115 | + // Wait for the dropdown menu to appear and settle (Radix animation) |
| 116 | + await page |
| 117 | + .getByRole("menu") |
| 118 | + .waitFor({ state: "visible", timeout: 3000 }) |
| 119 | + .catch(() => {}); |
| 120 | + await page.waitForTimeout(150); |
| 121 | + |
| 122 | + page.once("dialog", (dialog) => dialog.accept()); |
| 123 | + const deleteMenuItem = page.getByRole("menuitem", { name: /^Delete$/ }); |
| 124 | + await expect(deleteMenuItem).toBeVisible({ timeout: 5000 }); |
| 125 | + await deleteMenuItem.click({ force: true }); |
| 126 | + // Wait for deletion to complete and redirect |
| 127 | + await page.waitForURL("**/admin/indexing/status*", { timeout: 15000 }); |
| 128 | + await page.waitForLoadState("networkidle"); |
| 129 | +} |
| 130 | + |
| 131 | +test("Federated Slack Connector - Create, OAuth Modal, and User Settings Flow", async ({ |
| 132 | + page, |
| 133 | +}) => { |
| 134 | + try { |
| 135 | + // Setup: Clear cookies and log in as admin |
| 136 | + await page.context().clearCookies(); |
| 137 | + await loginAs(page, "admin"); |
| 138 | + |
| 139 | + // Create a federated Slack connector in admin panel |
| 140 | + await createFederatedSlackConnector(page); |
| 141 | + |
| 142 | + // Log in as a random user |
| 143 | + await page.context().clearCookies(); |
| 144 | + await loginAsRandomUser(page); |
| 145 | + |
| 146 | + // Navigate back to main page and verify OAuth modal appears |
| 147 | + await page.goto("http://localhost:3000/chat"); |
| 148 | + await page.waitForLoadState("networkidle"); |
| 149 | + |
| 150 | + // Check if the OAuth modal appears |
| 151 | + await expect( |
| 152 | + page.getByText(/improve answer quality by letting/i) |
| 153 | + ).toBeVisible({ timeout: 10000 }); |
| 154 | + await expect(page.getByText(/slack/i)).toBeVisible(); |
| 155 | + |
| 156 | + // Decline the OAuth connection |
| 157 | + await page.getByRole("button", { name: "Skip for now" }).click(); |
| 158 | + |
| 159 | + // Wait for modal to disappear |
| 160 | + await expect( |
| 161 | + page.getByText(/improve answer quality by letting/i) |
| 162 | + ).not.toBeVisible(); |
| 163 | + |
| 164 | + // Go to user settings and verify the connector appears |
| 165 | + await navigateToUserSettings(page); |
| 166 | + await openConnectorsTab(page); |
| 167 | + |
| 168 | + // Verify Slack connector appears in the federated connectors section |
| 169 | + await expect(page.getByText("Federated Connectors")).toBeVisible(); |
| 170 | + await expect(page.getByText("Slack")).toBeVisible(); |
| 171 | + await expect(page.getByText("Not connected")).toBeVisible(); |
| 172 | + |
| 173 | + // Verify there's a Connect button available |
| 174 | + await expect( |
| 175 | + page.locator("button", { hasText: /^Connect$/ }) |
| 176 | + ).toBeVisible(); |
| 177 | + } finally { |
| 178 | + // Cleanup: Delete the federated Slack connector |
| 179 | + // Log back in as admin to delete the connector |
| 180 | + await page.context().clearCookies(); |
| 181 | + await loginAs(page, "admin"); |
| 182 | + await deleteFederatedSlackConnector(page); |
| 183 | + } |
| 184 | +}); |
0 commit comments