From 78c6bb1cc2b95e41af37af5912b4f7178539188e Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 4 Jun 2023 14:50:30 +0200 Subject: [PATCH] dedupe CID test logic --- packages/backend/test/e2e/oauth.ts | 142 ++++++++++------------------- 1 file changed, 47 insertions(+), 95 deletions(-) diff --git a/packages/backend/test/e2e/oauth.ts b/packages/backend/test/e2e/oauth.ts index 168560fba0..8d6c9b42db 100644 --- a/packages/backend/test/e2e/oauth.ts +++ b/packages/backend/test/e2e/oauth.ts @@ -10,7 +10,7 @@ import { AuthorizationCode, type AuthorizationTokenConfig } from 'simple-oauth2' import pkceChallenge from 'pkce-challenge'; import { JSDOM } from 'jsdom'; import * as misskey from 'misskey-js'; -import Fastify, { type FastifyInstance } from 'fastify'; +import Fastify, { type FastifyReply, type FastifyInstance } from 'fastify'; import { port, relativeFetch, signup, startServer } from '../utils.js'; import type { INestApplicationContext } from '@nestjs/common'; @@ -83,7 +83,7 @@ async function fetchDecisionFromResponse(response: Response, user: misskey.entit return await fetchDecision(transactionId, user, { cancel }); } -async function fetchAuthorizationCode(user: ImmediateSignup, scope: string, code_challenge: string): Promise<{ client: AuthorizationCode, code: string }> { +async function fetchAuthorizationCode(user: misskey.entities.MeSignup, scope: string, code_challenge: string): Promise<{ client: AuthorizationCode, code: string }> { const client = getClient(); const response = await fetch(client.authorizeURL({ @@ -627,107 +627,59 @@ describe('OAuth', () => { describe('Client Information Discovery', () => { describe('Redirection', () => { - test('Read HTTP header', async () => { - await fastify.close(); - - fastify = Fastify(); - fastify.get('/', async (request, reply) => { + const tests: Record void> = { + 'Read HTTP header': reply => { reply.header('Link', '; rel="redirect_uri"'); reply.send(` - -
Misklient - `); - }); - await fastify.listen({ port: clientPort }); - - const client = getClient(); - - const response = await fetch(client.authorizeURL({ - redirect_uri, - scope: 'write:notes', - state: 'state', - code_challenge: 'code', - code_challenge_method: 'S256', - } as AuthorizationParamsExtended)); - assert.strictEqual(response.status, 200); - }); - - test('Mixed links', async () => { - await fastify.close(); - - fastify = Fastify(); - fastify.get('/', async (request, reply) => { + +
Misklient + `); + }, + 'Mixed links': reply => { reply.header('Link', '; rel="redirect_uri"'); reply.send(` - - -
Misklient - `); - }); - await fastify.listen({ port: clientPort }); - - const client = getClient(); - - const response = await fetch(client.authorizeURL({ - redirect_uri, - scope: 'write:notes', - state: 'state', - code_challenge: 'code', - code_challenge_method: 'S256', - } as AuthorizationParamsExtended)); - assert.strictEqual(response.status, 200); - }); - - test('Multiple items in Link header', async () => { - await fastify.close(); - - fastify = Fastify(); - fastify.get('/', async (request, reply) => { + + +
Misklient + `); + }, + 'Multiple items in Link header': reply => { reply.header('Link', '; rel="redirect_uri",; rel="redirect_uri"'); reply.send(` - -
Misklient - `); - }); - await fastify.listen({ port: clientPort }); - - const client = getClient(); - - const response = await fetch(client.authorizeURL({ - redirect_uri, - scope: 'write:notes', - state: 'state', - code_challenge: 'code', - code_challenge_method: 'S256', - } as AuthorizationParamsExtended)); - assert.strictEqual(response.status, 200); - }); - - test('Multiple items in HTML', async () => { - await fastify.close(); - - fastify = Fastify(); - fastify.get('/', async (request, reply) => { + +
Misklient + `); + }, + 'Multiple items in HTML': reply => { reply.send(` - - - -
Misklient - `); + + + +
Misklient + `); + }, + }; + + for (const [title, replyFunc] of Object.entries(tests)) { + test(title, async () => { + await fastify.close(); + + fastify = Fastify(); + fastify.get('/', async (request, reply) => replyFunc(reply)); + await fastify.listen({ port: clientPort }); + + const client = getClient(); + + const response = await fetch(client.authorizeURL({ + redirect_uri, + scope: 'write:notes', + state: 'state', + code_challenge: 'code', + code_challenge_method: 'S256', + } as AuthorizationParamsExtended)); + assert.strictEqual(response.status, 200); }); - await fastify.listen({ port: clientPort }); - - const client = getClient(); - - const response = await fetch(client.authorizeURL({ - redirect_uri, - scope: 'write:notes', - state: 'state', - code_challenge: 'code', - code_challenge_method: 'S256', - } as AuthorizationParamsExtended)); - assert.strictEqual(response.status, 200); - }); + } test('No item', async () => { await fastify.close();