Skip to content

Commit e7a394d

Browse files
committed
Add new test for http agent
1 parent e3eef4d commit e7a394d

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

.env.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ NEXTAUTH_URL=http://127.0.0.1:3000
22
NEXTAUTH_SECRET=next-auth-cypress-secret
33
DATABASE_URL=postgres://postgres:postgres@localhost:5434/osm-teams-test
44
TESTING=true
5-
LOG_LEVEL=silent
5+
LOG_LEVEL=silent
6+
AUTH_URL=http://127.0.0.1:3000

src/pages/api/introspect.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { decode } = require('next-auth/jwt')
2+
/**
3+
* !! This function is only used for testing
4+
* purposes, it mocks the hydra access token introspection
5+
*/
6+
export default async function handler(req, res) {
7+
if (req.method === 'POST') {
8+
// Process a POST request
9+
const { token } = req.body
10+
const decodedToken = await decode({
11+
token,
12+
secret: process.env.NEXT_AUTH_SECRET,
13+
})
14+
15+
const result = {
16+
active: true,
17+
sub: decodedToken.userId,
18+
}
19+
20+
res.status(200).json(result)
21+
} else {
22+
res.status(400)
23+
}
24+
}

tests/api/team-api.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ const { resetDb, disconnectDb } = require('../utils/db-helpers')
44
const createAgent = require('../utils/create-agent')
55

66
let user1Agent
7+
let user1HttpAgent
78
test.before(async () => {
89
await resetDb()
910
user1Agent = await createAgent({ id: 1 })
11+
user1HttpAgent = await createAgent({ id: 1, http: true })
1012
})
1113

1214
test.after.always(disconnectDb)
@@ -252,7 +254,13 @@ test('get my teams list', async (t) => {
252254
data: teams,
253255
},
254256
} = await user1Agent.get(`/api/my/teams`).expect(200)
257+
255258
t.is(total, 2)
256259
t.is(teams[0].name, 'Team 1')
257260
t.is(teams[1].name, 'Team 2')
261+
262+
const httpApiResponse = await user1HttpAgent.get(`/api/my/teams`).expect(200)
263+
264+
// Has to be the same
265+
t.deepEqual(httpApiResponse.body.data, teams)
258266
})

tests/utils/create-agent.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
const logger = require('../../src/lib/logger')
12
const getSessionToken = require('./get-session-token')
23

3-
async function createAgent(user) {
4+
async function createAgent(user, http = false) {
45
const agent = require('supertest').agent('http://localhost:3000')
56

67
if (user) {
78
const encryptedToken = await getSessionToken(
89
user,
910
process.env.NEXTAUTH_SECRET
1011
)
12+
if (http) {
13+
agent.set('Authorization', `Bearer ${encryptedToken}`)
14+
}
1115
agent.set('Cookie', [`next-auth.session-token=${encryptedToken}`])
1216
}
1317

tests/utils/get-session-token.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function getSessionToken(userObj, secret) {
55
const token = { ...userObj, userId: userObj.id }
66

77
// Function logic derived from https://github.com/nextauthjs/next-auth/blob/5c1826a8d1f8d8c2d26959d12375704b0a693bfc/packages/next-auth/src/jwt/index.ts#L113-L121
8-
const encryptionSecret = await await hkdf(
8+
const encryptionSecret = await hkdf(
99
'sha256',
1010
secret,
1111
'',

0 commit comments

Comments
 (0)