From 0615e0f72c89cd7c12a764d285722383783552d2 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Sat, 8 Oct 2016 12:22:18 +0200 Subject: [PATCH] add request new token method in auth service --- src/api/auth.js | 6 ++++++ test/auth.js | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/api/auth.js b/src/api/auth.js index de8d24b..24dfcae 100644 --- a/src/api/auth.js +++ b/src/api/auth.js @@ -45,4 +45,10 @@ export default class Auth { return response.data.token; }); } + + static _requestNewToken(token, config = {}) { + return axios.post('/api/tokens/refresh/', token, config).then(response => { + return response.data.token; + }); + } } diff --git a/test/auth.js b/test/auth.js index 27c9d43..f0e3fa1 100644 --- a/test/auth.js +++ b/test/auth.js @@ -14,13 +14,21 @@ function AuthFactory(token, localStorage = new LocalStorageMock()) { test('request token', t => { const token = '5e0651'; const user = {email: 'test@example.org', password: 'password'}; - nock('https://lesspass.com').post('/api/tokens/auth/', user).reply(201, {token}); return Auth._requestToken(user, {baseURL: 'https://lesspass.com'}).then(requestedToken => { t.is(requestedToken, token); }); }); +test('request new token', t => { + const token = '3e3231'; + const newToken = 'wibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9eyJzdWIiOiIxMjM0NTY3ODkwIi'; + nock('https://lesspass.com').post('/api/tokens/refresh/', {token}).reply(200, {token: newToken}); + return Auth._requestNewToken({token}, {baseURL: 'https://lesspass.com'}).then(refreshedToken => { + t.is(refreshedToken, newToken); + }); +}); + test('user first connection is guest', t => { const storage = new Storage(new LocalStorageMock()); const auth = new Auth(storage);