oauthHashRestore.test.js 1.8 KB
import { describe, expect, it } from 'vitest'

import { buildOAuthAuthorizeUrl, getOAuthRestoredUrl } from '../oauthHashRestore'

describe('oauthHashRestore', () => {
  it('should build oauth authorize url with split base and hash', () => {
    const href = 'https://wxm.behalo.cc/f/mlaj/#/studyDetail/3552321?from_course_id=2995248'

    expect(buildOAuthAuthorizeUrl(href)).toBe(
      '/srv/?f=behalo&a=openid&res=https%3A%2F%2Fwxm.behalo.cc%2Ff%2Fmlaj%2F&ret_hash=%23%2FstudyDetail%2F3552321%3Ffrom_course_id%3D2995248'
    )
  })

  it('should append test_openid in dev mode', () => {
    const href = 'https://wxm.behalo.cc/f/mlaj/#/studyDetail/3552321'

    expect(buildOAuthAuthorizeUrl(href, { is_dev: true, test_openid: 'abc123' })).toBe(
      '/srv/?f=behalo&a=openid&res=https%3A%2F%2Fwxm.behalo.cc%2Ff%2Fmlaj%2F&ret_hash=%23%2FstudyDetail%2F3552321&test_openid=abc123'
    )
  })

  it('should restore ret_hash when current url has no hash', () => {
    const href =
      'https://wxm.behalo.cc/f/mlaj/?code=abc&state=1&ret_hash=%23%2Flogin%3Fredirect%3D%252FstudyDetail%252F3552321%253Ffrom_course_id%253D2995248'

    expect(getOAuthRestoredUrl(href)).toBe(
      'https://wxm.behalo.cc/f/mlaj/?code=abc&state=1#/login?redirect=%2FstudyDetail%2F3552321%3Ffrom_course_id%3D2995248'
    )
  })

  it('should not override an existing hash', () => {
    const href =
      'https://wxm.behalo.cc/f/mlaj/?ret_hash=%23%2FstudyDetail%2F3552321#/checkin/index?id=3189684'

    expect(getOAuthRestoredUrl(href)).toBeNull()
  })

  it('should return null when ret_hash is missing', () => {
    const href = 'https://wxm.behalo.cc/f/mlaj/?code=abc&state=1'

    expect(getOAuthRestoredUrl(href)).toBeNull()
  })

  it('should return null for invalid authorize target', () => {
    expect(buildOAuthAuthorizeUrl('')).toBeNull()
  })
})