Browse Source

Update App.Session.get() method to use direct pointer to current user record

Ryan Lue 6 years ago
parent
commit
9e0d535483

+ 7 - 6
app/assets/javascripts/app/lib/app_init/session.coffee

@@ -5,15 +5,17 @@ class App.Session
     _instance ?= new _sessionSingleton
     _instance.clear()
 
+  # Do NOT modify the return value of this method!
+  # It is a direct reference to a value in the App.User.irecords object.
   @get: (key) ->
     if _instance == undefined
       _instance ?= new _sessionSingleton
     _instance.get(key)
 
-  @set: (user) ->
+  @set: (user_id) ->
     if _instance == undefined
       _instance ?= new _sessionSingleton
-    _instance.set(user)
+    _instance.set(user_id)
 
 class _sessionSingleton extends Spine.Module
   @include App.LogInclude
@@ -25,10 +27,9 @@ class _sessionSingleton extends Spine.Module
     @user = undefined
 
   get: (key) ->
-    return if !@user
     if key
-      return @user[key]
+      return @user?[key]
     @user
 
-  set: (user) ->
-    @user = user
+  set: (user_id) ->
+    @user = App.User.findNative(user_id)

+ 1 - 2
app/assets/javascripts/app/lib/app_post/auth.coffee

@@ -123,8 +123,7 @@ class App.Auth
       App.Collection.loadAssets(data.assets)
 
     # store user data
-    sessionUser = App.User.fullLocal(data.session.id)
-    App.Session.set(sessionUser)
+    App.Session.set(data.session.id)
 
     # trigger auth ok with new session data
     App.Event.trigger('auth', data.session)

+ 16 - 0
app/views/tests/session.html.erb

@@ -0,0 +1,16 @@
+
+<link rel="stylesheet" href="/assets/tests/qunit-1.21.0.css">
+<script src="/assets/tests/qunit-1.21.0.js"></script>
+<script src="/assets/tests/session.js"></script>
+
+<style type="text/css">
+body {
+  padding-top: 0px;
+}
+</style>
+
+<script type="text/javascript">
+</script>
+
+<div id="qunit" class="u-dontfold"></div>
+

+ 1 - 0
config/routes/test.rb

@@ -1,6 +1,7 @@
 Zammad::Application.routes.draw do
 
   match '/tests_core',                    to: 'tests#core',                       via: :get
+  match '/tests_session',                 to: 'tests#session',                    via: :get
   match '/tests_ui',                      to: 'tests#ui',                         via: :get
   match '/tests_model',                   to: 'tests#model',                      via: :get
   match '/tests_model_binding',           to: 'tests#model_binding',              via: :get

+ 72 - 0
public/assets/tests/session.js

@@ -0,0 +1,72 @@
+window.onload = function() {
+
+test('test current user behaviour by updating session user via assets', function() {
+
+  // load user
+  App.User.refresh([{
+    "login": "hh@example.com",
+    "firstname": "Harald",
+    "lastname": "Habebe",
+    "email": "hh@example.com",
+    "role_ids": [ 1, 2, 4 ],
+    "group_ids": [ 1 ],
+    "active": true,
+    "updated_at": "2017-02-09T09:17:04.770Z",
+    "address": "",
+    "vip": false,
+    "custom_key": undefined,
+    "asdf": "",
+    "id": 6
+  }]);
+
+  // set session user
+  App.Session.set(6)
+
+  // verify attributes
+  equal(App.Session.get('id'), 6)
+  equal(App.Session.get('login'), 'hh@example.com')
+  equal(App.Session.get('vip'), false)
+  equal(App.Session.get('custom_key'), undefined)
+  equal(App.Session.get().id, 6)
+  equal(App.Session.get().login, 'hh@example.com')
+  equal(App.Session.get().custom_key, undefined)
+  equal(App.Session.get().not_existing, undefined)
+
+  // update session user via assets
+  App.User.refresh([{
+    "login": "hh_new@example.com",
+    "firstname": "Harald",
+    "lastname": "Habebe",
+    "email": "hh_new@example.com",
+    "role_ids": [ 1, 2, 4 ],
+    "group_ids": [ 1 ],
+    "active": true,
+    "updated_at": "2017-02-09T09:17:04.770Z",
+    "address": "",
+    "vip": false,
+    "custom_key": undefined,
+    "asdf": "",
+    "id": 6
+  }]);
+
+  // verify attributes
+  equal(App.Session.get('id'), 6)
+  equal(App.Session.get('login'), 'hh_new@example.com')
+  equal(App.Session.get('vip'), false)
+  equal(App.Session.get('custom_key'), undefined)
+  equal(App.Session.get().id, 6)
+  equal(App.Session.get().login, 'hh_new@example.com')
+  equal(App.Session.get().custom_key, undefined)
+  equal(App.Session.get().not_existing, undefined)
+
+  // clear session
+  App.Session.init()
+  equal(App.Session.get(), undefined)
+  equal(App.Session.get('id'), undefined)
+  equal(App.Session.get('login'), undefined)
+  equal(App.Session.get('vip'), undefined)
+  equal(App.Session.get('custom_key'), undefined)
+
+});
+
+}

+ 12 - 10
public/assets/tests/ticket_selector.js

@@ -141,7 +141,7 @@ window.onload = function() {
       "id": 8
   };
 
-  var sessionData = {
+  App.User.refresh([{
     "login": "hh@zammad.com",
     "firstname": "Harald",
     "lastname": "Habebe",
@@ -175,7 +175,9 @@ window.onload = function() {
     "anrede": "",
     "asdf": "",
     "id": 6
-  };
+  }]);
+
+  var sessionData = App.User.find(6);
 
   /*
    * ------------------------------------------------------------------------
@@ -232,7 +234,7 @@ window.onload = function() {
   };
 
   var testPreConditionUser = function (key, specificValue, ticket, session) {
-    App.Session.set(sessionData);
+    App.Session.set(6);
 
     setting = {
       "condition": {
@@ -324,7 +326,7 @@ window.onload = function() {
   };
 
   var testPreConditionOrganization = function (key, specificValue, ticket, session) {
-    App.Session.set(sessionData);
+    App.Session.set(6);
 
     setting = {
       "condition": {
@@ -416,7 +418,7 @@ window.onload = function() {
   };
 
   var testPreConditionTags = function (key, ticket) {
-    App.Session.set(sessionData);
+    App.Session.set(6);
 
     setting = {
       "condition": {
@@ -701,7 +703,7 @@ window.onload = function() {
     ticket = new App.Ticket();
     ticket.load(ticketData);
 
-    App.Session.set(sessionData);
+    App.Session.set(6);
 
     testPreConditionUser('ticket.customer_id', '6', ticket, sessionData);
   });
@@ -724,7 +726,7 @@ window.onload = function() {
     ticket = new App.Ticket();
     ticket.load(ticketData);
 
-    App.Session.set(sessionData);
+    App.Session.set(6);
 
     testPreConditionUser('ticket.owner_id', '6', ticket, sessionData);
   });
@@ -889,7 +891,7 @@ window.onload = function() {
     ticket = new App.Ticket();
     ticket.load(ticketData);
 
-    App.Session.set(sessionData);
+    App.Session.set(6);
 
     testPreConditionUser('ticket.created_by_id', '6', ticket, sessionData);
   });
@@ -912,7 +914,7 @@ window.onload = function() {
     ticket = new App.Ticket();
     ticket.load(ticketData);
 
-    App.Session.set(sessionData);
+    App.Session.set(6);
 
     testPreConditionUser('ticket.updated_by_id', '6', ticket, sessionData);
   });
@@ -1105,4 +1107,4 @@ window.onload = function() {
 
     testContains('organization.domain', 'cool', ticket);
   });
-}
+}

+ 13 - 0
test/browser/aab_unit_test.rb

@@ -18,7 +18,20 @@ class AAbUnitTest < TestCase
 
   def test_ui
     @browser = browser_instance
+    location(url: browser_url + '/tests_session')
+    sleep 5
+    watch_for(
+      css:     '.result',
+      value:   'Tests completed',
+      timeout: 8,
+    )
+    match(
+      css:   '.result .failed',
+      value: '0',
+    )
+
     location(url: browser_url + '/tests_ui')
+    sleep 5
     watch_for(
       css:     '.result',
       value:   'Tests completed',