Skip to content

Commit

Permalink
add items to bulk operation and corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithad0703 committed Dec 13, 2024
1 parent 0e5105b commit 3fba7cf
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/stack/bulkOperation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ export function BulkOperation (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/bulk`

this.addItems = async ({ data, bulk_version = "" }) => {
this.urlPath = `/bulk/release/items`;
const headers = {
headers: {
...cloneDeep(this.stackHeaders),
},
};
if (bulk_version) headers.headers.bulk_version = bulk_version;
try {
const response = await http.post(this.urlPath, data, headers);
if (response.data) {
return response.data;
}
} catch (error) {
console.error(error);
}
};

/**
* The Publish entries and assets in bulk request allows you to publish multiple entries and assets at the same time.
* @memberof BulkOperation
Expand Down
36 changes: 36 additions & 0 deletions test/sanity-check/api/release-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,38 @@ describe('Relases api Test', () => {
.catch(done)
})

it('Bulk Operation: should add items to a release', done => {
const items = {
release: releaseUID,
action: 'publish',
locale: ['en-us'],
reference: true,
items: [
{
version: entries[1]._version,
uid: entries[1].uid,
content_type_uid: multiPageCT.content_type.uid,
locale: 'en-us',
title: entries[1].title
},
{
version: entries[2]._version,
uid: entries[2].uid,
content_type_uid: multiPageCT.content_type.uid,
locale: 'en-us',
title: entries[2].title
},
],
}
doBulkOperation().addItems({ data: items, bulk_version: '2.0' })
.then((response) => {
expect(response.notice).to.equal('Your add to release request is in progress.')
expect(response.job_id).to.not.equal(undefined)
done()
})
.catch(done)
})

it('should delete specific Releases with Uid ', done => {
makeRelease(releaseUID)
.delete()
Expand All @@ -237,3 +269,7 @@ describe('Relases api Test', () => {
function makeRelease (uid = null) {
return client.stack({ api_key: process.env.API_KEY }).release(uid)
}

function doBulkOperation(uid = null) {
return client.stack({ api_key: process.env.API_KEY }).bulkOperation()
}
186 changes: 186 additions & 0 deletions test/unit/bulkOperation-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import Axios from 'axios';
import { expect } from 'chai';
import MockAdapter from 'axios-mock-adapter';
import { describe, it } from 'mocha';
import { BulkOperation } from '../../lib/stack/bulkOperation';
import { stackHeadersMock } from './mock/objects';

describe('Contentstack BulkOperation test', () => {
it('BulkOperation test without uid', done => {
const bulkOperation = makeBulkOperation();
expect(bulkOperation.urlPath).to.be.equal('/bulk');
expect(bulkOperation.stackHeaders).to.be.equal(undefined);
expect(bulkOperation.addItems).to.not.equal(undefined);
expect(bulkOperation.publish).to.not.equal(undefined);
expect(bulkOperation.unpublish).to.not.equal(undefined);
expect(bulkOperation.delete).to.not.equal(undefined);
done();
});

it('BulkOperation test with stackHeaders', done => {
const bulkOperation = makeBulkOperation({ stackHeaders: { ...stackHeadersMock } });
expect(bulkOperation.urlPath).to.be.equal('/bulk');
expect(bulkOperation.stackHeaders).to.not.equal(undefined);
expect(bulkOperation.stackHeaders.api_key).to.be.equal(stackHeadersMock.api_key);
expect(bulkOperation.addItems).to.not.equal(undefined);
expect(bulkOperation.publish).to.not.equal(undefined);
expect(bulkOperation.unpublish).to.not.equal(undefined);
expect(bulkOperation.delete).to.not.equal(undefined);
done();
});

it('should add items to a release', async () => {
const items = {
release: 'blt05e951e5f3a1d342',
action: 'publish',
locale: ['en-us'],
reference: true,
items: [
{
content_type_uid: 'ct_1',
uid: 'bltf6e197a18a11ec5f',
version: 2,
locale: 'en-us',
title: 'validation test',
},
],
};

var mock = new MockAdapter(Axios);
mock.onPost('/bulk/release/items').reply(200, {
notice: 'Your add to release request is in progress.',
job_id: 'job_id',
});

const response = await makeBulkOperation().addItems({ data: items, bulk_version: '2.0' });
expect(response.notice).to.equal('Your add to release request is in progress.');
expect(response.job_id).to.not.equal(undefined);
});

it('should publish items in bulk', async () => {
const publishDetails = {
entries: [
{
uid: 'entry_uid',
content_type: 'content_type_uid',
version: 'version',
locale: 'entry_locale',
},
],
assets: [{ uid: 'uid' }],
locales: ['en'],
environments: ['env_uid'],
};

var mock = new MockAdapter(Axios);
mock.onPost('/bulk/publish').reply(200, {
notice: 'Your publish request is in progress.',
job_id: 'job_id',
});

const response = await makeBulkOperation().publish({ details: publishDetails });
expect(response.notice).to.equal('Your publish request is in progress.');
expect(response.job_id).to.not.equal(undefined);
});

it('should unpublish items in bulk', async () => {
const unpublishDetails = {
entries: [
{
uid: 'entry_uid',
content_type: 'content_type_uid',
version: 'version',
locale: 'entry_locale',
},
],
assets: [{ uid: 'uid' }],
locales: ['en'],
environments: ['env_uid'],
};

var mock = new MockAdapter(Axios);
mock.onPost('/bulk/unpublish').reply(200, {
notice: 'Your unpublish request is in progress.',
job_id: 'job_id',
});

const response = await makeBulkOperation().unpublish({ details: unpublishDetails });
expect(response.notice).to.equal('Your unpublish request is in progress.');
expect(response.job_id).to.not.equal(undefined);
});

it('should delete items in bulk', async () => {
const deleteDetails = {
entries: [
{
uid: 'entry_uid',
content_type: 'content_type_uid',
locale: 'entry_locale',
},
],
assets: [{ uid: 'uid' }],
};

var mock = new MockAdapter(Axios);
mock.onPost('/bulk/delete').reply(200, {
notice: 'Your delete request is in progress.',
job_id: 'job_id',
});

const response = await makeBulkOperation().delete({ details: deleteDetails });
expect(response.notice).to.equal('Your delete request is in progress.');
expect(response.job_id).to.not.equal(undefined);
});

it('should update items in bulk', async () => {
const updateBody = {
entries: [
{
content_type: 'content_type_uid1',
uid: 'entry_uid',
locale: 'en-us',
},
{
content_type: 'content_type_uid2',
uid: 'entry_uid',
locale: 'en-us',
},
],
workflow: {
workflow_stage: {
comment: 'Workflow-related Comments',
due_date: 'Thu Dec 01 2018',
notify: false,
uid: 'workflow_stage_uid',
assigned_to: [
{
uid: 'user_uid',
name: 'user_name',
email: 'user_email_id',
},
],
assigned_by_roles: [
{
uid: 'role_uid',
name: 'role_name',
},
],
},
},
};

var mock = new MockAdapter(Axios);
mock.onPost('/bulk/workflow').reply(200, {
notice: 'Your update request is in progress.',
job_id: 'job_id',
});

const response = await makeBulkOperation().update(updateBody);
expect(response.notice).to.equal('Your update request is in progress.');
expect(response.job_id).to.not.equal(undefined);
});
});

function makeBulkOperation(data) {
return new BulkOperation(Axios, data);
}
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require('./extension-test')
require('./branch-test')
require('./branchAlias-test')
require('./release-test')
require('./bulkOperation-test')
require('./asset-test')
require('./webhook-test')
require('./workflow-test')
Expand Down
8 changes: 7 additions & 1 deletion types/stack/bulkOperation/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ export interface BulkOperation extends SystemFields {
publish(config: BulkOperationConfig): Promise<Response>
unpublish(config: BulkOperationConfig): Promise<Response>
delete(config: BulkDeleteConfig): Promise<Response>
addItems(config: AddItemsConfig): Promise<Response>
}

export interface BulkOperationConfig {
details: PublishItems
skip_workflow_stage?: boolean
approvals?: boolean
is_nested?: boolean
api_version?: string
bulk_version?: string
}

export interface PublishItems extends PublishDetails {
Expand Down Expand Up @@ -44,4 +45,9 @@ export interface BulkDeleteAsset {
export interface BranchData extends AnyProperty {
name: string
source: string
}

export interface BulkAddItemsConfig {
data: AnyProperty;
bulk_version?: string;
}

0 comments on commit 3fba7cf

Please sign in to comment.