Skip to content

Commit 6bf2d45

Browse files
author
marc0l92
committed
Fix tests
1 parent 183eb5c commit 6bf2d45

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

DEVELOP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-sear
3333

3434
## Publish new release
3535

36+
- Make sure test are running `npm run test`
3637
- Define the new version to use x.y.z
3738
- Run `npm run version x.y.z`
3839
- Commit and push the file modified

src/client/jiraClient.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ function buildHeaders(account: IJiraIssueAccountSettings): Record<string, string
8686
}
8787

8888
function isJsonResponse(response: RequestUrlResponse): boolean {
89-
return response.headers['content-type'] && response.headers['content-type'].contains('json') && response.json
89+
return response.headers && response.headers['content-type'] && response.headers['content-type'].includes('json') && response.json !== undefined
90+
}
91+
92+
function isTextResponse(response: RequestUrlResponse): boolean {
93+
return response.headers && response.headers['content-type'] && response.headers['content-type'].includes('text') && response.text !== undefined
9094
}
9195

9296
async function sendRequest(requestOptions: RequestOptions): Promise<any> {
@@ -128,7 +132,7 @@ async function sendRequest(requestOptions: RequestOptions): Promise<any> {
128132
default:
129133
if (isJsonResponse(response) && response.json.message) {
130134
errorMsg = response.json.message
131-
} else if (response.headers['content-type'] && response.headers['content-type'].contains('text') && response.text.contains('<title>Log in')) {
135+
} else if (isTextResponse(response) && response.text.contains('<title>Log in')) {
132136
errorMsg = 'Login required'
133137
} else {
134138
errorMsg = `HTTP ${response.status}`

test/jiraClinet.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TestAccountOpen } from './testData'
44

55
const kIssueKey = 'AAA-123'
66
const requestUrlMock = jest.spyOn(obsidian, 'requestUrl')
7+
const defaultHeaders = { 'content-type': 'application/json' }
78

89
describe('JiraClient', () => {
910
describe('Positive tests', () => {
@@ -19,11 +20,15 @@ describe('JiraClient', () => {
1920
// })
2021

2122
test('testConnection', async () => {
22-
requestUrlMock.mockReturnValue({ status: 200 } as any)
23+
requestUrlMock.mockReturnValue({ status: 200, headers: defaultHeaders, json: { issues: [] } } as any)
2324
expect(await JiraClient.testConnection(TestAccountOpen)).toEqual(true)
2425
expect(requestUrlMock.mock.calls[0][0]).toEqual({
2526
contentType: 'application/json',
26-
headers: {},
27+
headers: {
28+
"Accept": "application/json",
29+
"User-Agent": "obsidian-jira-issue-plugin",
30+
"X-Atlassian-Token": "no-check",
31+
},
2732
method: 'GET',
2833
url: 'https://test-company.atlassian.net/rest/api/latest/project',
2934
})
@@ -33,14 +38,18 @@ describe('JiraClient', () => {
3338
describe('Negative tests', () => {
3439
test('testConnection', async () => {
3540
expect.assertions(2)
36-
requestUrlMock.mockReturnValue({ status: 401 } as any)
41+
requestUrlMock.mockReturnValue({ status: 401, headers: defaultHeaders } as any)
3742
try {
3843
await JiraClient.testConnection(TestAccountOpen)
3944
} catch (e) {
40-
expect(e).toEqual(new Error(`HTTP status 401`))
45+
expect(e).toEqual(new Error(`Unauthorized: Please check your authentication credentials`))
4146
expect(requestUrlMock.mock.calls[0][0]).toEqual({
4247
contentType: 'application/json',
43-
headers: {},
48+
headers: {
49+
"Accept": "application/json",
50+
"User-Agent": "obsidian-jira-issue-plugin",
51+
"X-Atlassian-Token": "no-check",
52+
},
4453
method: 'GET',
4554
url: 'https://test-company.atlassian.net/rest/api/latest/project',
4655
})

test/settings.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const StoredSettings = {
1717
color: 'colorVal',
1818
host: 'hostVal',
1919
bareToken: 'bareToken',
20+
use2025Api: false,
2021
}],
2122
apiBasePath: 'apiBasePathVal',
2223
cache: {

test/testData.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export const TestAccountOpen = {
1818
authenticationType: EAuthenticationTypes.OPEN,
1919
priority: 1,
2020
color: '#123456',
21-
cache: kEmptyAccountCache
21+
use2025Api: false,
22+
cache: kEmptyAccountCache,
2223
}
2324

2425
export const TestAccountBasic = {
@@ -29,5 +30,6 @@ export const TestAccountBasic = {
2930
password: 'password2',
3031
priority: 2,
3132
color: '#789012',
32-
cache: kEmptyAccountCache
33+
use2025Api: false,
34+
cache: kEmptyAccountCache,
3335
}

0 commit comments

Comments
 (0)