update api.js api client, and axios mock, and add api.js unit tests

This commit is contained in:
John Mitchell
2018-10-24 16:52:11 -04:00
parent 22112f3dd8
commit 0373058540
3 changed files with 147 additions and 2 deletions

View File

@@ -11,6 +11,8 @@ const API_ORGANIZATIONS = `${API_V2}organizations/`;
const CSRF_COOKIE_NAME = 'csrftoken';
const CSRF_HEADER_NAME = 'X-CSRFToken';
const LOGIN_CONTENT_TYPE = 'application/x-www-form-urlencoded';
class APIClient {
constructor () {
this.http = axios.create({
@@ -19,10 +21,15 @@ class APIClient {
});
}
/* eslint class-methods-use-this: ["error", { "exceptMethods": ["getCookie"] }] */
getCookie () {
return document.cookie;
}
isAuthenticated () {
let authenticated = false;
const parsed = (`; ${document.cookie}`).split('; userLoggedIn=');
const parsed = (`; ${this.getCookie()}`).split('; userLoggedIn=');
if (parsed.length === 2) {
authenticated = parsed.pop().split(';').shift() === 'true';
@@ -37,7 +44,7 @@ class APIClient {
const next = encodeURIComponent(redirect);
const data = `username=${un}&password=${pw}&next=${next}`;
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
const headers = { 'Content-Type': LOGIN_CONTENT_TYPE };
return this.http.get(API_LOGIN, { headers })
.then(() => this.http.post(API_LOGIN, data, { headers }));