codefumes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,161 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "Payload" do
4
+ after(:all) do
5
+ FakeWeb.allow_net_connect = false
6
+ FakeWeb.clean_registry
7
+ end
8
+
9
+ describe "save" do
10
+ before(:each) do
11
+ @project = Project.new(:public_key => "apk", :private_key => 'private_key_value')
12
+ end
13
+
14
+ context "with valid parameters" do
15
+ before(:each) do
16
+ FakeWeb.register_uri( :post, "http://www.codefumes.com:80/api/v1/xml/projects/apk/payloads?payload[commits]=data_to_send_up",
17
+ :status => ["201", "Created"],
18
+ :string => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<payload>\n <created_at>Creation Date</created_at>\n </payload>\n")
19
+ end
20
+ it "sets basic auth with the public and private key" do
21
+ payload = Payload.new(:public_key => @project.public_key, :private_key => @project.private_key, :content => {:commits => "data_to_send_up"})
22
+ Payload.should_receive(:post).with("/projects/#{@project.public_key}/payloads", :query => {:payload => {:commits => "data_to_send_up"}}, :basic_auth => {:username => @project.public_key, :password => @project.private_key}).and_return(mock("response", :code => 401))
23
+ payload.save
24
+ end
25
+ context "with Created response" do
26
+ [:created_at].each do |method_name|
27
+ it "sets the '#{method_name.to_s}'" do
28
+ payload = Payload.new(:public_key => @project.public_key, :content => {:commits => "data_to_send_up"})
29
+ payload.send(method_name).should == nil
30
+ payload.save.should == true
31
+ payload.send(method_name).should_not == nil
32
+ end
33
+ end
34
+ end
35
+ context "with Unauthorized response" do
36
+ before(:each) do
37
+ @project = Project.new(:public_key => "apk")
38
+ FakeWeb.register_uri( :post, "http://www.codefumes.com:80/api/v1/xml/projects/apk/payloads?payload[commits]=data_to_send_up",
39
+ :status => ["401", "Unauthorized"])
40
+ end
41
+ [:created_at].each do |method_name|
42
+ it "sets the '#{method_name.to_s}'" do
43
+ payload = Payload.new(:public_key => @project.public_key, :content => {:commits => "data_to_send_up"})
44
+ payload.send(method_name).should == nil
45
+ payload.save.should == false
46
+ payload.send(method_name).should == nil
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ context "when the payload does not have any content" do
54
+ before(:each) do
55
+ @payload = Payload.new(:public_key => @project.public_key, :content => {:commits => ""})
56
+ end
57
+
58
+ it "returns true without attempting to save to the site" do
59
+ @payload.save.should == true
60
+ end
61
+
62
+ it "does not set the value of created_at" do
63
+ @payload.save
64
+ @payload.created_at.should == nil
65
+ end
66
+ end
67
+
68
+ context "with invalid parameters" do
69
+ before(:each) do
70
+ FakeWeb.register_uri( :post, "http://www.codefumes.com:80/api/v1/xml/projects/apk/payloads?payload[commits]=invalid_data",
71
+ :status => ["422", "Unprocessable Entity"])
72
+ end
73
+
74
+ [:created_at].each do |method_name|
75
+ it "does not set a value for '#{method_name.to_s}'" do
76
+ payload = Payload.new(:public_key => @project.public_key, :content => {:commits => "invalid_data"})
77
+ payload.save.should == false
78
+ payload.send(method_name).should == nil
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "calling the class method" do
85
+ describe "'prepare'" do
86
+ context "when supplying nil content" do
87
+ it "returns an empty array" do
88
+ Payload.prepare(nil).should == []
89
+ end
90
+ end
91
+
92
+ context "when supplying an empty Hash" do
93
+ it "returns an empty array" do
94
+ Payload.prepare({}).should == []
95
+ end
96
+ end
97
+
98
+ context "when supplying hash does not contain a :public_key key" do
99
+ it "raises an ArgumentError exception" do
100
+ lambda {Payload.prepare({:commits => []})}.should raise_error(ArgumentError)
101
+ end
102
+ end
103
+
104
+ context "when supplying hash does not contain a :commits key" do
105
+ it "raises an ArgumentError exception" do
106
+ lambda {Payload.prepare({:public_key => "pub_key1"})}.should raise_error(ArgumentError)
107
+ end
108
+ end
109
+
110
+ context "when supplying a hash with less than 4,000 characters" do
111
+ before(:each) do
112
+ single_commit_ex = {:identifier => "92dd08477f0ca144ee0f12ba083760dd810760a2_000"}
113
+ commit_count = 4000 / single_commit_ex.to_s.length
114
+ commits = commit_count.times.map do |index|
115
+ {:identifier => "92dd08477f0ca144ee0f12ba083760dd810760a2_#{index}"}
116
+ end
117
+ @prepared = Payload.prepare({:public_key => 'fjsk', :private_key => 'something_super_secret', :content => {:commits => commits}})
118
+ end
119
+
120
+ it "returns an Array with a single payload element" do
121
+ @prepared.should be_instance_of(Array)
122
+ @prepared.size.should == 1
123
+ @prepared.first.should be_instance_of(Payload)
124
+ end
125
+ it "sets the private_key on all payloads" do
126
+ @prepared.each do |payload|
127
+ payload.project_private_key.should == 'something_super_secret'
128
+ end
129
+ end
130
+ end
131
+
132
+ context "when supplying a hash with approximately 15,000 characters" do
133
+ before(:each) do
134
+ single_commit_ex = {:identifier => "92dd08477f0ca144ee0f12ba083760dd810760a2_000"}
135
+ commit_count = 15000 / single_commit_ex.to_s.length + 1
136
+ commits = commit_count.times.map do |index|
137
+ {:identifier => "92dd08477f0ca144ee0f12ba083760dd810760a2_#{index}"}
138
+ end
139
+ raw_payload = {:public_key => 'fjsk', :private_key => 'something_super_secret', :content => {:commits => commits}}
140
+ @prepared = Payload.prepare(raw_payload)
141
+ end
142
+
143
+ it "returns an Array with a four payload elements" do
144
+ @prepared.should be_instance_of(Array)
145
+ @prepared.size.should == 4
146
+ all_are_payloads = @prepared.all? {|chunk| chunk.instance_of?(Payload)}
147
+ all_are_payloads.should == true
148
+ end
149
+
150
+ it "sets the private_key on all payloads" do
151
+ @prepared.each do |payload|
152
+ payload.project_private_key.should == 'something_super_secret'
153
+ end
154
+ end
155
+
156
+ it "the first payload contains approximately 10,000 characters"
157
+ it "the second payload contains approximately 5,000 characters"
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,274 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ def register_create_uri
4
+ FakeWeb.register_uri( :post, "http://www.codefumes.com:80/api/v1/xml/projects?project[name]=&project[public_key]=",
5
+ :status => ["201", "Created"],
6
+ :string => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n <access-secret nil=\"true\"></access-secret>\n <created-at type=\"datetime\">2009-04-29T23:18:03Z</created-at>\n <public-key>foofoolerue</public-key>\n <private-key>foobarbaz</private-key>\n <updated-at type=\"datetime\">2009-04-29T23:18:03Z</updated-at>\n <short_uri>http://www.codefumes.com/p/foofoolerue</short_uri>\n <community_uri>http://www.codefumes.com/community/projects/1</community_uri>\n <api-uri>http://www.codefumes.com/api/v1/xml/projects/1.xml</api-uri>\n</project>")
7
+ end
8
+
9
+ def register_update_uri(public_key = "public_key_value", project_name = "The Project Name(tm)")
10
+ response = {:status => ["200", "Ok"], :string => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n <access-secret nil=\"true\"></access-secret>\n <created-at type=\"datetime\">2009-04-29T23:18:03Z</created-at>\n <public-key>existing_public_key</public-key>\n <private-key>private_key_value</private-key>\n <updated-at type=\"datetime\">2009-04-29T23:18:03Z</updated-at>\n <short_uri>http://www.codefumes.com/p/#{public_key}</short_uri>\n <community_uri>http://www.codefumes.com/community/projects/#{public_key}</community_uri>\n <api-uri>http://www.codefumes.com/api/v1/xml/projects/#{public_key}.xml</api-uri>\n <name>#{project_name}</name>\n</project>\n"}
11
+ FakeWeb.register_uri(:put, "http://www.codefumes.com/api/v1/xml/projects/existing_public_key?project[name]=#{project_name}",
12
+ response)
13
+ end
14
+
15
+ def register_show_uri(public_key = "public_key_value", project_name = "The Project Name(tm)", status_code = ["200", "Ok"])
16
+ FakeWeb.register_uri(:get, "http://www.codefumes.com/api/v1/xml/projects/#{public_key}",
17
+ :status => status_code,
18
+ :string => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n <access-secret nil=\"true\"></access-secret>\n <created-at type=\"datetime\">2009-04-29T23:18:03Z</created-at>\n <public-key>#{public_key}</public-key>\n <private-key>private_key_value</private-key>\n <updated-at type=\"datetime\">2009-04-29T23:18:03Z</updated-at>\n <short_uri>http://www.codefumes.com/p/#{public_key}</short_uri>\n <community_uri>http://www.codefumes.com/community/projects/#{public_key}</community_uri>\n <api-uri>http://www.codefumes.com/api/v1/xml/projects/#{public_key}.xml</api-uri>\n <name>original_name</name>\n</project>\n")
19
+
20
+ end
21
+
22
+ describe "Project" do
23
+ before(:each) do
24
+ end
25
+
26
+ after(:all) do
27
+ FakeWeb.allow_net_connect = false
28
+ FakeWeb.clean_registry
29
+ end
30
+
31
+ describe "save" do
32
+ context "with valid parameters" do
33
+ context "when the public key has not been taken yet (or no key provided)" do
34
+ before(:each) do
35
+ register_create_uri
36
+ end
37
+
38
+ [ :public_key,
39
+ :private_key,
40
+ :short_uri,
41
+ :community_uri,
42
+ :api_uri].each do |method_name|
43
+ it "sets the '#{method_name.to_s}'" do
44
+ project = CodeFumes::Project.new
45
+ project.send(method_name).should be_nil
46
+ #project.save
47
+ #project.send(method_name).should_not be_nil
48
+ end
49
+ end
50
+ end
51
+
52
+ context "when the public key has already been taken" do
53
+
54
+ context "when response is success" do
55
+ before(:each) do
56
+ @updated_name = "different_name"
57
+ @project = CodeFumes::Project.new(:public_key => "existing_public_key", :private_key => 'private_key_value', :name => @updated_name)
58
+ register_show_uri(@project.public_key, @updated_name)
59
+ register_update_uri(@project.public_key, @updated_name)
60
+ @project.stub!(:exists?).and_return(true)
61
+ @project.save.should be_true
62
+ end
63
+
64
+ it "sets basic auth with the public and private key" do
65
+ Project.should_receive(:put).with("/projects/#{@project.public_key}", :query => {:project => {:name => @updated_name}}, :basic_auth => {:username => @project.public_key, :password => @project.private_key}).and_return(mock("response", :code => 401))
66
+ @project.save
67
+ end
68
+
69
+ it "updates the value of 'name' for the project associated with the supplied public key" do
70
+ @project.name.should == @updated_name
71
+ end
72
+
73
+ [ :public_key,
74
+ :private_key,
75
+ :short_uri,
76
+ :community_uri,
77
+ :name,
78
+ :api_uri].each do |method_name|
79
+ it "sets the '#{method_name.to_s}'" do
80
+ @project.send(method_name).should_not be_nil
81
+ end
82
+ end
83
+ end
84
+ context "respons is Unauthorized" do
85
+ before(:each) do
86
+ @updated_name = "different_name"
87
+ @project = CodeFumes::Project.new(:public_key => "existing_public_key", :private_key => 'bad_key', :name => @updated_name)
88
+ FakeWeb.register_uri(:put, "http://www.codefumes.com/api/v1/xml/projects/existing_public_key?project[name]=#{@project.name}",
89
+ :status => ["401", "Unauthorized"])
90
+ @project.stub!(:exists?).and_return(true)
91
+ end
92
+ it "returns false" do
93
+ @project.save.should be_false
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ context "with invalid parameters" do
100
+ before(:each) do
101
+ FakeWeb.register_uri( :post, "http://www.codefumes.com/api/v1/xml/projects?project[public_key]=&project[name]=",
102
+ :status => ["422", "Unprocessable Entity"])
103
+ end
104
+
105
+ [:private_key].each do |method_name|
106
+ it "does not set the '#{method_name.to_s}'" do
107
+ project = CodeFumes::Project.new
108
+ project.save.should be_false
109
+ project.send(method_name).should be_nil
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ context "delete" do
116
+ before(:each) do
117
+ @project = Project.new(:public_key => 'public_key_value', :private_key => 'private_key_value')
118
+ FakeWeb.register_uri( :delete, "http://www.codefumes.com/api/v1/xml/projects/#{@project.public_key}",
119
+ :status => ["200", "Successful"],
120
+ :string => "")
121
+ end
122
+ it "sets basic auth with the public and private key" do
123
+ Project.should_receive(:delete).with("/projects/#{@project.public_key}", :basic_auth => {:username => @project.public_key, :password => @project.private_key}).and_return(mock("response", :code => 401))
124
+ @project.delete
125
+ end
126
+ context "with Sucessful response" do
127
+ it "returns true" do
128
+ @project.delete.should be_true
129
+ end
130
+ end
131
+ context "with Unauthorized response" do
132
+ before(:each) do
133
+ @project = Project.new(:public_key => 'public_key_value')
134
+ FakeWeb.register_uri( :delete, "http://www.codefumes.com/api/v1/xml/projects/#{@project.public_key}",
135
+ :status => ["401", "Unauthorized"],
136
+ :string => "")
137
+ end
138
+ it "returns false when invalid Unauthorized response is received" do
139
+ @project.delete.should be_false
140
+ end
141
+ end
142
+ end
143
+
144
+ describe "exists?" do
145
+ before(:each) do
146
+ @public_key = "some_public_key"
147
+ end
148
+
149
+ context "when the specified public_key has been reserved already" do
150
+ before(:each) do
151
+ FakeWeb.register_uri(:get, "http://www.codefumes.com/api/v1/xml/projects/#{@public_key}",
152
+ :status => ["200", "Ok"],
153
+ :string => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n <access-secret nil=\"true\"></access-secret>\n <created-at type=\"datetime\">2009-04-29T23:18:03Z</created-at>\n <public-key>foofoolerue</public-key>\n <private-key>foobarbaz</private-key>\n <updated-at type=\"datetime\">2009-04-29T23:18:03Z</updated-at>\n <short_uri>http://www.codefumes.com/p/foofoolerue</short_uri>\n <community_uri>http://www.codefumes.com/community/projects/1</community_uri>\n <api-uri>http://www.codefumes.com/api/v1/xml/projects/1.xml</api-uri>\n <name>original_name</name>\n")
154
+ end
155
+
156
+ it "returns true" do
157
+ Project.new(:public_key => @public_key).exists?.should be_true
158
+ end
159
+ end
160
+
161
+ context "when the public_key is not set" do
162
+ it "returns false when the public key is nil" do
163
+ Project.new.exists?.should be_false
164
+ end
165
+
166
+ it "returns false when the public key is an empty string" do
167
+ Project.new(:public_key => "").exists?.should be_false
168
+ end
169
+ end
170
+
171
+ context "when the specified public_key is available" do
172
+ before(:each) do
173
+ FakeWeb.register_uri(:get, "http://www.codefumes.com/api/v1/xml/projects/#{@public_key}",
174
+ :status => ["404", "Not Found"],
175
+ :string => "")
176
+
177
+ end
178
+
179
+ it "returns false" do
180
+ Project.new(:public_key => @public_key).exists?.should be_false
181
+ end
182
+ end
183
+ end
184
+
185
+ describe "to_config" do
186
+ before(:each) do
187
+ register_create_uri
188
+ FakeWeb.register_uri( :post, "http://www.codefumes.com:80/api/v1/xml/projects?project[name]=&project[public_key]=",
189
+ :status => ["201", "Created"],
190
+ :string => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n <access-secret nil=\"true\"></access-secret>\n <created-at type=\"datetime\">2009-04-29T23:18:03Z</created-at>\n <public-key>foofoolerue</public-key>\n <private-key>foobarbaz</private-key>\n <updated-at type=\"datetime\">2009-04-29T23:18:03Z</updated-at>\n <short_uri>http://www.codefumes.com/p/foofoolerue</short_uri>\n <community_uri>http://www.codefumes.com/community/projects/1</community_uri>\n <api-uri>http://www.codefumes.com/api/v1/xml/projects/1.xml</api-uri>\n</project>")
191
+ @project = Project.new
192
+ @project.save
193
+ end
194
+
195
+ it "returns an object keyed by the project's public_key as a symbol" do
196
+ @project.to_config.should include(@project.public_key.to_sym)
197
+ end
198
+
199
+ context "the content under the project's public_key element" do
200
+ [:private_key, :api_uri, :short_uri].each do |project_attribute|
201
+ it "includes a key-value pair of ':#{project_attribute.to_s} => [project's #{project_attribute.to_s}]'" do
202
+ value = @project.send(project_attribute)
203
+ @project.to_config[@project.public_key.to_sym].should include(project_attribute => value)
204
+ end
205
+ end
206
+ end
207
+
208
+ it "doesn't include the private_key in the hash if it is nil" do
209
+ public_key = "key_to_happiness"
210
+ Project.new(:public_key => public_key).to_config[public_key.to_sym].should_not have_key(:private_key)
211
+ end
212
+ end
213
+
214
+ describe "protected attributes" do
215
+ [:api_uri, :community_uri, :short_uri].each do |attribute_name|
216
+ it "values passed in during initiazation for '#{attribute_name.to_s}' are silently ignored" do
217
+ p = Project.new(attribute_name => Time.now.to_s)
218
+ p.send(attribute_name).should be_nil
219
+ end
220
+
221
+ it "calling '#{attribute_name.to_s}= [value]' is not supported" do
222
+ p = Project.new
223
+ lambda {p.send("#{attribute_name}=", "my_value")}.should raise_error
224
+ end
225
+ end
226
+ end
227
+
228
+ describe "accessible attributes" do
229
+ before(:each) do
230
+ @value = 'my_value'
231
+ end
232
+
233
+ [:public_key, :name].each do |attribute_name|
234
+ it "values for '#{attribute_name.to_s}' are allowed to be modified by the client" do
235
+ p = Project.new(attribute_name => @value )
236
+ p.send(attribute_name).should == @value
237
+ end
238
+ end
239
+ end
240
+
241
+ describe "calling the class method" do
242
+ describe "'find'" do
243
+ context "and specifying a public_key which is already associated to a project on the site" do
244
+ before(:each) do
245
+ @public_key = "an_existing_public_key"
246
+ register_show_uri(@public_key)
247
+ end
248
+
249
+ it "returns an initialized instance of the Project class" do
250
+ expected_config = {
251
+ :an_existing_public_key =>
252
+ {
253
+ :private_key=>"private_key_value",
254
+ :api_uri=>"http://www.codefumes.com/api/v1/xml/projects/an_existing_public_key.xml",
255
+ :short_uri=>"http://www.codefumes.com/p/an_existing_public_key"
256
+ }
257
+ }
258
+ Project.find(@public_key).to_config.should == expected_config
259
+ end
260
+ end
261
+
262
+ context "and specifying a public_key which is not associated to any project on the site yet" do
263
+ before(:each) do
264
+ @public_key = "non_existant_public_key"
265
+ register_show_uri(@public_key, "Some Name", ["404", "Not Found"])
266
+ end
267
+
268
+ it "returns nil" do
269
+ Project.find(@public_key).should == nil
270
+ end
271
+ end
272
+ end
273
+ end
274
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
- -format specdoc
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+
3
+ begin
4
+ require 'spec/autorun'
5
+ rescue LoadError
6
+ gem 'rspec'
7
+ require 'spec'
8
+ end
9
+
10
+ gem 'ruby-debug'
11
+ require 'ruby-debug'
12
+
13
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
14
+ require 'codefumes'
15
+ require 'fakeweb'
16
+
17
+ include CodeFumes
18
+
19
+ ENV['CODEFUMES_CONFIG_FILE'] = File.expand_path(File.dirname(__FILE__) + '/sample_codefumes_config.tmp')