Skip to content

Commit

Permalink
/instances/:id looks up instance and redirects to project instance route
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed Feb 9, 2024
1 parent cfda163 commit 17149ab
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/pages/lookups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { redirect, type LoaderFunctionArgs } from 'react-router-dom'

import { apiQueryClient } from '@oxide/api'

import { trigger404 } from 'app/components/ErrorBoundary'
import { pb } from 'app/util/path-builder'

export async function instanceLookupLoader({ params }: LoaderFunctionArgs) {
try {
const instance = await apiQueryClient.fetchQuery('instanceView', {
path: { instance: params.instance! },
})
const project = await apiQueryClient.fetchQuery('projectView', {
path: { project: instance.projectId },
})
return redirect(pb.instance({ project: project.name, instance: instance.name }))
} catch (_e) {
throw trigger404
}
}
10 changes: 10 additions & 0 deletions app/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import DeviceAuthSuccessPage from './pages/DeviceAuthSuccessPage'
import DeviceAuthVerifyPage from './pages/DeviceAuthVerifyPage'
import { LoginPage } from './pages/LoginPage'
import { LoginPageSaml } from './pages/LoginPageSaml'
import { instanceLookupLoader } from './pages/lookups'
import {
DisksPage,
ImagesPage,
Expand Down Expand Up @@ -229,6 +230,15 @@ export const routes = createRoutesFromElements(
loader={SiloUtilizationPage.loader}
handle={{ crumb: 'Utilization' }}
/>

{/* let's do both. what could go wrong*/}
<Route
path="lookup/instances/:instance"
element={null}
loader={instanceLookupLoader}
/>
<Route path="lookup/i/:instance" element={null} loader={instanceLookupLoader} />

<Route loader={ProjectsPage.loader} element={<ProjectsPage />}>
<Route path="projects" handle={{ crumb: 'Projects' }} element={null} />
<Route
Expand Down
42 changes: 42 additions & 0 deletions app/test/e2e/lookup-routes.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { expect, test } from '@playwright/test'

test.describe('/lookup/i', () => {
test('404s on existing name', async ({ page }) => {
await page.goto('/lookup/i/db1')
await expect(page.getByText('Page not found')).toBeVisible()
})

test('404s on empty ID', async ({ page }) => {
await page.goto('/lookup/i/')
await expect(page.getByText('Page not found')).toBeVisible()
})

test('looks up instance by ID', async ({ page }) => {
await page.goto('/lookup/i/935499b3-fd96-432a-9c21-83a3dc1eece4')
await expect(page).toHaveURL('/projects/mock-project/instances/db1/storage')
})
})

test.describe('/lookup/instances', () => {
test('404s on existing name', async ({ page }) => {
await page.goto('/lookup/instances/db1')
await expect(page.getByText('Page not found')).toBeVisible()
})

test('404s on empty ID', async ({ page }) => {
await page.goto('/lookup/instances/')
await expect(page.getByText('Page not found')).toBeVisible()
})

test('looks up instance by ID', async ({ page }) => {
await page.goto('/lookup/instances/935499b3-fd96-432a-9c21-83a3dc1eece4')
await expect(page).toHaveURL('/projects/mock-project/instances/db1/storage')
})
})

0 comments on commit 17149ab

Please sign in to comment.