agree2 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,138 @@
1
+ require File.join(File.dirname(__FILE__),"spec_helper")
2
+ require 'open-uri'
3
+ #require 'hpricot'
4
+ require 'oauth/request_proxy/rack_request'
5
+ require 'rack/request'
6
+ require 'rack/mock'
7
+
8
+ include Agree2
9
+ describe Party do
10
+ before(:each) do
11
+ @client=Agree2::Client.new "client_key","client_secret"
12
+ @user=Agree2::User.new(@client,"token","token_secret")
13
+ @agreement=Agreement.new @user,{:permalink=>'hello'}
14
+ @json=IO.read(File.join(File.dirname(__FILE__),"fixtures","party.json"))
15
+ end
16
+
17
+ describe "Create new" do
18
+ before(:each) do
19
+ @party=Party.new(@agreement,{:first_name=>"Bob",:last_name=>"Wildcat",:email=>'bob@gmail.inv'})
20
+ end
21
+ it "should be new record" do
22
+ @party.should be_new_record
23
+ end
24
+
25
+ it "should have agreement" do
26
+ @party.agreement.should==@agreement
27
+ end
28
+
29
+ it "should have a user" do
30
+ @party.user.should==@user
31
+ end
32
+
33
+ it "should have first_name" do
34
+ @party.first_name.should=="Bob"
35
+ end
36
+
37
+ it "should have last_name" do
38
+ @party.last_name.should=="Wildcat"
39
+ end
40
+
41
+ describe "Save to Server" do
42
+ before(:each) do
43
+ @user.should_receive(:post).with("/agreements/hello/parties",
44
+ {'party'=>{:first_name=>"Bob",:last_name=>"Wildcat",:email=>'bob@gmail.inv'}}).and_return(@json)
45
+ end
46
+
47
+ it "should save and return true" do
48
+ @party.save
49
+ end
50
+
51
+ it "should no longer be new record" do
52
+ @party.save
53
+ @party.should_not be_new_record
54
+ end
55
+
56
+ it "should have a permalink" do
57
+ @party.save
58
+ @party.id.should==1102
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ describe "from json" do
65
+
66
+ before(:each) do
67
+ @party=Party.new(@agreement,@json)
68
+ end
69
+
70
+ it "should have agreement" do
71
+ @party.agreement.should==@agreement
72
+ end
73
+
74
+ it "should have a user" do
75
+ @party.user.should==@user
76
+ end
77
+
78
+ it "should have first_name" do
79
+ @party.first_name.should=="Bob"
80
+ end
81
+
82
+ it "should have last_name" do
83
+ @party.last_name.should=="Wildcat"
84
+ end
85
+
86
+ it "should have a path" do
87
+ @party.path.should=="/agreements/hello/parties/1102"
88
+ end
89
+
90
+ it "should have a url" do
91
+ @party.to_url.should=="https://agree2.com/agreements/hello/parties/1102"
92
+ end
93
+
94
+ it "should generate present url" do
95
+ present_url=@party.present
96
+ present_url=~/^(http.*)\?(.*)$/
97
+ $1.should=="https://agree2.com/present/hello/to/bob@gmail.inv"
98
+ end
99
+
100
+ it "should verify present url" do
101
+ present_url=@party.present
102
+ request = Rack::Request.new(Rack::MockRequest.env_for(present_url))
103
+ OAuth::Signature.verify(request,{:consumer=>@client.consumer,:token=>@user.access_token}).should==true
104
+
105
+ end
106
+
107
+ describe "Save to Server" do
108
+ before(:each) do
109
+ @user.should_receive(:put).with("/agreements/hello/parties/1102",
110
+ {'party'=>@party.attributes}).and_return(@json)
111
+ end
112
+
113
+ it "should save and return true" do
114
+ @party.save
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+ end
121
+
122
+ describe Agree2::Party, "party hash validation" do
123
+ before(:each) do
124
+ @hash={:first_name=>'Bob',:last_name=>'Wilson',:email=>'bob@gmail.com',:organization_name=>'SomeCo Inc.'}
125
+ end
126
+
127
+ it "should validate" do
128
+ lambda {Party.validate_party_hash(@hash).should==true}.should_not raise_error
129
+ end
130
+
131
+ [:first_name,:last_name,:email].each do |field|
132
+ it "should require #{field.to_s}" do
133
+ @hash.delete(field)
134
+ lambda {Party.validate_party_hash(@hash)}.should raise_error(ArgumentError)
135
+ end
136
+ end
137
+
138
+ end
@@ -0,0 +1,124 @@
1
+ require File.join(File.dirname(__FILE__),"spec_helper")
2
+
3
+ describe Agree2::ProxyCollection do
4
+
5
+ before(:each) do
6
+ @client=Agree2::Client.new "client_key","client_secret"
7
+ @user=Agree2::User.new(@client,"token","token_secret")
8
+ end
9
+ describe "load" do
10
+ before(:each) do
11
+ @agreements=Agree2::ProxyCollection.new(@user,"/agreements")
12
+ @json="[#{IO.read(File.join(File.dirname(__FILE__),"fixtures","agreement.json"))}]"
13
+ @user.stub!(:get).and_return(@json)
14
+ end
15
+
16
+ it "should have a path" do
17
+ @agreements.path.should=="/agreements"
18
+ end
19
+
20
+ it "should have singular" do
21
+ @agreements.singular.should=='Agreement'
22
+ end
23
+
24
+ it "should call web service" do
25
+ @user.should_receive(:get).with("/agreements").and_return(@json)
26
+ @agreements.length.should==1
27
+ end
28
+
29
+ it "should have a user" do
30
+ @agreements.user.should==@user
31
+ end
32
+ end
33
+
34
+ describe "with default value" do
35
+ before(:each) do
36
+ @agreements=Agree2::ProxyCollection.new(@user,"/agreements",nil,[{:permalink=>'hello'}])
37
+ end
38
+
39
+ it "should have a path" do
40
+ @agreements.path.should=="/agreements"
41
+ end
42
+
43
+ it "should have singular" do
44
+ @agreements.singular.should=='Agreement'
45
+ end
46
+
47
+ it "should have a user" do
48
+ @agreements.user.should==@user
49
+ end
50
+
51
+ it "should have one item" do
52
+ @agreements.length.should==1
53
+ end
54
+
55
+ it "should not call web service" do
56
+ @user.should_not_receive(:get)
57
+ @agreements.first
58
+ end
59
+
60
+ it "should find an individual resource" do
61
+ @user.should_receive(:get).with('/agreements/something').and_return(
62
+ IO.read(File.join(File.dirname(__FILE__),"fixtures","agreement.json")))
63
+ @agreements.find('something')
64
+ end
65
+
66
+ it "should instantiate a new party" do
67
+ Agree2::Agreement.should_receive(:new).with(@user,{:title=>"Test",:body=>"Body"})
68
+ @agreements.build :title=>"Test",:body=>"Body"
69
+ end
70
+
71
+ it "should create and save a new party" do
72
+ @agreement=mock("agreement")
73
+ Agree2::Agreement.should_receive(:new).with(@user,{:title=>"Test",:body=>"Body"}).and_return(@agreement)
74
+ @agreement.should_receive(:save)
75
+ @agreements.create :title=>"Test",:body=>"Body"
76
+ end
77
+
78
+ end
79
+
80
+ describe "nested load" do
81
+ before(:each) do
82
+ @agreement=Agree2::Agreement.new(@user,{:permalink=>'hello'})
83
+ @parties=@agreement.parties
84
+ @json="[#{IO.read(File.join(File.dirname(__FILE__),"fixtures","party.json"))}]"
85
+ @user.stub!(:get).and_return(@json)
86
+ end
87
+
88
+ it "should have a path" do
89
+ @parties.path.should=="/agreements/hello/parties"
90
+ end
91
+
92
+ it "should have singular" do
93
+ @parties.singular.should=='Party'
94
+ end
95
+
96
+ it "should call web service" do
97
+ @user.should_receive(:get).with("/agreements/hello/parties").and_return(@json)
98
+ @parties.length.should==1
99
+ end
100
+
101
+ it "should have a user" do
102
+ @parties.user.should==@user
103
+ end
104
+
105
+ it "should find an individual resource" do
106
+ @user.should_receive(:get).with('/agreements/hello/parties/123').and_return(
107
+ IO.read(File.join(File.dirname(__FILE__),"fixtures","party.json")))
108
+ @parties.find(123)
109
+ end
110
+
111
+ it "should instantiate a new party" do
112
+ Agree2::Party.should_receive(:new).with(@agreement,{:first_name=>"Bob",:last_name=>"Galbraith",:email=>'bob@bob.inv'})
113
+ @parties.build :first_name=>"Bob",:last_name=>"Galbraith",:email=>'bob@bob.inv'
114
+ end
115
+
116
+ it "should create and save a new party" do
117
+ @party=mock("party")
118
+ Agree2::Party.should_receive(:new).with(@agreement,{:first_name=>"Bob",:last_name=>"Galbraith",:email=>'bob@bob.inv'}).and_return(@party)
119
+ @party.should_receive(:save)
120
+ @parties.create :first_name=>"Bob",:last_name=>"Galbraith",:email=>'bob@bob.inv'
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,5 @@
1
+ $:<<File.dirname(__FILE__)
2
+ $:<<File.join(File.dirname(__FILE__),"..","lib")
3
+ require 'agree2'
4
+ require 'spec'
5
+
@@ -0,0 +1,89 @@
1
+ require File.join(File.dirname(__FILE__),"spec_helper")
2
+
3
+ describe Agree2::Template do
4
+ before(:each) do
5
+ @client=Agree2::Client.new "client_key","client_secret"
6
+ @user=Agree2::User.new(@client,"token","token_secret")
7
+ @template=Agree2::Template.new(@user,{:permalink=>'hello'})
8
+ @fields={:amount=>100}
9
+ @bob={:first_name=>'Bob',:last_name=>'Green',:email=>'bob@green.inv'}
10
+ @json=IO.read(File.join(File.dirname(__FILE__),"fixtures","agreement.json"))
11
+
12
+ end
13
+
14
+ it "should have a user" do
15
+ @template.user.should==@user
16
+ end
17
+
18
+ it "should have correct path" do
19
+ @template.path.should=="/masters/hello"
20
+ end
21
+
22
+ it "should have correct url" do
23
+ @template.to_url.should=="https://agree2.com/masters/hello"
24
+ end
25
+
26
+ describe "prepare" do
27
+ before(:each) do
28
+ @agreement=mock('agreement')
29
+ @user.stub!(:get).and_return(@agreement)
30
+ end
31
+
32
+ it "should call raw_prepare with correct parameters" do
33
+ @template.should_receive(:raw_prepare).with(:fields=>@fields,:parties=>{:client=>@bob})
34
+ @template.prepare(@fields,{:client=>@bob})
35
+ end
36
+
37
+ it "should call raw_prepare with correct parameters for application" do
38
+ @template.should_receive(:raw_prepare).with(:fields=>@fields,:parties=>{:client=>@bob,'service'=>:application})
39
+ @template.prepare(@fields,{:client=>@bob},'service')
40
+ end
41
+
42
+ it "should call raw_prepare with correct parameters" do
43
+ @user.should_receive(:post).with("/masters/hello/prepare",:fields=>@fields,:parties=>{:client=>@bob}).and_return(@json)
44
+ @template.prepare(@fields,{:client=>@bob})
45
+ end
46
+
47
+ it "should raise exception and not receive raw_prepare when parties aren't valid" do
48
+ @bob.delete(:email)
49
+ @template.should_not_receive(:raw_prepare)
50
+ lambda {@template.prepare(@fields,{:client=>@bob})}.should raise_error(ArgumentError)
51
+ end
52
+ end
53
+
54
+ describe "prepare and sign" do
55
+ before(:each) do
56
+ @agreement=mock('agreement')
57
+ @user.stub!(:get).and_return(@agreement)
58
+ end
59
+
60
+ it "should call raw_prepare with correct parameters" do
61
+ @template.should_receive(:raw_prepare).with(:fields=>@fields,
62
+ :parties=>{:client=>@bob,'application'=>:application},:sign=>'application')
63
+ @template.prepare_and_sign(@fields,{:client=>@bob})
64
+ end
65
+
66
+ it "should call raw_prepare with correct parameters for application" do
67
+ @template.should_receive(:raw_prepare).with(:fields=>@fields,:parties=>{:client=>@bob,'service'=>:application},:sign=>'service')
68
+ @template.prepare_and_sign(@fields,{:client=>@bob},'service')
69
+ end
70
+
71
+ it "should call raw_prepare with correct parameters" do
72
+ @user.should_receive(:post).with("/masters/hello/prepare",:fields=>@fields,
73
+ :parties=>{:client=>@bob,'application'=>:application},:sign=>'application').and_return(@json)
74
+ @template.prepare_and_sign(@fields,{:client=>@bob})
75
+ end
76
+
77
+ it "should raise exception and not receive raw_prepare when parties aren't valid" do
78
+ @bob.delete(:email)
79
+ @template.should_not_receive(:raw_prepare)
80
+ lambda {@template.prepare_and_sign(@fields,{:client=>@bob})}.should raise_error(ArgumentError)
81
+ end
82
+
83
+ it "should raise exception and not receive raw_prepare when no parties are present" do
84
+ @bob.delete(:email)
85
+ @template.should_not_receive(:raw_prepare)
86
+ lambda {@template.prepare_and_sign(@fields,{})}.should raise_error(ArgumentError)
87
+ end
88
+ end
89
+ end
data/spec/user_spec.rb ADDED
@@ -0,0 +1,77 @@
1
+ require File.join(File.dirname(__FILE__),"spec_helper")
2
+
3
+ describe Agree2::User do
4
+
5
+ before(:each) do
6
+ @client=Agree2::Client.new "client_key","client_secret"
7
+ @user=Agree2::User.new(@client,"token","token_secret")
8
+ end
9
+
10
+ it "should create user" do
11
+ @user.client.should==@client
12
+ @user.access_token.consumer.should==@client.consumer
13
+ @user.token.should=="token"
14
+ @user.secret.should=="token_secret"
15
+ end
16
+
17
+ it "should find all agreements" do
18
+ @agreements=[]
19
+ @json="[]"
20
+ @user.should_receive(:get).with("/agreements").and_return(@json)
21
+ @user.agreements.length.should==0
22
+ @user.agreements.should==@agreements
23
+ end
24
+
25
+ it "should find all templates" do
26
+ @templates=[]
27
+ @json="[]"
28
+ @user.should_receive(:get).with("/masters").and_return(@json)
29
+ @user.templates.length.should==0
30
+ @user.templates.should==@templates
31
+ end
32
+
33
+ describe "http calls" do
34
+ before(:each) do
35
+ @token=@user.access_token
36
+ @json='{"hello":"world"}'
37
+ @response=mock("response")
38
+ @response.stub!(:code).and_return("200")
39
+ @response.stub!(:body).and_return(@json)
40
+ end
41
+
42
+ [:get,:head,:delete].each do |m|
43
+ it "should perform http #{m.to_s}" do
44
+ @token.should_receive(m).with("/test",{'Content-Type'=>'application/json','Accept'=>'application/json'}).and_return(@response)
45
+ @user.send(m,"/test").should==@json
46
+ end
47
+ end
48
+
49
+ [:post,:put].each do |m|
50
+ it "should perform http #{m.to_s} with no data" do
51
+ @token.should_receive(m).with("/test",nil,{'Content-Type'=>'application/json','Accept'=>'application/json'}).and_return(@response)
52
+ @user.send(m,"/test").should==@json
53
+ end
54
+
55
+ it "should perform http #{m.to_s} with hash" do
56
+ @token.should_receive(m).with("/test",'{"test":"this"}',{'Content-Type'=>'application/json','Accept'=>'application/json'}).and_return(@response)
57
+ @user.send(m,"/test",{:test=>'this'}).should==@json
58
+ end
59
+ end
60
+
61
+ describe "handle_response" do
62
+ it "should return body" do
63
+ @user.send(:handle_response,@response).should==@json
64
+ end
65
+
66
+ it "should return handle redirect" do
67
+ @response.stub!(:code).and_return("302")
68
+ @response.stub!(:[]).and_return('https://agree2.com/agreements/my_agreement')
69
+ @user.should_receive(:get).with('/agreements/my_agreement').and_return('{"permalink":"my_agreement","title":"hello there"}')
70
+ @agreement=@user.send(:handle_response,@response)
71
+ @agreement.permalink.should=='my_agreement'
72
+ @agreement.title.should=='hello there'
73
+ end
74
+
75
+ end
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: agree2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Pelle Braendgaard
8
+ - Lau Taarnskov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-08-23 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: ruby-hmac
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.3.1
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: oauth
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.2.4
35
+ version:
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ type: :runtime
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 1.1.3
45
+ version:
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ type: :runtime
49
+ version_requirement:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.2
55
+ version:
56
+ description: Agree2 Ruby client library
57
+ email:
58
+ - support@agree2.com
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ extra_rdoc_files:
64
+ - History.txt
65
+ - License.txt
66
+ - README.rdoc
67
+ files:
68
+ - History.txt
69
+ - License.txt
70
+ - README.rdoc
71
+ - Rakefile
72
+ - lib/agree2.rb
73
+ - lib/agree2/base.rb
74
+ - lib/agree2/client.rb
75
+ - lib/agree2/user.rb
76
+ - lib/agree2/agreement.rb
77
+ - lib/agree2/party.rb
78
+ - lib/agree2/proxy_collection.rb
79
+ - lib/agree2/template.rb
80
+ has_rdoc: true
81
+ homepage: http://agree2.rubyforge.org
82
+ licenses: []
83
+
84
+ post_install_message:
85
+ rdoc_options:
86
+ - --main
87
+ - README.rdoc
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ requirements: []
103
+
104
+ rubyforge_project: agree2
105
+ rubygems_version: 1.3.2
106
+ signing_key:
107
+ specification_version: 2
108
+ summary: Agree2 Ruby client library
109
+ test_files:
110
+ - spec/spec_helper.rb
111
+ - spec/agreement_spec.rb
112
+ - spec/client_spec.rb
113
+ - spec/party_spec.rb
114
+ - spec/proxy_collection_spec.rb
115
+ - spec/template_spec.rb
116
+ - spec/user_spec.rb
117
+ - spec/fixtures/agreement.json
118
+ - spec/fixtures/party.json