dedupe CID test logic

This commit is contained in:
Kagami Sascha Rosylight 2023-06-04 14:50:30 +02:00
parent 9a5fa00f9a
commit 78c6bb1cc2
1 changed files with 47 additions and 95 deletions

View File

@ -10,7 +10,7 @@ import { AuthorizationCode, type AuthorizationTokenConfig } from 'simple-oauth2'
import pkceChallenge from 'pkce-challenge'; import pkceChallenge from 'pkce-challenge';
import { JSDOM } from 'jsdom'; import { JSDOM } from 'jsdom';
import * as misskey from 'misskey-js'; 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 { port, relativeFetch, signup, startServer } from '../utils.js';
import type { INestApplicationContext } from '@nestjs/common'; import type { INestApplicationContext } from '@nestjs/common';
@ -83,7 +83,7 @@ async function fetchDecisionFromResponse(response: Response, user: misskey.entit
return await fetchDecision(transactionId, user, { cancel }); 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 client = getClient();
const response = await fetch(client.authorizeURL({ const response = await fetch(client.authorizeURL({
@ -627,94 +627,45 @@ describe('OAuth', () => {
describe('Client Information Discovery', () => { describe('Client Information Discovery', () => {
describe('Redirection', () => { describe('Redirection', () => {
test('Read HTTP header', async () => { const tests: Record<string, (reply: FastifyReply) => void> = {
await fastify.close(); 'Read HTTP header': reply => {
fastify = Fastify();
fastify.get('/', async (request, reply) => {
reply.header('Link', '</redirect>; rel="redirect_uri"'); reply.header('Link', '</redirect>; rel="redirect_uri"');
reply.send(` reply.send(`
<!DOCTYPE html> <!DOCTYPE html>
<div class="h-app"><div class="p-name">Misklient <div class="h-app"><div class="p-name">Misklient
`); `);
}); },
await fastify.listen({ port: clientPort }); 'Mixed links': reply => {
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) => {
reply.header('Link', '</redirect>; rel="redirect_uri"'); reply.header('Link', '</redirect>; rel="redirect_uri"');
reply.send(` reply.send(`
<!DOCTYPE html> <!DOCTYPE html>
<link rel="redirect_uri" href="/redirect2" /> <link rel="redirect_uri" href="/redirect2" />
<div class="h-app"><div class="p-name">Misklient <div class="h-app"><div class="p-name">Misklient
`); `);
}); },
await fastify.listen({ port: clientPort }); 'Multiple items in Link header': reply => {
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) => {
reply.header('Link', '</redirect2>; rel="redirect_uri",</redirect>; rel="redirect_uri"'); reply.header('Link', '</redirect2>; rel="redirect_uri",</redirect>; rel="redirect_uri"');
reply.send(` reply.send(`
<!DOCTYPE html> <!DOCTYPE html>
<div class="h-app"><div class="p-name">Misklient <div class="h-app"><div class="p-name">Misklient
`); `);
}); },
await fastify.listen({ port: clientPort }); 'Multiple items in HTML': reply => {
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) => {
reply.send(` reply.send(`
<!DOCTYPE html> <!DOCTYPE html>
<link rel="redirect_uri" href="/redirect2" /> <link rel="redirect_uri" href="/redirect2" />
<link rel="redirect_uri" href="/redirect" /> <link rel="redirect_uri" href="/redirect" />
<div class="h-app"><div class="p-name">Misklient <div class="h-app"><div class="p-name">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 }); await fastify.listen({ port: clientPort });
const client = getClient(); const client = getClient();
@ -728,6 +679,7 @@ describe('OAuth', () => {
} as AuthorizationParamsExtended)); } as AuthorizationParamsExtended));
assert.strictEqual(response.status, 200); assert.strictEqual(response.status, 200);
}); });
}
test('No item', async () => { test('No item', async () => {
await fastify.close(); await fastify.close();