diff --git a/test/ConfluenceTest.ts b/test/ConfluenceTest.ts index a4688e0..6229224 100644 --- a/test/ConfluenceTest.ts +++ b/test/ConfluenceTest.ts @@ -11,7 +11,7 @@ describe('The Confluence API', (): void => { const mockServer = new MockServer('https://example.com') mockServer.addSearchEndpoint() mockServer.addDocumentEndpoint() - const confluence = new Confluence('https://example.com', 'nobody', '', 'nothing') + const confluence = new Confluence('https://example.com', 'nobody', 'nothing', '') const results = await confluence.findDocumentsOlderThan('', 1, 1) chai.expect(results).to.have.lengthOf(2) chai.expect(results[0].url).to.eq('https://example.com/display/SAMPLE/Test') @@ -35,6 +35,39 @@ describe('The Confluence API', (): void => { it('should add a configuration document', async (): Promise => { const mockServer = new MockServer('https://example.com') mockServer.addCreateEndpoint() + const confluence = new Confluence('https://example.com', 'nobody', 'nothing', '') + const result = await confluence.createConfigurationDocument('example', 'test', '0123') + chai.expect(result).to.eq('12345') + }) + + it('should search for old documents using an access token', async (): Promise => { + const mockServer = new MockServer('https://example.com') + mockServer.addSearchEndpointToken() + mockServer.addDocumentEndpointToken() + const confluence = new Confluence('https://example.com', 'nobody', '', 'nothing') + const results = await confluence.findDocumentsOlderThan('', 1, 1) + chai.expect(results).to.have.lengthOf(2) + chai.expect(results[0].url).to.eq('https://example.com/display/SAMPLE/Test') + chai.expect(results[0].shortUrl).to.eq('/display/SAMPLE/Test') + chai.expect(results[0].author).to.eq('author') + chai.expect(results[0].id).to.eq(123) + chai.expect(results[0].lastVersionDate).to.eq('2019-12-31T22:00:00.000Z') + chai.expect(results[0].lastVersionMessage).to.eq('Some change') + chai.expect(results[0].title).to.eq('Test') + chai.expect(results[1].url).to.eq('https://example.com/display/SAMPLE/Test2') + chai.expect(results[1].shortUrl).to.eq('/display/SAMPLE/Test2') + chai.expect(results[1].author).to.eq('author2') + chai.expect(results[1].id).to.eq(234) + chai.expect(results[1].lastVersionDate).to.eq('2020-01-31T22:00:00.000Z') + chai.expect(results[1].lastVersionMessage).to.eq('') + chai.expect(results[1].title).to.eq('Test2') + chai.expect(results[0].labels.length).to.eq(1) + chai.expect(results[0].labels[0]).to.eq('Test') + }) + + it('should add a configuration document using an access token', async (): Promise => { + const mockServer = new MockServer('https://example.com') + mockServer.addCreateEndpointToken() const confluence = new Confluence('https://example.com', 'nobody', '', 'nothing') const result = await confluence.createConfigurationDocument('example', 'test', '0123') chai.expect(result).to.eq('12345') diff --git a/test/MockServer.ts b/test/MockServer.ts index 7ae816c..65b3dad 100644 --- a/test/MockServer.ts +++ b/test/MockServer.ts @@ -665,6 +665,34 @@ export class MockServer { }) } + public addSearchEndpointToken(): void { + this._scope + .get(new RegExp('/rest/api/content/search\\?cql=.+&start=0')) + .matchHeader('authorization', 'Bearer nothing') + .reply(200, { + results: [ + { + id: 123, + }, + ], + start: 0, + size: 1, + totalSize: 2, + }) + .get(new RegExp('/rest/api/content/search\\?cql=.+&start=1')) + .matchHeader('authorization', 'Bearer nothing') + .reply(200, { + results: [ + { + id: 234, + }, + ], + start: 1, + size: 1, + totalSize: 2, + }) + } + public addDocumentEndpoint(): void { this._scope .get('/rest/api/content/123?expand=ancestors,version,metadata.labels,history') @@ -740,6 +768,75 @@ export class MockServer { }) } + public addDocumentEndpointToken(): void { + this._scope + .get('/rest/api/content/123?expand=ancestors,version,metadata.labels,history') + .matchHeader('authorization', 'Bearer nothing') + .reply(200, { + _links: { + base: 'https://example.com', + webui: '/display/SAMPLE/Test', + }, + ancestors: [ + { + title: 'main', + }, + ], + version: { + by: { + username: 'author', + }, + when: '2020-01-01T00:00:00.000+02:00', + message: 'Some change', + }, + history: { + createdBy: { + username: 'creator', + }, + }, + title: 'Test', + metadata: { + labels: { + results: [ + { + prefix: 'global', + name: 'Test', + id: '90734603', + }, + ], + }, + }, + }) + .get('/rest/api/content/234?expand=ancestors,version,metadata.labels,history') + .matchHeader('authorization', 'Bearer nothing') + .reply(200, { + _links: { + base: 'https://example.com', + webui: '/display/SAMPLE/Test2', + }, + ancestors: [ + { + title: 'main', + }, + { + title: 'Test', + }, + ], + history: { + createdBy: { + username: 'creator', + }, + }, + version: { + by: { + username: 'author2', + }, + when: '2020-02-01T00:00:00.000+02:00', + }, + title: 'Test2', + }) + } + public addCreateEndpoint(): void { this._scope .post('/rest/api/content') @@ -754,4 +851,16 @@ export class MockServer { return requestObject }) } + + public addCreateEndpointToken(): void { + this._scope + .post('/rest/api/content') + .matchHeader('authorization', 'Bearer nothing') + .reply(200, (uri, requestBody) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const requestObject = requestBody as Record + requestObject.id = '12345' + return requestObject + }) + } }