-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.js
More file actions
178 lines (134 loc) · 4.09 KB
/
Copy pathindex.test.js
File metadata and controls
178 lines (134 loc) · 4.09 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require('dotenv').config()
const faunadb = require('faunadb')
const bcrypt = require('bcrypt')
const initAuth = require('./index')
const jwt = require('jsonwebtoken')
const q = faunadb.query
const {
DB_SECRET,
ACCESS_SECRET,
REFRESH_SECRET,
USERNAME,
PASSWORD,
EXISTING_USER_ID,
EXISTING_USER_USERNAME,
EXISTING_USER_PASSWORD,
} = process.env
let newUserId = null
let existingUser = null
let accessToken = null
let refreshToken = null
const FaunaAuth = initAuth({
dbSecret: DB_SECRET,
accessSecret: ACCESS_SECRET,
refreshSecret: REFRESH_SECRET,
tokenDuration: 1000 * 5,
})
const delay = ms => new Promise(res => setTimeout(res, ms))
test('creates a user', async () => {
const result = await FaunaAuth.create(USERNAME, PASSWORD, { some: 'data' })
expect(result).toEqual({
id: expect.any(String),
username: USERNAME,
some: 'data',
})
newUserId = result.id
})
test('updates a password', async () => {
expect(newUserId).toBeTruthy()
const result = await FaunaAuth.changePassword(newUserId, 'newPassword')
expect(result).toEqual({
id: expect.any(String),
username: USERNAME,
some: 'data',
})
})
test('Deletes a user', async () => {
expect(newUserId).toBeTruthy()
const result = await FaunaAuth.delete(newUserId)
expect(result.data.username).toEqual(USERNAME)
})
test('gets a user', async () => {
const result = await FaunaAuth.get(EXISTING_USER_ID)
expect(result).toEqual({
id: EXISTING_USER_ID,
username: 'existing@test.com',
randomNumber: expect.any(Number),
})
existingUser = result
})
test('updates a user', async () => {
expect(existingUser).toBeTruthy()
const randomNumber = Math.floor(Math.random() * 20)
const result = await FaunaAuth.update(existingUser.id, {
randomNumber,
})
expect(result).toEqual({ ...existingUser, randomNumber })
existingUser = result
})
test('authenticate', async () => {
const result = await FaunaAuth.authenticate(
EXISTING_USER_USERNAME,
EXISTING_USER_PASSWORD
)
expect(result).toEqual({
id: expect.any(String),
username: EXISTING_USER_USERNAME,
randomNumber: expect.any(Number),
})
})
test('createTokens', async () => {
expect(existingUser).toBeTruthy()
const result = await FaunaAuth.createTokens(existingUser)
expect(result).toEqual({
accessToken: expect.any(String),
refreshToken: expect.any(String),
expiration: expect.any(Number),
})
jwt.verify(result.accessToken, ACCESS_SECRET, (err, { id }) => {
expect(id).toEqual(EXISTING_USER_ID)
})
accessToken = result.accessToken
refreshToken = result.refreshToken
})
test('verify', async () => {
expect(accessToken).toBeTruthy()
expect(existingUser).toBeTruthy()
const result = await FaunaAuth.verify(accessToken)
expect(result).toEqual(existingUser)
})
test('refreshToken - valid refresh token', async () => {
expect(refreshToken).toBeTruthy()
await delay(100) // to ensure the jwt will be different
const result = await FaunaAuth.refreshToken(refreshToken)
expect(result).toEqual({
accessToken: expect.any(String),
refreshToken: expect.any(String),
expiration: expect.any(Number),
})
const exists = await new faunadb.Client({ secret: DB_SECRET })
.query(q.Exists(q.Match(q.Index('refreshTokens'), refreshToken)))
.catch(() => false)
expect(exists).toBe(false)
jwt.verify(result.accessToken, ACCESS_SECRET, (err, { id }) => {
expect(id).toEqual(EXISTING_USER_ID)
})
refreshToken = result.refreshToken
})
test('refreshToken - invalid refresh token', async () => {
FaunaAuth.refreshToken('abcdefg').catch(err => {
expect(err.message).toEqual('invalid token')
})
})
test('deleteRefreshToken', async () => {
await FaunaAuth.deleteRefreshToken(refreshToken)
FaunaAuth.refreshToken(refreshToken).catch(err => {
expect(err.message).toEqual('invalid token')
})
})
test('deauthenticate', async () => {
expect(existingUser).toBeTruthy()
const { refreshToken } = await FaunaAuth.createTokens(existingUser)
const result = await FaunaAuth.deauthenticate(existingUser)
expect(result.data[0].data.refreshToken).toEqual(refreshToken)
})