"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const shared_1 = require("../../shared");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test blocklist API validators', function () {
    let servers;
    let server;
    let userAccessToken;
    before(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(60000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            server = servers[0];
            const user = { username: 'user1', password: 'password' };
            yield server.users.create({ username: user.username, password: user.password });
            userAccessToken = yield server.login.getAccessToken(user);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
        });
    });
    describe('When managing user blocklist', function () {
        describe('When managing user accounts blocklist', function () {
            const path = '/api/v1/users/me/blocklist/accounts';
            describe('When listing blocked accounts', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeGetRequest)({
                            url: server.url,
                            path,
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with a bad start pagination', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadStartPagination)(server.url, path, server.accessToken);
                    });
                });
                it('Should fail with a bad count pagination', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadCountPagination)(server.url, path, server.accessToken);
                    });
                });
                it('Should fail with an incorrect sort', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadSortPagination)(server.url, path, server.accessToken);
                    });
                });
            });
            describe('When blocking an account', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            path,
                            fields: { accountName: 'user1' },
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with an unknown account', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { accountName: 'user2' },
                            expectedStatus: 404
                        });
                    });
                });
                it('Should fail to block ourselves', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { accountName: 'root' },
                            expectedStatus: 409
                        });
                    });
                });
                it('Should succeed with the correct params', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { accountName: 'user1' },
                            expectedStatus: 204
                        });
                    });
                });
            });
            describe('When unblocking an account', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/user1',
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with an unknown account block', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/user2',
                            token: server.accessToken,
                            expectedStatus: 404
                        });
                    });
                });
                it('Should succeed with the correct params', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/user1',
                            token: server.accessToken,
                            expectedStatus: 204
                        });
                    });
                });
            });
        });
        describe('When managing user servers blocklist', function () {
            const path = '/api/v1/users/me/blocklist/servers';
            describe('When listing blocked servers', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeGetRequest)({
                            url: server.url,
                            path,
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with a bad start pagination', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadStartPagination)(server.url, path, server.accessToken);
                    });
                });
                it('Should fail with a bad count pagination', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadCountPagination)(server.url, path, server.accessToken);
                    });
                });
                it('Should fail with an incorrect sort', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadSortPagination)(server.url, path, server.accessToken);
                    });
                });
            });
            describe('When blocking a server', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            path,
                            fields: { host: 'localhost:9002' },
                            expectedStatus: 401
                        });
                    });
                });
                it('Should succeed with an unknown server', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { host: 'localhost:9003' },
                            expectedStatus: 204
                        });
                    });
                });
                it('Should fail with our own server', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { host: 'localhost:' + server.port },
                            expectedStatus: 409
                        });
                    });
                });
                it('Should succeed with the correct params', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { host: 'localhost:' + servers[1].port },
                            expectedStatus: 204
                        });
                    });
                });
            });
            describe('When unblocking a server', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/localhost:' + servers[1].port,
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with an unknown server block', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/localhost:9004',
                            token: server.accessToken,
                            expectedStatus: 404
                        });
                    });
                });
                it('Should succeed with the correct params', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/localhost:' + servers[1].port,
                            token: server.accessToken,
                            expectedStatus: 204
                        });
                    });
                });
            });
        });
    });
    describe('When managing server blocklist', function () {
        describe('When managing server accounts blocklist', function () {
            const path = '/api/v1/server/blocklist/accounts';
            describe('When listing blocked accounts', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeGetRequest)({
                            url: server.url,
                            path,
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with a user without the appropriate rights', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeGetRequest)({
                            url: server.url,
                            token: userAccessToken,
                            path,
                            expectedStatus: 403
                        });
                    });
                });
                it('Should fail with a bad start pagination', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadStartPagination)(server.url, path, server.accessToken);
                    });
                });
                it('Should fail with a bad count pagination', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadCountPagination)(server.url, path, server.accessToken);
                    });
                });
                it('Should fail with an incorrect sort', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadSortPagination)(server.url, path, server.accessToken);
                    });
                });
            });
            describe('When blocking an account', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            path,
                            fields: { accountName: 'user1' },
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with a user without the appropriate rights', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: userAccessToken,
                            path,
                            fields: { accountName: 'user1' },
                            expectedStatus: 403
                        });
                    });
                });
                it('Should fail with an unknown account', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { accountName: 'user2' },
                            expectedStatus: 404
                        });
                    });
                });
                it('Should fail to block ourselves', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { accountName: 'root' },
                            expectedStatus: 409
                        });
                    });
                });
                it('Should succeed with the correct params', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { accountName: 'user1' },
                            expectedStatus: 204
                        });
                    });
                });
            });
            describe('When unblocking an account', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/user1',
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with a user without the appropriate rights', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/user1',
                            token: userAccessToken,
                            expectedStatus: 403
                        });
                    });
                });
                it('Should fail with an unknown account block', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/user2',
                            token: server.accessToken,
                            expectedStatus: 404
                        });
                    });
                });
                it('Should succeed with the correct params', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/user1',
                            token: server.accessToken,
                            expectedStatus: 204
                        });
                    });
                });
            });
        });
        describe('When managing server servers blocklist', function () {
            const path = '/api/v1/server/blocklist/servers';
            describe('When listing blocked servers', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeGetRequest)({
                            url: server.url,
                            path,
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with a user without the appropriate rights', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeGetRequest)({
                            url: server.url,
                            token: userAccessToken,
                            path,
                            expectedStatus: 403
                        });
                    });
                });
                it('Should fail with a bad start pagination', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadStartPagination)(server.url, path, server.accessToken);
                    });
                });
                it('Should fail with a bad count pagination', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadCountPagination)(server.url, path, server.accessToken);
                    });
                });
                it('Should fail with an incorrect sort', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, shared_1.checkBadSortPagination)(server.url, path, server.accessToken);
                    });
                });
            });
            describe('When blocking a server', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            path,
                            fields: { host: 'localhost:' + servers[1].port },
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with a user without the appropriate rights', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: userAccessToken,
                            path,
                            fields: { host: 'localhost:' + servers[1].port },
                            expectedStatus: 403
                        });
                    });
                });
                it('Should succeed with an unknown server', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { host: 'localhost:9003' },
                            expectedStatus: 204
                        });
                    });
                });
                it('Should fail with our own server', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { host: 'localhost:' + server.port },
                            expectedStatus: 409
                        });
                    });
                });
                it('Should succeed with the correct params', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makePostBodyRequest)({
                            url: server.url,
                            token: server.accessToken,
                            path,
                            fields: { host: 'localhost:' + servers[1].port },
                            expectedStatus: 204
                        });
                    });
                });
            });
            describe('When unblocking a server', function () {
                it('Should fail with an unauthenticated user', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/localhost:' + servers[1].port,
                            expectedStatus: 401
                        });
                    });
                });
                it('Should fail with a user without the appropriate rights', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/localhost:' + servers[1].port,
                            token: userAccessToken,
                            expectedStatus: 403
                        });
                    });
                });
                it('Should fail with an unknown server block', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/localhost:9004',
                            token: server.accessToken,
                            expectedStatus: 404
                        });
                    });
                });
                it('Should succeed with the correct params', function () {
                    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                        yield (0, server_commands_1.makeDeleteRequest)({
                            url: server.url,
                            path: path + '/localhost:' + servers[1].port,
                            token: server.accessToken,
                            expectedStatus: 204
                        });
                    });
                });
            });
        });
    });
    describe('When getting blocklist status', function () {
        const path = '/api/v1/blocklist/status';
        it('Should fail with a bad token', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: 'false',
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with a bad accounts field', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    query: {
                        accounts: 1
                    },
                    expectedStatus: 400
                });
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    query: {
                        accounts: [1]
                    },
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with a bad hosts field', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    query: {
                        hosts: 1
                    },
                    expectedStatus: 400
                });
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    query: {
                        hosts: [1]
                    },
                    expectedStatus: 400
                });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    query: {},
                    expectedStatus: 200
                });
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    query: {
                        hosts: ['example.com'],
                        accounts: ['john@example.com']
                    },
                    expectedStatus: 200
                });
            });
        });
    });
    after(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=blocklist.js.map