Skip to content

Commit

Permalink
chore!: Remove meteor/check from cloud endpoints (#32533)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbsilva137 authored and ggazzo committed Sep 25, 2024
1 parent 83857d4 commit d3d8619
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 25 deletions.
24 changes: 4 additions & 20 deletions apps/meteor/app/api/server/v1/cloud.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { check } from 'meteor/check';
import { isCloudConfirmationPollProps, isCloudCreateRegistrationIntentProps, isCloudManualRegisterProps } from '@rocket.chat/rest-typings';

import { CloudWorkspaceRegistrationError } from '../../../../lib/errors/CloudWorkspaceRegistrationError';
import { SystemLogger } from '../../../../server/lib/logger/system';
Expand All @@ -19,13 +19,9 @@ import { API } from '../api';

API.v1.addRoute(
'cloud.manualRegister',
{ authRequired: true, permissionsRequired: ['register-on-cloud'] },
{ authRequired: true, permissionsRequired: ['register-on-cloud'], validateParams: isCloudManualRegisterProps },
{
async post() {
check(this.bodyParams, {
cloudBlob: String,
});

const registrationInfo = await retrieveRegistrationStatus();

if (registrationInfo.workspaceRegistered) {
Expand All @@ -43,14 +39,9 @@ API.v1.addRoute(

API.v1.addRoute(
'cloud.createRegistrationIntent',
{ authRequired: true, permissionsRequired: ['manage-cloud'] },
{ authRequired: true, permissionsRequired: ['manage-cloud'], validateParams: isCloudCreateRegistrationIntentProps },
{
async post() {
check(this.bodyParams, {
resend: Boolean,
email: String,
});

const intentData = await startRegisterWorkspaceSetupWizard(this.bodyParams.resend, this.bodyParams.email);

if (intentData) {
Expand All @@ -74,17 +65,10 @@ API.v1.addRoute(

API.v1.addRoute(
'cloud.confirmationPoll',
{ authRequired: true, permissionsRequired: ['manage-cloud'] },
{ authRequired: true, permissionsRequired: ['manage-cloud'], validateParams: isCloudConfirmationPollProps },
{
async get() {
const { deviceCode } = this.queryParams;
check(this.queryParams, {
deviceCode: String,
});

if (!deviceCode) {
return API.v1.failure('Invalid query');
}

const pollData = await getConfirmationPoll(deviceCode);
if (pollData) {
Expand Down
181 changes: 181 additions & 0 deletions apps/meteor/tests/end-to-end/api/cloud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { expect } from 'chai';
import { after, before, describe, it } from 'mocha';

import { getCredentials, api, request, credentials } from '../../data/api-data';
import { updatePermission } from '../../data/permissions.helper';

describe('[Cloud]', function () {
this.retries(0);

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

describe('[/cloud.manualRegister]', () => {
before(async () => {
return updatePermission('register-on-cloud', ['admin']);
});

after(async () => {
return updatePermission('register-on-cloud', ['admin']);
});

it('should fail if user is not authenticated', async () => {
return request
.post(api('cloud.manualRegister'))
.expect('Content-Type', 'application/json')
.expect(401)
.expect((res: Response) => {
expect(res.body).to.have.property('status', 'error');
expect(res.body).to.have.property('message', 'You must be logged in to do this.');
});
});

it('should fail when cloudBlob property is not provided', async () => {
return request
.post(api('cloud.manualRegister'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'invalid-params');
expect(res.body).to.have.property('error', "must have required property 'cloudBlob' [invalid-params]");
});
});

it('should fail when user does not have the register-on-cloud permission', async () => {
await updatePermission('register-on-cloud', []);
return request
.post(api('cloud.manualRegister'))
.set(credentials)
.send({
cloudBlob: 'test-blob',
})
.expect('Content-Type', 'application/json')
.expect(403)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error', 'User does not have the permissions required for this action [error-unauthorized]');
});
});
});

describe('[/cloud.createRegistrationIntent]', () => {
before(async () => {
return updatePermission('manage-cloud', ['admin']);
});

after(async () => {
return updatePermission('manage-cloud', ['admin']);
});

it('should fail if user is not authenticated', async () => {
return request
.post(api('cloud.createRegistrationIntent'))
.expect('Content-Type', 'application/json')
.expect(401)
.expect((res: Response) => {
expect(res.body).to.have.property('status', 'error');
expect(res.body).to.have.property('message', 'You must be logged in to do this.');
});
});

it('should fail when resend property is not provided', async () => {
return request
.post(api('cloud.createRegistrationIntent'))
.set(credentials)
.send({
email: '[email protected]',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'invalid-params');
expect(res.body).to.have.property('error', "must have required property 'resend' [invalid-params]");
});
});

it('should fail when email property is not provided', async () => {
return request
.post(api('cloud.createRegistrationIntent'))
.set(credentials)
.send({
resend: true,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'invalid-params');
expect(res.body).to.have.property('error', "must have required property 'email' [invalid-params]");
});
});

it('should fail when user does not have the manage-cloud permission', async () => {
await updatePermission('manage-cloud', []);
return request
.post(api('cloud.createRegistrationIntent'))
.set(credentials)
.send({
email: '[email protected]',
resend: true,
})
.expect('Content-Type', 'application/json')
.expect(403)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error', 'User does not have the permissions required for this action [error-unauthorized]');
});
});
});

describe('[/cloud.confirmationPoll]', () => {
before(async () => {
return updatePermission('manage-cloud', ['admin']);
});

after(async () => {
return updatePermission('manage-cloud', ['admin']);
});

it('should fail if user is not authenticated', async () => {
return request
.get(api('cloud.confirmationPoll'))
.expect('Content-Type', 'application/json')
.expect(401)
.expect((res: Response) => {
expect(res.body).to.have.property('status', 'error');
expect(res.body).to.have.property('message', 'You must be logged in to do this.');
});
});

it('should fail when deviceCode property is not provided', async () => {
return request
.get(api('cloud.confirmationPoll'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'invalid-params');
expect(res.body).to.have.property('error', "must have required property 'deviceCode' [invalid-params]");
});
});

it('should fail when user does not have the manage-cloud permission', async () => {
await updatePermission('manage-cloud', []);
return request
.get(api('cloud.confirmationPoll'))
.set(credentials)
.query({
deviceCode: 'test-code',
})
.expect('Content-Type', 'application/json')
.expect(403)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error', 'User does not have the permissions required for this action [error-unauthorized]');
});
});
});
});
1 change: 1 addition & 0 deletions packages/rest-typings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,4 @@ export * from './v1/rooms';
export * from './v1/groups';
export * from './v1/chat';
export * from './v1/auth';
export * from './v1/cloud';
6 changes: 1 addition & 5 deletions packages/rest-typings/src/v1/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ const CloudConfirmationPollSchema = {
properties: {
deviceCode: {
type: 'string',
},
resend: {
type: 'string',
nullable: true,
minLength: 1,
},
},
required: ['deviceCode'],
optionalProperties: ['resend'],
additionalProperties: false,
};

Expand Down

0 comments on commit d3d8619

Please sign in to comment.