oauthHashRestore.test.js
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()
})
})