"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test services API validators', function () {
    let server;
    let playlistUUID;
    let privateVideo;
    let unlistedVideo;
    let privatePlaylist;
    let unlistedPlaylist;
    before(function () {
        return 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' } });
            privateVideo = yield server.videos.quickUpload({ name: 'private', privacy: 3 });
            unlistedVideo = yield server.videos.quickUpload({ name: 'unlisted', privacy: 2 });
            {
                const created = yield server.playlists.create({
                    attributes: {
                        displayName: 'super playlist',
                        privacy: 1,
                        videoChannelId: server.store.channel.id
                    }
                });
                playlistUUID = created.uuid;
                privatePlaylist = yield server.playlists.create({
                    attributes: {
                        displayName: 'private',
                        privacy: 3,
                        videoChannelId: server.store.channel.id
                    }
                });
                unlistedPlaylist = yield server.playlists.create({
                    attributes: {
                        displayName: 'unlisted',
                        privacy: 2,
                        videoChannelId: server.store.channel.id
                    }
                });
            }
        });
    });
    describe('Test oEmbed API validators', function () {
        it('Should fail with an invalid url', function () {
            return 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 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 tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/blabla`;
                yield checkParamEmbed(server, embedUrl);
            });
        });
        it('Should fail with an unknown element', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/88fc0165-d1f0-4a35-a51a-3b47f668689c`;
                yield checkParamEmbed(server, embedUrl, 404);
            });
        });
        it('Should fail with an invalid path', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watchs/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl);
            });
        });
        it('Should fail with an invalid max height', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl, 400, { maxheight: 'hello' });
            });
        });
        it('Should fail with an invalid max width', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl, 400, { maxwidth: 'hello' });
            });
        });
        it('Should fail with an invalid format', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl, 400, { format: 'blabla' });
            });
        });
        it('Should fail with a non supported format', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}`;
                yield checkParamEmbed(server, embedUrl, 501, { format: 'xml' });
            });
        });
        it('Should fail with a private video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/${privateVideo.uuid}`;
                yield checkParamEmbed(server, embedUrl, 403);
            });
        });
        it('Should fail with an unlisted video with the int id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/${unlistedVideo.id}`;
                yield checkParamEmbed(server, embedUrl, 403);
            });
        });
        it('Should succeed with an unlisted video using the uuid id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const uuid of [unlistedVideo.uuid, unlistedVideo.shortUUID]) {
                    const embedUrl = `${server.url}/videos/watch/${uuid}`;
                    yield checkParamEmbed(server, embedUrl, 200);
                }
            });
        });
        it('Should fail with a private playlist', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/playlist/${privatePlaylist.uuid}`;
                yield checkParamEmbed(server, embedUrl, 403);
            });
        });
        it('Should fail with an unlisted playlist using the int id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/playlist/${unlistedPlaylist.id}`;
                yield checkParamEmbed(server, embedUrl, 403);
            });
        });
        it('Should succeed with an unlisted playlist using the uuid id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const uuid of [unlistedPlaylist.uuid, unlistedPlaylist.shortUUID]) {
                    const embedUrl = `${server.url}/videos/watch/playlist/${uuid}`;
                    yield checkParamEmbed(server, embedUrl, 200);
                }
            });
        });
        it('Should succeed with the correct params with a video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/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 tslib_1.__awaiter(this, void 0, void 0, function* () {
                const embedUrl = `${server.url}/videos/watch/playlist/${playlistUUID}`;
                const query = {
                    format: 'json',
                    maxheight: 400,
                    maxwidth: 400
                };
                yield checkParamEmbed(server, embedUrl, 200, query);
            });
        });
    });
    after(function () {
        return 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