From 90611ffacc1ca9865464a6f8ac541c09c3ca882c Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Thu, 11 Jan 2018 23:02:45 +0100
Subject: [PATCH] Added missing ajax unit tests

---
 front/src/store/auth.js                  |  3 +-
 front/test/unit/specs/store/auth.spec.js | 92 ++++++++++++++++++++++++
 front/test/unit/utils.js                 |  4 +-
 3 files changed, 95 insertions(+), 4 deletions(-)

diff --git a/front/src/store/auth.js b/front/src/store/auth.js
index e276d0a1..24dafcd7 100644
--- a/front/src/store/auth.js
+++ b/front/src/store/auth.js
@@ -39,7 +39,6 @@ export default {
       state.token = value
       if (value) {
         state.tokenData = jwtDecode(value)
-        console.log(state.tokenData)
       } else {
         state.tokenData = {}
       }
@@ -50,7 +49,7 @@ export default {
   },
   actions: {
     // Send a request to the login URL and save the returned JWT
-    login ({commit, dispatch, state}, {next, credentials, onError}) {
+    login ({commit, dispatch}, {next, credentials, onError}) {
       return axios.post('token/', credentials).then(response => {
         logger.default.info('Successfully logged in as', credentials.username)
         commit('token', response.data.token)
diff --git a/front/test/unit/specs/store/auth.spec.js b/front/test/unit/specs/store/auth.spec.js
index 8be2708f..aa07f9f8 100644
--- a/front/test/unit/specs/store/auth.spec.js
+++ b/front/test/unit/specs/store/auth.spec.js
@@ -1,8 +1,21 @@
+var sinon = require('sinon')
+import moxios from 'moxios'
 import store from '@/store/auth'
 
 import { testAction } from '../../utils'
 
 describe('store/auth', () => {
+  var sandbox
+
+  beforeEach(function () {
+    sandbox = sinon.sandbox.create()
+    moxios.install()
+  })
+  afterEach(function () {
+    sandbox.restore()
+    moxios.uninstall()
+  })
+
   describe('mutations', () => {
     it('profile', () => {
       const state = {}
@@ -104,5 +117,84 @@ describe('store/auth', () => {
         ]
       }, done)
     })
+    it('login success', (done) => {
+      moxios.stubRequest('token/', {
+        status: 200,
+        response: {
+          token: 'test'
+        }
+      })
+      const credentials = {
+        username: 'bob'
+      }
+      testAction({
+        action: store.actions.login,
+        payload: {credentials: credentials},
+        expectedMutations: [
+          { type: 'token', payload: 'test' },
+          { type: 'username', payload: 'bob' },
+          { type: 'authenticated', payload: true }
+        ],
+        expectedActions: [
+          { type: 'fetchProfile' }
+        ]
+      }, done)
+    })
+    it('login error', (done) => {
+      moxios.stubRequest('token/', {
+        status: 500,
+        response: {
+          token: 'test'
+        }
+      })
+      const credentials = {
+        username: 'bob'
+      }
+      let spy = sandbox.spy()
+      testAction({
+        action: store.actions.login,
+        payload: {credentials: credentials, onError: spy}
+      }, () => {
+        expect(spy.calledOnce).to.equal(true)
+        done()
+      })
+    })
+    it('fetchProfile', (done) => {
+      const profile = {
+        username: 'bob',
+        permissions: {
+          admin: {
+            status: true
+          }
+        }
+      }
+      moxios.stubRequest('users/users/me/', {
+        status: 200,
+        response: profile
+      })
+      testAction({
+        action: store.actions.fetchProfile,
+        expectedMutations: [
+          { type: 'profile', payload: profile },
+          { type: 'permission', payload: {key: 'admin', status: true} }
+        ],
+        expectedActions: [
+          { type: 'favorites/fetch', payload: null, options: {root: true} }
+        ]
+      }, done)
+    })
+    it('refreshToken', (done) => {
+      moxios.stubRequest('token/refresh/', {
+        status: 200,
+        response: {token: 'newtoken'}
+      })
+      testAction({
+        action: store.actions.refreshToken,
+        params: {state: {token: 'oldtoken'}},
+        expectedMutations: [
+          { type: 'token', payload: 'newtoken' }
+        ]
+      }, done)
+    })
   })
 })
diff --git a/front/test/unit/utils.js b/front/test/unit/utils.js
index a48e4d30..233ee982 100644
--- a/front/test/unit/utils.js
+++ b/front/test/unit/utils.js
@@ -66,8 +66,8 @@ export const testAction = ({action, payload, params, expectedMutations, expected
   // call the action with mocked store and arguments
   let promise = action({ commit, dispatch, ...params }, payload)
   if (promise) {
-    promise.then(end)
+    return promise.then(end)
   } else {
-    end()
+    return end()
   }
 }
-- 
GitLab