Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: Fix import endpoints #26074

Merged
merged 6 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 8 additions & 54 deletions apps/meteor/app/api/server/v1/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from '@rocket.chat/rest-typings';

import { API } from '../api';
import { hasPermission } from '../../../authorization/server';
import { Imports } from '../../../models/server';
import { Importers } from '../../../importer/server';
import { executeUploadImportFile } from '../../../importer/server/methods/uploadImportFile';
Expand Down Expand Up @@ -42,9 +41,7 @@ API.v1.addRoute(
post() {
const { fileUrl, importerKey } = this.bodyParams;

Meteor.runAsUser(this.userId, () => {
API.v1.success(Meteor.call('downloadPublicImportFile', fileUrl, importerKey));
});
Meteor.call('downloadPublicImportFile', fileUrl, importerKey);

return API.v1.success();
},
Expand All @@ -61,9 +58,7 @@ API.v1.addRoute(
post() {
const { input } = this.bodyParams;

Meteor.runAsUser(this.userId, () => {
API.v1.success(Meteor.call('startImport', input));
});
Meteor.call('startImport', input);

return API.v1.success();
},
Expand All @@ -78,11 +73,7 @@ API.v1.addRoute(
},
{
get() {
let result;
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getImportFileData');
});

const result = Meteor.call('getImportFileData');
return API.v1.success(result);
},
},
Expand All @@ -96,11 +87,7 @@ API.v1.addRoute(
},
{
get() {
let result;
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getImportProgress');
});

const result = Meteor.call('getImportProgress');
return API.v1.success(result);
},
},
Expand All @@ -114,11 +101,7 @@ API.v1.addRoute(
},
{
get() {
let result;
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getLatestImportOperations');
});

const result = Meteor.call('getLatestImportOperations');
return API.v1.success(result);
},
},
Expand All @@ -129,19 +112,10 @@ API.v1.addRoute(
{
authRequired: true,
validateParams: isDownloadPendingFilesParamsPOST,
permissionsRequired: ['run-import'],
},
{
post() {
if (!this.userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'downloadPendingFiles',
});
}

if (!hasPermission(this.userId, 'run-import')) {
throw new Meteor.Error('not_authorized');
}

const importer = Importers.get('pending-files');
if (!importer) {
throw new Meteor.Error('error-importer-not-defined', 'The Pending File Importer was not found.', {
Expand All @@ -153,7 +127,6 @@ API.v1.addRoute(
const count = importer.instance.prepareFileCount();

return API.v1.success({
success: true,
count,
});
},
Expand All @@ -165,19 +138,10 @@ API.v1.addRoute(
{
authRequired: true,
validateParams: isDownloadPendingAvatarsParamsPOST,
permissionsRequired: ['run-import'],
},
{
post() {
if (!this.userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'downloadPendingAvatars',
});
}

if (!hasPermission(this.userId, 'run-import')) {
throw new Meteor.Error('not_authorized');
}

const importer = Importers.get('pending-avatars');
if (!importer) {
throw new Meteor.Error('error-importer-not-defined', 'The Pending File Importer was not found.', {
Expand All @@ -189,7 +153,6 @@ API.v1.addRoute(
const count = importer.instance.prepareFileCount();

return API.v1.success({
success: true,
count,
});
},
Expand All @@ -201,19 +164,10 @@ API.v1.addRoute(
{
authRequired: true,
validateParams: isGetCurrentImportOperationParamsGET,
permissionsRequired: ['run-import'],
},
{
get() {
if (!this.userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'getCurrentImportOperation',
});
}

if (!hasPermission(this.userId, 'run-import')) {
throw new Meteor.Error('not_authorized');
}

const operation = Imports.findLastImport();
return API.v1.success({
success: true,
Expand Down
37 changes: 37 additions & 0 deletions apps/meteor/tests/end-to-end/api/import.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect } from 'chai';
import { Response } from 'supertest';

import { getCredentials, api, request, credentials } from '../../data/api-data.js';

describe('Imports', function () {
this.retries(0);

before((done) => getCredentials(done));

describe('[/getCurrentImportOperation]', () => {
it('should return the current import operation', (done) => {
request
.get(api('getCurrentImportOperation'))
.set(credentials)
.expect(200)
.expect((res: Response) => {
expect(res.body.success).to.be.true;
expect(res.body.operation).not.be.null;
})
.end(done);
});
it('should return an error if params are not valid', (done) => {
request
.get(api('getCurrentImportOperation'))
.set(credentials)
.query({
any: 'test',
})
.expect(400)
.expect((res: Response) => {
expect(res.body.success).to.be.false;
})
.end(done);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@ const ajv = new Ajv({
coerceTypes: true,
});

export type DownloadPendingAvatarsParamsPOST = {
userId: string;
count: number;
};
export type DownloadPendingAvatarsParamsPOST = {};

const DownloadPendingAvatarsParamsPOSTSchema = {
type: 'object',
properties: {
userId: {
type: 'string',
},
count: {
type: 'number',
},
},
properties: {},
additionalProperties: false,
required: ['userId', 'count'],
required: [],
};

export const isDownloadPendingAvatarsParamsPOST = ajv.compile<DownloadPendingAvatarsParamsPOST>(DownloadPendingAvatarsParamsPOSTSchema);
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ export type DownloadPendingFilesParamsPOST = {

const DownloadPendingFilesParamsPOSTSchema = {
type: 'object',
properties: {
userId: {
type: 'string',
},
count: {
type: 'number',
},
},
properties: {},
additionalProperties: false,
required: ['userId', 'count'],
required: [],
};

export const isDownloadPendingFilesParamsPOST = ajv.compile<DownloadPendingFilesParamsPOST>(DownloadPendingFilesParamsPOSTSchema);
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@ const ajv = new Ajv({
coerceTypes: true,
});

export type GetCurrentImportOperationParamsGET = {
userId: string;
operation: string;
};
export type GetCurrentImportOperationParamsGET = {};

const GetCurrentImportOperationParamsGETSchema = {
type: 'object',
properties: {
userId: {
type: 'string',
},
operation: {
type: 'string',
},
},
properties: {},
additionalProperties: false,
required: ['userId', 'operation'],
required: [],
};

export const isGetCurrentImportOperationParamsGET = ajv.compile<GetCurrentImportOperationParamsGET>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ export type GetImportFileDataParamsGET = {

const GetImportFileDataParamsGETSchema = {
type: 'object',
properties: {
userId: {
type: 'string',
},
},
properties: {},
additionalProperties: false,
required: ['userId'],
required: [],
};

export const isGetImportFileDataParamsGET = ajv.compile<GetImportFileDataParamsGET>(GetImportFileDataParamsGETSchema);
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ const ajv = new Ajv({
coerceTypes: true,
});

export type GetImportProgressParamsGET = {
userId: string;
};
export type GetImportProgressParamsGET = {};

const GetImportProgressParamsGETSchema = {
type: 'object',
properties: {
userId: {
type: 'string',
},
},
properties: {},
additionalProperties: false,
required: ['userId'],
required: [],
};

export const isGetImportProgressParamsGET = ajv.compile<GetImportProgressParamsGET>(GetImportProgressParamsGETSchema);
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ const ajv = new Ajv({
coerceTypes: true,
});

export type GetLatestImportOperationsParamsGET = {
userId: string;
};
export type GetLatestImportOperationsParamsGET = {};

const GetLatestImportOperationsParamsGETSchema = {
type: 'object',
properties: {
userId: {
type: 'string',
},
},
properties: {},
additionalProperties: false,
required: ['userId'],
required: [],
};

export const isGetLatestImportOperationsParamsGET = ajv.compile<GetLatestImportOperationsParamsGET>(
Expand Down