changeset 162:9908260d1e6a

Refactor: Login expiry refactored to lib test: First unit test for session Cleaned up Code regarding expired sessions. First unit test established for session mostly for educational purposes.
author Thomas Junk <thomas.junk@intevation.de>
date Thu, 05 Jul 2018 18:01:39 +0200
parents b7ac2e4f9c5c
children 30c212a6f580
files client/src/lib/session.js client/src/router.js client/src/stores/user.js client/tests/unit/HelloWorld.spec.js client/tests/unit/session.spec.js
diffstat 5 files changed, 36 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/src/lib/session.js	Thu Jul 05 18:01:39 2018 +0200
@@ -0,0 +1,9 @@
+function sessionStillActive(expiresFromPastSession) {
+  if (!expiresFromPastSession) return false;
+  const now = Date.now();
+  const sessionEnd = expiresFromPastSession;
+  const stillActive = now < sessionEnd;
+  return stillActive;
+}
+
+export { sessionStillActive };
--- a/client/src/router.js	Thu Jul 05 16:52:39 2018 +0200
+++ b/client/src/router.js	Thu Jul 05 18:01:39 2018 +0200
@@ -3,6 +3,7 @@
 import Login from "./views/Login.vue";
 import Main from "./views/Main.vue";
 import store from "./store";
+import { sessionStillActive } from "./lib/session";
 
 Vue.use(Router);
 
@@ -22,18 +23,13 @@
       },
       beforeEnter: (to, from, next) => {
         const expiresFromPastSession = localStorage.getItem("expires");
-        const pastSessionStillActive = () => {
-          if (!expiresFromPastSession) return false;
-          const now = new Date();
-          const sessionEnd = new Date(expiresFromPastSession * 1000);
-          const stillActive = now < sessionEnd;
-          return stillActive;
-        };
-        if (pastSessionStillActive()) {
+        if (sessionStillActive(expiresFromPastSession)) {
           store.commit("user/set_user", localStorage.getItem("user"));
           store.commit("user/set_expires", expiresFromPastSession);
           store.commit("user/set_roles", localStorage.getItem("roles"));
-          store.commit("user/authenticate", true);
+          store.commit("user/set_authenticate", true);
+        } else {
+          store.commit("user/clear_auth");
         }
         next();
       }
@@ -47,11 +43,13 @@
 
 router.beforeEach((to, from, next) => {
   const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
-  const currentUser = localStorage.getItem("token") != null;
-  const userIsAuthenticated = requiresAuth && !currentUser;
-  if (userIsAuthenticated) {
+  const loggedIn = store.getters.authenticated;
+  const expiresFromPastSession = localStorage.getItem("expires");
+  const authRequired =
+    requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession));
+  if (authRequired) {
     next("/login");
-  } else if (!userIsAuthenticated) {
+  } else if (!authRequired) {
     next();
   } else {
     next();
--- a/client/src/stores/user.js	Thu Jul 05 16:52:39 2018 +0200
+++ b/client/src/stores/user.js	Thu Jul 05 18:01:39 2018 +0200
@@ -50,7 +50,7 @@
     set_expires: (state, expires) => {
       state.expires = expires;
     },
-    authenticate: state => {
+    set_authenticate: state => {
       state.authenticated = true;
     }
   },
--- a/client/tests/unit/HelloWorld.spec.js	Thu Jul 05 16:52:39 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-describe("HelloWorld.vue", () => {
-  it("renders props.msg when passed", () => {
-    expect(true);
-  });
-});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/tests/unit/session.spec.js	Thu Jul 05 18:01:39 2018 +0200
@@ -0,0 +1,15 @@
+import { sessionStillActive } from "../../src/lib/session";
+
+test("No session counts as expired session", () => {
+  expect(sessionStillActive(null)).toBe(false);
+});
+
+test("Session expired", () => {
+  const expired = Date.now() - 6000;
+  expect(sessionStillActive(expired)).toBe(false);
+});
+
+test("Session alive", () => {
+  const active = Date.now() + 60000;
+  expect(sessionStillActive(active)).toBe(true);
+});