Browse Source

add request new token method in auth service

pull/342/head
Guillaume Vincent 8 years ago
parent
commit
0615e0f72c
2 changed files with 15 additions and 1 deletions
  1. +6
    -0
      src/api/auth.js
  2. +9
    -1
      test/auth.js

+ 6
- 0
src/api/auth.js View File

@@ -45,4 +45,10 @@ export default class Auth {
return response.data.token; return response.data.token;
}); });
} }

static _requestNewToken(token, config = {}) {
return axios.post('/api/tokens/refresh/', token, config).then(response => {
return response.data.token;
});
}
} }

+ 9
- 1
test/auth.js View File

@@ -14,13 +14,21 @@ function AuthFactory(token, localStorage = new LocalStorageMock()) {
test('request token', t => { test('request token', t => {
const token = '5e0651'; const token = '5e0651';
const user = {email: 'test@example.org', password: 'password'}; const user = {email: 'test@example.org', password: 'password'};

nock('https://lesspass.com').post('/api/tokens/auth/', user).reply(201, {token}); nock('https://lesspass.com').post('/api/tokens/auth/', user).reply(201, {token});
return Auth._requestToken(user, {baseURL: 'https://lesspass.com'}).then(requestedToken => { return Auth._requestToken(user, {baseURL: 'https://lesspass.com'}).then(requestedToken => {
t.is(requestedToken, token); 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 => { test('user first connection is guest', t => {
const storage = new Storage(new LocalStorageMock()); const storage = new Storage(new LocalStorageMock());
const auth = new Auth(storage); const auth = new Auth(storage);


Loading…
Cancel
Save