public
Description: Easy and customizable generation of forged data.
Homepage: http://sevenwire.com
Clone URL: git://github.com/sevenwire/forgery.git
Further extending array and better spec coverage.
nate (author)
Sun Jun 01 13:22:27 -0700 2008
commit  a9638a53c91c443fd1b38b903ea7ed89214f93c7
tree    53984c2b857c772c36d4d95ab0c10a29f0c4a9ea
parent  5f0b9e6113ab51082dcb86856d42edf332417970
...
 
 
1
2
3
...
14
15
16
17
 
18
19
20
 
21
22
23
24
25
26
27
28
29
30
31
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
34
35
...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
...
1
2
3
4
5
...
16
17
18
 
19
20
21
 
22
23
24
25
 
 
 
 
26
27
28
29
 
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
...
66
67
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
0
@@ -1,3 +1,5 @@
0
+require 'digest/sha1'
0
+
0
 class BasicForgery < Forgery
0
   dictionaries :colors
0
 
0
@@ -14,22 +16,39 @@ class BasicForgery < Forgery
0
                :allow_upper => true,
0
                :allow_numeric => true,
0
                :allow_special => false}.merge!(options)
0
- self.random_text(options)
0
+ self.text(options)
0
   end
0
 
0
- def self.encrypt(password="password", salt="salt")
0
+ def self.encrypt(password="password", salt=Time.now.to_s)
0
     Digest::SHA1.hexdigest("--#{salt}--#{password}--")
0
   end
0
 
0
- def self.salt
0
- Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{self.password}--")
0
- end
0
-
0
   def self.boolean
0
     [true, false].random
0
   end
0
 
0
- def self.random_text(options={})
0
+ def self.color
0
+ COLORS.random
0
+ end
0
+
0
+ def self.hex_color
0
+ hex_value = ""
0
+ 6.times { hex_value << HEX_DIGITS.random }
0
+ "##{hex_value}"
0
+ end
0
+
0
+ def self.short_hex_color
0
+ hex_color[0,4]
0
+ end
0
+
0
+ def self.number(options={})
0
+ options = {:at_least => 1,
0
+ :at_most => 10}.merge(options)
0
+
0
+ (options[:at_least]..options[:at_most]).random
0
+ end
0
+
0
+ def self.text(options={})
0
     options = {:at_least => 10,
0
                :at_most => 15,
0
                :allow_lower => true,
0
@@ -47,25 +66,4 @@ class BasicForgery < Forgery
0
 
0
     allowed_characters.random_subset(length).join
0
   end
0
-
0
- def self.number(options={})
0
- options = {:at_least => 1,
0
- :at_most => 10}.merge(options)
0
-
0
- (options[:at_least]..options[:at_most]).random
0
- end
0
-
0
- def self.color
0
- COLORS.random
0
- end
0
-
0
- def self.hex_color
0
- hex_value = ""
0
- 6.times { hex_value << HEX_DIGITS.random }
0
- "##{hex_value}"
0
- end
0
-
0
- def self.short_hex_color
0
- hex_color[0,4]
0
- end
0
 end
...
12
13
14
 
 
 
 
 
 
 
 
 
 
15
...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
@@ -12,4 +12,14 @@ describe Array do
0
   it "should return nil if the array is empty" do
0
     [].random.should be_nil
0
   end
0
+
0
+ it "should return a random subset of the array" do
0
+ @array.random_subset(5).each do |i|
0
+ @array.should include(i)
0
+ end
0
+ end
0
+
0
+ it "should return a random subset of the array that is 3 long" do
0
+ @array.random_subset(3).size.should == 3
0
+ end
0
 end
...
14
15
16
17
 
18
19
20
...
14
15
16
 
17
18
19
20
0
@@ -14,7 +14,7 @@ describe AddressForgery do
0
     AddressForgery::STREET_SUFFIXES.should include(AddressForgery.street_suffix)
0
   end
0
 
0
- describe "#street_address" do
0
+ describe ".street_address" do
0
     before do
0
       @split_address = AddressForgery.street_address.split
0
     end
...
1
2
3
4
 
5
6
7
...
56
57
58
59
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
 
4
5
6
7
...
56
57
58
 
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
0
@@ -1,7 +1,7 @@
0
 require File.dirname(__FILE__) + '/../spec_helper'
0
 
0
 describe BasicForgery do
0
- describe "#password" do
0
+ describe ".password" do
0
     it "should only uppercase characters" do
0
       BasicForgery.password(:allow_lower => false,
0
                             :allow_upper => true,
0
@@ -56,4 +56,114 @@ describe BasicForgery do
0
                                                                         BasicForgery::UPPER_ALPHA)
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+
0
+ describe ".encrypt" do
0
+ it "should encrypt to hex digits" do
0
+ BasicForgery.encrypt("something").should only_contain(BasicForgery::HEX_DIGITS)
0
+ end
0
+
0
+ it "should encrypt different words to different output" do
0
+ BasicForgery.encrypt("foo").should_not == BasicForgery.encrypt("bar")
0
+ end
0
+
0
+ it "should allow a salt that changes the output" do
0
+ BasicForgery.encrypt("foo", "baz").should_not == BasicForgery.encrypt("foo", "bar")
0
+ end
0
+
0
+ it "should have the same output when encrypting twice" do
0
+ BasicForgery.encrypt("foo", "bar").should == BasicForgery.encrypt("foo", "bar")
0
+ end
0
+ end
0
+
0
+ describe ".boolean" do
0
+ it "should return true or false" do
0
+ [true, false].should include(BasicForgery.boolean)
0
+ end
0
+ end
0
+
0
+ it "should return a random color" do
0
+ BasicForgery::COLORS.should include(BasicForgery.color)
0
+ end
0
+
0
+ it "should return a 6-character hex color" do
0
+ BasicForgery.hex_color.should match(/#(#{BasicForgery::HEX_DIGITS.join('|')}){6}/)
0
+ end
0
+
0
+ it "should return a 3-character hex color" do
0
+ BasicForgery.short_hex_color.should match(/#(#{BasicForgery::HEX_DIGITS.join('|')}){3}/)
0
+ end
0
+
0
+ describe ".number" do
0
+ it "should return a number >= the at_least option" do
0
+ BasicForgery.number(:at_least => 2).should >= 2
0
+ end
0
+
0
+ it "should return a number <= the at_most option" do
0
+ BasicForgery.number(:at_most => 12).should <= 12
0
+ end
0
+ end
0
+
0
+ describe ".text" do
0
+ it "should return text whose length is >= the at_least option" do
0
+ BasicForgery.text(:at_least => 5).size.should >= 5
0
+ end
0
+
0
+ it "should return text whose length is <= the at_most option" do
0
+ BasicForgery.text(:at_least => 15).size.should <= 15
0
+ end
0
+
0
+ it "should only uppercase characters" do
0
+ BasicForgery.text(:allow_lower => false,
0
+ :allow_upper => true,
0
+ :allow_numeric => false,
0
+ :allow_special => false).should only_contain(BasicForgery::UPPER_ALPHA)
0
+ end
0
+
0
+ it "should only contain lowercase characters" do
0
+ BasicForgery.text(:allow_lower => true,
0
+ :allow_upper => false,
0
+ :allow_numeric => false,
0
+ :allow_special => false).should only_contain(BasicForgery::LOWER_ALPHA)
0
+ end
0
+
0
+ it "should only contain numeric characters" do
0
+ BasicForgery.text(:allow_lower => false,
0
+ :allow_upper => false,
0
+ :allow_numeric => true,
0
+ :allow_special => false).should only_contain(BasicForgery::NUMERIC)
0
+ end
0
+
0
+ it "should only contain special characters" do
0
+ BasicForgery.text(:allow_lower => false,
0
+ :allow_upper => false,
0
+ :allow_numeric => false,
0
+ :allow_special => true).should only_contain(BasicForgery::SPECIAL_CHARACTERS)
0
+ end
0
+
0
+ it "should only contain lower and uppercase characters" do
0
+ BasicForgery.text(:allow_lower => true,
0
+ :allow_upper => true,
0
+ :allow_numeric => false,
0
+ :allow_special => false).should only_contain(BasicForgery::LOWER_ALPHA,
0
+ BasicForgery::UPPER_ALPHA)
0
+ end
0
+
0
+ it "should only contain numeric and special characters" do
0
+ BasicForgery.text(:allow_lower => false,
0
+ :allow_upper => false,
0
+ :allow_numeric => true,
0
+ :allow_special => true).should only_contain(BasicForgery::NUMERIC,
0
+ BasicForgery::SPECIAL_CHARACTERS)
0
+ end
0
+
0
+ it "should contain any of the defined characters" do
0
+ BasicForgery.text(:allow_lower => true,
0
+ :allow_upper => true,
0
+ :allow_numeric => true,
0
+ :allow_special => true).should only_contain(BasicForgery::NUMERIC,
0
+ BasicForgery::SPECIAL_CHARACTERS,
0
+ BasicForgery::LOWER_ALPHA,
0
+ BasicForgery::UPPER_ALPHA)
0
+ end
0
+ end
0
+end

Comments

    No one has commented yet.