dify/web/utils/app-redirection.spec.ts
Wu Tianwei 33edf97f81
feat: RBAC (#37107)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: fatelei <fatelei@gmail.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: 盐粒 Yanli <yanli@dify.ai>
Co-authored-by: Charles Yao <chongbinyao33@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: yunlu.wen <yunlu.wen@dify.ai>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: Jingyi <jingyi.qi@dify.ai>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: hjlarry <hjlarry@163.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
Co-authored-by: gigglewang <gigglewang@dify.ai>
Co-authored-by: chariri <w@chariri.moe>
Co-authored-by: Evan <2869018789@qq.com>
Co-authored-by: zyssyz123 <916125788@qq.com>
2026-06-18 16:35:29 +00:00

132 lines
5.5 KiB
TypeScript

/**
* Test suite for app redirection utility functions
* Tests navigation path generation based on user permissions and app modes
*/
import { AppModeEnum } from '@/types/app'
import { AppACLPermission } from '@/utils/permission'
import { getRedirection, getRedirectionPath } from './app-redirection'
describe('app-redirection', () => {
/**
* Tests getRedirectionPath which determines the correct path based on:
* - App ACL layout access permissions
* - App ACL monitor and access config permissions
* - App mode (workflow, advanced-chat, chat, completion, agent-chat)
*/
describe('getRedirectionPath', () => {
it('returns develop path when app ACL cannot access guarded pages', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/develop')
})
it('returns workflow path for workflow mode when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.WORKFLOW, permission_keys: [AppACLPermission.ViewLayout] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/workflow')
})
it('returns workflow path for advanced-chat mode when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.ADVANCED_CHAT, permission_keys: [AppACLPermission.TestAndRun] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/workflow')
})
it('returns configuration path for chat mode when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [AppACLPermission.Edit] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/configuration')
})
it('returns configuration path for completion mode when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.COMPLETION, permission_keys: [AppACLPermission.ViewLayout] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-123/configuration')
})
it('returns configuration path for agent-chat mode when app ACL can access layout', () => {
const app = { id: 'app-456', mode: AppModeEnum.AGENT_CHAT, permission_keys: [AppACLPermission.ViewLayout] }
const result = getRedirectionPath(app)
expect(result).toBe('/app/app-456/configuration')
})
it('handles different app IDs', () => {
const app1 = { id: 'abc-123', mode: AppModeEnum.CHAT, permission_keys: [] }
const app2 = { id: 'xyz-789', mode: AppModeEnum.WORKFLOW, permission_keys: [AppACLPermission.ViewLayout] }
expect(getRedirectionPath(app1)).toBe('/app/abc-123/develop')
expect(getRedirectionPath(app2)).toBe('/app/xyz-789/workflow')
})
it('returns layout path when the app maintainer has app.create_and_management permission without app ACL keys', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [] }
expect(getRedirectionPath(app, {
currentUserId: 'user-1',
resourceMaintainer: 'user-1',
workspacePermissionKeys: ['app.create_and_management'],
})).toBe('/app/app-123/configuration')
})
it('returns access config path when app ACL can only configure access', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [AppACLPermission.AccessConfig] }
expect(getRedirectionPath(app)).toBe('/app/app-123/access-config')
})
it('returns overview path when app ACL can only monitor the app', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [AppACLPermission.Monitor] }
expect(getRedirectionPath(app)).toBe('/app/app-123/overview')
})
})
/**
* Tests getRedirection which combines path generation with a redirect callback
*/
describe('getRedirection', () => {
/**
* Tests that the redirection function is called with the correct path
*/
it('calls redirection function with develop path when app ACL cannot access guarded pages', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [] }
const mockRedirect = vi.fn()
getRedirection(app, mockRedirect)
expect(mockRedirect).toHaveBeenCalledWith('/app/app-123/develop')
expect(mockRedirect).toHaveBeenCalledTimes(1)
})
it('calls redirection function with workflow path when app ACL can access layout', () => {
const app = { id: 'app-123', mode: AppModeEnum.WORKFLOW, permission_keys: [AppACLPermission.ViewLayout] }
const mockRedirect = vi.fn()
getRedirection(app, mockRedirect)
expect(mockRedirect).toHaveBeenCalledWith('/app/app-123/workflow')
expect(mockRedirect).toHaveBeenCalledTimes(1)
})
it('calls redirection function with configuration path for chat mode with layout access', () => {
const app = { id: 'app-123', mode: AppModeEnum.CHAT, permission_keys: [AppACLPermission.ViewLayout] }
const mockRedirect = vi.fn()
getRedirection(app, mockRedirect)
expect(mockRedirect).toHaveBeenCalledWith('/app/app-123/configuration')
expect(mockRedirect).toHaveBeenCalledTimes(1)
})
it('works with different redirection functions', () => {
const app = { id: 'app-123', mode: AppModeEnum.WORKFLOW, permission_keys: [AppACLPermission.ViewLayout] }
const paths: string[] = []
const customRedirect = (path: string) => paths.push(path)
getRedirection(app, customRedirect)
expect(paths).toEqual(['/app/app-123/workflow'])
})
})
})