"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = require("ava");
const _1 = require(".");
const Schemas = require("./Schemas");
const Perms = require("./Permissions");
ava_1.default('createValidator - should eval complex validator', t => {
    const validator = _1.createValidator(Schemas.TrackSchema);
    eval(`const validate = ${validator}`);
    t.pass();
});
ava_1.default('createValidator - should not throw for valid Track', t => {
    const validator = _1.createValidator(Schemas.TrackSchema);
    const val = {};
    eval(`val.idate = ${validator}`);
    val.idate({
        name: 'foo',
        artist: '42',
        album: '13370'
    });
    val.idate({
        name: 'foo',
        artist: '42',
        album: '13370',
        track_artist: 'foo'
    });
    val.idate({
        name: 'foo',
        artist: '42',
        album: '1337',
        number: 1337
    });
    t.pass();
});
ava_1.default('createValidator - should throw for missing required', t => {
    const validator = _1.createValidator(Schemas.TrackSchema);
    const val = {};
    eval(`val.idate = ${validator}`);
    t.throws(() => val.idate({
        name: 'foo',
        album: '13370'
    }));
    t.pass();
});
ava_1.default('createValidator - should throw for invalid value', t => {
    const validator = _1.createValidator(Schemas.TrackSchema);
    const val = {};
    eval(`val.idate = ${validator}`);
    t.throws(() => val.idate({
        name: 1337,
        album: '13370'
    }));
    t.throws(() => val.idate({
        name: 1337,
        album: { foo: 'bar' }
    }));
    t.pass();
});
ava_1.default('createValidator - should not throw for _rev and _revisions', t => {
    const validator = _1.createValidator(Schemas.TrackSchema);
    const val = {};
    eval(`val.idate = ${validator}`);
    val.idate({
        name: 'foo',
        artist: '42',
        album: '13370',
        _rev: 'test',
        _revisions: 'test'
    });
    t.pass();
});
ava_1.default('createValidator - should throw for invalid value', t => {
    const validator = _1.createValidator(Schemas.TrackSchema);
    const val = {};
    eval(`val.idate = ${validator}`);
    t.throws(() => val.idate({
        name: 'foo',
        artist: '42',
        album: '1337',
        number: 1337,
        foo: 'bar'
    }));
    t.pass();
});
ava_1.default('createValidator - allows only admin/app to create track', t => {
    const validator = _1.createValidator(Schemas.TrackSchema, Perms.TrackPermissions);
    const props = {
        name: 'foo',
        artist: '42',
        album: '1337',
        number: 1337
    };
    let validate = {};
    eval(`validate = ${validator}`);
    validate(props, undefined, {
        roles: ['_admin']
    });
    validate(props, undefined, {
        roles: ['foo', 'app', 'end_user']
    });
    t.throws(() => validate(props, undefined, {
        roles: ['end_user', 'user', 'foo']
    }));
});
ava_1.default('createValidator - allows only admin/app to update track', t => {
    const validator = _1.createValidator(Schemas.TrackSchema, Perms.TrackPermissions);
    const props = {
        name: 'bar',
        number: 42
    };
    const old = {
        name: 'foo',
        artist: '42',
        album: '1337',
        number: 1337
    };
    let validate = {};
    eval(`validate = ${validator}`);
    validate(props, old, {
        roles: ['_admin']
    });
    validate(props, old, {
        roles: ['foo', 'app', 'end_user']
    });
    t.throws(() => validate(props, old, {
        roles: ['foo']
    }));
});
ava_1.default('createValidator - allows only to update specific props', t => {
    const validator = _1.createValidator(Schemas.TrackSchema, Perms.TrackPermissions);
    const props = {
        name: 'bar',
        number: 42,
        artist: 'foo'
    };
    const old = {
        name: 'foo',
        artist: '42',
        album: '1337',
        number: 1337
    };
    let validate = {};
    eval(`validate = ${validator}`);
    t.throws(() => validate(props, old, {
        roles: ['_admin']
    }), Object);
});
//# sourceMappingURL=index.test.js.map