HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/javago_test/node_modules/node-input-validator/test/rules/dimensions.js
const assert = require('assert');
const fs = require('fs');

const { Validator } = require('../../lib/index');

describe('dimensions', () => {
  it('should pass with exact dimension', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'dimensions:minWidth=50,minHeight=32' },
    );

    const matched = await v.check();

    assert.equal(matched, true);
  });

  it('should fail with  min height', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'dimensions:minWidth=50,minHeight=50' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('should fail due to min width', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'dimensions:minWidth=100' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('should fail due to max width', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'dimensions:maxWidth=32' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('should fail due to max height', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'dimensions:maxHeight=30' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('should fail due to exact height', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'dimensions:width=100' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('should pass with excat width,height', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'dimensions:width=50,height=32' },
    );

    const matched = await v.check();

    assert.equal(matched, true);
  });

  it('should fail with excat width,height', async () => {
    const v = new Validator(
      { file: { path: './test/stubs/file-small.png' } }, { file: 'dimensions:width=50,height=50' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('should pass with buffer', async () => {
    const v = new Validator(
      { file: fs.readFileSync('./test/stubs/file-small.png') }, { file: 'dimensions:width:50' },
    );
    const matched = await v.check();

    assert.equal(matched, true);
  });

  it('should pass with nested buffer', async () => {
    const v = new Validator(
      { file: { buffer: fs.readFileSync('./test/stubs/file-small.png') } }, { file: 'dimensions:width:50' },
    );
    const matched = await v.check();

    assert.equal(matched, true);
  });

  it('should throw exception', async () => {
    try {
      const v = new Validator(
        { file: ['test'] }, { file: 'dimensions:width:50' },
      );
      await v.check();
    } catch (e) {
      assert.equal(e, 'Error: Dimensions rule only accepts Buffer,file path or size property in file object.');
    }
  });

  it('message should exist', async () => {
    const v = new Validator(
      { file: { path: './test/stubs/file-small.png' } }, { file: 'dimensions:width=50,height=50' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
    assert.equal(
      v.errors.file.message,
      v.getExistinParsedMessage({
        rule: 'dimensions',
        value: './test/stubs/file-small.png',
        attr: 'file',
        args: ['width=50', 'height=50'],
      }),
    );
  });
});