From f83d01768845540a02b137e3c70262eca611ce9e Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Sat, 24 Apr 2021 21:22:38 +0200 Subject: [PATCH] workflow(server): added login-arl tests --- server/src/routes/api/post/login-arl.spec.ts | 40 ++++++++++++++++++++ server/src/routes/api/post/login-arl.ts | 11 +++++- server/src/tests/utils.ts | 2 + 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 server/src/routes/api/post/login-arl.spec.ts diff --git a/server/src/routes/api/post/login-arl.spec.ts b/server/src/routes/api/post/login-arl.spec.ts new file mode 100644 index 0000000..562bc02 --- /dev/null +++ b/server/src/routes/api/post/login-arl.spec.ts @@ -0,0 +1,40 @@ +import { appSendPost } from '../../../tests/utils' + +describe('login-arl requests', () => { + it('should respond 200 to calls with arl', async () => { + const responseStatusCollector: number[] = [] + const batchCalls = ['/api/login-arl/?arl=abcdef1234'] + + for (const uri of batchCalls) { + responseStatusCollector.push((await appSendPost(uri)).status) + } + + expect(responseStatusCollector).toMatchObject(new Array(batchCalls.length).fill(200)) + expect(responseStatusCollector).toMatchObject(new Array(responseStatusCollector.length).fill(200)) + }) + + it('should respond 400 to calls without arl', async () => { + const responseStatusCollector: number[] = [] + const batchCalls = ['/api/login-arl/', '/api/login-arl/?dummy=test', '/api/login-arl/?email=aaa@aa.com'] + + for (const uri of batchCalls) { + responseStatusCollector.push((await appSendPost(uri)).status) + } + + expect(responseStatusCollector).toMatchObject(new Array(responseStatusCollector.length).fill(400)) + }) + + it('should login using ARL', async () => { + const response = await appSendPost(`/api/login-arl/?arl=${process.env.DEEZER_ARL}`) + + expect(response.status).toBe(200) + expect(response.body).toBe(true) + }) + + it('should not login using wrong ARL', async () => { + const response = await appSendPost(`/api/login-arl/?arl=abcdef1234`) + + expect(response.status).toBe(200) + expect(response.body).toBe(false) + }) +}) diff --git a/server/src/routes/api/post/login-arl.ts b/server/src/routes/api/post/login-arl.ts index dfef45d..f72e7c3 100644 --- a/server/src/routes/api/post/login-arl.ts +++ b/server/src/routes/api/post/login-arl.ts @@ -1,4 +1,6 @@ import { RequestHandler } from 'express' +// @ts-expect-error +import { Deezer } from 'deezer-js' import { ApiHandler } from '../../../types' import { dz } from '../../../main' @@ -28,7 +30,14 @@ const handler: RequestHandler<{}, {}, {}, RawLoginArlQuery> = async (req, res, n loginParams.push(req.query.child) } - const response = await dz.login_via_arl(...loginParams) + let response + + if (process.env.NODE_ENV !== 'test') { + response = await dz.login_via_arl(...loginParams) + } else { + const testDz = new Deezer() + response = await testDz.login_via_arl(...loginParams) + } res.status(200).send(response) next() diff --git a/server/src/tests/utils.ts b/server/src/tests/utils.ts index 748ce95..f7b13fe 100644 --- a/server/src/tests/utils.ts +++ b/server/src/tests/utils.ts @@ -3,5 +3,7 @@ import request from 'supertest' import { app } from '../app' export const sendGet = (app: Application) => (uri: string) => request(app).get(uri).send() +export const sendPost = (app: Application) => (uri: string) => request(app).post(uri).send() export const appSendGet = sendGet(app) +export const appSendPost = sendPost(app)