Skip to content

Commit 61a7a74

Browse files
committed
feat(tests): add tests to have a better coverage
1 parent dd1afec commit 61a7a74

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

tests/guard.spec.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ test.group('Jwt guard | authenticate', () => {
9999
secret: 'thisisasecret',
100100
expiresIn: '1h',
101101
content: jwtContentFn,
102-
useCookies: false,
103102
})
104103
const user = await userProvider.findById(1)
105104

@@ -118,6 +117,64 @@ test.group('Jwt guard | authenticate', () => {
118117
assert.equal(decoded.otherProperty, content.otherProperty)
119118
})
120119

120+
test('throw error when the userId is not found in the payload', async ({ assert }) => {
121+
const userProvider = new JwtFakeUserProvider()
122+
const ctx = new HttpContextFactory().create()
123+
const guard = new JwtGuard(ctx, userProvider, { secret: 'thisisasecret' })
124+
const token = jwt.sign({ foo: 'bar' }, 'thisisasecret')
125+
126+
ctx.request.request.headers.authorization = `Bearer ${token}`
127+
const [result] = await Promise.allSettled([guard.authenticate()])
128+
129+
assert.equal(result!.status, 'rejected')
130+
if (result!.status === 'rejected') {
131+
assert.instanceOf(result!.reason, errors.E_UNAUTHORIZED_ACCESS)
132+
}
133+
assert.isUndefined(guard.user)
134+
assert.throws(() => guard.getUserOrFail(), 'Unauthorized access')
135+
assert.isFalse(guard.isAuthenticated)
136+
assert.isTrue(guard.authenticationAttempted)
137+
})
138+
139+
test('throw error when the payload is not an object', async ({ assert }) => {
140+
const ctx = new HttpContextFactory().create()
141+
const userProvider = new JwtFakeUserProvider()
142+
143+
const guard = new JwtGuard(ctx, userProvider, { secret: 'thisisasecret' })
144+
ctx.request.request.headers.authorization = `Bearer ${jwt.sign('foo', 'thisisasecret')}`
145+
const [result] = await Promise.allSettled([guard.authenticate()])
146+
147+
assert.equal(result!.status, 'rejected')
148+
if (result!.status === 'rejected') {
149+
assert.instanceOf(result!.reason, errors.E_UNAUTHORIZED_ACCESS)
150+
}
151+
152+
assert.isUndefined(guard.user)
153+
assert.throws(() => guard.getUserOrFail(), 'Unauthorized access')
154+
155+
assert.isFalse(guard.isAuthenticated)
156+
assert.isTrue(guard.authenticationAttempted)
157+
})
158+
159+
test('throw error when the payload contains a userId that does not exist', async ({ assert }) => {
160+
const ctx = new HttpContextFactory().create()
161+
const userProvider = new JwtFakeUserProvider()
162+
const guard = new JwtGuard(ctx, userProvider, { secret: 'thisisasecret' })
163+
ctx.request.request.headers.authorization = `Bearer ${jwt.sign({ userId: 999 }, 'thisisasecret')}`
164+
const [result] = await Promise.allSettled([guard.authenticate()])
165+
166+
assert.equal(result!.status, 'rejected')
167+
if (result!.status === 'rejected') {
168+
assert.instanceOf(result!.reason, errors.E_UNAUTHORIZED_ACCESS)
169+
}
170+
171+
assert.isUndefined(guard.user)
172+
assert.throws(() => guard.getUserOrFail(), 'Unauthorized access')
173+
174+
assert.isFalse(guard.isAuthenticated)
175+
assert.isTrue(guard.authenticationAttempted)
176+
})
177+
121178
test('throw error when cookie header is invalid', async ({ assert }) => {
122179
const ctx = new HttpContextFactory().create()
123180
const userProvider = new JwtFakeUserProvider()

0 commit comments

Comments
 (0)