|
| 1 | +import { mockRequestInstance } from '~/__tests__/helpers/instance' |
| 2 | +import { mockResponse } from '~/__tests__/helpers/response' |
| 3 | +import { UserController } from '~/controllers' |
| 4 | +import camelcaseKeys from 'camelcase-keys' |
| 5 | + |
| 6 | +describe('test owner client', () => { |
| 7 | + const client = mockRequestInstance(UserController) |
| 8 | + |
| 9 | + test('GET /owner', async () => { |
| 10 | + const mocked = mockResponse('/owner', { |
| 11 | + id: '5ea4fe632507ba128f4c938c', |
| 12 | + introduce: '这是我的小世界呀', |
| 13 | + mail: 'i@innei.ren', |
| 14 | + url: 'https://innei.in', |
| 15 | + name: 'Innei', |
| 16 | + social_ids: { |
| 17 | + bili_id: 26578164, |
| 18 | + netease_id: 84302804, |
| 19 | + github: 'Innei', |
| 20 | + }, |
| 21 | + username: 'Innei', |
| 22 | + created: '2020-04-26T03:22:11.784Z', |
| 23 | + modified: '2020-11-13T09:38:49.014Z', |
| 24 | + last_login_time: '2021-11-17T13:42:48.209Z', |
| 25 | + avatar: 'https://cdn.innei.ren/avatar.png', |
| 26 | + }) |
| 27 | + const data = await client.owner.getOwnerInfo() |
| 28 | + expect(data.id).toEqual(mocked.id) |
| 29 | + expect(data).toEqual(camelcaseKeys(mocked, { deep: true })) |
| 30 | + expect(data.$raw.data).toEqual(mocked) |
| 31 | + }) |
| 32 | + |
| 33 | + test('POST /auth/sign-in/username', async () => { |
| 34 | + const mocked = mockResponse( |
| 35 | + '/auth/sign-in/username', |
| 36 | + { |
| 37 | + token: 'txo123', |
| 38 | + user: { |
| 39 | + id: '5ea4fe632507ba128f4c938c', |
| 40 | + }, |
| 41 | + }, |
| 42 | + 'post', |
| 43 | + { |
| 44 | + username: 'test', |
| 45 | + password: 'test', |
| 46 | + }, |
| 47 | + ) |
| 48 | + const data = await client.owner.login('test', 'test') |
| 49 | + expect(data.token).toEqual(mocked.token) |
| 50 | + expect(data.$raw.data).toEqual(mocked) |
| 51 | + }) |
| 52 | + |
| 53 | + test('GET /owner/check_logged', async () => { |
| 54 | + const mocked = mockResponse('/owner/check_logged?token=token', { |
| 55 | + isGuest: true, |
| 56 | + ok: 0, |
| 57 | + }) |
| 58 | + const data = await client.owner.checkTokenValid('bearer token') |
| 59 | + expect(data).toEqual(mocked) |
| 60 | + }) |
| 61 | + |
| 62 | + test('GET /owner/check_logged without token', async () => { |
| 63 | + const mocked = mockResponse('/owner/check_logged', { |
| 64 | + isGuest: true, |
| 65 | + ok: 0, |
| 66 | + }) |
| 67 | + const data = await client.owner.checkTokenValid() |
| 68 | + expect(data).toEqual(mocked) |
| 69 | + }) |
| 70 | + |
| 71 | + test('GET /owner/allow-login', async () => { |
| 72 | + const mocked = mockResponse('/owner/allow-login', { |
| 73 | + password: true, |
| 74 | + passkey: false, |
| 75 | + github: true, |
| 76 | + google: false, |
| 77 | + }) |
| 78 | + const data = await client.owner.getAllowLoginMethods() |
| 79 | + expect(data).toEqual(mocked) |
| 80 | + }) |
| 81 | + |
| 82 | + test('GET /auth/session', async () => { |
| 83 | + const mocked = mockResponse('/auth/session', { |
| 84 | + id: 'owner-id', |
| 85 | + provider: 'github', |
| 86 | + provider_account_id: 'github-id', |
| 87 | + role: 'owner', |
| 88 | + }) |
| 89 | + const data = await client.owner.getSession() |
| 90 | + expect(data).toEqual(camelcaseKeys(mocked, { deep: true })) |
| 91 | + }) |
| 92 | + |
| 93 | + test('GET /auth/get-session', async () => { |
| 94 | + const mocked = mockResponse('/auth/get-session', { |
| 95 | + session: { |
| 96 | + token: 'session-token', |
| 97 | + user_id: 'owner-id', |
| 98 | + expires_at: '2026-02-08T00:00:00.000Z', |
| 99 | + created_at: '2026-02-08T00:00:00.000Z', |
| 100 | + updated_at: '2026-02-08T00:00:00.000Z', |
| 101 | + }, |
| 102 | + user: { |
| 103 | + id: 'owner-id', |
| 104 | + }, |
| 105 | + }) |
| 106 | + const data = await client.owner.getAuthSession() |
| 107 | + expect(data).toEqual(camelcaseKeys(mocked, { deep: true })) |
| 108 | + }) |
| 109 | + |
| 110 | + test('GET /auth/providers', async () => { |
| 111 | + const mocked = mockResponse('/auth/providers', ['github']) |
| 112 | + const data = await client.owner.getProviders() |
| 113 | + expect(data).toEqual(mocked) |
| 114 | + }) |
| 115 | + |
| 116 | + test('GET /auth/list-sessions', async () => { |
| 117 | + const mocked = mockResponse('/auth/list-sessions', [ |
| 118 | + { |
| 119 | + token: 'session-token', |
| 120 | + user_id: 'owner-id', |
| 121 | + expires_at: '2026-02-08T00:00:00.000Z', |
| 122 | + created_at: '2026-02-08T00:00:00.000Z', |
| 123 | + updated_at: '2026-02-08T00:00:00.000Z', |
| 124 | + }, |
| 125 | + ]) |
| 126 | + const data = await client.owner.listSessions() |
| 127 | + expect(data).toEqual(camelcaseKeys(mocked, { deep: true })) |
| 128 | + }) |
| 129 | + |
| 130 | + test('POST /auth/revoke-session', async () => { |
| 131 | + const mocked = mockResponse( |
| 132 | + '/auth/revoke-session', |
| 133 | + { status: true }, |
| 134 | + 'post', |
| 135 | + { token: 'session-token' }, |
| 136 | + ) |
| 137 | + const data = await client.owner.revokeSession('session-token') |
| 138 | + expect(data).toEqual(mocked) |
| 139 | + }) |
| 140 | + |
| 141 | + test('POST /auth/revoke-sessions', async () => { |
| 142 | + const mocked = mockResponse( |
| 143 | + '/auth/revoke-sessions', |
| 144 | + { status: true }, |
| 145 | + 'post', |
| 146 | + ) |
| 147 | + const data = await client.owner.revokeSessions() |
| 148 | + expect(data).toEqual(mocked) |
| 149 | + }) |
| 150 | + |
| 151 | + test('POST /auth/revoke-other-sessions', async () => { |
| 152 | + const mocked = mockResponse( |
| 153 | + '/auth/revoke-other-sessions', |
| 154 | + { status: true }, |
| 155 | + 'post', |
| 156 | + ) |
| 157 | + const data = await client.owner.revokeOtherSessions() |
| 158 | + expect(data).toEqual(mocked) |
| 159 | + }) |
| 160 | + |
| 161 | + test('POST /auth/sign-out', async () => { |
| 162 | + const mocked = mockResponse('/auth/sign-out', { success: true }, 'post') |
| 163 | + const data = await client.owner.logout() |
| 164 | + expect(data).toEqual(mocked) |
| 165 | + }) |
| 166 | + |
| 167 | + it('should inject owner client work', () => { |
| 168 | + expect(client.owner.getOwnerInfo).toBeInstanceOf(Function) |
| 169 | + }) |
| 170 | +}) |
0 commit comments