Преглед изворни кода

add tests for auth service

pull/342/head
Guillaume Vincent пре 8 година
родитељ
комит
5c8fa8376d
4 измењених фајлова са 108 додато и 19 уклоњено
  1. +9
    -2
      package.json
  2. +1
    -1
      src/app/Navbar/Navbar.vue
  3. +13
    -16
      src/services/auth.js
  4. +85
    -0
      tests/services/auth.tests.js

+ 9
- 2
package.json Прегледај датотеку

@@ -4,7 +4,8 @@
"author": "Guillaume Vincent <guillaume@oslab.fr>",
"scripts": {
"dev": "webpack-dev-server --inline --hot --host 0.0.0.0",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules",
"test": "mocha --ui tdd --require babel-core/register --globals localStorage tests/**/*.js"
},
"dependencies": {
"bootstrap": "^4.0.0-alpha.2",
@@ -19,7 +20,8 @@
"toastr": "^2.1.2",
"vue": "^1.0.20",
"vue-i18n": "^2.4.1",
"vue-router": "^0.7.11"
"vue-router": "^0.7.11",
"whatwg-fetch": "^0.11.0"
},
"devDependencies": {
"babel-core": "^6.7.4",
@@ -28,8 +30,13 @@
"babel-preset-es2015": "^6.6.0",
"cross-env": "^1.0.7",
"css-loader": "^0.23.1",
"exports-loader": "^0.6.3",
"file-loader": "^0.8.5",
"imports-loader": "^0.6.5",
"json-loader": "^0.5.4",
"mocha": "^2.4.5",
"nock": "^7.7.2",
"node-localstorage": "^1.1.2",
"url-loader": "^0.5.7",
"vue-hot-reload-api": "^1.3.2",
"vue-html-loader": "^1.2.0",


+ 1
- 1
src/app/Navbar/Navbar.vue Прегледај датотеку

@@ -48,7 +48,7 @@
},
methods: {
logout() {
auth.logout(() => {
auth.logout().then(() => {
logging.success(this.$t('login.logout_ok'));
this.$router.go('/');
});


+ 13
- 16
src/services/auth.js Прегледај датотеку

@@ -1,5 +1,5 @@
import promise from 'es6-promise';
promise.polyfill();
import { polyfill } from 'es6-promise';
polyfill();
import 'isomorphic-fetch';

function checkStatus(response) {
@@ -15,7 +15,7 @@ function parseJSON(response) {
return response.json();
}

module.exports = {
export default {
user: {
authenticated: false,
},
@@ -37,23 +37,20 @@ module.exports = {
});
},

logout(callback) {
localStorage.removeItem('token');
this.user.authenticated = false;
if (callback) {
callback();
}
logout() {
return new Promise((resolve, reject) => {
try {
localStorage.removeItem('token');
this.user.authenticated = false;
resolve();
} catch (e) {
reject('cannot logout');
}
});
},

checkAuth() {
const jwt = localStorage.getItem('token');
this.user.authenticated = !!jwt;
},

getAuthHeader() {
const token = localStorage.getItem('token');
return {
Authorization: `Bearer ${token}!`,
};
},
};

+ 85
- 0
tests/services/auth.tests.js Прегледај датотеку

@@ -0,0 +1,85 @@
import assert from 'assert';
import auth from '../../src/services/auth';
import nock from 'nock';


suite('Auth', () => {
var credentials = {
email: 'test@lesspass.com',
password: 'password'
};
var token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ';

before(() => {
var LocalStorage = require('node-localstorage').LocalStorage;
global.localStorage = new LocalStorage('./tests/localStorage');
});

beforeEach(() => {
nock('//lesspass.com/').post('/api/sessions/', credentials).reply(201, {token: token});
});

test('should make a post request to create a session', (done) => {
auth.login(credentials)
.then(() => {
done();
});
});

test('should throw error if bad request', (done) => {
nock.cleanAll();
var badCredentials = {email: 'test@lesspass.com', password: '黑客'};
nock('//lesspass.com/').post('/api/sessions/', badCredentials).reply(400, {});
auth.login(credentials)
.catch((error) => {
done();
});
});

test('should store token in localStorage', (done) => {
auth.login(credentials)
.then((data) => {
assert.equal(token, localStorage.getItem('token'));
done();
});
});

test('should authenticate the user', (done) => {
auth.user.authenticated = false;
auth.login(credentials)
.then((data) => {
assert(auth.user.authenticated);
done();
});
});

test('check auth', (done) => {
auth.login(credentials)
.then((data) => {
assert(auth.user.authenticated);
localStorage.removeItem('token');
auth.checkAuth();
assert(!auth.user.authenticated);
done();
});
});

test('logout', (done) => {
auth.login(credentials)
.then((data) => {
assert(auth.user.authenticated);
auth.logout();
assert(!auth.user.authenticated);
assert(localStorage.getItem('token') === null);
done();
});
});

test('logout return promise', (done) => {
auth.logout().then(done)
});

after(() => {
global.localStorage._deleteLocation()
})
});

Loading…
Откажи
Сачувај