diff --git a/test/sanity-check/api/bulkOperation-test.js b/test/sanity-check/api/bulkOperation-test.js index ccf3d1e4..22643db3 100644 --- a/test/sanity-check/api/bulkOperation-test.js +++ b/test/sanity-check/api/bulkOperation-test.js @@ -3,21 +3,21 @@ import { describe, it, setup } from 'mocha' import { jsonReader } from '../../sanity-check/utility/fileOperations/readwrite' import { contentstackClient } from '../../sanity-check/utility/ContentstackClient' import { singlepageCT, multiPageCT } from '../mock/content-type.js' -// import { createManagementToken } from '../mock/managementToken.js' +import { createManagementToken } from '../mock/managementToken.js' import dotenv from 'dotenv' dotenv.config() let client = {} -// let clientWithManagementToken = {} +let clientWithManagementToken = {} let entryUid1 = '' let assetUid1 = '' let entryUid2 = '' let assetUid2 = '' -// let jobId1 = '' -// let jobId2 = '' -// let jobId3 = '' -// let tokenUidDev = '' -// let tokenUid = '' +let jobId1 = '' +let jobId2 = '' +let jobId3 = '' +let tokenUidDev = '' +let tokenUid = '' describe('BulkOperation api test', () => { setup(() => { @@ -31,23 +31,23 @@ describe('BulkOperation api test', () => { entryUid2 = entryRead2.uid assetUid2 = assetRead2.uid client = contentstackClient(user.authtoken) - // clientWithManagementToken = contentstackClient() + clientWithManagementToken = contentstackClient() }) - // it('should create a Management Token for get job status', done => { - // makeManagementToken() - // .create(createManagementToken) - // .then((token) => { - // tokenUidDev = token.token - // tokenUid = token.uid - // expect(token.name).to.be.equal(createManagementToken.token.name) - // expect(token.description).to.be.equal(createManagementToken.token.description) - // expect(token.scope[0].module).to.be.equal(createManagementToken.token.scope[0].module) - // expect(token.uid).to.be.not.equal(null) - // done() - // }) - // .catch(done) - // }) + it('should create a Management Token for get job status', done => { + makeManagementToken() + .create(createManagementToken) + .then((token) => { + tokenUidDev = token.token + tokenUid = token.uid + expect(token.name).to.be.equal(createManagementToken.token.name) + expect(token.description).to.be.equal(createManagementToken.token.description) + expect(token.scope[0].module).to.be.equal(createManagementToken.token.scope[0].module) + expect(token.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) it('should publish one entry when publishDetails of an entry is passed', done => { const publishDetails = { @@ -70,7 +70,7 @@ describe('BulkOperation api test', () => { .then((response) => { expect(response.notice).to.not.equal(undefined) expect(response.job_id).to.not.equal(undefined) - // jobId1 = response.job_id + jobId1 = response.job_id done() }) .catch(done) @@ -95,7 +95,7 @@ describe('BulkOperation api test', () => { .then((response) => { expect(response.notice).to.not.equal(undefined) expect(response.job_id).to.not.equal(undefined) - // jobId2 = response.job_id + jobId2 = response.job_id done() }) .catch(done) @@ -135,128 +135,121 @@ describe('BulkOperation api test', () => { .then((response) => { expect(response.notice).to.not.equal(undefined) expect(response.job_id).to.not.equal(undefined) - // jobId3 = response.job_id + jobId3 = response.job_id done() }) .catch(done) }) - // it('should get job status for the first publish job', done => { - // doBulkOperationWithManagementToken(tokenUidDev) - // .jobStatus({ job_id: jobId1, api_version: '3.2' }) - // .then((response) => { - // expect(response).to.not.equal(undefined) - // expect(response.uid).to.not.equal(undefined) - // expect(response.status).to.not.equal(undefined) - // expect(response.action).to.not.equal(undefined) - // expect(response.summary).to.not.equal(undefined) - // expect(response.body).to.not.equal(undefined) - // done() - // }) - // .catch((error) => { - // console.error('Job status error:', error) - // done(error) - // }) - // }) - - // it('should validate detailed job status response structure', done => { - // doBulkOperationWithManagementToken(tokenUidDev) - // .jobStatus({ job_id: jobId1, api_version: '3.2' }) - // .then((response) => { - // expect(response).to.not.equal(undefined) - // // Validate main job properties - // expect(response.uid).to.not.equal(undefined) - // expect(response.api_key).to.not.equal(undefined) - // expect(response.status).to.not.equal(undefined) + it('should get job status for the first publish job', done => { + doBulkOperationWithManagementToken(tokenUidDev) + .jobStatus({ job_id: jobId1, api_version: '3.2' }) + .then((response) => { + expect(response).to.not.equal(undefined) + expect(response.uid).to.not.equal(undefined) + expect(response.status).to.not.equal(undefined) + expect(response.action).to.not.equal(undefined) + expect(response.summary).to.not.equal(undefined) + expect(response.body).to.not.equal(undefined) + done() + }) + .catch((error) => { + console.error('Job status error:', error) + done(error) + }) + }) - // // Validate body structure - // expect(response.body).to.not.equal(undefined) - // expect(response.body.locales).to.be.an('array') - // expect(response.body.environments).to.be.an('array') - // // Validate summary structure - // expect(response.summary).to.not.equal(undefined) - // done() - // }) - // .catch((error) => { - // console.error('Detailed job status error:', error) - // done(error) - // }) - // }) + it('should validate detailed job status response structure', done => { + doBulkOperationWithManagementToken(tokenUidDev) + .jobStatus({ job_id: jobId1, api_version: '3.2' }) + .then((response) => { + expect(response).to.not.equal(undefined) + // Validate main job properties + expect(response.uid).to.not.equal(undefined) + expect(response.api_key).to.not.equal(undefined) + expect(response.status).to.not.equal(undefined) - // it('should get job status for the second publish job', done => { - // doBulkOperationWithManagementToken(tokenUidDev) - // .jobStatus({ job_id: jobId2, api_version: '3.2' }) - // .then((response) => { - // expect(response).to.not.equal(undefined) - // expect(response.uid).to.not.equal(undefined) - // expect(response.status).to.not.equal(undefined) - // expect(response.action).to.not.equal(undefined) - // expect(response.summary).to.not.equal(undefined) - // expect(response.body).to.not.equal(undefined) - // done() - // }) - // .catch(done) - // }) + // Validate body structure + expect(response.body).to.not.equal(undefined) + expect(response.body.locales).to.be.an('array') + expect(response.body.environments).to.be.an('array') + // Validate summary structure + expect(response.summary).to.not.equal(undefined) + done() + }) + .catch((error) => { + console.error('Detailed job status error:', error) + done(error) + }) + }) - // it('should get job status for the third publish job', done => { - // doBulkOperationWithManagementToken(tokenUidDev) - // .jobStatus({ job_id: jobId3, api_version: '3.2' }) - // .then((response) => { - // expect(response).to.not.equal(undefined) - // expect(response.uid).to.not.equal(undefined) - // expect(response.status).to.not.equal(undefined) - // expect(response.action).to.not.equal(undefined) - // expect(response.summary).to.not.equal(undefined) - // expect(response.body).to.not.equal(undefined) - // done() - // }) - // .catch(done) - // }) + it('should get job status for the second publish job', done => { + doBulkOperationWithManagementToken(tokenUidDev) + .jobStatus({ job_id: jobId2, api_version: '3.2' }) + .then((response) => { + expect(response).to.not.equal(undefined) + expect(response.uid).to.not.equal(undefined) + expect(response.status).to.not.equal(undefined) + expect(response.action).to.not.equal(undefined) + expect(response.summary).to.not.equal(undefined) + expect(response.body).to.not.equal(undefined) + done() + }) + .catch(done) + }) - // it('should get job status with bulk_version parameter', done => { - // doBulkOperationWithManagementToken(tokenUidDev) - // .jobStatus({ job_id: jobId1, bulk_version: 'v3', api_version: '3.2' }) - // .then((response) => { - // expect(response).to.not.equal(undefined) - // expect(response.uid).to.not.equal(undefined) - // expect(response.status).to.not.equal(undefined) - // expect(response.action).to.not.equal(undefined) - // expect(response.summary).to.not.equal(undefined) - // expect(response.body).to.not.equal(undefined) - // done() - // }) - // .catch(done) - // }) + it('should get job status for the third publish job', done => { + doBulkOperationWithManagementToken(tokenUidDev) + .jobStatus({ job_id: jobId3, api_version: '3.2' }) + .then((response) => { + expect(response).to.not.equal(undefined) + expect(response.uid).to.not.equal(undefined) + expect(response.status).to.not.equal(undefined) + expect(response.action).to.not.equal(undefined) + expect(response.summary).to.not.equal(undefined) + expect(response.body).to.not.equal(undefined) + done() + }) + .catch(done) + }) - // it('should test job status endpoint accessibility', done => { - // doBulkOperationWithManagementToken(tokenUidDev) - // .jobStatus({ job_id: 'test-job-id', api_version: '3.2' }) - // .then((response) => { - // done() - // }) - // .catch((error) => { - // console.log('Job status endpoint error (expected for invalid job ID):', error.message) - // // This is expected to fail with invalid job ID, but should not be an auth error - // if (error.message && (error.message.includes('authentication') || error.message.includes('401'))) { - // done(error) - // } else { - // done() // Expected error for invalid job ID - // } - // }) - // }) + it('should get job status with bulk_version parameter', done => { + doBulkOperationWithManagementToken(tokenUidDev) + .jobStatus({ job_id: jobId1, bulk_version: 'v3', api_version: '3.2' }) + .then((response) => { + expect(response).to.not.equal(undefined) + expect(response.uid).to.not.equal(undefined) + expect(response.status).to.not.equal(undefined) + expect(response.action).to.not.equal(undefined) + expect(response.summary).to.not.equal(undefined) + expect(response.body).to.not.equal(undefined) + done() + }) + .catch(done) + }) - // it('should delete a Management Token', done => { - // makeManagementToken(tokenUid) - // .delete() - // .then((data) => { - // expect(data.notice).to.be.equal('Management Token deleted successfully.') - // done() - // }) - // .catch(done) - // }) + it('should delete a Management Token', done => { + makeManagementToken(tokenUid) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Management Token deleted successfully.') + done() + }) + .catch(done) + }) }) function doBulkOperation (uid = null) { // @ts-ignore-next-line secret-detection return client.stack({ api_key: process.env.API_KEY }).bulkOperation() } + +function doBulkOperationWithManagementToken (tokenUidDev) { + // @ts-ignore-next-line secret-detection + return clientWithManagementToken.stack({ api_key: process.env.API_KEY, management_token: tokenUidDev }).bulkOperation() +} + +function makeManagementToken (uid = null) { + // @ts-ignore-next-line secret-detection + return client.stack({ api_key: process.env.API_KEY }).managementToken(uid) +}