jakewendt-simply_sessions 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ = Simply Sessions
2
+
3
+ I use this little gem SOLELY IN TESTING to simulate user sessions in other
4
+ GRAPEs (Gem/RailsApp/PluginEngine).
5
+ It serves no purpose in a real app.
6
+
7
+ == Gemified with Jeweler
8
+
9
+ vi Rakefile
10
+ rake version:write
11
+
12
+ rake version:bump:patch
13
+ rake version:bump:minor
14
+ rake version:bump:major
15
+
16
+ rake gemspec
17
+
18
+ rake install
19
+ rake release
20
+
21
+ Copyright (c) 2011 [Jake Wendt], released under the MIT license
@@ -0,0 +1,11 @@
1
+ class SessionsController < ApplicationController
2
+
3
+ # skip_before_filter :login_required, :only => :destroy
4
+
5
+ def destroy
6
+ reset_session
7
+ flash[:notice] = "Successfully logged out"
8
+ redirect_to root_path
9
+ end
10
+
11
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+
3
+ map.logout 'logout', :controller => 'sessions', :action => 'destroy'
4
+ map.resource :session, :only => [ :destroy ]
5
+
6
+ end
@@ -0,0 +1,14 @@
1
+ script/generate simply_sessions [options]
2
+
3
+
4
+ Description:
5
+ Explain the generator
6
+
7
+ Example:
8
+ ./script/generate simply_sessions
9
+
10
+ This will create:
11
+ a migration file for
12
+
13
+
14
+ blah blah blah
@@ -0,0 +1,72 @@
1
+ #class SimplySessionsGenerator < Rails::Generator::NamedBase
2
+ class SimplySessionsGenerator < Rails::Generator::Base
3
+
4
+ # def initialize(runtime_args, runtime_options = {})
5
+ # puts "In initialize"
6
+ # # # Rails::Generator::NamedBase apparently requires
7
+ # # # at least 1 argumnet. The first will be used
8
+ # # # for things like migration class name
9
+ # # runtime_args.unshift 'CreateTracksTable'
10
+ # super
11
+ # end
12
+
13
+ def manifest
14
+ record do |m|
15
+ m.directory('config/autotest')
16
+ m.file('autotest_simply_sessions.rb', 'config/autotest/simply_sessions.rb')
17
+ m.directory('lib/tasks')
18
+ m.file('simply_sessions.rake', 'lib/tasks/simply_sessions.rake')
19
+
20
+ # m.directory('public/javascripts')
21
+ # Dir["#{File.dirname(__FILE__)}/templates/javascripts/*js"].each{|file|
22
+ # f = file.split('/').slice(-2,2).join('/')
23
+ # m.file(f, "public/javascripts/#{File.basename(file)}")
24
+ # }
25
+ # m.directory('public/stylesheets')
26
+ # Dir["#{File.dirname(__FILE__)}/templates/stylesheets/*css"].each{|file|
27
+ # f = file.split('/').slice(-2,2).join('/')
28
+ # m.file(f, "public/stylesheets/#{File.basename(file)}")
29
+ # }
30
+ # m.directory('test/functional/calnet_authenticated')
31
+ # Dir["#{File.dirname(__FILE__)}/templates/functional/*rb"].each{|file|
32
+ # f = file.split('/').slice(-2,2).join('/')
33
+ # m.file(f, "test/functional/calnet_authenticated/#{File.basename(file)}")
34
+ # }
35
+ # m.directory('test/unit/calnet_authenticated')
36
+ # Dir["#{File.dirname(__FILE__)}/templates/unit/*rb"].each{|file|
37
+ # f = file.split('/').slice(-2,2).join('/')
38
+ # m.file(f, "test/unit/calnet_authenticated/#{File.basename(file)}")
39
+ # }
40
+ end
41
+ end
42
+
43
+ end
44
+ module Rails::Generator::Commands
45
+ class Create
46
+ def migration_template(relative_source,
47
+ relative_destination, template_options = {})
48
+ migration_directory relative_destination
49
+ migration_file_name = template_options[
50
+ :migration_file_name] || file_name
51
+ if migration_exists?(migration_file_name)
52
+ puts "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}: Skipping"
53
+ else
54
+ template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
55
+ end
56
+ end
57
+ end # Create
58
+ class Base
59
+ protected
60
+ # the loop through migrations happens so fast
61
+ # that they all have the same timestamp which
62
+ # won't work when you actually try to migrate.
63
+ # All the timestamps MUST be unique.
64
+ def next_migration_string(padding = 3)
65
+ @s = (!@s.nil?)? @s.to_i + 1 : if ActiveRecord::Base.timestamped_migrations
66
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
67
+ else
68
+ "%.#{padding}d" % next_migration_number
69
+ end
70
+ end
71
+ end # Base
72
+ end
@@ -0,0 +1,5 @@
1
+ # From `script/generate simply_sessions` ...
2
+ unless Gem.source_index.find_name('jakewendt-simply_sessions').empty?
3
+ gem 'jakewendt-simply_sessions'
4
+ require 'simply_sessions/autotest'
5
+ end
@@ -0,0 +1,5 @@
1
+ # From `script/generate simply_sessions` ...
2
+ unless Gem.source_index.find_name('jakewendt-simply_sessions').empty?
3
+ gem 'jakewendt-simply_sessions'
4
+ require 'simply_sessions/test_tasks'
5
+ end
@@ -0,0 +1 @@
1
+ require 'simply_sessions'
@@ -0,0 +1,32 @@
1
+ require 'active_support'
2
+ require 'ruby_extension'
3
+ require 'simply_helpful'
4
+ require 'simply_authorized'
5
+ require 'acts_as_list'
6
+ module SimplySessions
7
+ # predefine namespace
8
+ end
9
+
10
+ # This doesn't seem necessary
11
+ %w{models controllers}.each do |dir|
12
+ path = File.expand_path(File.join(File.dirname(__FILE__), '../app', dir))
13
+ ActiveSupport::Dependencies.autoload_paths << path
14
+ ActiveSupport::Dependencies.autoload_once_paths << path
15
+ end
16
+
17
+ if defined?(Rails) && Rails.env == 'test' && Rails.class_variable_defined?("@@configuration")
18
+ require 'active_support/test_case'
19
+ require 'simply_sessions/test_helper'
20
+ require 'simply_sessions/controller'
21
+ require 'factory_girl'
22
+ # require 'simply_pages/factories'
23
+ # else
24
+ # running a rake task
25
+ end
26
+
27
+ ActionController::Routing::Routes.add_configuration_file(
28
+ File.expand_path(
29
+ File.join(
30
+ File.dirname(__FILE__), '../config/routes.rb')))
31
+
32
+
@@ -0,0 +1,26 @@
1
+ class Autotest::Rails
2
+
3
+ #
4
+ # Need both the mapping and the extra files
5
+ #
6
+ def run_with_simply_sessions
7
+ add_exception %r%config/%
8
+ add_exception %r%versions/%
9
+ add_exception %r%\.git/%
10
+ self.extra_files << File.expand_path(File.join(
11
+ File.dirname(__FILE__),'/../../test/unit/simply_sessions/'))
12
+
13
+ self.extra_files << File.expand_path(File.join(
14
+ File.dirname(__FILE__),'/../../test/functional/simply_sessions/'))
15
+
16
+ add_mapping(
17
+ %r{^#{File.expand_path(File.join(File.dirname(__FILE__),'/../../test/'))}/(unit|functional)/simply_sessions/.*_test\.rb$}
18
+ ) do |filename, _|
19
+ filename
20
+ end
21
+ run_without_simply_sessions
22
+ end
23
+ alias_method_chain :run, :simply_sessions
24
+
25
+
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'action_controller'
2
+
3
+ module SimplySessions::Controller
4
+
5
+ def self.included(base)
6
+ base.helper_method :current_user, :logged_in?
7
+ base.extend(ClassMethods)
8
+ base.send(:include, InstanceMethods)
9
+ base.class_eval do
10
+ class << self
11
+ alias_method_chain :inherited, :simply_sessions_before_filter
12
+ end
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ private
19
+
20
+ def inherited_with_simply_sessions_before_filter(base)
21
+ identifier = 'inherited_simply_sessions_login_required'
22
+ unless filter_chain.select(&:before?).map(&:identifier
23
+ ).include?(identifier)
24
+ before_filter :login_required,
25
+ :identifier => identifier
26
+ end
27
+ inherited_without_simply_sessions_before_filter(base)
28
+ end
29
+
30
+ end
31
+
32
+ module InstanceMethods
33
+
34
+ def logged_in?
35
+ !current_user.nil?
36
+ end
37
+
38
+ def current_user_required
39
+ unless logged_in?
40
+ access_denied("goodbye","/some_fake_login_path")
41
+ end
42
+ end
43
+ alias_method :login_required, :current_user_required
44
+
45
+ def current_user
46
+ @current_user ||= if( session && session[:uid] )
47
+ # if the user model hasn't been loaded yet
48
+ # this will return nil and fail.
49
+ User.find_by_uid(session[:uid])
50
+ else
51
+ nil
52
+ end
53
+ end
54
+
55
+ end # InstanceMethods
56
+
57
+ end # SimplySessions::Controller
58
+ ActionController::Base.send(:include,SimplySessions::Controller)
@@ -0,0 +1,42 @@
1
+ module SimplySessions::TestHelper
2
+
3
+
4
+ def login_as( user=nil )
5
+ uid = ( user.is_a?(User) ) ? user.uid : user
6
+ if !uid.blank?
7
+ @request.session[:uid] = uid
8
+ User.find_or_create_by_uid(uid)
9
+ end
10
+ end
11
+ alias :login :login_as
12
+ alias :log_in :login_as
13
+
14
+ def assert_redirected_to_login
15
+ assert_response :redirect
16
+ # puts @response.inspect
17
+ assert_match "login", @response.redirected_to
18
+ # assert_match "https://auth-test.berkeley.edu/cas/login",
19
+ # @response.redirected_to
20
+ end
21
+
22
+ def assert_redirected_to_logout
23
+ assert_response :redirect
24
+ assert_redirected_to root_path
25
+ assert_not_nil flash[:notice]
26
+ # assert_match "logout", @response.redirected_to
27
+ # assert_match "https://auth-test.berkeley.edu/cas/logout",
28
+ # @response.redirected_to
29
+ end
30
+
31
+ def assert_logged_in
32
+ assert_not_nil session[:uid]
33
+ end
34
+
35
+ def assert_not_logged_in
36
+ assert_nil session[:uid]
37
+ end
38
+
39
+ end
40
+ require 'active_support'
41
+ require 'active_support/test_case'
42
+ ActiveSupport::TestCase.send(:include,SimplySessions::TestHelper)
@@ -0,0 +1,48 @@
1
+ module SimplySessions;end
2
+ namespace :test do
3
+ namespace :units do
4
+ Rake::TestTask.new(:simply_sessions => "db:test:prepare") do |t|
5
+ t.pattern = File.expand_path(File.join(
6
+ File.dirname(__FILE__),'/../../test/unit/simply_sessions/*_test.rb'))
7
+ t.libs << "test"
8
+ t.verbose = true
9
+ end
10
+ end
11
+ namespace :functionals do
12
+ Rake::TestTask.new(:simply_sessions => "db:test:prepare") do |t|
13
+ t.pattern = File.expand_path(File.join(
14
+ File.dirname(__FILE__),'/../../test/functional/simply_sessions/*_test.rb'))
15
+ t.libs << "test"
16
+ t.verbose = true
17
+ end
18
+ end
19
+ end
20
+ Rake::Task['test:functionals'].prerequisites.unshift(
21
+ "test:functionals:simply_sessions" )
22
+ Rake::Task['test:units'].prerequisites.unshift(
23
+ "test:units:simply_sessions" )
24
+
25
+ # I thought of possibly just including this file
26
+ # but that would make __FILE__ different.
27
+ # Hmmm
28
+
29
+ #
30
+ # used in simply_helpful's rake test:coverage to run gem's
31
+ # tests in the context of the application
32
+ #
33
+ @gem_test_dirs ||= []
34
+ #@gem_test_dirs << File.expand_path(File.join(File.dirname(__FILE__),
35
+ # '/../../test/unit/simply_sessions/'))
36
+ #@gem_test_dirs << File.expand_path(File.join(File.dirname(__FILE__),
37
+ # '/../../test/functional/simply_sessions/'))
38
+
39
+ #
40
+ # More flexible. Find all test files, pick out their dir, uniq 'em and add.
41
+ #
42
+ Dir.glob( File.expand_path(File.join(File.dirname(__FILE__),
43
+ '/../../test/*/simply_sessions/*_test.rb'))
44
+ ).collect{|f|
45
+ File.dirname(f)
46
+ }.uniq.each{ |dir|
47
+ @gem_test_dirs << dir
48
+ }
@@ -0,0 +1,2 @@
1
+ # From `script/generate simply_authorized` ...
2
+ require 'simply_authorized/test_tasks'
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'jakewendt-simply_sessions'
@@ -0,0 +1,39 @@
1
+ class ApplicationController < ActionController::Base
2
+
3
+ before_filter :login_required
4
+
5
+ helper :all # include all helpers, all the time
6
+
7
+ # See ActionController::RequestForgeryProtection for details
8
+ protect_from_forgery
9
+
10
+ # def logged_in?
11
+ # !current_user.nil?
12
+ # end
13
+ #
14
+ # def current_user_required
15
+ # unless logged_in?
16
+ # access_denied("goodbye","/some_fake_login_path")
17
+ # end
18
+ # end
19
+ # alias_method :login_required, :current_user_required
20
+ #
21
+ # def current_user
22
+ # @current_user ||= if( session && session[:uid] )
23
+ # # if the user model hasn't been loaded yet
24
+ # # this will return nil and fail.
25
+ # User.find_by_uid(session[:uid])
26
+ # else
27
+ # nil
28
+ # end
29
+ # end
30
+
31
+ def redirections
32
+ @redirections ||= HashWithIndifferentAccess.new({
33
+ :not_be_user => {
34
+ :redirect_to => user_path(current_user)
35
+ }
36
+ })
37
+ end
38
+
39
+ end
@@ -0,0 +1,10 @@
1
+ class HomeController < ApplicationController
2
+
3
+ skip_before_filter :login_required
4
+
5
+ def show
6
+ render :text => "You are home.",
7
+ :layout => true
8
+ end
9
+
10
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ authorized
3
+ end
@@ -0,0 +1,5 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+
3
+ map.root :controller => :home, :action => :show
4
+
5
+ end
data/test/factories.rb ADDED
@@ -0,0 +1,3 @@
1
+ Factory.define :user do |f|
2
+ f.sequence(:uid) { |n| "UID#{n}" }
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class SimplySessions::SessionsControllerTest < ActionController::TestCase
4
+ tests SessionsController
5
+
6
+ test "should logout if authenticated" do
7
+ login_as Factory(:user)
8
+ assert_logged_in
9
+ delete :destroy
10
+ assert_not_logged_in
11
+ assert_redirected_to_logout
12
+ end
13
+
14
+ test "should NOT logout if NOT authenticated" do
15
+ assert_not_logged_in
16
+ delete :destroy
17
+ assert_redirected_to_login
18
+ end
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jakewendt-simply_sessions
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 5
10
+ version: 0.0.5
11
+ platform: ruby
12
+ authors:
13
+ - George 'Jake' Wendt
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-10 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 2
32
+ version: "2"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: A really simple session manager
36
+ email: github@jakewendt.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - app/controllers/sessions_controller.rb
45
+ - config/routes.rb
46
+ - generators/simply_sessions/USAGE
47
+ - generators/simply_sessions/simply_sessions_generator.rb
48
+ - generators/simply_sessions/templates/autotest_simply_sessions.rb
49
+ - generators/simply_sessions/templates/simply_sessions.rake
50
+ - lib/jakewendt-simply_sessions.rb
51
+ - lib/simply_sessions.rb
52
+ - lib/simply_sessions/autotest.rb
53
+ - lib/simply_sessions/controller.rb
54
+ - lib/simply_sessions/test_helper.rb
55
+ - lib/simply_sessions/test_tasks.rb
56
+ - lib/tasks/simply_authorized.rake
57
+ - rails/init.rb
58
+ - README.rdoc
59
+ - test/app/controllers/application_controller.rb
60
+ - test/app/controllers/home_controller.rb
61
+ - test/app/models/user.rb
62
+ - test/config/routes.rb
63
+ - test/factories.rb
64
+ - test/functional/simply_sessions/sessions_controller_test.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/jakewendt/simply_sessions
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.6.2
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: A simple session manager
99
+ test_files:
100
+ - test/app/controllers/application_controller.rb
101
+ - test/app/controllers/home_controller.rb
102
+ - test/app/models/user.rb
103
+ - test/config/routes.rb
104
+ - test/factories.rb
105
+ - test/functional/simply_sessions/sessions_controller_test.rb