alanj-shoulda_extras 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +6 -0
  2. data/init.rb +4 -0
  3. data/lib/should_be_protected.rb +48 -0
  4. metadata +73 -0
data/README ADDED
@@ -0,0 +1,6 @@
1
+ This is a quick set of extras for shoulda that I needed for my projects.
2
+
3
+ should_be_protected
4
+ ====================
5
+ Takes a whitelist approach for should_protect_attributes. Makes sure that every attribute is secure except for the ones listed in the :except option.
6
+
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ if RAILS_ENV == 'test'
2
+ require File.expand_path(File.dirname(__FILE__)) +
3
+ "/lib/should_be_protected.rb"
4
+ end
@@ -0,0 +1,48 @@
1
+ class Test::Unit::TestCase
2
+
3
+ # defines a new test method on the test class for checking models with
4
+ # less code
5
+ def self.should_be_protected(options)
6
+ name = self.name
7
+
8
+ define_method('test: Should be secure') do
9
+
10
+ # construct a new instance of the model
11
+ if name.ends_with?("Test")
12
+ model_name = name[0, name.length - 4]
13
+ obj = Object.const_get(model_name).new
14
+ end
15
+
16
+ if obj
17
+ # loop through and set a value for each item
18
+ test_hash = {}
19
+ attributes = obj.attributes
20
+ attributes[:id] = obj.id
21
+ attributes.each do |attribute, value|
22
+ test_hash[attribute] = rand(10000000000).to_s + Time.now.to_i.to_s
23
+ end
24
+
25
+ # mass assign the test hash
26
+ obj.attributes = test_hash
27
+
28
+ # see what items stuck
29
+ failures = []
30
+ attributes = obj.attributes_before_type_cast
31
+ attributes[:id] = obj.id_before_type_cast
32
+ attributes.each do |attribute, value|
33
+ if value == test_hash[attribute] && !options[:except].include?(attribute) &&
34
+ !options[:except].include?(attribute.to_sym)
35
+ failures << attribute
36
+ end
37
+ end
38
+
39
+ # raise the assertion failure
40
+ if failures.length > 0
41
+ flunk "Attributes [#{failures.join(",")}] are not secure."
42
+ end
43
+ else
44
+ flunk "Could not determine model being tested."
45
+ end
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alanj-shoulda_extras
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Alan Johnson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">"
21
+ - !ruby/object:Gem::Version
22
+ version: "2.0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: rails
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">"
30
+ - !ruby/object:Gem::Version
31
+ version: "2.1"
32
+ version:
33
+ description: A set of extra helpers for shoulda
34
+ email: alan@gnoso.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ files:
42
+ - README
43
+ - init.rb
44
+ - lib/should_be_protected.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/alanj/shoulda_extras
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --main
50
+ - README
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Test Helpers for Shoulda
72
+ test_files: []
73
+