Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 165 lines (142 sloc) 4.614 kB
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
1 require "test/unit"
2 require "rubygems"
3 require "ruby-debug"
4 require "active_record"
5 require 'active_record/fixtures'
f606857 @binarylogic Reorganized tests
binarylogic authored
6 require File.dirname(__FILE__) + '/../lib/authlogic' unless defined?(Authlogic)
1d38644 @binarylogic Added last_request_at_threshold cconfig option
binarylogic authored
7 require File.dirname(__FILE__) + '/libs/mock_request'
8 require File.dirname(__FILE__) + '/libs/mock_cookie_jar'
9 require File.dirname(__FILE__) + '/libs/mock_controller'
783e3f2 @binarylogic Allow options to change if password is optional or not
binarylogic authored
10 require File.dirname(__FILE__) + '/libs/user'
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
11
12 ActiveRecord::Schema.verbose = false
13 ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
14 ActiveRecord::Base.configurations = true
15 ActiveRecord::Schema.define(:version => 1) do
16 create_table :companies do |t|
17 t.datetime :created_at
18 t.datetime :updated_at
19 t.string :name
20 t.boolean :active
21 end
22
23 create_table :projects do |t|
24 t.datetime :created_at
25 t.datetime :updated_at
26 t.string :name
27 end
28
29 create_table :projects_users, :id => false do |t|
30 t.integer :project_id
31 t.integer :user_id
32 end
33
34 create_table :users do |t|
35 t.datetime :created_at
36 t.datetime :updated_at
37 t.integer :company_id
38 t.string :login
39 t.string :crypted_password
40 t.string :password_salt
4caccd0 @binarylogic Released 1.2.1
binarylogic authored
41 t.string :persistence_token
ebdebfa @binarylogic Released v1.1.1
binarylogic authored
42 t.string :single_access_token
4caccd0 @binarylogic Released 1.2.1
binarylogic authored
43 t.string :perishable_token
dbd8b8f @binarylogic Release v1.2.0
binarylogic authored
44 t.string :email
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
45 t.string :first_name
46 t.string :last_name
47 t.integer :login_count
48 t.datetime :last_request_at
49 t.datetime :current_login_at
50 t.datetime :last_login_at
51 t.string :current_login_ip
52 t.string :last_login_ip
1f74ab9 @binarylogic Added disable_magic_states option for sessions
binarylogic authored
53 t.boolean :active, :default => true
54 t.boolean :approved, :default => true
55 t.boolean :confirmed, :default => true
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
56 end
57
58 create_table :employees do |t|
59 t.datetime :created_at
60 t.datetime :updated_at
61 t.integer :company_id
62 t.string :email
63 t.string :crypted_password
64 t.string :password_salt
4caccd0 @binarylogic Released 1.2.1
binarylogic authored
65 t.string :persistence_token
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
66 t.string :first_name
67 t.string :last_name
68 t.integer :login_count
69 t.datetime :last_request_at
70 t.datetime :current_login_at
71 t.datetime :last_login_at
72 t.string :current_login_ip
73 t.string :last_login_ip
74 end
75 end
76
77 class Project < ActiveRecord::Base
78 has_and_belongs_to_many :users
79 end
80
81 class UserSession < Authlogic::Session::Base
82 end
83
84 class EmployeeSession < Authlogic::Session::Base
85 end
86
87 class Company < ActiveRecord::Base
88 authenticates_many :employee_sessions
89 authenticates_many :user_sessions
90 has_many :employees, :dependent => :destroy
91 has_many :users, :dependent => :destroy
92 end
93
72f3a21 @binarylogic Added AES256 as an option for crypto_providers
binarylogic authored
94 Authlogic::CryptoProviders::AES256.key = "myafdsfddddddddddddddddddddddddddddddddddddddddddddddd"
95
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
96 class Employee < ActiveRecord::Base
72f3a21 @binarylogic Added AES256 as an option for crypto_providers
binarylogic authored
97 acts_as_authentic :crypto_provider => Authlogic::CryptoProviders::AES256
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
98 belongs_to :company
99 end
100
101 class Test::Unit::TestCase
102 self.fixture_path = File.dirname(__FILE__) + "/fixtures"
103 self.use_transactional_fixtures = true
104 self.use_instantiated_fixtures = false
105 self.pre_loaded_fixtures = true
106 fixtures :all
107 setup :activate_authlogic
108
109 private
110 def activate_authlogic
111 @controller = MockController.new
112 Authlogic::Session::Base.controller = @controller
113 end
114
a623f67 @binarylogic Release v1.3.3
binarylogic authored
115 def password_for(user)
116 case user
117 when users(:ben)
118 "benrocks"
119 when users(:zack)
120 "zackrocks"
121 end
122 end
123
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
124 def http_basic_auth_for(user = nil, &block)
125 unless user.blank?
126 @controller.http_user = user.login
a623f67 @binarylogic Release v1.3.3
binarylogic authored
127 @controller.http_password = password_for(user)
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
128 end
129 yield
130 @controller.http_user = @controller.http_password = nil
131 end
132
133 def set_cookie_for(user, id = nil)
4caccd0 @binarylogic Released 1.2.1
binarylogic authored
134 @controller.cookies["user_credentials"] = {:value => user.persistence_token, :expires => nil}
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
135 end
136
137 def unset_cookie
138 @controller.cookies["user_credentials"] = nil
139 end
140
b83abca @binarylogic Added in authentication by params / token
binarylogic authored
141 def set_params_for(user, id = nil)
ebdebfa @binarylogic Released v1.1.1
binarylogic authored
142 @controller.params["user_credentials"] = user.single_access_token
b83abca @binarylogic Added in authentication by params / token
binarylogic authored
143 end
144
145 def unset_params
146 @controller.params["user_credentials"] = nil
147 end
148
ebdebfa @binarylogic Released v1.1.1
binarylogic authored
149 def set_request_content_type(type)
150 @controller.request_content_type = type
151 end
152
153 def unset_request_content_type
154 @controller.request_content_type = nil
155 end
156
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
157 def set_session_for(user, id = nil)
4caccd0 @binarylogic Released 1.2.1
binarylogic authored
158 @controller.session["user_credentials"] = user.persistence_token
9544076 @binarylogic Store record id with session to allow for quick DB lookups
binarylogic authored
159 @controller.session["user_credentials_id"] = user.id
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
160 end
161
162 def unset_session
9544076 @binarylogic Store record id with session to allow for quick DB lookups
binarylogic authored
163 @controller.session["user_credentials"] = @controller.session["user_credentials_id"] = nil
6862f60 @binarylogic Released v1.0.0 (see changelog)
binarylogic authored
164 end
165 end
Something went wrong with that request. Please try again.