-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApiTokensSocketAuthentication.spec.ts
More file actions
76 lines (68 loc) · 2.16 KB
/
ApiTokensSocketAuthentication.spec.ts
File metadata and controls
76 lines (68 loc) · 2.16 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import faker from '@faker-js/faker'
import Database from '@ioc:Adonis/Lucid/Database'
import {
ApiTokensSignUpUserResponseBody,
SignUpUserRequestBody,
} from 'App/Controllers/Http/UserAuthenticationsController'
import test from 'japa'
import { io } from 'socket.io-client'
import supertest from 'supertest'
import {
BASE_URL,
createSpyOnClientSocketEvent,
TypedClientSocket,
waitFor,
} from './utils/testUtils'
test.group('Web auth socket io authentication tests group', (group) => {
group.beforeEach(async () => {
await Database.beginGlobalTransaction()
})
group.afterEach(async () => {
await Database.rollbackGlobalTransaction()
})
test('It should fail to perform a socket connection with invalid api tokens token', async (assert) => {
const socket: TypedClientSocket = io(BASE_URL, {
auth: {
Authorization: `Bearer ${faker.datatype.uuid()}`,
},
withCredentials: true,
})
const socketCreationAcknowledgementSpy = createSpyOnClientSocketEvent(
socket,
'ACKNOWLEDGE_SOCKET_CONNECTION'
)
try {
await waitFor(() => {
socket.emit('GET_ACKNOWLEDGE_SOCKET_CONNECTION')
assert.isTrue(socketCreationAcknowledgementSpy.called)
})
assert.isTrue(false)
} catch {
assert.isFalse(socket.connected)
}
})
test('It should be able to perform a socket connection using web auth cookies', async (assert) => {
const rawResponse = await supertest(BASE_URL)
.post('/authentication/sign-up/api-tokens')
.send({
email: faker.internet.email(),
password: faker.internet.password(),
} as SignUpUserRequestBody)
.expect(200)
const { token } = ApiTokensSignUpUserResponseBody.parse(rawResponse.body)
const socket: TypedClientSocket = io(BASE_URL, {
withCredentials: true,
auth: {
Authorization: `Bearer ${token}`,
},
})
const socketCreationAcknowledgementSpy = createSpyOnClientSocketEvent(
socket,
'ACKNOWLEDGE_SOCKET_CONNECTION'
)
await waitFor(() => {
socket.emit('GET_ACKNOWLEDGE_SOCKET_CONNECTION')
assert.isTrue(socketCreationAcknowledgementSpy.called)
})
})
})