"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test follow constraints', function () {
    let servers = [];
    let video1UUID;
    let video2UUID;
    let userToken;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(240000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            {
                const { uuid } = yield servers[0].videos.upload({ attributes: { name: 'video server 1' } });
                video1UUID = uuid;
            }
            {
                const { uuid } = yield servers[1].videos.upload({ attributes: { name: 'video server 2' } });
                video2UUID = uuid;
            }
            const user = {
                username: 'user1',
                password: 'super_password'
            };
            yield servers[0].users.create({ username: user.username, password: user.password });
            userToken = yield servers[0].login.getAccessToken(user);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
        });
    });
    describe('With a followed instance', function () {
        describe('With an unlogged user', function () {
            it('Should get the local video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].videos.get({ id: video1UUID });
                });
            });
            it('Should get the remote video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].videos.get({ id: video2UUID });
                });
            });
            it('Should list local account videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].videos.listByAccount({ handle: 'root@' + servers[0].host });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list remote account videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].videos.listByAccount({ handle: 'root@' + servers[1].host });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list local channel videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const handle = 'root_channel@' + servers[0].host;
                    const { total, data } = yield servers[0].videos.listByChannel({ handle });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list remote channel videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const handle = 'root_channel@' + servers[1].host;
                    const { total, data } = yield servers[0].videos.listByChannel({ handle });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
        });
        describe('With a logged user', function () {
            it('Should get the local video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].videos.getWithToken({ token: userToken, id: video1UUID });
                });
            });
            it('Should get the remote video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].videos.getWithToken({ token: userToken, id: video2UUID });
                });
            });
            it('Should list local account videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].videos.listByAccount({ token: userToken, handle: 'root@' + servers[0].host });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list remote account videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].videos.listByAccount({ token: userToken, handle: 'root@' + servers[1].host });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list local channel videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const handle = 'root_channel@' + servers[0].host;
                    const { total, data } = yield servers[0].videos.listByChannel({ token: userToken, handle });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list remote channel videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const handle = 'root_channel@' + servers[1].host;
                    const { total, data } = yield servers[0].videos.listByChannel({ token: userToken, handle });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
        });
    });
    describe('With a non followed instance', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield servers[0].follows.unfollow({ target: servers[1] });
            });
        });
        describe('With an unlogged user', function () {
            it('Should get the local video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].videos.get({ id: video1UUID });
                });
            });
            it('Should not get the remote video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const body = yield servers[0].videos.get({ id: video2UUID, expectedStatus: 403 });
                    const error = body;
                    const doc = 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/does_not_respect_follow_constraints';
                    (0, chai_1.expect)(error.type).to.equal(doc);
                    (0, chai_1.expect)(error.code).to.equal("does_not_respect_follow_constraints");
                    (0, chai_1.expect)(error.detail).to.equal('Cannot get this video regarding follow constraints');
                    (0, chai_1.expect)(error.error).to.equal(error.detail);
                    (0, chai_1.expect)(error.status).to.equal(403);
                    (0, chai_1.expect)(error.originUrl).to.contains(servers[1].url);
                });
            });
            it('Should list local account videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].videos.listByAccount({
                        token: null,
                        handle: 'root@' + servers[0].host
                    });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should not list remote account videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].videos.listByAccount({
                        token: null,
                        handle: 'root@' + servers[1].host
                    });
                    (0, chai_1.expect)(total).to.equal(0);
                    (0, chai_1.expect)(data).to.have.lengthOf(0);
                });
            });
            it('Should list local channel videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const handle = 'root_channel@' + servers[0].host;
                    const { total, data } = yield servers[0].videos.listByChannel({ token: null, handle });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should not list remote channel videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const handle = 'root_channel@' + servers[1].host;
                    const { total, data } = yield servers[0].videos.listByChannel({ token: null, handle });
                    (0, chai_1.expect)(total).to.equal(0);
                    (0, chai_1.expect)(data).to.have.lengthOf(0);
                });
            });
        });
        describe('With a logged user', function () {
            it('Should get the local video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].videos.getWithToken({ token: userToken, id: video1UUID });
                });
            });
            it('Should get the remote video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield servers[0].videos.getWithToken({ token: userToken, id: video2UUID });
                });
            });
            it('Should list local account videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].videos.listByAccount({ token: userToken, handle: 'root@' + servers[0].host });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list remote account videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const { total, data } = yield servers[0].videos.listByAccount({ token: userToken, handle: 'root@' + servers[1].host });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list local channel videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const handle = 'root_channel@' + servers[0].host;
                    const { total, data } = yield servers[0].videos.listByChannel({ token: userToken, handle });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
            it('Should list remote channel videos', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const handle = 'root_channel@' + servers[1].host;
                    const { total, data } = yield servers[0].videos.listByChannel({ token: userToken, handle });
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                });
            });
        });
    });
    describe('When following a remote account', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                yield servers[0].follows.follow({ handles: ['root@' + servers[1].host] });
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Should get the remote video with an unlogged user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.get({ id: video2UUID });
            });
        });
        it('Should get the remote video with a logged in user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.getWithToken({ token: userToken, id: video2UUID });
            });
        });
    });
    describe('When unfollowing a remote account', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                yield servers[0].follows.unfollow({ target: 'root@' + servers[1].host });
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Should not get the remote video with an unlogged user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const body = yield servers[0].videos.get({ id: video2UUID, expectedStatus: 403 });
                const error = body;
                (0, chai_1.expect)(error.code).to.equal("does_not_respect_follow_constraints");
            });
        });
        it('Should get the remote video with a logged in user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.getWithToken({ token: userToken, id: video2UUID });
            });
        });
    });
    describe('When following a remote channel', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                yield servers[0].follows.follow({ handles: ['root_channel@' + servers[1].host] });
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Should get the remote video with an unlogged user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.get({ id: video2UUID });
            });
        });
        it('Should get the remote video with a logged in user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.getWithToken({ token: userToken, id: video2UUID });
            });
        });
    });
    describe('When unfollowing a remote channel', function () {
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                yield servers[0].follows.unfollow({ target: 'root_channel@' + servers[1].host });
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Should not get the remote video with an unlogged user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const body = yield servers[0].videos.get({ id: video2UUID, expectedStatus: 403 });
                const error = body;
                (0, chai_1.expect)(error.code).to.equal("does_not_respect_follow_constraints");
            });
        });
        it('Should get the remote video with a logged in user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.getWithToken({ token: userToken, id: video2UUID });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=follow-constraints.js.map