Skip to content
Open
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
6 changes: 5 additions & 1 deletion apps/web/e2e/tests/platform/keys-ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ test(
await api.post('/api/v1/boards', { data: { name: `Nope ${marker}` } }),
"Requires the 'board.manage' permission"
)
await expectJson(await api.get('/api/v1/help-center/articles'), 200)
// Help center reads need read:article, which this key does not hold.
await expectForbidden(
await api.get('/api/v1/help-center/articles'),
"This API key is missing the 'read:article' scope"
)
await api.dispose()
await owner.api.dispose()
}
Expand Down
6 changes: 5 additions & 1 deletion apps/web/e2e/tests/platform/keys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,12 +633,16 @@ test.describe('Scopes on the non-support resources', () => {
'/api/v1/tags',
'/api/v1/statuses',
'/api/v1/roadmaps',
'/api/v1/help-center/articles',
'/api/v1/status/summary',
]) {
const res = await chat.api.get(path)
expect(res.status(), `${path}: ${await res.text()}`).toBe(200)
}
// Help center reads need read:article, even for what is public.
await expectForbidden(
await chat.api.get('/api/v1/help-center/articles'),
"This API key is missing the 'read:article' scope"
)
await chat.api.dispose()
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ describe('HelpCenterArticleFeedback', () => {
expect(screen.getByRole('textbox')).toBeTruthy()
})

it('changes the vote already cast rather than casting a second one', async () => {
renderFeedback()

fireEvent.click(screen.getByRole('button', { name: /yes/i }))
await waitFor(() => expect(recordFeedback).toHaveBeenCalledTimes(1))
expect(recordFeedback.mock.calls[0][0]).toEqual({
data: { articleId: 'article_1', helpful: true },
})

await voteUnhelpful()
expect(recordFeedback.mock.calls[1][0]).toEqual({
data: {
articleId: 'article_1',
helpful: false,
replacesFeedbackId: 'kb_article_feedback_1',
},
})
})

it('sends the typed reason against the vote it explains', async () => {
renderFeedback()
await voteUnhelpful()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ export function HelpCenterArticleFeedback({
try {
// The vote lands on the click, before any typing: a visitor who never
// writes a word still counts, and the reason stays optional.
// Sending back the vote already cast turns a changed mind into a change
// of that vote, rather than a second vote, for a signed-out visitor too.
const result = await recordArticleFeedbackFn({
data: { articleId: articleId as KbArticleId, helpful },
data: {
articleId: articleId as KbArticleId,
helpful,
...(feedbackId ? { replacesFeedbackId: feedbackId } : {}),
},
})
setFeedback(newFeedback)
setFeedbackId((result.feedbackId as KbArticleFeedbackId) ?? null)
Expand Down
12 changes: 12 additions & 0 deletions apps/web/src/lib/server/domains/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ export function assertApiPermissions(
}
}

/**
* Whether a key's authority holds a permission: the resolved permission set
* grants it AND, for a scoped key, the key holds the scope it maps to. For a
* read that answers everyone but shows more to keys that hold the permission.
*/
export function apiKeyHolds(auth: ApiAuthContext, permission: PermissionKey): boolean {
return (
auth.permissions.has(permission) &&
hasApiScope(auth.apiKey.scopes, scopeForPermission(permission))
)
}

/**
* Assert a key holds a scope, for a read whose rows are authorized one by one
* rather than by a single permission. Legacy keys (NULL scopes) hold every
Expand Down
23 changes: 15 additions & 8 deletions apps/web/src/lib/server/domains/api/schemas/help-center.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,23 @@ const articleIdParam = {
} as const

const MANAGE = 'Requires the help_center.manage permission.'
const PUBLIC_READ =
'Requires the read:article scope. A key reads what is published to everyone; drafts, private categories and segment-gated content need the help_center.manage permission.'

registerPath('/help-center/categories', {
get: {
tags: ['Help Center'],
summary: 'List help center categories',
description: 'Returns every category with its article count.',
description:
'Requires the read:article scope. Returns every category with its article count. A key without the help_center.manage permission gets the public categories only, counted by the articles everyone can open.',
responses: {
200: {
description: 'List of categories',
content: {
'application/json': { schema: z.object({ data: z.array(CategoryWithCountSchema) }) },
},
},
...errorResponses(),
...errorResponses(403),
...featureOff,
},
},
Expand Down Expand Up @@ -173,6 +176,7 @@ registerPath('/help-center/categories/{categoryId}', {
get: {
tags: ['Help Center'],
summary: 'Get a help center category',
description: PUBLIC_READ,
parameters: [categoryIdParam],
responses: {
200: {
Expand All @@ -181,7 +185,7 @@ registerPath('/help-center/categories/{categoryId}', {
'application/json': { schema: createItemResponseSchema(CategorySchema, 'Category') },
},
},
...errorResponses(400),
...errorResponses(400, 403),
...featureOff,
},
},
Expand Down Expand Up @@ -222,7 +226,8 @@ registerPath('/help-center/articles', {
get: {
tags: ['Help Center'],
summary: 'List help center articles',
description: 'Returns articles newest first, with cursor pagination.',
description:
'Requires the read:article scope. Returns articles newest first, with cursor pagination. A key without the help_center.manage permission gets published public articles only, whatever the status filter.',
parameters: [
{
name: 'categoryId',
Expand Down Expand Up @@ -269,7 +274,7 @@ registerPath('/help-center/articles', {
},
},
},
...errorResponses(),
...errorResponses(403),
...featureOff,
},
},
Expand Down Expand Up @@ -298,6 +303,7 @@ registerPath('/help-center/articles/{articleId}', {
get: {
tags: ['Help Center'],
summary: 'Get a help center article',
description: PUBLIC_READ,
parameters: [articleIdParam],
responses: {
200: {
Expand All @@ -306,7 +312,7 @@ registerPath('/help-center/articles/{articleId}', {
'application/json': { schema: createItemResponseSchema(ArticleSchema, 'Article') },
},
},
...errorResponses(400),
...errorResponses(400, 403),
...featureOff,
},
},
Expand Down Expand Up @@ -347,7 +353,8 @@ registerPath('/help-center/articles/{articleId}/feedback', {
post: {
tags: ['Help Center'],
summary: 'Record article feedback',
description: 'Records whether the article was helpful, on behalf of the API key owner.',
description:
'Records whether the article was helpful, on behalf of the API key owner. Requires the read:article scope; a draft, or an article in a private or gated category, needs the help_center.manage permission.',
parameters: [articleIdParam],
requestBody: {
required: true,
Expand All @@ -362,7 +369,7 @@ registerPath('/help-center/articles/{articleId}/feedback', {
},
},
},
...errorResponses(400),
...errorResponses(400, 403),
...featureOff,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ vi.mock('@/lib/server/db', () => ({
inArray: vi.fn(),
}))

/** The public list's per-category count of articles the viewer can open. */
function visibleArticleCounts(rows: Array<{ categoryId: string; visibleCount: number }>) {
mockSelectFrom.mockReturnValue({
innerJoin: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({ groupBy: vi.fn().mockResolvedValue(rows) }),
}),
})
}

let listCategories: typeof import('../help-center.category.service').listCategories
let listPublicCategories: typeof import('../help-center.category.service').listPublicCategories
let getCategoryById: typeof import('../help-center.category.service').getCategoryById
Expand Down Expand Up @@ -287,14 +296,10 @@ describe('listPublicCategories', () => {
},
])

mockSelectFrom.mockReturnValue({
where: vi.fn().mockReturnValue({
groupBy: vi.fn().mockResolvedValue([
{ categoryId: 'kb_category_1', totalCount: 2, publishedCount: 2 },
{ categoryId: 'kb_category_2', totalCount: 1, publishedCount: 1 },
]),
}),
})
visibleArticleCounts([
{ categoryId: 'kb_category_1', visibleCount: 2 },
{ categoryId: 'kb_category_2', visibleCount: 1 },
])

const result = await listPublicCategories()
expect(result).toHaveLength(1)
Expand Down Expand Up @@ -341,15 +346,7 @@ describe('listPublicCategories', () => {
},
])

mockSelectFrom.mockReturnValue({
where: vi.fn().mockReturnValue({
groupBy: vi
.fn()
.mockResolvedValue([
{ categoryId: 'kb_category_child', totalCount: 14, publishedCount: 14 },
]),
}),
})
visibleArticleCounts([{ categoryId: 'kb_category_child', visibleCount: 14 }])

const result = await listPublicCategories()
const slugs = result.map((c) => c.slug).sort()
Expand Down Expand Up @@ -601,13 +598,7 @@ describe('listPublicCategories returns parentId and icon', () => {
},
])

mockSelectFrom.mockReturnValue({
where: vi.fn().mockReturnValue({
groupBy: vi
.fn()
.mockResolvedValue([{ categoryId: 'kb_category_1', totalCount: 2, publishedCount: 2 }]),
}),
})
visibleArticleCounts([{ categoryId: 'kb_category_1', visibleCount: 2 }])

const result = await listPublicCategories()
expect(result).toHaveLength(1)
Expand Down
Loading