From f08227f93ccf3b536557249977306d7a1cb229a5 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Sat, 4 Feb 2017 10:25:58 +0100 Subject: [PATCH] add api user --- src/api/user.js | 20 ++++++++++++++++++++ test/api.user.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/api/user.js create mode 100644 test/api.user.js diff --git a/src/api/user.js b/src/api/user.js new file mode 100644 index 0000000..2148674 --- /dev/null +++ b/src/api/user.js @@ -0,0 +1,20 @@ +import axios from 'axios'; + +export default { + login(user, config) { + return axios.post('/api/tokens/auth/', user, config).then(response => { + return response.data; + }); + }, + register(user, config) { + return axios.post('/api/auth/register/', user, config).then(response => { + return response.data; + }); + }, + resetPassword(email, config) { + return axios.post('/api/auth/password/reset/', email, config); + }, + confirmResetPassword(password, config) { + return axios.post('/api/auth/password/reset/confirm/', password, config); + } +} diff --git a/test/api.user.js b/test/api.user.js new file mode 100644 index 0000000..2d310df --- /dev/null +++ b/test/api.user.js @@ -0,0 +1,36 @@ +import test from 'ava'; +import nock from 'nock'; +import User from '../src/api/user'; + +test('login', 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 User.login(user, {baseURL: 'https://lesspass.com'}).then(response => { + t.is(response.token, token); + }); +}); + +test('register', t => { + const user = {email: 'test@example.org', password: 'password'}; + nock('https://lesspass.com').post('/api/auth/register/', user).reply(201, {email: user.email, pk: 1}); + return User.register(user, {baseURL: 'https://lesspass.com'}).then(response => { + t.is(response.email, user.email); + }); +}); + +test('resetPassword', t => { + var email = 'test@lesspass.com'; + nock('https://lesspass.com').post('/api/auth/password/reset/', {email}).reply(204); + t.notThrows(User.resetPassword({email}, {baseURL: 'https://lesspass.com'})); +}); + +test('confirmResetPassword', t => { + var newPassword = { + uid: 'MQ', + token: '5g1-2bd69bd6f6dcd73f8124', + new_password: 'password1' + }; + nock('https://lesspass.com').post('/api/auth/password/reset/confirm/', newPassword).reply(204); + t.notThrows(User.confirmResetPassword(newPassword, {baseURL: 'https://lesspass.com'})); +}); \ No newline at end of file