Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: prefix unused params with underscores #246

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('Clean status code through auth pipeline', (t, done) => {
app.register(fastifyAuth)
.after(() => {
app.addHook('preHandler', app.auth([failWithCode('one', 501), failWithCode('two', 502)]))
app.get('/', (req, res) => res.send(42))
app.get('/', (_req, res) => res.send(42))
})

app.inject({
Expand All @@ -51,7 +51,7 @@ test('defaultRelation: used when relation not specified', async (t) => {
method: 'GET',
url: '/welcome',
preHandler: app.auth([successWithCode('one', 200), failWithCode('two', 502)]),
handler: async (req, reply) => {
handler: async (_req, reply) => {
console.log('fawzihandler1')
await reply.send({ hello: 'welcome' })
}
Expand All @@ -61,7 +61,7 @@ test('defaultRelation: used when relation not specified', async (t) => {
method: 'GET',
url: '/bye',
preHandler: app.auth([failWithCode('one', 503), successWithCode('two', 200)], { relation: 'or' }),
handler: (req, reply) => {
handler: (_req, reply) => {
reply.send({ hello: 'bye' })
}
})
Expand Down Expand Up @@ -89,7 +89,7 @@ test('Options: non-array functions input', (t, done) => {
app.register(fastifyAuth).after(() => {
try {
app.addHook('preHandler', app.auth('bogus'))
app.get('/', (req, res) => res.send(42))
app.get('/', (_req, res) => res.send(42))
} catch (error) {
t.assert.ok(error)
t.assert.strictEqual(error.message, 'You must give an array of functions to the auth function')
Expand All @@ -113,7 +113,7 @@ test('Options: empty array functions input', (t, done) => {
app.register(fastifyAuth).after(() => {
try {
app.addHook('preHandler', app.auth([]))
app.get('/', (req, res) => res.send(42))
app.get('/', (_req, res) => res.send(42))
} catch (error) {
t.assert.ok(error)
t.assert.strictEqual(error.message, 'Missing auth functions')
Expand All @@ -137,7 +137,7 @@ test('Options: faulty relation', (t, done) => {
app.register(fastifyAuth).after(() => {
try {
app.addHook('preHandler', app.auth([successWithCode('one', 201)], { relation: 'foo' }))
app.get('/', (req, res) => res.send(42))
app.get('/', (_req, res) => res.send(42))
} catch (error) {
t.assert.ok(error)
t.assert.strictEqual(error.message, 'The value of options.relation should be one of [\'or\', \'and\']')
Expand All @@ -161,7 +161,7 @@ test('Options: faulty run', (t, done) => {
app.register(fastifyAuth).after(() => {
try {
app.addHook('preHandler', app.auth([successWithCode('one', 201)], { run: 'foo' }))
app.get('/', (req, res) => res.send(42))
app.get('/', (_req, res) => res.send(42))
} catch (error) {
t.assert.ok(error)
t.assert.strictEqual(error.message, 'The value of options.run must be \'all\'')
Expand All @@ -185,7 +185,7 @@ test('Avoid status code overwriting', (t, done) => {
app.register(fastifyAuth)
.after(() => {
app.addHook('preHandler', app.auth([successWithCode('one', 201), successWithCode('two', 202)]))
app.get('/', (req, res) => res.send(42))
app.get('/', (_req, res) => res.send(42))
})

app.inject({
Expand All @@ -209,7 +209,7 @@ test('Last win when all failures', (t, done) => {
app.register(fastifyAuth)
.after(() => {
app.addHook('preHandler', app.auth([failWithCode('one', 501), failWithCode('two', 502)]))
app.get('/', (req, res) => res.send(42))
app.get('/', (_req, res) => res.send(42))
})

app.inject({
Expand All @@ -232,7 +232,7 @@ test('First success win', (t, done) => {
app.register(fastifyAuth)
.after(() => {
app.addHook('preHandler', app.auth([successWithCode('one', 201), successWithCode('two', 202)]))
app.get('/', (req, res) => res.send(42))
app.get('/', (_req, res) => res.send(42))
})

app.inject({
Expand Down
4 changes: 2 additions & 2 deletions test/example-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function build (opts) {
const jwt = this.jwt
const level = this.level['authdb-async']

if (request.body && request.body.failureWithReply) {
if (request.body?.failureWithReply) {
reply.code(401).send({ error: 'Unauthorized' })
return Promise.reject(new Error())
}
Expand All @@ -44,7 +44,7 @@ function build (opts) {
})
}

function verifyUserAndPassword (request, reply, done) {
function verifyUserAndPassword (request, _reply, done) {
const level = this.level['authdb-async']

level.get(request.body.user, onUser)
Expand Down
8 changes: 4 additions & 4 deletions test/example-composited.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function build (opts) {
fastify.decorate('verifyOddAsync', verifyOddAsync)
fastify.decorate('verifyBigAsync', verifyBigAsync)

function verifyNumber (request, reply, done) {
function verifyNumber (request, _reply, done) {
const n = request.body.n

if (typeof (n) !== 'number') {
Expand All @@ -27,7 +27,7 @@ function build (opts) {
return done()
}

function verifyOdd (request, reply, done) {
function verifyOdd (request, _reply, done) {
const n = request.body.n

if (typeof (n) !== 'number' || n % 2 === 0) {
Expand All @@ -39,7 +39,7 @@ function build (opts) {
return done()
}

function verifyBig (request, reply, done) {
function verifyBig (request, _reply, done) {
const n = request.body.n

if (typeof (n) !== 'number' || n < 100) {
Expand Down Expand Up @@ -75,7 +75,7 @@ function build (opts) {
fastify.route({
method: 'GET',
url: '/',
handler: (req, reply) => {
handler: (_req, reply) => {
reply.send({ hello: 'world' })
}
})
Expand Down
60 changes: 30 additions & 30 deletions test/example-composited.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,13 +660,13 @@ test('Check run all line fail with AND', (t, done) => {
method: 'GET',
url: '/run-all-pipe',
preHandler: fastify.auth([
(request, reply, done) => { t.assert.ok('executed 1'); done() },
(request, reply, done) => { t.assert.ok('executed 2'); done(new Error('second')) },
(request, reply, done) => { t.assert.ok('executed 3'); done() },
(request, reply, done) => { t.assert.ok('executed 4'); done() },
(request, reply, done) => { t.assert.ok('executed 5'); done(new Error('fifth')) }
(_request, _reply, done) => { t.assert.ok('executed 1'); done() },
(_request, _reply, done) => { t.assert.ok('executed 2'); done(new Error('second')) },
(_request, _reply, done) => { t.assert.ok('executed 3'); done() },
(_request, _reply, done) => { t.assert.ok('executed 4'); done() },
(_request, _reply, done) => { t.assert.ok('executed 5'); done(new Error('fifth')) }
], { relation: 'and', run: 'all' }),
handler: (req, reply) => { reply.send({ hello: 'world' }) }
handler: (_req, reply) => { reply.send({ hello: 'world' }) }
})
})

Expand All @@ -693,13 +693,13 @@ test('Check run all line with AND', (t, done) => {
method: 'GET',
url: '/run-all-pipe',
preHandler: fastify.auth([
(request, reply, done) => { t.assert.ok('executed 1'); done() },
(request, reply, done) => { t.assert.ok('executed 2'); done() },
(request, reply, done) => { t.assert.ok('executed 3'); done() },
(request, reply, done) => { t.assert.ok('executed 4'); done() },
(request, reply, done) => { t.assert.ok('executed 5'); done() }
(_request, _reply, done) => { t.assert.ok('executed 1'); done() },
(_request, _reply, done) => { t.assert.ok('executed 2'); done() },
(_request, _reply, done) => { t.assert.ok('executed 3'); done() },
(_request, _reply, done) => { t.assert.ok('executed 4'); done() },
(_request, _reply, done) => { t.assert.ok('executed 5'); done() }
], { relation: 'and', run: 'all' }),
handler: (req, reply) => { reply.send({ hello: 'world' }) }
handler: (_req, reply) => { reply.send({ hello: 'world' }) }
})
})

Expand All @@ -722,13 +722,13 @@ test('Check run all line with OR', (t, done) => {
method: 'GET',
url: '/run-all-pipe',
preHandler: fastify.auth([
(req, reply, done) => { t.assert.ok('executed 1'); done(new Error('primo')) },
(req, reply, done) => { t.assert.ok('executed 2'); done(new Error('secondo')) },
(req, reply, done) => { t.assert.ok('executed 3'); done() },
(req, reply, done) => { t.assert.ok('executed 4'); done(new Error('quarto')) },
(req, reply, done) => { t.assert.ok('executed 5'); done() }
(_req, _reply, done) => { t.assert.ok('executed 1'); done(new Error('primo')) },
(_req, _reply, done) => { t.assert.ok('executed 2'); done(new Error('secondo')) },
(_req, _reply, done) => { t.assert.ok('executed 3'); done() },
(_req, _reply, done) => { t.assert.ok('executed 4'); done(new Error('quarto')) },
(_req, _reply, done) => { t.assert.ok('executed 5'); done() }
], { relation: 'or', run: 'all' }),
handler: (req, reply) => { reply.send({ hello: 'world' }) }
handler: (_req, reply) => { reply.send({ hello: 'world' }) }
})
})

Expand All @@ -751,13 +751,13 @@ test('Check run all fail line with OR', (t, done) => {
method: 'GET',
url: '/run-all-pipe',
preHandler: fastify.auth([
(req, reply, done) => { t.assert.ok('executed 1'); done(new Error('primo')) },
(req, reply, done) => { t.assert.ok('executed 2'); done(new Error('secondo')) },
(req, reply, done) => { t.assert.ok('executed 3'); done(new Error('terzo')) },
(req, reply, done) => { t.assert.ok('executed 4'); done(new Error('quarto')) },
(req, reply, done) => { t.assert.ok('executed 5'); done(new Error('quinto')) }
(_req, _reply, done) => { t.assert.ok('executed 1'); done(new Error('primo')) },
(_req, _reply, done) => { t.assert.ok('executed 2'); done(new Error('secondo')) },
(_req, _reply, done) => { t.assert.ok('executed 3'); done(new Error('terzo')) },
(_req, _reply, done) => { t.assert.ok('executed 4'); done(new Error('quarto')) },
(_req, _reply, done) => { t.assert.ok('executed 5'); done(new Error('quinto')) }
], { relation: 'or', run: 'all' }),
handler: (req, reply) => { reply.send({ hello: 'world' }) }
handler: (_req, reply) => { reply.send({ hello: 'world' }) }
})
})

Expand All @@ -784,10 +784,10 @@ test('Ignore last status', (t, done) => {
method: 'GET',
url: '/run-all-status',
preHandler: fastify.auth([
(req, reply, done) => { t.assert.ok('executed 1'); done() },
(req, reply, done) => { t.assert.ok('executed 2'); done(new Error('last')) }
(_req, _reply, done) => { t.assert.ok('executed 1'); done() },
(_req, _reply, done) => { t.assert.ok('executed 2'); done(new Error('last')) }
], { relation: 'or', run: 'all' }),
handler: (req, reply) => { reply.send({ hello: 'world' }) }
handler: (_req, reply) => { reply.send({ hello: 'world' }) }
})
})

Expand Down Expand Up @@ -883,10 +883,10 @@ test('Clean status code settle by user', (t, done) => {
method: 'GET',
url: '/run-all-status',
preHandler: fastify.auth([
(req, reply, done) => { t.assert.ok('executed 1'); done() },
(req, reply, done) => { t.assert.ok('executed 2'); reply.code(400); done(new Error('last')) }
(_req, _reply, done) => { t.assert.ok('executed 1'); done() },
(_req, reply, done) => { t.assert.ok('executed 2'); reply.code(400); done(new Error('last')) }
], { relation: 'or', run: 'all' }),
handler: (req, reply) => { reply.send({ hello: 'world' }) }
handler: (_req, reply) => { reply.send({ hello: 'world' }) }
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function build (opts) {
}
}

function verifyUserAndPassword (request, reply, done) {
function verifyUserAndPassword (request, _reply, done) {
const level = this.level.authdb

if (!request.body || !request.body.user) {
Expand Down
12 changes: 6 additions & 6 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ app.register(fastifyAuth).after((_err) => {
},
])
app.auth([
function (request, reply, done) {
function () {
expectType<FastifyInstance>(this)
},
])
const auth = app.auth([(request, reply, done) => {}])
const auth = app.auth([() => {}])
expectType<preHandlerHookHandler>(auth)
app.get('/secret', { preHandler: auth }, (request, reply) => {})
app.get('/private', { preHandler: [auth] }, (request, reply) => {})
app.get('/secret', { preHandler: auth }, () => {})
app.get('/private', { preHandler: [auth] }, () => {})
})

const typebox = fastify().withTypeProvider<TypeBoxTypeProvider>()
Expand Down Expand Up @@ -99,7 +99,7 @@ async function usersController (fastify: FastifyInstance): Promise<void> {
usersMutationAccessPolicy(fastify),
]),
},
async (req, res) => ({ success: true })
async () => ({ success: true })
)
}
await usersController(app)
Expand All @@ -113,7 +113,7 @@ async function usersControllerV2 (fastify: FastifyInstance): Promise<void> {
{
onRequest: usersMutationAccessPolicy(fastify),
},
async (req, res) => ({ success: true })
async () => ({ success: true })
)
}
await usersControllerV2(app)
Loading