workflow(server): added login-arl tests

This commit is contained in:
Roberto Tonino 2021-04-24 21:22:38 +02:00
parent d3e7e5a168
commit f83d017688
3 changed files with 52 additions and 1 deletions

View File

@ -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)
})
})

View File

@ -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()

View File

@ -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)