Skip to content

Commit

Permalink
Revert: Obtain JWT on client init (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
roni-frantchi authored Sep 20, 2022
1 parent 37361db commit a759d35
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 23 deletions.
4 changes: 2 additions & 2 deletions node/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@env0/cli",
"version": "1.0.19",
"version": "1.0.20",
"description": "env0 CLI",
"repository": {
"type": "git",
Expand Down
14 changes: 3 additions & 11 deletions node/src/lib/api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ const { version } = require('../../package.json');

class Env0ApiClient {
async init(apiKey, apiSecret) {
const baseURL = process.env.ENV0_API_URL || 'https://api.env0.com';

const { data: jwt } = await axios.get('auth/token?encoded=true', {
baseURL,
method: 'GET',
this.apiClient = axios.create({
baseURL: process.env.ENV0_API_URL || 'https://api.env0.com',
auth: {
username: apiKey,
password: apiSecret
}
});

this.apiClient = axios.create({
baseURL: process.env.ENV0_API_URL || 'https://api.env0.com',
},
headers: {
Authorization: `Bearer ${jwt}`,
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': `env0-node-cli-${version}`
Expand Down
11 changes: 2 additions & 9 deletions node/tests/lib/api-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ const mockKey = 'key0';
const mockSecret = 'secret0';

describe('api-client', () => {
const jwt = 'iamjwt';

beforeEach(() => {
(axios.get).mockResolvedValue({ data: jwt });
new ApiClient().init(mockKey, mockSecret);
});

it('should obtain a jwt', () => {
expect(axios.get).toBeCalledWith('auth/token?encoded=true', expect.objectContaining({ auth: { username: mockKey, password: mockSecret } }));
});

it('should set authorization to jwt', () => {
expect(axios.create).toBeCalledWith(expect.objectContaining({ headers: expect.objectContaining({ Authorization: `Bearer ${jwt}` }) }));
it('should set authorization header', () => {
expect(axios.create).toBeCalledWith(expect.objectContaining({ auth: { username: mockKey, password: mockSecret } }));
});

it('should set user agent header', () => {
Expand Down

0 comments on commit a759d35

Please sign in to comment.