Skip to content

Commit

Permalink
Add custom Lansweeper headers to identify our app
Browse files Browse the repository at this point in the history
  • Loading branch information
Fried Hoeben committed Jan 9, 2024
1 parent 6a4d7f1 commit 42cbe24
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lansweeper/aws/integration-lambda/lansweeper_api_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -164,5 +166,7 @@ class LansweeperApiHelper {
}
}
}
LansweeperApiHelper.LS_INTEGRATION_VERSION = '1.0';
LansweeperApiHelper.LS_INTEGRATION_ID = 'a561a72d-aa5e-4c52-af6d-084129ab5f09';

module.exports = LansweeperApiHelper;
module.exports = LansweeperApiHelper;
118 changes: 118 additions & 0 deletions lansweeper/aws/integration-lambda/tests/lansweeper_api_helper.test.js
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');
});
});

0 comments on commit 42cbe24

Please sign in to comment.