Skip to content

Commit

Permalink
fix: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dploeger committed Nov 18, 2024
1 parent dca8f3f commit 82c975c
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 1 deletion.
35 changes: 34 additions & 1 deletion test/ConfluenceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -35,6 +35,39 @@ describe('The Confluence API', (): void => {
it('should add a configuration document', async (): Promise<void> => {
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<void> => {
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<void> => {
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')
Expand Down
109 changes: 109 additions & 0 deletions test/MockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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<string, any>
requestObject.id = '12345'
return requestObject
})
}
}

0 comments on commit 82c975c

Please sign in to comment.