{"size": 2837, "ext": "js", "max_stars_repo_path": "test/unit/registry-domain-sanitiser.js", "max_stars_repo_name": "qbikez/oc", "max_stars_repo_stars_event_min_datetime": "2018-01-26T12:50:16.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-30T05:09:14.000Z", "max_issues_repo_path": "test/unit/registry-domain-sanitiser.js", "max_issues_repo_name": "qbikez/oc", "max_issues_repo_issues_event_min_datetime": "2015-01-28T18:03:29.000Z", "max_issues_repo_issues_event_max_datetime": "2018-01-25T13:31:28.000Z", "max_forks_repo_path": "test/unit/registry-domain-sanitiser.js", "max_forks_repo_name": "qbikez/oc", "max_forks_repo_forks_event_min_datetime": "2015-01-21T11:34:15.000Z", "max_forks_repo_forks_event_max_datetime": "2018-01-04T04:48:15.000Z"}
{"max_stars_count": 851, "max_issues_count": 457, "max_forks_count": 79, "avg_line_length": 27.019047619, "max_line_length": 74, "alphanum_fraction": 0.5400070497}
'use strict';

const expect = require('chai').expect;

describe('registry : domain : sanitiser', () => {
  const sanitiser = require('../../dist/registry/domain/sanitiser');

  describe("when sanitising component's request parameters", () => {
    const sanitise = function (a, b) {
      return sanitiser.sanitiseComponentParameters(a, b);
    };

    describe('when component has boolean parameter', () => {
      it('should convert string to boolean when true', () => {
        const componentParameters = {
          isTrue: {
            type: 'boolean',
            mandatory: true,
            example: true
          }
        };

        const requestParameters = { isTrue: 'true' };
        const sanitisedParameters = sanitise(
          requestParameters,
          componentParameters
        );

        expect(sanitisedParameters).to.eql({ isTrue: true });
      });

      it('should convert string to boolean when true', () => {
        const componentParameters = {
          isTrue: {
            type: 'boolean',
            mandatory: true,
            example: true
          }
        };

        const requestParameters = { isTrue: 'false' };
        const sanitisedParameters = sanitise(
          requestParameters,
          componentParameters
        );

        expect(sanitisedParameters).to.eql({ isTrue: false });
      });
    });

    describe('when component has string parameter', () => {
      it('should convert null to empty', () => {
        const componentParameters = {
          myString: {
            type: 'string',
            mandatory: false,
            example: 'hello'
          }
        };

        const requestParameters = { myString: null };
        const sanitisedParameters = sanitise(
          requestParameters,
          componentParameters
        );

        expect(sanitisedParameters).to.eql({ myString: '' });
      });
    });

    describe('when component has number parameter', () => {
      it('should convert string to number', () => {
        const componentParameters = {
          age: {
            type: 'number',
            mandatory: true,
            example: 1234
          }
        };

        const requestParameters = { age: '123' };
        const sanitisedParameters = sanitise(
          requestParameters,
          componentParameters
        );

        expect(sanitisedParameters).to.eql({ age: 123 });
      });
    });

    describe('when component have not defined optional parameter', () => {
      it('should keep the parameter as it is', () => {
        const componentParameters = {};
        const requestParameters = { age: 123 };
        const sanitisedParameters = sanitise(
          requestParameters,
          componentParameters
        );

        expect(sanitisedParameters).to.eql({ age: 123 });
      });
    });
  });
});
