balena-request
Advanced tools
Comparing version 13.2.0 to 13.2.1-build-only-throws-expired-when-really-expired-7c125fb699e677d770fa0f313b4e7187b3ed126a-1
@@ -346,3 +346,5 @@ "use strict"; | ||
catch (err) { | ||
if (err.code === 'BalenaRequestError' && err.statusCode === 401) { | ||
if (err.code === 'BalenaRequestError' && | ||
err.statusCode === 401 && | ||
(yield auth.isExpired())) { | ||
const expiredKey = yield auth.getKey(); | ||
@@ -349,0 +351,0 @@ yield auth.removeKey(); |
@@ -7,2 +7,6 @@ # Change Log | ||
## 13.2.1 - 2024-02-12 | ||
* fix: Throws BalenaExpiredToken only when token is really expired [Otávio Jacobi] | ||
## 13.2.0 - 2023-11-01 | ||
@@ -9,0 +13,0 @@ |
@@ -64,3 +64,3 @@ /* | ||
percentage: state.percentage, | ||
}) | ||
}) | ||
: undefined; | ||
@@ -67,0 +67,0 @@ }); |
@@ -109,3 +109,3 @@ /* | ||
? // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
utils.getRequestAsync(require('fetch-readablestream') as typeof fetch) | ||
utils.getRequestAsync(require('fetch-readablestream') as typeof fetch) | ||
: requestAsync; | ||
@@ -116,3 +116,3 @@ | ||
// noop | ||
} | ||
} | ||
: utils.debugRequest; | ||
@@ -474,3 +474,7 @@ | ||
} catch (err: any) { | ||
if (err.code === 'BalenaRequestError' && err.statusCode === 401) { | ||
if ( | ||
err.code === 'BalenaRequestError' && | ||
err.statusCode === 401 && | ||
(await auth.isExpired()) | ||
) { | ||
const expiredKey = await auth.getKey(); | ||
@@ -477,0 +481,0 @@ await auth.removeKey(); |
@@ -25,3 +25,3 @@ /* | ||
import type BalenaAuth from 'balena-auth'; | ||
import { FormData as FormDataNodeType } from 'formdata-node'; | ||
import type { FormData as FormDataNodeType } from 'formdata-node'; | ||
import { TokenType } from 'balena-auth'; | ||
@@ -28,0 +28,0 @@ import type { |
{ | ||
"name": "balena-request", | ||
"version": "13.2.0", | ||
"version": "13.2.1-build-only-throws-expired-when-really-expired-7c125fb699e677d770fa0f313b4e7187b3ed126a-1", | ||
"description": "Balena HTTP client", | ||
@@ -85,4 +85,4 @@ "main": "build/request.js", | ||
"versionist": { | ||
"publishedAt": "2023-11-01T13:42:29.151Z" | ||
"publishedAt": "2024-02-12T14:58:55.164Z" | ||
} | ||
} |
@@ -87,6 +87,2 @@ import { expect } from 'chai'; | ||
this.utilsShouldUpdateToken.returns(Promise.resolve(true)); | ||
this.authIsExpired = sinon.stub(auth, 'isExpired'); | ||
this.authIsExpired.returns(Promise.resolve(false)); | ||
return auth.setKey(johnDoeFixture.token); | ||
@@ -97,12 +93,18 @@ }); | ||
this.utilsShouldUpdateToken.restore(); | ||
return this.authIsExpired.restore(); | ||
}); | ||
describe('given a working /user/v1/refresh-token endpoint', function () { | ||
beforeEach(() => | ||
mockServer | ||
beforeEach(async function () { | ||
await mockServer | ||
.forGet('/user/v1/refresh-token') | ||
.thenReply(200, janeDoeFixture.token), | ||
); | ||
.thenReply(200, janeDoeFixture.token); | ||
this.authIsExpired = sinon.stub(auth, 'isExpired'); | ||
this.authIsExpired.returns(Promise.resolve(false)); | ||
}); | ||
afterEach(function () { | ||
return this.authIsExpired.restore(); | ||
}); | ||
describe('given no base url', () => | ||
@@ -168,26 +170,27 @@ it('should not refresh the token', () => | ||
describe('given /user/v1/refresh-token returns 401', function () { | ||
beforeEach(() => | ||
mockServer | ||
describe('given /user/v1/refresh-token returns 401 & token is expired', function () { | ||
beforeEach(async function () { | ||
await mockServer | ||
.forGet('/user/v1/refresh-token') | ||
.thenReply(401, 'Unauthorized'), | ||
); | ||
.thenReply(401, 'Unauthorized'); | ||
this.authIsExpired = sinon.stub(auth, 'isExpired'); | ||
this.authIsExpired.returns(Promise.resolve(true)); | ||
}); | ||
afterEach(function () { | ||
return this.authIsExpired.restore(); | ||
}); | ||
describe('given an absolute url', () => | ||
it('should not attempt to refresh the token', () => | ||
auth | ||
.getKey() | ||
.then((savedToken) => { | ||
expect(savedToken).to.equal(johnDoeFixture.token); | ||
return request.send({ | ||
url: mockServer.url + '/foo', | ||
}); | ||
}) | ||
.then(function (response) { | ||
expect(response.body).to.equal('bar'); | ||
return auth.getKey(); | ||
}) | ||
.then((savedToken) => | ||
expect(savedToken).to.equal(johnDoeFixture.token), | ||
))); | ||
it('should fail with an expiration error', () => { | ||
const promise = auth.getKey().then((savedToken) => { | ||
expect(savedToken).to.equal(johnDoeFixture.token); | ||
return request.send({ | ||
url: mockServer.url + '/foo', | ||
}); | ||
}); | ||
return expect(promise).to.be.rejectedWith( | ||
errors.BalenaExpiredToken, | ||
); | ||
})); | ||
@@ -225,6 +228,8 @@ it('should be rejected with an expiration error', function () { | ||
describe('given /user/v1/refresh-token returns a non 401 status code', function () { | ||
beforeEach(() => | ||
mockServer.forGet('/user/v1/refresh-token').thenReply(500), | ||
); | ||
describe('given /user/v1/refresh-token returns a non 401 status code or 401 with not expired token', function () { | ||
beforeEach(async function () { | ||
await mockServer.forGet('/user/v1/refresh-token').thenReply(500); | ||
this.authIsExpired = sinon.stub(auth, 'isExpired'); | ||
this.authIsExpired.returns(Promise.resolve(false)); | ||
}); | ||
@@ -231,0 +236,0 @@ it('should be rejected with a request error', function () { |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
227494
4410
1