sinatra-security 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -0,0 +1,16 @@
1
+ module Sinatra
2
+ module Security
3
+ module LoginField
4
+ def self.attr_name(attr_name = nil)
5
+ @attr_name = attr_name if attr_name
6
+ @attr_name
7
+ end
8
+ attr_name(:email)
9
+
10
+ def self.included(user)
11
+ user.attribute LoginField.attr_name
12
+ user.index LoginField.attr_name
13
+ end
14
+ end
15
+ end
16
+ end
@@ -2,11 +2,12 @@ module Sinatra
2
2
  module Security
3
3
  module User
4
4
  def self.included(user)
5
- user.send :include, Validations
5
+ user.send :include, LoginField
6
6
  user.send :include, Password
7
+ user.send :include, Validations
7
8
 
8
9
  user.extend Identification
9
10
  end
10
11
  end
11
12
  end
12
- end
13
+ end
@@ -2,7 +2,18 @@ module Sinatra
2
2
  module Security
3
3
  module Validations
4
4
  EMAIL_FORMAT = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
5
-
5
+
6
+ def validate
7
+ # TODO : email requirement should only be done if
8
+ # LoginField.attr_name == :email maybe
9
+ # then just let users expilicity declare there own
10
+ # validation rules
11
+ assert_login_using_email :email
12
+ assert_password :password
13
+
14
+ super
15
+ end
16
+
6
17
  protected
7
18
  def assert_login_using_email(att, error = [att, :not_email])
8
19
  if assert_present att
@@ -13,6 +13,7 @@ module Sinatra
13
13
  autoload :Validations, 'sinatra/security/validations'
14
14
  autoload :Password, 'sinatra/security/password'
15
15
  autoload :Identification, 'sinatra/security/identification'
16
+ autoload :LoginField, 'sinatra/security/login_field'
16
17
 
17
18
  def self.registered(app)
18
19
  app.helpers Helpers
@@ -29,4 +30,4 @@ module Sinatra
29
30
  end
30
31
 
31
32
  register Security
32
- end
33
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra-security}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cyril David"]
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/sinatra/security.rb",
30
30
  "lib/sinatra/security/helpers.rb",
31
31
  "lib/sinatra/security/identification.rb",
32
+ "lib/sinatra/security/login_field.rb",
32
33
  "lib/sinatra/security/password.rb",
33
34
  "lib/sinatra/security/user.rb",
34
35
  "lib/sinatra/security/validations.rb",
data/test/helper.rb CHANGED
@@ -23,32 +23,36 @@ class Test::Unit::TestCase
23
23
  end
24
24
 
25
25
  # Test Fixtures appear here
26
- class User
27
- attr :id
28
- attr_accessor :email, :password
26
+ module TestFixtures
27
+ class User
28
+ attr :id
29
+ attr_accessor :email, :password, :password_confirmation
29
30
 
30
- def initialize(id = nil)
31
- @id = id
32
- end
33
-
34
- def errors
35
- @errors ||= []
36
- end
31
+ def initialize(id = nil)
32
+ @id = id
33
+ end
37
34
 
38
- protected
39
- def assert(value, error)
40
- value or errors.push(error) && false
41
- end
35
+ def errors
36
+ @errors ||= []
37
+ end
38
+
39
+ def validate
40
+ # placed here so included validate method has a super to call
41
+ end
42
+ protected
43
+ def assert(value, error)
44
+ value or errors.push(error) && false
45
+ end
42
46
 
43
- def assert_present(att, error = [att, :not_present])
44
- assert(!send(att).to_s.empty?, error)
45
- end
47
+ def assert_present(att, error = [att, :not_present])
48
+ assert(!send(att).to_s.empty?, error)
49
+ end
46
50
 
47
- def assert_format(att, format, error = [att, :format])
48
- if assert_present(att, error)
49
- assert(send(att).to_s.match(format), error)
51
+ def assert_format(att, format, error = [att, :format])
52
+ if assert_present(att, error)
53
+ assert(send(att).to_s.match(format), error)
54
+ end
50
55
  end
51
- end
52
56
 
57
+ end
53
58
  end
54
-
@@ -5,6 +5,8 @@ class ExecutionContext < Struct.new(:session, :request)
5
5
  end
6
6
  end
7
7
 
8
+ include TestFixtures
9
+
8
10
  class TestSinatraSecurityHelpers < Test::Unit::TestCase
9
11
  setup do
10
12
  @context = ExecutionContext.new({})
@@ -1,6 +1,6 @@
1
1
  require "helper"
2
2
 
3
- class UserWithEmailValidation < User
3
+ class UserWithEmailValidation < TestFixtures::User
4
4
  include Sinatra::Security::Validations
5
5
 
6
6
  def initialize
@@ -19,9 +19,8 @@ class UserWithEmailValidation < User
19
19
  end
20
20
  end
21
21
 
22
- class UserWithPasswordValidation < User
22
+ class UserWithPasswordValidation < TestFixtures::User
23
23
  include Sinatra::Security::Validations
24
- attr_accessor :password_confirmation
25
24
 
26
25
  def initialize
27
26
  super
@@ -38,6 +37,14 @@ class UserWithPasswordValidation < User
38
37
  end
39
38
  end
40
39
 
40
+ class UserDefaultValidation < TestFixtures::User
41
+ include Sinatra::Security::Validations
42
+
43
+ def errors
44
+ @errors ||= []
45
+ end
46
+ end
47
+
41
48
  class TestValidations < Test::Unit::TestCase
42
49
  context "without an email" do
43
50
  should "require the email to be present" do
@@ -156,4 +163,51 @@ class TestValidations < Test::Unit::TestCase
156
163
  end
157
164
  end
158
165
  end
166
+
167
+ context "the default validations" do
168
+ setup do
169
+ @user = UserDefaultValidation.new
170
+ @user.stubs(:new?).returns(true)
171
+ end
172
+
173
+ should "assert presence of email" do
174
+ @user.validate
175
+
176
+ assert @user.errors.include?([:email, :not_present])
177
+ end
178
+
179
+ should "assert the email is an email" do
180
+ @user.email = '_foobar_'
181
+ @user.validate
182
+
183
+ assert @user.errors.include?([:email, :not_email])
184
+ end
185
+
186
+ should "assert the email is unique" do
187
+ @user.email = 'foo@bar.com'
188
+ @user.expects(:assert_unique).with(:email).returns(false)
189
+ @user.validate
190
+ end
191
+
192
+ should "assert the password is there" do
193
+ @user.password = ''
194
+ @user.validate
195
+ assert @user.errors.include?([:password, :not_present])
196
+ end
197
+
198
+ should "assert the password is confirmed" do
199
+ @user.password = 'aoeu'
200
+ @user.validate
201
+ assert @user.errors.include?([:password, :not_confirmed])
202
+ end
203
+
204
+ context "when the user is existing" do
205
+ should "not require a password" do
206
+ @user.stubs(:new?).returns(false)
207
+ @user.validate
208
+
209
+ assert ! @user.errors.include?([:password, :not_presen])
210
+ end
211
+ end
212
+ end
159
213
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril David
@@ -40,6 +40,7 @@ files:
40
40
  - lib/sinatra/security.rb
41
41
  - lib/sinatra/security/helpers.rb
42
42
  - lib/sinatra/security/identification.rb
43
+ - lib/sinatra/security/login_field.rb
43
44
  - lib/sinatra/security/password.rb
44
45
  - lib/sinatra/security/user.rb
45
46
  - lib/sinatra/security/validations.rb