diff --git a/lansweeper/aws/integration-lambda/lansweeper_api_helper.js b/lansweeper/aws/integration-lambda/lansweeper_api_helper.js index 8b3cb99..d35b4a4 100644 --- a/lansweeper/aws/integration-lambda/lansweeper_api_helper.js +++ b/lansweeper/aws/integration-lambda/lansweeper_api_helper.js @@ -19,6 +19,8 @@ class LansweeperApiHelper { createClient(url, bearerToken) { const headers = { 'Content-Type': 'application/json', + 'x-ls-integration-id': LansweeperApiHelper.LS_INTEGRATION_ID, + 'x-ls-integration-version': LansweeperApiHelper.LS_INTEGRATION_VERSION, }; if (bearerToken) { headers['authorization'] = `Bearer ${bearerToken}`; @@ -118,7 +120,7 @@ class LansweeperApiHelper { try { const client = this.createClient(this.apiUrl, accessToken); const lecResponse = await client.post( - `/v2/graphql`, + '/v2/graphql', { query: query, variables: vars, @@ -164,5 +166,7 @@ class LansweeperApiHelper { } } } +LansweeperApiHelper.LS_INTEGRATION_VERSION = '1.0'; +LansweeperApiHelper.LS_INTEGRATION_ID = 'a561a72d-aa5e-4c52-af6d-084129ab5f09'; -module.exports = LansweeperApiHelper; \ No newline at end of file +module.exports = LansweeperApiHelper; diff --git a/lansweeper/aws/integration-lambda/tests/lansweeper_api_helper.test.js b/lansweeper/aws/integration-lambda/tests/lansweeper_api_helper.test.js new file mode 100644 index 0000000..8eb03a4 --- /dev/null +++ b/lansweeper/aws/integration-lambda/tests/lansweeper_api_helper.test.js @@ -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'); + }); +});