{"size": 5920, "ext": "js", "max_stars_repo_path": "test/unit/lib/oauth/OAuthValidator.test.js", "max_stars_repo_name": "georgi-logodazhki/ml-testing-toolkit", "max_stars_repo_stars_event_min_datetime": "2020-05-03T06:05:30.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-24T22:52:48.000Z", "max_issues_repo_path": "test/unit/lib/oauth/OAuthValidator.test.js", "max_issues_repo_name": "georgi-logodazhki/ml-testing-toolkit", "max_issues_repo_issues_event_min_datetime": "2020-04-16T09:24:10.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-18T10:10:50.000Z", "max_forks_repo_path": "test/unit/lib/oauth/OAuthValidator.test.js", "max_forks_repo_name": "georgi-logodazhki/ml-testing-toolkit", "max_forks_repo_forks_event_min_datetime": "2020-03-31T11:35:55.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-21T12:07:14.000Z"}
{"max_stars_count": 6, "max_issues_count": 26, "max_forks_count": 19, "avg_line_length": 39.4666666667, "max_line_length": 287, "alphanum_fraction": 0.6516891892}
/*****
 License
 --------------
 Copyright © 2017 Bill & Melinda Gates Foundation
 The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 Contributors
 --------------
 This is the official list of the Mojaloop project contributors for this file.
 Names of the original copyright holders (individuals or organizations)
 should be listed with a '*' in the first column. People who have
 contributed from an organization can be listed under the organization
 that actually holds the copyright for their contributions (see the
 Gates Foundation organization for an example). Those individuals should have
 their names indented and be marked with a '-'. Email address can be added
 optionally within square brackets <email>.
 * Gates Foundation

 * ModusBox
 * Vijaya Kumar Guthi <vijaya.guthi@modusbox.com> (Original Author)
 --------------
 ******/

const OAuthValidator = require('../../../../src/lib/oauth/OAuthValidator')

const Config = require('../../../../src/lib/config')
const SpyGetSystemConfig = jest.spyOn(Config, 'getSystemConfig')
const requestLogger = require('../../../../src/lib/requestLogger')
const jwt = require('jsonwebtoken')


jest.mock('../../../../src/lib/requestLogger')
jest.mock('../../../../src/lib/utils')
jest.mock('jsonwebtoken')
const SpyVerify = jest.spyOn(jwt, 'verify');
const token = 'asdf'

describe('OAuthValidator tests', () => {
  beforeAll(() => {
    requestLogger.logMessage.mockReturnValue()
  })
  describe('verifyToken', () => {
    beforeEach(() => {
      jest.resetAllMocks()
      SpyVerify.mockImplementation((token, secret, options, callback) => {
        callback(null, { iss: 'asdf', sub: 'asdf', clientId: 'asdf' });
      });
    })
    afterEach(() => {
      jest.resetAllMocks()
    })
    it('should not throw an error when EMBEDDED_CERTIFICATE and CERTIFICATE_FILE_NAME are empty', async () => {
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {}
      })
      await expect(OAuthValidator.verifyToken(token)).resolves.toBeTruthy()
    });
    it('should not throw an error when EMBEDDED_CERTIFICATE is not empty', async () => {
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {
          EMBEDDED_CERTIFICATE: 'EMBEDDED_CERTIFICATE'
        }
      })
      await expect(OAuthValidator.verifyToken(token)).resolves.toBeTruthy()
    });
    it('should not throw an error when EMBEDDED_CERTIFICATE is empty and CERTIFICATE_FILE_NAME is not empty and starts with "/"', async () => {
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {
          EMBEDDED_CERTIFICATE: null,
          CERTIFICATE_FILE_NAME: '/CERTIFICATE_FILE_NAME'
        }
      })
      await expect(OAuthValidator.verifyToken(token)).resolves.toBeTruthy()
    });
    it('should not throw an error when EMBEDDED_CERTIFICATE is empty and CERTIFICATE_FILE_NAME is not empty and not starts with "/"', async () => {
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {
          EMBEDDED_CERTIFICATE: null,
          CERTIFICATE_FILE_NAME: 'test/CERTIFICATE_FILE_NAME'
        }
      })
      await expect(OAuthValidator.verifyToken(token)).resolves.toBeTruthy()
    });
  });
  describe('additionalVerification', () => {
    beforeEach(() => {
      jest.resetAllMocks()
    })
    afterEach(() => {
      jest.resetAllMocks()
    })
    it('should not throw an error when jwtPayload has no sub and iss', async () => {
      SpyVerify.mockImplementation((token, secret, options, callback) => {
        callback(null, { iss: 'asdf', sub: 'asdf', clientId: 'asdf' });
      });
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {
          EMBEDDED_CERTIFICATE: 'asdf'
        }
      });
      await expect(OAuthValidator.verifyToken(token)).resolves.toBeTruthy()
    });
    it('should throw an error when jwtPayload has no sub', async () => {
      SpyVerify.mockImplementation((token, secret, options, callback) => {
        callback(null, { iss: 'asdf', clientId: 'asdf' });
      });
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {
          EMBEDDED_CERTIFICATE: 'asdf'
        }
      });
      await expect(OAuthValidator.verifyToken(token)).rejects.toThrowError()
    });
    it('should throw an error when jwtPayload has no iss', async () => {
      SpyVerify.mockImplementation((token, secret, options, callback) => {
        callback(null, { sub: 'asdf', clientId: 'asdf' });
      });
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {
          EMBEDDED_CERTIFICATE: 'asdf'
        }
      });
      await expect(OAuthValidator.verifyToken(token)).rejects.toThrowError()
    });
    it('should throw an error when jwtPayload has no clientId', async () => {
      SpyVerify.mockImplementation((token, secret, options, callback) => {
        callback(null, { iss: 'asdf', sub: 'asdf' });
      });
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {
          EMBEDDED_CERTIFICATE: 'asdf'
        }
      });
      await expect(OAuthValidator.verifyToken(token)).rejects.toThrowError()
    });
    it('should throw an error when jwtPayload is empty', async () => {
      SpyVerify.mockImplementation((token, secret, options, callback) => {
        callback(null, { });
      });
      SpyGetSystemConfig.mockReturnValue({
        OAUTH: {
          EMBEDDED_CERTIFICATE: 'asdf'
        }
      });
      await expect(OAuthValidator.verifyToken(token)).rejects.toThrowError()
    });

  });
});