<commit_before>import Linter from 'linter';
import ManifestJSONParser from 'parsers/manifestjson';

import { PACKAGE_EXTENSION, VALID_MANIFEST_VERSION } from 'const';
import * as messages from 'messages';
import { assertHasMatchingError, validManifestJSON } from '../helpers';

describe('ManifestJSONParser', function() {

  it('should have empty metadata if bad JSON', () => {
    var addonLinter = new Linter({_: ['bar']});
    var manifestJSONParser = new ManifestJSONParser('blah',
                                                    addonLinter.collector);
    assert.equal(manifestJSONParser.isValid, false);
    var errors = addonLinter.collector.errors;
    assert.equal(errors.length, 1);
    assert.equal(errors[0].code, messages.JSON_INVALID.code);
    assert.include(errors[0].message, 'Your JSON is not valid.');

    var metadata = manifestJSONParser.getMetadata();
    assert.equal(metadata.manifestVersion, null);
    assert.equal(metadata.name, null);
    assert.equal(metadata.version, null);
  });

  describe('id', function() {

    it('should return the correct id', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON();
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, true);
      var metadata = manifestJSONParser.getMetadata();
      assert.equal(metadata.id, '{daf44bf7-a45e-4450-979c-91cf07434c3d}');
    });

    it('should fail on invalid id', () => {
      // This is probably covered in other tests, but verifies that if the
      // id is something incorrect, you shouldn't even be calling getMetadata.
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({applications: {gecko: {id: 'wat'}}});
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      assertHasMatchingError(addonLinter.collector.errors, {
        code: messages.JSON_INVALID.code,
        message: /"\/applications\/gecko\/id" should match pattern/,
      });
    });

    it('should return null if undefined', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({applications: {}});
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, true);
      var metadata = manifestJSONParser.getMetadata();
      assert.equal(metadata.id, null);
    });

  });

  describe('manifestVersion', function() {

    it('should collect an error on invalid manifest_version value', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({manifest_version: 'whatever'});
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var errors = addonLinter.collector.errors;
      assert.equal(errors.length, 1);
      assert.equal(errors[0].code, messages.MANIFEST_FIELD_INVALID.code);
      assert.include(errors[0].message, '/manifest_version');
    });

    it('should collect an error with numeric string value', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({manifest_version: '1'});
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var errors = addonLinter.collector.errors;
      assert.equal(errors[0].code, messages.MANIFEST_FIELD_INVALID.code);
      assert.include(errors[0].message, '/manifest_version');
    });

    it('should have the right manifestVersion', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON();
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, true);
      var metadata = manifestJSONParser.getMetadata();
      assert.equal(metadata.manifestVersion, VALID_MANIFEST_VERSION);
    });

  });

  describe('bad permissions', function() {

    it('should not error if permission is a string (even if unknown)', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({
        permissions: [
          'identity',
          'fileSystem',
        ],
      });

      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var warnings = addonLinter.collector.warnings;
      assert.lengthOf(addonLinter.collector.errors, 0);
      assertHasMatchingError(warnings, {
        code: messages.MANIFEST_PERMISSIONS.code,
        message: /Unknown permissions "fileSystem"/,
      });
    });

    it('should error if permission is not a string', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({
        permissions: [
          'identity',
          {
            fileSystem: ['write'],
          },
        ],
      });

      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var errors = addonLinter.collector.errors;
      assert.equal(errors[0].code, messages.MANIFEST_BAD_PERMISSION.code);
      assert.include(errors[0].message, 'should be equal to one');
    });

    it('should error if permission is duplicated', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({
        permissions: [
          'identity',
          'identity',
        ],
      });

      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var errors = addonLinter.collector.errors;
      assert.equal(errors[0].code, messages.MANIFEST_BAD_PERMISSION.code);
      assert.include(errors[0].message, 'should NOT have duplicate items');
    });

  });

<commit_msg>Fix test<commit_after>import Linter from 'linter';
import ManifestJSONParser from 'parsers/manifestjson';

import { PACKAGE_EXTENSION, VALID_MANIFEST_VERSION } from 'const';
import * as messages from 'messages';
import { assertHasMatchingError, validManifestJSON } from '../helpers';

describe('ManifestJSONParser', function() {

  it('should have empty metadata if bad JSON', () => {
    var addonLinter = new Linter({_: ['bar']});
    var manifestJSONParser = new ManifestJSONParser('blah',
                                                    addonLinter.collector);
    assert.equal(manifestJSONParser.isValid, false);
    var errors = addonLinter.collector.errors;
    assert.equal(errors.length, 1);
    assert.equal(errors[0].code, messages.JSON_INVALID.code);
    assert.include(errors[0].message, 'Your JSON is not valid.');

    var metadata = manifestJSONParser.getMetadata();
    assert.equal(metadata.manifestVersion, null);
    assert.equal(metadata.name, null);
    assert.equal(metadata.version, null);
  });

  describe('id', function() {

    it('should return the correct id', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON();
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, true);
      var metadata = manifestJSONParser.getMetadata();
      assert.equal(metadata.id, '{daf44bf7-a45e-4450-979c-91cf07434c3d}');
    });

    it('should fail on invalid id', () => {
      // This is probably covered in other tests, but verifies that if the
      // id is something incorrect, you shouldn't even be calling getMetadata.
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({applications: {gecko: {id: 'wat'}}});
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      assertHasMatchingError(addonLinter.collector.errors, {
        code: messages.JSON_INVALID.code,
        message: /"\/applications\/gecko\/id" should match pattern/,
      });
    });

    it('should return null if undefined', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({applications: {}});
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, true);
      var metadata = manifestJSONParser.getMetadata();
      assert.equal(metadata.id, null);
    });

  });

  describe('manifestVersion', function() {

    it('should collect an error on invalid manifest_version value', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({manifest_version: 'whatever'});
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var errors = addonLinter.collector.errors;
      assert.equal(errors.length, 1);
      assert.equal(errors[0].code, messages.MANIFEST_FIELD_INVALID.code);
      assert.include(errors[0].message, '/manifest_version');
    });

    it('should collect an error with numeric string value', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({manifest_version: '1'});
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var errors = addonLinter.collector.errors;
      assert.equal(errors[0].code, messages.MANIFEST_FIELD_INVALID.code);
      assert.include(errors[0].message, '/manifest_version');
    });

    it('should have the right manifestVersion', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON();
      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, true);
      var metadata = manifestJSONParser.getMetadata();
      assert.equal(metadata.manifestVersion, VALID_MANIFEST_VERSION);
    });

  });

  describe('bad permissions', function() {

    it('should not error if permission is a string (even if unknown)', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({
        permissions: [
          'identity',
          'fileSystem',
        ],
      });

      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var warnings = addonLinter.collector.warnings;
      assert.lengthOf(addonLinter.collector.errors, 0);
      assertHasMatchingError(warnings, {
        code: messages.MANIFEST_PERMISSIONS.code,
        message: /Unknown permissions "fileSystem"/,
      });
    });

    it('should error if permission is not a string', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({
        permissions: [
          'identity',
          {
            fileSystem: ['write'],
          },
        ],
      });

      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var errors = addonLinter.collector.errors;
      assert.equal(errors[0].code, messages.MANIFEST_BAD_PERMISSION.code);
      assert.include(errors[0].message, 'should be string');
    });

    it('should error if permission is duplicated', () => {
      var addonLinter = new Linter({_: ['bar']});
      var json = validManifestJSON({
        permissions: [
          'identity',
          'identity',
        ],
      });

      var manifestJSONParser = new ManifestJSONParser(json,
                                                      addonLinter.collector);
      assert.equal(manifestJSONParser.isValid, false);
      var errors = addonLinter.collector.errors;
      assert.equal(errors[0].code, messages.MANIFEST_BAD_PERMISSION.code);
      assert.include(errors[0].message, 'should NOT have duplicate items');
    });

  });


{'fraction_non_alphanumeric': 0.12087487441069635, 'fraction_numerical': 0.0052554293222041885, 'mean_word_length': 2.467309753483387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4992594', 'n_tokens_mistral': 3508, 'n_tokens_neox': 3420, 'n_words': 757}