Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 7 additions & 5 deletions src/entrypoints/app/routes/auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
fetchUserProfile,
hasAllowedEmailDomain,
} from '../../../../services/auth'
import { resolveUrl } from '../../../../shared/base-path'
import { getBasePath, resolveUrl } from '../../../../shared/base-path'
import {
AccessDeniedPage,
AccessPendingPage,
Expand Down Expand Up @@ -254,15 +254,17 @@ export function createAuthRoutes(
}
accessStore.requestAccess(user.login)

// Fire notification (best-effort)
// Fire notification (best-effort). Access lists are per-deployment, so
// name the branch and link to that deployment's admin page.
try {
const { notifyEvent } = await import('../../../../services/notifications')
const deployment = getBasePath().replace(/^\/|\/$/g, '') || 'root'
await notifyEvent({
type: 'access.requested',
title: `Access requested by @${user.login}`,
title: `Access requested by @${user.login} on ${deployment}`,
status: 'info',
details: `${user.name} (${user.login}) requested access to Forms Lab`,
url: 'https://forms.labs.flexion.us/admin/users',
details: `${user.name} (${user.login}) requested access to Forms Lab (deployment: ${deployment})`,
url: `${getExternalOrigin(c)}${resolveUrl('/admin/users')}`,
})
} catch {
// Notification failure should not block the request
Expand Down
45 changes: 44 additions & 1 deletion test/auth-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Hono } from 'hono'
import { sessionReader } from '../src/entrypoints/app/middleware/auth'
import { createAuthRoutes } from '../src/entrypoints/app/routes/auth'
import type { AccessStore, GitHubUser, UserStore } from '../src/services/auth'
import { COOKIE_NAME } from '../src/services/auth'
import { COOKIE_NAME, encryptSession } from '../src/services/auth'

describe('Auth Routes', () => {
let app: Hono
Expand Down Expand Up @@ -247,4 +247,47 @@ describe('Auth Routes', () => {
expect(cookie).toContain('Max-Age=0')
})
})

describe('POST /auth/request-access', () => {
async function postRequestAccess(): Promise<Record<string, unknown>> {
let notified: Record<string, unknown> | undefined
global.fetch = mock(async (_url: string | URL, init?: RequestInit) => {
notified = JSON.parse(String(init?.body))
return new Response('{}', { status: 200 })
}) as unknown as typeof fetch

const cookie = await encryptSession(
{ login: 'outsider', name: 'Out Sider', avatarUrl: '' },
process.env.SESSION_SECRET as string,
)
const res = await app.request('/auth/request-access', {
method: 'POST',
headers: {
Cookie: `${COOKIE_NAME}=${cookie}`,
'x-forwarded-proto': 'https',
'x-forwarded-host': 'forms.example.test',
},
})
expect(res.status).toBe(302)
expect(mockAccessStore.requestAccess).toHaveBeenCalledWith('outsider')
if (!notified) throw new Error('no notification sent')
return notified
}

it('links to the admin page of the branch the request came from', async () => {
process.env.BASE_PATH = '/story-7-thing'
const event = await postRequestAccess()
expect(event.url).toBe(
'https://forms.example.test/story-7-thing/admin/users',
)
expect(String(event.title)).toContain('story-7-thing')
expect(String(event.details)).toContain('story-7-thing')
})

it('links to the root admin page when there is no base path', async () => {
delete process.env.BASE_PATH
const event = await postRequestAccess()
expect(event.url).toBe('https://forms.example.test/admin/users')
})
})
})
Loading