-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom Lansweeper headers to identify our app
- Loading branch information
Fried Hoeben
committed
Jan 9, 2024
1 parent
6a4d7f1
commit 42cbe24
Showing
2 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
lansweeper/aws/integration-lambda/tests/lansweeper_api_helper.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
'use strict'; | ||
|
||
const axios = require('axios') | ||
jest.mock('axios'); | ||
const LansweeperApiHelper = require('../lansweeper_api_helper'); | ||
|
||
const expectedOAuthConfig = { | ||
"baseURL": "https://api.lansweeper.com/api/integrations/oauth", | ||
"headers": { | ||
"Content-Type": "application/json", | ||
"x-ls-integration-version": LansweeperApiHelper.LS_INTEGRATION_VERSION, | ||
"x-ls-integration-id": LansweeperApiHelper.LS_INTEGRATION_ID, | ||
}, | ||
"timeout": 30000 | ||
}; | ||
|
||
describe('getRefreshToken', () => { | ||
it('posts correct data', async () => { | ||
axios.create.mockImplementation((config) => { | ||
expect(config).toEqual(expectedOAuthConfig); | ||
|
||
return { | ||
post: async (path, data) => { | ||
expect(path).toBe('/token'); | ||
expect(data.client_id).toBe('client id'); | ||
expect(data.client_secret).toBe('my secret'); | ||
expect(data.grant_type).toBe('authorization_code'); | ||
expect(data.code).toBe('my code'); | ||
expect(data.redirect_uri).toBe('my uri'); | ||
return { | ||
status: 200, | ||
data: { | ||
refresh_token: 'A9B8C8', | ||
}, | ||
}; | ||
} | ||
} | ||
}); | ||
|
||
const apiHelper = new LansweeperApiHelper('client id', 'my secret', null); | ||
expect(await apiHelper.getRefreshToken('my code', 'my uri')).toEqual('A9B8C8'); | ||
}); | ||
}); | ||
|
||
describe('getAccessToken', () => { | ||
it('posts correct data', async () => { | ||
axios.create.mockImplementation((config) => { | ||
expect(config).toEqual(expectedOAuthConfig); | ||
|
||
return { | ||
post: async (path, data) => { | ||
expect(path).toBe('/token'); | ||
expect(data.client_id).toBe('client id2'); | ||
expect(data.client_secret).toBe('my secret2'); | ||
expect(data.grant_type).toBe('refresh_token'); | ||
expect(data.refresh_token).toBe('my refresh token'); | ||
return { | ||
status: 200, | ||
data: { | ||
access_token: 'my token', | ||
}, | ||
}; | ||
} | ||
} | ||
}); | ||
|
||
const apiHelper = new LansweeperApiHelper('client id2', 'my secret2', 'my refresh token'); | ||
expect(await apiHelper.getAccessToken()).toEqual('my token'); | ||
}); | ||
}); | ||
|
||
describe('getGraphQLQuery', () => { | ||
it('posts correct data', async () => { | ||
const query = 'some graphql'; | ||
const queryVars = {a: 1, b: 'abav'}; | ||
let clientCounter = 0; | ||
|
||
axios.create.mockImplementation((config) => { | ||
clientCounter++; | ||
if (clientCounter === 1) { | ||
// refresh token client is created in constructor | ||
expect(config).toEqual(expectedOAuthConfig); | ||
} else { | ||
// GraphQL client | ||
expect(config).toEqual( | ||
{ | ||
"baseURL": "https://api.lansweeper.com/api", | ||
"headers": { | ||
"Content-Type": "application/json", | ||
"x-ls-integration-id": "a561a72d-aa5e-4c52-af6d-084129ab5f09", | ||
"x-ls-integration-version": "1.0", | ||
"authorization": "Bearer my token", | ||
}, | ||
"timeout": 30000 | ||
} | ||
); | ||
} | ||
|
||
return { | ||
post: async (path, data) => { | ||
expect(path).toBe('/v2/graphql'); | ||
expect(data.query).toBe(query); | ||
expect(data.variables).toBe(queryVars) | ||
return { | ||
status: 200, | ||
data: { | ||
data: 'query result', | ||
}, | ||
}; | ||
} | ||
} | ||
}); | ||
|
||
const apiHelper = new LansweeperApiHelper('client id2', 'my secret2', 'abc'); | ||
apiHelper.accessToken = 'my token'; | ||
expect(await apiHelper.getGraphQLQuery('my descr', query, queryVars)).toEqual('query result'); | ||
}); | ||
}); |