Skip to content

Commit

Permalink
fix: update to latest content sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Nov 19, 2024
1 parent 60a7d01 commit cec527f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ export default class ContentClient {
this.log.info(`Getting page metadata for ${this.site.getId()} and path ${path}`);

const docPath = this.#resolveDocPath(path);
const document = await this.rawClient.read(docPath);
const metadata = document.getMetadata();
const document = await this.rawClient.getDocument(docPath);
const metadata = await document.getMetadata();

this.#logDuration('getPageMetadata', startTime);

Expand All @@ -284,8 +284,8 @@ export default class ContentClient {
this.log.info(`Updating page metadata for ${this.site.getId()} and path ${path}`);

const docPath = this.#resolveDocPath(path);
const document = await this.rawClient.read(docPath);
const originalMetadata = document.getMetadata();
const document = await this.rawClient.getDocument(docPath);
const originalMetadata = await document.getMetadata();

let mergedMetadata;
if (overwrite) {
Expand All @@ -296,10 +296,7 @@ export default class ContentClient {

const response = await document.updateMetadata(mergedMetadata);
if (response?.status !== 200) {
const saveResponse = await this.rawClient.save(document);
if (saveResponse.status !== 200) {
throw new Error(`Failed to update metadata for path ${path}`);
}
throw new Error(`Failed to update metadata for path ${path}`);
}

this.#logDuration('updatePageMetadata', startTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import esmock from 'esmock';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { createSite } from '@adobe/spacecat-shared-data-access/src/models/site.js';
import { Mdast } from '@adobe/spacecat-helix-content-sdk/src/mdast.js';
import nock from 'nock';

use(chaiAsPromised);
Expand Down Expand Up @@ -64,11 +63,10 @@ describe('ContentClient', () => {
const createContentClient = async (getPageMetadata) => {
documentSdk = {
getMetadata: sinon.stub().returns(getPageMetadata),
updateMetadata: sinon.stub().resolves(),
updateMetadata: sinon.stub().resolves({ status: 200 }),
};
const contentSDK = sinon.stub().returns({
read: sinon.stub().resolves(documentSdk),
save: sinon.stub().resolves({ status: 200 }),
getDocument: sinon.stub().resolves(documentSdk),
});

return esmock('../../src/clients/content-client.js', {
Expand All @@ -78,87 +76,13 @@ describe('ContentClient', () => {

const createErrorContentClient = async (getError, updateError, errorMessage) => {
const contentSDK = sinon.stub().returns({
read: getError ? sinon.stub().rejects(new Error(errorMessage))
: sinon.stub().resolves(new Mdast({
type: 'root',
children: [
{
type: 'heading',
depth: 1,
children: [
{ type: 'text', value: 'Document Title' },
],
},
{
type: 'paragraph',
children: [
{ type: 'text', value: 'This is an introductory paragraph.' },
],
},
{
type: 'thematicBreak',
},
{
type: 'table',
children: [
{
type: 'tableRow',
children: [
{
type: 'tableCell',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'metadata',
},
],
},
],
},
{ type: 'tableCell', children: [{ type: 'text', value: ' ' }] },
],
},
{
type: 'tableRow',
children: [
{
type: 'tableCell',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'Title',
},
],
},
],
},
{
type: 'tableCell',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'TestTitle',
},
],
},
],
},
],
}],
},
],
})),
save: updateError ? sinon.stub().resolves({ status: 500 })
: sinon.stub().resolves({ status: 200 }),
getDocument: getError ? sinon.stub().rejects(new Error(errorMessage))
: sinon.stub().resolves({
updateMetadata: updateError ? sinon.stub().resolves({ status: 500 })
: sinon.stub().resolves({ status: 200 }),
getMetadata: getError

Check warning

Code scanning / CodeQL

Useless conditional Warning test

This use of variable 'getError' always evaluates to false.
? sinon.stub().rejects(new Error(errorMessage)) : sinon.stub().resolves(new Map()),
}),
getRedirects: getError
? sinon.stub().rejects(new Error(errorMessage)) : sinon.stub().resolves(existingRedirects),
appendRedirects: sinon.stub().resolves(updateError ? { status: 500 } : { status: 200 }),
Expand Down Expand Up @@ -354,7 +278,7 @@ describe('ContentClient', () => {

expect(metadata).to.deep.equal(sampleMetadata);
expect(log.info.calledOnceWith(`Getting page metadata for test-site and path ${path}`)).to.be.true;
expect(client.rawClient.read.calledOnceWith('/test-path')).to.be.true;
expect(client.rawClient.getDocument.calledOnceWith('/test-path')).to.be.true;
expect(documentSdk.getMetadata.calledOnce).to.be.true;
expect(log.debug.calledOnce).to.be.true;
});
Expand All @@ -366,7 +290,7 @@ describe('ContentClient', () => {

expect(metadata).to.deep.equal(sampleMetadata);
expect(log.info.calledOnceWith(`Getting page metadata for test-site and path ${path}`)).to.be.true;
expect(client.rawClient.read.calledOnceWith('/test-path.docx')).to.be.true;
expect(client.rawClient.getDocument.calledOnceWith('/test-path.docx')).to.be.true;
expect(documentSdk.getMetadata.calledOnce).to.be.true;
expect(log.debug.calledOnce).to.be.true;
});
Expand All @@ -384,13 +308,13 @@ describe('ContentClient', () => {
it('correctly resolves paths ending with / for Google Drive', async () => {
const client = ContentClient.createFrom(context, siteConfigGoogleDrive);
await client.getPageMetadata('/test-path/');
expect(client.rawClient.read.calledOnceWith('/test-path/index')).to.be.true;
expect(client.rawClient.getDocument.calledOnceWith('/test-path/index')).to.be.true;
});

it('correctly resolves paths ending with / for OneDrive', async () => {
const client = ContentClient.createFrom(context, siteConfigOneDrive);
await client.getPageMetadata('/test-path/');
expect(client.rawClient.read.calledOnceWith('/test-path/index.docx')).to.be.true;
expect(client.rawClient.getDocument.calledOnceWith('/test-path/index.docx')).to.be.true;
});
});

Expand Down Expand Up @@ -420,9 +344,8 @@ describe('ContentClient', () => {
const updatedMetadata = await client.updatePageMetadata(path, metadata);

expect(updatedMetadata).to.deep.equal(expectedMetadata);
expect(client.rawClient.read.calledOnceWith('/test-path')).to.be.true;
expect(client.rawClient.getDocument.calledOnceWith('/test-path')).to.be.true;
expect(documentSdk.updateMetadata.calledOnceWith(expectedMetadata)).to.be.true;
expect(client.rawClient.save.calledOnce).to.be.true;
expect(log.info.calledOnce).to.be.true;
expect(log.info.firstCall.args[0]).to.equal(`Updating page metadata for test-site and path ${path}`);
});
Expand Down Expand Up @@ -478,7 +401,7 @@ describe('ContentClient', () => {
const updatedMetadata = await client.updatePageMetadata(path, newMetadata);

expect(updatedMetadata).to.deep.equal(expectedMetadata);
expect(client.rawClient.read.calledOnceWith('/test-path')).to.be.true;
expect(client.rawClient.getDocument.calledOnceWith('/test-path')).to.be.true;
expect(documentSdk.updateMetadata.calledOnceWith(expectedMetadata)).to.be.true;
});

Expand All @@ -503,7 +426,7 @@ describe('ContentClient', () => {
});

expect(updatedMetadata).to.deep.equal(expectedMetadata);
expect(client.rawClient.read.calledOnceWith('/test-path')).to.be.true;
expect(client.rawClient.getDocument.calledOnceWith('/test-path')).to.be.true;
expect(documentSdk.updateMetadata.calledOnceWith(expectedMetadata)).to.be.true;
});
});
Expand Down

0 comments on commit cec527f

Please sign in to comment.