Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.3 KB
Raw Blame
import { render, screen } from '@testing-library/react'
import { describe, expect, it, vi } from 'vite-plus/test'
import { createReactI18nextMock } from '@/test/i18n-mock'

vi.mock('react-i18next', () =>
  createReactI18nextMock({
    'oauth.callback.success': 'Authorization complete',
    'oauth.callback.successHint': 'You can now close this tab and return to the previous page.',
    'oauth.callback.error': 'Authorization failed',
    'oauth.callback.errorHint': 'Please return to the previous page and try again.',
    'login.signBtn': 'Sign in',
  }),
)

vi.mock('@/hooks/use-oauth', () => ({
  useOAuthCallback: vi.fn(),
}))

vi.mock('@/hooks/use-document-title', () => ({
  default: vi.fn(),
}))

const { useOAuthCallback } = await import('@/hooks/use-oauth')
const OAuthCallback = (await import('../page')).default

describe('OAuthCallback page', () => {
  it('renders a success message when opened in a new tab with a subscription_id (#39752)', () => {
    vi.mocked(useOAuthCallback).mockReturnValue({
      hasOpener: false,
      finished: true,
      error: null,
      errorDescription: null,
    })

    render(<OAuthCallback />)
    expect(screen.getByRole('main')).toBeInTheDocument()
    expect(screen.getByText('Authorization complete')).toBeInTheDocument()
    expect(
      screen.getByText('You can now close this tab and return to the previous page.'),
    ).toBeInTheDocument()
  })

  it('renders an error message with the provider description when the provider reported an error (#39752)', () => {
    vi.mocked(useOAuthCallback).mockReturnValue({
      hasOpener: false,
      finished: true,
      error: 'access_denied',
      errorDescription: 'Please re-authorize the app.',
    })

    render(<OAuthCallback />)
    expect(screen.getByText('Authorization failed')).toBeInTheDocument()
    expect(screen.getByText('Please re-authorize the app.')).toBeInTheDocument()
  })

  it('falls back to a generic error hint when the provider did not supply an error_description', () => {
    vi.mocked(useOAuthCallback).mockReturnValue({
      hasOpener: false,
      finished: true,
      error: 'server_error',
      errorDescription: null,
    })

    render(<OAuthCallback />)
    expect(screen.getByText('Authorization failed')).toBeInTheDocument()
    expect(
      screen.getByText('Please return to the previous page and try again.'),
    ).toBeInTheDocument()
  })
})