Skip to content
Snippets Groups Projects
findUserPdl.spec.js 1.75 KiB
Newer Older
  • Learn to ignore specific revisions
  • const { errors } = require('cozy-konnector-libs')
    const { findUserPdl } = require('../src/findUserPdl')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const xml2js = require('xml2js')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    var mockParseUserPdl
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    var mockSoapRequest
    
    
    jest.mock('../src/parsing', () => ({
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      parseUserPdl: (mockParseUserPdl = jest.fn()),
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValue('response')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    jest.mock('easy-soap-request', () => (mockSoapRequest = jest.fn()))
    
    const responseMock = {
      response: {
        body: 'mockedBody',
      },
    }
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    
    
    describe('recherchePoint', () => {
      it('should throw LOGIN_FAILED for too many responses', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        mockSoapRequest.mockResolvedValue(responseMock)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        mockParseUserPdl.mockImplementationOnce(() => {
    
          throw new Error('fail')
        })
    
        try {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          await findUserPdl(
    
            'azertyuiop',
            'apiKey',
            'login@user.com',
            'John',
            '1 street',
            '69069',
            '69069'
          )
          expect(true).toBe(false)
        } catch (error) {
          expect(error).toBe(errors.LOGIN_FAILED)
        }
      })
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      it('should throw LOGIN_FAIL if soapRequest fails', async () => {
        mockSoapRequest.mockRejectedValueOnce('reject')
        try {
          await findUserPdl(
            'azertyuiop',
            'apiKey',
            'login@user.com',
            'John',
            '1 street',
            '69069',
            '69069'
          )
          expect(true).toBe(false)
        } catch (error) {
          expect(error).toBe(errors.LOGIN_FAILED)
        }
      })
    
    
      it('should return a correct pdl number', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        mockParseUserPdl.mockResolvedValue('12345')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        mockSoapRequest.mockResolvedValue(responseMock)
    
        expect(
          await findUserPdl(
            'azertyuiop',
            'apiKey',
            'login@user.com',
            'John',
            '1 street',
            '69069',
            '69069'
          )
        ).toBe('12345')
      })
    })