Skip to content
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
8 changes: 4 additions & 4 deletions src/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class StitchAdminClient extends StitchClient {
create: data => api._post(`${appUrl}/services`, data),
service: serviceId => ({
get: () => api._get(`${appUrl}/services/${serviceId}`),
remove: () => api._delete(`${appUrl}/services/${serviceId}`),
remove: (params) => api._delete(`${appUrl}/services/${serviceId}`, params),
update: data =>
api._patch(`${appUrl}/services/${serviceId}`, {
body: JSON.stringify(data)
Expand All @@ -297,13 +297,13 @@ export class StitchAdminClient extends StitchClient {

rules: () => ({
list: () => api._get(`${appUrl}/services/${serviceId}/rules`),
create: data => api._post(`${appUrl}/services/${serviceId}/rules`, data),
create: (data, params) => api._post(`${appUrl}/services/${serviceId}/rules`, data, params),
rule: ruleId => {
const ruleUrl = `${appUrl}/services/${serviceId}/rules/${ruleId}`;
return {
get: () => api._get(ruleUrl),
update: data => api._put(ruleUrl, { body: JSON.stringify(data) }),
remove: () => api._delete(ruleUrl)
update: (data, params) => api._put(ruleUrl, { body: JSON.stringify(data), queryParams: params }),
remove: (params) => api._delete(ruleUrl, params)
};
}
}),
Expand Down
122 changes: 122 additions & 0 deletions test/admin/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,126 @@ describe('Sync', () => {
});
});
});

describe('destructive changes', () => {
it('can use the allow_destructive_changes param to do a destructive service delete', async() => {
const syncService = await createSampleMongodbSyncService(services);
let stitchError;
try {
await services.service(syncService._id).remove();
} catch (e) {
stitchError = e;
}
expect(stitchError.code).toBe('DestructiveChangeNotAllowed');

const deleteResult = await services.service(syncService._id).remove({ allow_destructive_changes: true });
expect(deleteResult.status).toBe(204);
});

it('can use the allow_destructive_changes param to do a destructive rule create', async() => {
const syncService = await createSampleMongodbSyncService(services);

const schemaInvalidatingRule = {
database: 'db',
collection: 'coll',
config: {
schema: {
title: 'double',
properties: {
_id: { bsonType: 'objectId' },
key: { bsonType: 'string' },
obj: {
bsonType: 'object',
title: 'double'
}
}
}
}
};

let stitchError;
try {
await addRuleToMongodbService(services, syncService, schemaInvalidatingRule );
} catch (e) {
stitchError = e;
}
expect(stitchError.code).toBe('InvalidSyncSchema');

const createResult = await addRuleToMongodbService(services, syncService, schemaInvalidatingRule, { allow_destructive_changes: true });
expect(createResult).toBeTruthy();
});

it('can use the allow_destructive_changes param to do a destructive rule change', async() => {
const syncService = await createSampleMongodbSyncService(services);

const baseRule = {
database: 'db',
collection: 'coll',
config: {
schema: {
properties: {
_id: { bsonType: 'objectId' },
key: { bsonType: 'string' },
plsKeep: { bsonType: 'string' }
}
}
}
};
const createdRule = await addRuleToMongodbService(services, syncService, baseRule);

const destructiveRule = {
_id: createdRule._id,
database: 'db',
collection: 'coll',
schema: {
properties: {
_id: { bsonType: 'objectId' },
key: { bsonType: 'string' }
}
}
};

const updateRuleFunc = services.service(syncService._id).rules().rule(createdRule._id).update;
let stitchError;
try {
await updateRuleFunc(destructiveRule);
} catch (e) {
stitchError = e;
}
expect(stitchError.code).toBe('DestructiveChangeNotAllowed');

const updateResult = await updateRuleFunc(destructiveRule, { allow_destructive_changes: true });
expect(updateResult.status).toBe(204);
});

it('can use the allow_destructive_changes param to do a destructive rule remove', async() => {
const syncService = await createSampleMongodbSyncService(services);

const createdRule = await addRuleToMongodbService(services, syncService, {
database: 'db',
collection: 'coll',
config: {
schema: {
properties: {
_id: { bsonType: 'objectId' },
key: { bsonType: 'string' },
plsKeep: { bsonType: 'string' }
}
}
}
});

const removeRuleFunc = services.service(syncService._id).rules().rule(createdRule._id).remove;
let stitchError;
try {
await removeRuleFunc();
} catch (e) {
stitchError = e;
}
expect(stitchError.code).toBe('DestructiveChangeNotAllowed');

const updateResult = await removeRuleFunc({ allow_destructive_changes: true });
expect(updateResult.status).toBe(204);
});
});
});
4 changes: 2 additions & 2 deletions test/testutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export const createSampleMongodbSyncService = async(services, partitionKey = 'ke
return syncService;
};

export const addRuleToMongodbService = async(services, mongodbService, { database, collection, config }) => {
export const addRuleToMongodbService = async(services, mongodbService, { database, collection, config }, params) => {
const mongoSvcObj = services.service(mongodbService._id);
await mongoSvcObj.rules().create(Object.assign({}, config, { database, collection }));
return await mongoSvcObj.rules().create(Object.assign({}, config, { database, collection }), params);
};

class TestHarness {
Expand Down