/* eslint-disable security/detect-non-literal-fs-filename */
const chai = require('chai')
const sinon = require('sinon')
const got = require('got')
const { globalErrorHandler } = require('../server/utils/error-handler.js')

const assert = chai.assert

async function generateStrategyError () {
  const provider = 'apple'
  const options = {
    method: 'POST',
    url: `http://127.0.0.1:8090/passport/auth/${provider}/callback`,
    json: {
      state: 'xxxxxxxxxxxx',
      error: 'user_cancelled_authorize'
    },
    responseType: 'json',
    throwHttpErrors: false,
    followRedirect: true
  }
  const response = await got(options)
  const headers = response.headers
  assert.equal(headers.location, '/passport/error')
}

describe('error-handler.js test', () => {
  it('should call globalErrorHandler with correct error message', () => {
    const globalErrorHandlerSpy = sinon.spy(globalErrorHandler)
    generateStrategyError()
    assert(globalErrorHandlerSpy.calledOnce)
    sinon.restore()
  })
})

@@ -141,16 +141,49 @@
 ('got')%0A
+const config = require('config')%0A
 const %7B 
@@ -276,16 +276,68 @@
 i.assert
+%0Aconst passportConfig = config.get('passportConfig')
 %0A%0Aasync 
@@ -384,253 +384,79 @@
 nst 
-provider = 'apple'%0A  const opti
+resp
 ons
+e
  = 
-%7B%0A    method: 'POST',%0A    url: %60http://127.0.0.1:8090/passport/auth/$%7Bprovider%7D/callback%60,%0A    json: %7B%0A      state: 'xxxxxxxxxxxx',%0A      error: 'user_cancelled_authorize'%0A    %7D,%0A    responseType: 'json',%0A   
+await got(%0A    'http://127.0.0.1:8090/passport/error',%0A    %7B
  thr
@@ -475,20 +475,16 @@
 : false,
-%0A   
  followR
@@ -496,53 +496,18 @@
 ct: 
-true%0A  %7D%0A  const response = await got(options
+false %7D%0A  
 )%0A  
@@ -576,18 +576,19 @@
 on, 
-'/
+%60$%7B
 passport
 /err
@@ -583,23 +583,73 @@
 passport
-/error'
+Config.failureRedirectUrl%7D?failure=An%2520error%2520occurred%60
 )%0A%7D%0A%0Ades
@@ -752,16 +752,22 @@
 essage',
+ async
  () =%3E %7B
@@ -835,16 +835,22 @@
 er)%0A    
+await 
 generate
@@ -869,22 +869,39 @@
 r()%0A    
-assert
+sinon.assert.calledOnce
 (globalE
@@ -914,27 +914,16 @@
 ndlerSpy
-.calledOnce
 )%0A    si

check error with correct message