From 4f7106d9ee2d3b2c91c8a893008171dc19aa69ac Mon Sep 17 00:00:00 2001 From: Carlos Purificacao Date: Fri, 2 Oct 2015 11:56:43 -0300 Subject: [PATCH] Added the missing session store --- lib/noosfero/api/session_store.rb | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/api/session_store_test.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 0 deletions(-) create mode 100644 lib/noosfero/api/session_store.rb create mode 100644 test/unit/api/session_store_test.rb diff --git a/lib/noosfero/api/session_store.rb b/lib/noosfero/api/session_store.rb new file mode 100644 index 0000000..6e75ed5 --- /dev/null +++ b/lib/noosfero/api/session_store.rb @@ -0,0 +1,53 @@ +## A session store for the API. It can store +## generic data on the Rails Cache to simulate +## a stateful session for API methods +class Noosfero::API::SessionStore + + ## A generic data value to allow storing any + ## value within this SessionStore + attr_accessor :data + ## The user private_token associated with this SessionStore + attr_reader :private_token + ## The key for this SessionStore in the Rails Cache + attr_reader :key + + ## Call this method to create and store a SessionStore + ## in Rails Cache. The SessionStore is returned. The + ## client_key parameter, if used, will uniquely identify + ## this SessionStore in Rails Cache, along with the user + ## private_token in the form: client_key#private_token + def self.create(client_key = nil) + private_token = SecureRandom.hex + store = Noosfero::API::SessionStore.new(client_key, private_token) + Rails.cache.write(store.key, store, expires_in: 300) + return store + end + + ## Creates a new SessionStore. Do not use directly in cliente code. + ## Please use the self.create method instead + def initialize(client_key, private_token) + ## Creates the key to store this object in Rails Cache + key = "#{client_key}#" if client_key + key = "#{key}#{private_token}" + @key = key + @private_token = private_token + end + + ## Returns the SessionStore in Rails Cache associated + ## with the given key + def self.get(key) + Rails.cache.fetch(key) + end + + ## Stores this SessionStore in Rails Cache using the + ## key attribute as the unique identifier + def store + Rails.cache.write(@key, self) + end + + ## Remove this session store from Rails Cache + def destroy + Rails.cache.delete(@key) + end + +end diff --git a/test/unit/api/session_store_test.rb b/test/unit/api/session_store_test.rb new file mode 100644 index 0000000..575198f --- /dev/null +++ b/test/unit/api/session_store_test.rb @@ -0,0 +1,47 @@ +require File.dirname(__FILE__) + '/test_helper' + +class SessionStoreTest < ActiveSupport::TestCase + + should 'create a session store without client key' do + store = Noosfero::API::SessionStore.create + assert_not_nil store + private_token = store.private_token + assert_not_nil private_token + key = store.key + assert_not_nil key + assert_equal key, private_token + end + + should 'create a session store with client key' do + store = Noosfero::API::SessionStore.create("mykey") + assert_not_nil store + private_token = store.private_token + assert_not_nil private_token + key = store.key + assert_not_nil key + assert_equal key, "mykey##{private_token}" + end + + should 'get a session store with client key' do + store = Noosfero::API::SessionStore.create("mykey") + retrieved = Noosfero::API::SessionStore.get(store.key) + assert_not_nil retrieved + end + + should 'not get a destroyed session store with client key' do + store = Noosfero::API::SessionStore.create("mykey") + store.destroy + retrieved = Noosfero::API::SessionStore.get(store.key) + assert_nil retrieved + end + + should 'store data in session store' do + store = Noosfero::API::SessionStore.create("mykey") + store.data = [1, 2] + ## Put it back in cache + store.store + retrieved = Noosfero::API::SessionStore.get(store.key) + assert_equal [1,2], retrieved.data + end + +end \ No newline at end of file -- libgit2 0.21.2