"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test services API validators', function () {
    let server;
    let playlistUUID;
    before(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(60000);
            server = yield (0, server_commands_1.createSingleServer)(1);
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            yield (0, server_commands_1.setDefaultVideoChannel)([server]);
            server.store.videoCreated = yield server.videos.upload({ attributes: { name: 'my super name' } });
            {
                const created = yield server.playlists.create({
                    attributes: {
                        displayName: 'super playlist',
                        privacy: 1,
                        videoChannelId: server.store.channel.id
                    }
                });
                playlistUUID = created.uuid;
            }
        });
    });
    describe('Test oEmbed API validators', function () {
        it('Should fail with an invalid url', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = 'hello.com';
                yield checkParamEmbed(server, embedUrl);
            });
        });
        it('Should fail with an invalid host', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = 'http://hello.com/videos/watch/' + server.store.videoCreated.uuid;
                yield checkParamEmbed(server, embedUrl);
            });
        });
        it('Should fail with an invalid element id', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watch/blabla`;
                yield checkParamEmbed(server, embedUrl);
            });
        });
        it('Should fail with an unknown element', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watch/88fc0165-d1f0-4a35-a51a-3b47f668689c`;
                yield checkParamEmbed(server, embedUrl, 404);
            });
        });
        it('Should fail with an invalid path', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watchs/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl);
            });
        });
        it('Should fail with an invalid max height', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl, 400, { maxheight: 'hello' });
            });
        });
        it('Should fail with an invalid max width', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl, 400, { maxwidth: 'hello' });
            });
        });
        it('Should fail with an invalid format', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl, 400, { format: 'blabla' });
            });
        });
        it('Should fail with a non supported format', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl, 501, { format: 'xml' });
            });
        });
        it('Should succeed with the correct params with a video', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`;
                const query = {
                    format: 'json',
                    maxheight: 400,
                    maxwidth: 400
                };
                yield checkParamEmbed(server, embedUrl, 200, query);
            });
        });
        it('Should succeed with the correct params with a playlist', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${playlistUUID}`;
                const query = {
                    format: 'json',
                    maxheight: 400,
                    maxwidth: 400
                };
                yield checkParamEmbed(server, embedUrl, 200, query);
            });
        });
    });
    after(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
function checkParamEmbed(server, embedUrl, expectedStatus = 400, query = {}) {
    const path = '/services/oembed';
    return (0, server_commands_1.makeGetRequest)({
        url: server.url,
        path,
        query: Object.assign(query, { url: embedUrl }),
        expectedStatus
    });
}
//# sourceMappingURL=services.js.map