瀏覽代碼

add api user

pull/342/head
Guillaume Vincent 7 年之前
父節點
當前提交
f08227f93c
共有 2 個文件被更改,包括 56 次插入0 次删除
  1. +20
    -0
      src/api/user.js
  2. +36
    -0
      test/api.user.js

+ 20
- 0
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);
}
}

+ 36
- 0
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'}));
});

Loading…
取消
儲存