activeforce 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/Gemfile +15 -0
  2. data/Gemfile.lock +128 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.md +112 -0
  5. data/Rakefile +47 -0
  6. data/VERSION +1 -0
  7. data/activeforce.gemspec +151 -0
  8. data/app/models/salesforce/account.rb +4 -0
  9. data/app/models/salesforce/activity_history.rb +4 -0
  10. data/app/models/salesforce/approval.rb +4 -0
  11. data/app/models/salesforce/campaign.rb +4 -0
  12. data/app/models/salesforce/campaign_feed.rb +4 -0
  13. data/app/models/salesforce/campaign_member.rb +4 -0
  14. data/app/models/salesforce/case.rb +4 -0
  15. data/app/models/salesforce/case_comment.rb +4 -0
  16. data/app/models/salesforce/case_contact_role.rb +4 -0
  17. data/app/models/salesforce/case_feed.rb +4 -0
  18. data/app/models/salesforce/case_history.rb +4 -0
  19. data/app/models/salesforce/case_share.rb +4 -0
  20. data/app/models/salesforce/case_solution.rb +4 -0
  21. data/app/models/salesforce/case_status.rb +4 -0
  22. data/app/models/salesforce/case_team_member.rb +4 -0
  23. data/app/models/salesforce/community.rb +4 -0
  24. data/app/models/salesforce/contact.rb +4 -0
  25. data/app/models/salesforce/contact_feed.rb +4 -0
  26. data/app/models/salesforce/contact_history.rb +4 -0
  27. data/app/models/salesforce/contract.rb +4 -0
  28. data/app/models/salesforce/document.rb +4 -0
  29. data/app/models/salesforce/event.rb +4 -0
  30. data/app/models/salesforce/feed_item.rb +4 -0
  31. data/app/models/salesforce/group.rb +4 -0
  32. data/app/models/salesforce/group_member.rb +4 -0
  33. data/app/models/salesforce/idea.rb +4 -0
  34. data/app/models/salesforce/lead.rb +4 -0
  35. data/app/models/salesforce/lead_status.rb +4 -0
  36. data/app/models/salesforce/name.rb +4 -0
  37. data/app/models/salesforce/note.rb +4 -0
  38. data/app/models/salesforce/open_activity.rb +4 -0
  39. data/app/models/salesforce/opportunity.rb +4 -0
  40. data/app/models/salesforce/organization.rb +4 -0
  41. data/app/models/salesforce/partner.rb +4 -0
  42. data/app/models/salesforce/period.rb +4 -0
  43. data/app/models/salesforce/product2.rb +4 -0
  44. data/app/models/salesforce/product2_feed.rb +4 -0
  45. data/app/models/salesforce/profile.rb +4 -0
  46. data/app/models/salesforce/quote.rb +4 -0
  47. data/app/models/salesforce/solution.rb +4 -0
  48. data/app/models/salesforce/task.rb +4 -0
  49. data/app/models/salesforce/task_feed.rb +4 -0
  50. data/app/models/salesforce/task_priority.rb +4 -0
  51. data/app/models/salesforce/task_status.rb +4 -0
  52. data/app/models/salesforce/user.rb +4 -0
  53. data/app/models/salesforce/user_role.rb +4 -0
  54. data/app/models/salesforce/vote.rb +4 -0
  55. data/lib/activeforce.rb +29 -0
  56. data/lib/salesforce/attributes.rb +13 -0
  57. data/lib/salesforce/authentication.rb +25 -0
  58. data/lib/salesforce/base.rb +240 -0
  59. data/lib/salesforce/bulk/batch.rb +77 -0
  60. data/lib/salesforce/bulk/job.rb +103 -0
  61. data/lib/salesforce/bulk/operations.rb +25 -0
  62. data/lib/salesforce/bulk/update_job.rb +24 -0
  63. data/lib/salesforce/column.rb +98 -0
  64. data/lib/salesforce/columns.rb +60 -0
  65. data/lib/salesforce/config.rb +110 -0
  66. data/lib/salesforce/connection.rb +33 -0
  67. data/lib/salesforce/connection/async.rb +36 -0
  68. data/lib/salesforce/connection/conversion.rb +37 -0
  69. data/lib/salesforce/connection/http_methods.rb +72 -0
  70. data/lib/salesforce/connection/rest_api.rb +52 -0
  71. data/lib/salesforce/connection/soap_api.rb +74 -0
  72. data/lib/salesforce/engine.rb +6 -0
  73. data/lib/salesforce/errors.rb +33 -0
  74. data/test/salesforce/authentication_test.rb +52 -0
  75. data/test/salesforce/base_test.rb +440 -0
  76. data/test/salesforce/bulk/batch_test.rb +79 -0
  77. data/test/salesforce/bulk/update_job_test.rb +125 -0
  78. data/test/salesforce/column_test.rb +115 -0
  79. data/test/salesforce/config_test.rb +163 -0
  80. data/test/salesforce/connection/async_test.rb +138 -0
  81. data/test/salesforce/connection/http_methods_test.rb +242 -0
  82. data/test/salesforce/connection/rest_api_test.rb +61 -0
  83. data/test/salesforce/connection/soap_api_test.rb +148 -0
  84. data/test/salesforce/connection_test.rb +148 -0
  85. data/test/test_helper.rb +79 -0
  86. metadata +295 -0
@@ -0,0 +1,138 @@
1
+ require 'test_helper'
2
+
3
+ class Salesforce.connection::AsyncTest < ActiveSupport::TestCase
4
+ def async_post(path, body, options = {})
5
+ as_logged_in_user do
6
+ convert_body RestClient.post(async_api_url(path), body, async_headers(options)), options
7
+ end
8
+ rescue => e
9
+ raise e
10
+ end
11
+
12
+ def test_async_post__json
13
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
14
+ http_body = stub(:body => { :result => 'foo' }.to_json)
15
+ RestClient.expects(:post).with('https://.salesforce.com/services/async/22.0/path', :body, {'X-SFDC-Session' => 'session_id', :content_type => 'application/json'}).returns(http_body)
16
+ assert_equal({'result' => 'foo'}, Salesforce.connection.async_post('path', :body, :format => :json))
17
+ end
18
+
19
+ def test_async_post__400_error_json
20
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
21
+ error = RestClient::BadRequest.new
22
+ error.stubs(:http_body).returns("[{\"message\":\"someproblem\",\"errorCode\":\"MALFORMED_QUERY\"}]")
23
+ RestClient.stubs(:post).raises(error)
24
+ begin
25
+ Salesforce.connection.async_post('path', :body, :format => :json)
26
+ assert false, "Shouldn't have gotten here"
27
+ rescue => e
28
+ assert_equal "Salesforce::InvalidRequest", e.class.name
29
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/async/22.0/path", e.message
30
+ end
31
+ end
32
+
33
+ def test_async_post__404_error_xml
34
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
35
+ error = RestClient::BadRequest.new
36
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
37
+ RestClient.expects(:post).with('https://.salesforce.com/services/async/22.0/path', :body, {'X-SFDC-Session' => 'session_id', :content_type => 'application/xml'}).raises(error)
38
+
39
+ begin
40
+ Salesforce.connection.async_post('path', :body, :format => :xml)
41
+ assert false, "Shouldn't have gotten here"
42
+ rescue => e
43
+ assert_equal "Salesforce::InvalidRequest", e.class.name
44
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/async/22.0/path", e.message
45
+ end
46
+ end
47
+
48
+ def test_async_post__400_error_json
49
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
50
+ error = RestClient::ResourceNotFound.new
51
+ error.stubs(:http_body).returns("[{\"message\":\"someproblem\",\"errorCode\":\"MALFORMED_QUERY\"}]")
52
+ RestClient.expects(:post).with('https://.salesforce.com/services/async/22.0/path', :body, {'X-SFDC-Session' => 'session_id', :content_type => 'application/json'}).raises(error)
53
+ begin
54
+ Salesforce.connection.async_post('path', :body, :format => :json)
55
+ assert false, "Shouldn't have gotten here"
56
+ rescue => e
57
+ assert_equal "Salesforce::InvalidRequest", e.class.name
58
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/async/22.0/path", e.message
59
+ end
60
+ end
61
+
62
+ def test_async_post__400_error_xml
63
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
64
+ error = RestClient::ResourceNotFound.new
65
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
66
+ RestClient.expects(:post).with('https://.salesforce.com/services/async/22.0/path', :body, {'X-SFDC-Session' => 'session_id', :content_type => 'application/xml'}).raises(error)
67
+
68
+ begin
69
+ Salesforce.connection.async_post('path', :body, :format => :xml)
70
+ assert false, "Shouldn't have gotten here"
71
+ rescue => e
72
+ assert_equal "Salesforce::InvalidRequest", e.class.name
73
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/async/22.0/path", e.message
74
+ end
75
+ end
76
+
77
+ def test_async_post__xml
78
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
79
+ http_body = stub(:body => { :result => 'foo' }.to_xml)
80
+ RestClient.expects(:post).with('https://.salesforce.com/services/async/22.0/path', :body, {'X-SFDC-Session' => 'session_id', :content_type => 'application/xml'}).returns(http_body)
81
+ assert_equal({'result' => 'foo'}, Salesforce.connection.async_post('path', :body, :format => :xml))
82
+ end
83
+
84
+ def test_async_get__json
85
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
86
+ http_body = stub(:body => { :result => 'foo' }.to_json)
87
+ RestClient.expects(:get).with('https://.salesforce.com/services/async/22.0/path', {'X-SFDC-Session' => 'session_id', :content_type => 'application/json'}).returns(http_body)
88
+ assert_equal({'result' => 'foo'}, Salesforce.connection.async_get('path', :format => :json))
89
+ end
90
+
91
+ def test_async_get__error_json
92
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
93
+ error = RestClient::BadRequest.new
94
+ error.stubs(:http_body).returns("[{\"message\":\"someproblem\",\"errorCode\":\"MALFORMED_QUERY\"}]")
95
+ RestClient.expects(:get).with('https://.salesforce.com/services/async/22.0/path', {'X-SFDC-Session' => 'session_id', :content_type => 'application/json'}).raises(error)
96
+
97
+ begin
98
+ Salesforce.connection.async_get('path', :format => :json)
99
+ assert false, "Shouldn't have gotten here"
100
+ rescue => e
101
+ assert_equal "Salesforce::InvalidRequest", e.class.name
102
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/async/22.0/path", e.message
103
+ end
104
+ end
105
+
106
+ def test_async_get__error_xml
107
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
108
+ error = RestClient::BadRequest.new
109
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
110
+ RestClient.expects(:get).with('https://.salesforce.com/services/async/22.0/path', {'X-SFDC-Session' => 'session_id', :content_type => 'application/xml'}).raises(error)
111
+
112
+ begin
113
+ Salesforce.connection.async_get('path', :format => :xml)
114
+ assert false, "Shouldn't have gotten here"
115
+ rescue => e
116
+ assert_equal "Salesforce::InvalidRequest", e.class.name
117
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/async/22.0/path", e.message
118
+ end
119
+ end
120
+
121
+ def test_async_get__xml
122
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
123
+ http_body = stub(:body => { :result => 'foo' }.to_xml)
124
+ RestClient.expects(:get).with('https://.salesforce.com/services/async/22.0/path', {'X-SFDC-Session' => 'session_id', :content_type => 'application/xml'}).returns(http_body)
125
+ assert_equal({'result' => 'foo'}, Salesforce.connection.async_get('path', :format => :xml))
126
+ end
127
+
128
+ def test_async_api_url
129
+ assert_equal 'https://.salesforce.com/services/async/22.0/path', Salesforce.connection.async_api_url('path')
130
+ end
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+ end
@@ -0,0 +1,242 @@
1
+ require 'test_helper'
2
+
3
+ class Salesforce.connection::HttpMethodsTest < ActiveSupport::TestCase
4
+ def test_content_type_headers
5
+ assert_equal({ :content_type => 'application/json'}, Salesforce.connection.content_type_headers(:format => :json))
6
+ assert_equal({ :content_type => 'application/json'}, Salesforce.connection.content_type_headers(:format => "json"))
7
+ assert_equal({ :content_type => 'application/xml'}, Salesforce.connection.content_type_headers(:format => :xml))
8
+ assert_equal({ :content_type => 'application/xml'}, Salesforce.connection.content_type_headers(:format => 'xml'))
9
+ assert_equal({ :content_type => 'foobar'}, Salesforce.connection.content_type_headers(:content_type => 'foobar'))
10
+ assert_equal({ :content_type => nil }, Salesforce.connection.content_type_headers(:format => 'foobar'))
11
+ end
12
+
13
+ def test_get__json
14
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
15
+ http_body = stub(:body => { :result => 'foo' }.to_json)
16
+ RestClient.expects(:get).with('https://.salesforce.com/services/data/v22.0/path', {'Authorization' => 'OAuth session_id', :content_type => 'application/json'}).returns(http_body)
17
+ assert_equal({'result' => 'foo'}, Salesforce.connection.get('path', :format => :json))
18
+ end
19
+
20
+ def test_get__error_json
21
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
22
+ error = RestClient::BadRequest.new
23
+ error.stubs(:http_body).returns("[{\"message\":\"someproblem\",\"errorCode\":\"MALFORMED_QUERY\"}]")
24
+ RestClient.expects(:get).with('https://.salesforce.com/services/data/v22.0/path', {'Authorization' => 'OAuth session_id', :content_type => 'application/json'}).raises(error)
25
+
26
+ begin
27
+ Salesforce.connection.get('path', :format => :json)
28
+ assert false, "Shouldn't have gotten here"
29
+ rescue => e
30
+ assert_equal "Salesforce::InvalidRequest", e.class.name
31
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
32
+ end
33
+ end
34
+
35
+ def test_get__error_xml
36
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
37
+ error = RestClient::BadRequest.new
38
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
39
+ RestClient.expects(:get).with('https://.salesforce.com/services/data/v22.0/path', {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).raises(error)
40
+
41
+ begin
42
+ Salesforce.connection.get('path', :format => :xml)
43
+ assert false, "Shouldn't have gotten here"
44
+ rescue => e
45
+ assert_equal "Salesforce::InvalidRequest", e.class.name
46
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
47
+ end
48
+ end
49
+
50
+ def test_get__xml
51
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
52
+ http_body = stub(:body => { :result => 'foo' }.to_xml)
53
+ RestClient.expects(:get).with('https://.salesforce.com/services/data/v22.0/path', {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).returns(http_body)
54
+ assert_equal({'result' => 'foo'}, Salesforce.connection.get('path', :format => :xml))
55
+ end
56
+
57
+ def test_patch__json
58
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
59
+ http_body = stub(:code => 204, :body => '')
60
+ RestClient.expects(:patch).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/json'}).returns(http_body)
61
+ assert Salesforce.connection.patch('path', :body, :format => :json)
62
+ end
63
+
64
+ def test_patch__400_error_json
65
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
66
+ error = RestClient::BadRequest.new
67
+ error.stubs(:http_body).returns("[{\"message\":\"someproblem\",\"errorCode\":\"MALFORMED_QUERY\"}]")
68
+ RestClient.expects(:patch).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/json'}).raises(error)
69
+ begin
70
+ Salesforce.connection.patch('path', :body, :format => :json)
71
+ assert false, "Shouldn't have gotten here"
72
+ rescue => e
73
+ assert_equal "Salesforce::InvalidRequest", e.class.name
74
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
75
+ end
76
+ end
77
+
78
+ def test_patch__400_error_xml
79
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
80
+ error = RestClient::BadRequest.new
81
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
82
+ RestClient.expects(:patch).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).raises(error)
83
+
84
+ begin
85
+ Salesforce.connection.patch('path', :body, :format => :xml)
86
+ assert false, "Shouldn't have gotten here"
87
+ rescue => e
88
+ assert_equal "Salesforce::InvalidRequest", e.class.name
89
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
90
+ end
91
+ end
92
+
93
+ def test_patch__404_error_json
94
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
95
+ error = RestClient::ResourceNotFound.new
96
+ error.stubs(:http_body).returns("[{\"message\":\"someproblem\",\"errorCode\":\"MALFORMED_QUERY\"}]")
97
+ RestClient.expects(:patch).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/json'}).raises(error)
98
+ begin
99
+ Salesforce.connection.patch('path', :body, :format => :json)
100
+ assert false, "Shouldn't have gotten here"
101
+ rescue => e
102
+ assert_equal "Salesforce::InvalidRequest", e.class.name
103
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
104
+ end
105
+ end
106
+
107
+ def test_patch__404_error_xml
108
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
109
+ error = RestClient::ResourceNotFound.new
110
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
111
+ RestClient.expects(:patch).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).raises(error)
112
+
113
+ begin
114
+ Salesforce.connection.patch('path', :body, :format => :xml)
115
+ assert false, "Shouldn't have gotten here"
116
+ rescue => e
117
+ assert_equal "Salesforce::InvalidRequest", e.class.name
118
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
119
+ end
120
+ end
121
+
122
+ def test_patch__xml
123
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
124
+ http_body = stub(:code => 204, :body => '')
125
+ RestClient.expects(:patch).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).returns(http_body)
126
+ assert Salesforce.connection.patch('path', :body, :format => :xml)
127
+ end
128
+
129
+
130
+ def test_post__json
131
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
132
+ http_body = stub(:body => { :result => 'foo' }.to_json)
133
+ RestClient.expects(:post).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/json'}).returns(http_body)
134
+ assert_equal({'result' => 'foo'}, Salesforce.connection.post('path', :body, :format => :json))
135
+ end
136
+
137
+ def test_post__400_error_json
138
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
139
+ error = RestClient::BadRequest.new
140
+ error.stubs(:http_body).returns("[{\"message\":\"someproblem\",\"errorCode\":\"MALFORMED_QUERY\"}]")
141
+ RestClient.expects(:post).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/json'}).raises(error)
142
+ begin
143
+ Salesforce.connection.post('path', :body, :format => :json)
144
+ assert false, "Shouldn't have gotten here"
145
+ rescue => e
146
+ assert_equal "Salesforce::InvalidRequest", e.class.name
147
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
148
+ end
149
+ end
150
+
151
+ def test_post__404_error_xml
152
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
153
+ error = RestClient::BadRequest.new
154
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
155
+ RestClient.expects(:post).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).raises(error)
156
+
157
+ begin
158
+ Salesforce.connection.post('path', :body, :format => :xml)
159
+ assert false, "Shouldn't have gotten here"
160
+ rescue => e
161
+ assert_equal "Salesforce::InvalidRequest", e.class.name
162
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
163
+ end
164
+ end
165
+
166
+ def test_post__400_error_json
167
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
168
+ error = RestClient::ResourceNotFound.new
169
+ error.stubs(:http_body).returns("[{\"message\":\"someproblem\",\"errorCode\":\"MALFORMED_QUERY\"}]")
170
+ RestClient.expects(:post).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/json'}).raises(error)
171
+ begin
172
+ Salesforce.connection.post('path', :body, :format => :json)
173
+ assert false, "Shouldn't have gotten here"
174
+ rescue => e
175
+ assert_equal "Salesforce::InvalidRequest", e.class.name
176
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
177
+ end
178
+ end
179
+
180
+ def test_post__400_error_xml
181
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
182
+ error = RestClient::ResourceNotFound.new
183
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
184
+ RestClient.expects(:post).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).raises(error)
185
+
186
+ begin
187
+ Salesforce.connection.post('path', :body, :format => :xml)
188
+ assert false, "Shouldn't have gotten here"
189
+ rescue => e
190
+ assert_equal "Salesforce::InvalidRequest", e.class.name
191
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
192
+ end
193
+ end
194
+
195
+ def test_post__xml
196
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
197
+ http_body = stub(:body => { :result => 'foo' }.to_xml)
198
+ RestClient.expects(:post).with('https://.salesforce.com/services/data/v22.0/path', :body, {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).returns(http_body)
199
+ assert_equal({'result' => 'foo'}, Salesforce.connection.post('path', :body, :format => :xml))
200
+ end
201
+
202
+ def test_delete__400_error_xml
203
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
204
+ error = RestClient::BadRequest.new
205
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
206
+ RestClient.expects(:delete).with('https://.salesforce.com/services/data/v22.0/path', {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).raises(error)
207
+
208
+ begin
209
+ Salesforce.connection.delete('path')
210
+ assert false, "Shouldn't have gotten here"
211
+ rescue => e
212
+ assert_equal "Salesforce::InvalidRequest", e.class.name
213
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
214
+ end
215
+ end
216
+
217
+ def test_delete__404_error_xml
218
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
219
+ error = RestClient::ResourceNotFound.new
220
+ error.stubs(:http_body).returns("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Errors><Error><errorCode>MALFORMED_QUERY</errorCode><message>someproblem</message></Error></Errors>" )
221
+ RestClient.expects(:delete).with('https://.salesforce.com/services/data/v22.0/path', {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).raises(error)
222
+
223
+ begin
224
+ Salesforce.connection.delete('path')
225
+ assert false, "Shouldn't have gotten here"
226
+ rescue => e
227
+ assert_equal "Salesforce::InvalidRequest", e.class.name
228
+ assert_equal "MALFORMED_QUERY: someproblem while accessing https://.salesforce.com/services/data/v22.0/path", e.message
229
+ end
230
+ end
231
+
232
+ def test_delete__xml
233
+ Salesforce::Authentication.stubs(:session_id).returns('session_id')
234
+ RestClient.expects(:delete).with('https://.salesforce.com/services/data/v22.0/path', {'Authorization' => 'OAuth session_id', :content_type => 'application/xml'}).returns(stub(:body => ''))
235
+ assert Salesforce.connection.delete('path')
236
+ end
237
+
238
+ def test_salesforce_url
239
+ assert_equal 'https://.salesforce.com/services/data/v22.0/path', Salesforce.connection.salesforce_url("path")
240
+ assert_equal 'https://.salesforce.com/services/data/23.0/foo', Salesforce.connection.salesforce_url("/services/data/23.0/foo")
241
+ end
242
+ end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+
3
+ class Salesforce.connection::RestApiTest < ActiveSupport::TestCase
4
+ def test_supported_objects
5
+ connection.expects(:get).with("sobjects.json", :format => :json).returns({ "sobjects" => [ { "name" => "Account"}, { "name" => "Contact"}]})
6
+ assert_equal [ "Account", "Contact" ], connection.supported_objects
7
+ end
8
+
9
+ def fields(object_name)
10
+ raise ObjectNotSupported.new(object_name) unless supported_objects.include?(object_name)
11
+ get("sobjects/#{object_name}/describe.json", :format => :json)["fields"]
12
+ end
13
+
14
+ def test_fields
15
+ connection.expects(:supported_objects).returns(["Account"])
16
+ connection.expects(:get).with("sobjects/Account/describe.json", :format => :json).returns("fields" => :fields)
17
+ assert_equal :fields, connection.fields("Account")
18
+ end
19
+
20
+ def test_fields__fails
21
+ connection.expects(:supported_objects).returns(["Account__c"])
22
+ connection.expects(:get).never
23
+ assert_raises Salesforce::ObjectNotSupported do
24
+ connection.fields("Account")
25
+ end
26
+ end
27
+
28
+ def test_find_object_by_id
29
+ connection.expects(:get).with("sobjects/Bill__c/a123456789.json?fields=Account__c,Name,Due_Date__c", :format => :json).returns(:result)
30
+ assert_equal :result, connection.find_object_by_id('Bill__c', 'a123456789', 'Account__c,Name,Due_Date__c' )
31
+ end
32
+
33
+ def test_update
34
+ fields = { "Account__c" => 'a4000', 'Due_Date__c' => "08/10/2029"}
35
+ connection.expects(:patch).with("sobjects/Bill__c/a123456789.json", fields.to_json, :format => :json).returns(stub(:code => 204))
36
+ assert connection.update('Bill__c', 'a123456789', fields)
37
+ end
38
+
39
+ def test_create__succeeds
40
+ fields = { "Account__c" => 'a4000', 'Due_Date__c' => "08/10/2029"}
41
+ connection.expects(:post).with('sobjects/Bill__c.json', fields.to_json, :format => :json).returns({ "success" => true, "id" => "newid"})
42
+ assert_equal "newid", connection.create('Bill__c', fields)
43
+ end
44
+
45
+ def test_create__fails
46
+ fields = { "Account__c" => 'a4000', 'Due_Date__c' => "08/10/2029"}
47
+ connection.expects(:post).with('sobjects/Bill__c.json', fields.to_json, :format => :json).returns({ "success" => false, "errors" => "something"})
48
+ assert_raises Salesforce::RecordInvalid do
49
+ connection.create('Bill__c', fields)
50
+ end
51
+ end
52
+
53
+ def test_delete
54
+ connection.expects(:delete).with("sobjects/Bill__c/a123456789")
55
+ connection.destroy("Bill__c", "a123456789")
56
+ end
57
+
58
+ def connection
59
+ Salesforce.connection
60
+ end
61
+ end
@@ -0,0 +1,148 @@
1
+ require 'test_helper'
2
+
3
+ class Salesforce::Connection::SoapApiTest < ActiveSupport::TestCase
4
+
5
+ def test_login
6
+ expected_options = {
7
+ :endpoint_url => :soap_login_url,
8
+ :body => {
9
+ :username => :soap_username,
10
+ :password => :soap_password,
11
+ :order! => [ :username, :password ]
12
+ }
13
+ }
14
+
15
+ Salesforce::Config.expects(:login_url).returns(:soap_login_url)
16
+ Salesforce::Config.expects(:username).returns(:soap_username)
17
+ Salesforce::Config.expects(:password).returns(:soap_password)
18
+
19
+ Salesforce.connection.expects(:invoke_soap).with(:login, expected_options).returns(:login_result)
20
+
21
+ assert_equal :login_result, Salesforce.connection.login
22
+ end
23
+
24
+ def test_convert_lead
25
+ expected_options = {
26
+ :body => {
27
+ :leadConverts => :some_lead_converts
28
+ }
29
+ }
30
+
31
+ Salesforce.connection.expects(:as_logged_in_user).yields.returns(:convert_lead_result)
32
+ Salesforce.connection.expects(:invoke_soap).with(:convertLead, expected_options)
33
+
34
+ assert_equal :convert_lead_result, Salesforce.connection.convert_lead(:some_lead_converts)
35
+ end
36
+
37
+ def test_invoke_soap__not_login__success
38
+ options = {
39
+ :body => :soap_body,
40
+ :other_option => 'foo'
41
+ }
42
+
43
+ result = {
44
+ :not_login_response => {
45
+ :result => {
46
+ :stuff => "i like",
47
+ :success => true
48
+ }
49
+ }
50
+ }
51
+
52
+ expected_result = result[:not_login_response][:result]
53
+
54
+ soap_client_mock = mock
55
+ Salesforce.connection.expects(:soap_client).with(options).returns(soap_client_mock)
56
+
57
+ Salesforce::Config.stubs(:session_id).returns('boyahh_session_id')
58
+ Salesforce::Config.stubs(:soap_enterprise_namespace).returns('soap:enterprise:namespace')
59
+
60
+ soap_mock = mock
61
+ soap_mock.expects(:body=).with(:soap_body)
62
+ soap_mock.expects(:header=).with({ "ns1:SessionHeader" => { "ns1:sessionId" => 'boyahh_session_id' }})
63
+
64
+
65
+ namespaces_mock = mock
66
+ namespaces_mock.expects(:[]=).with("xmlns:ns1", 'soap:enterprise:namespace')
67
+ soap_mock.expects(:namespaces).returns(namespaces_mock)
68
+
69
+ soap_client_mock.expects(:request).with(:wsdl, :notLogin).yields(soap_mock).returns(mock(:to_hash => result))
70
+
71
+ assert_equal expected_result, Salesforce.connection.send(:invoke_soap, :notLogin, options)
72
+ end
73
+
74
+ def test_invoke_soap__not_login__not_success
75
+ options = {
76
+ :body => :soap_body,
77
+ :other_option => 'foo'
78
+ }
79
+
80
+ result = {
81
+ :not_login_response => {
82
+ :result => {
83
+ :errors => { :inspect => :me },
84
+ :success => false
85
+ }
86
+ }
87
+ }
88
+
89
+ soap_client_mock = mock
90
+ Salesforce.connection.expects(:soap_client).with(options).returns(soap_client_mock)
91
+
92
+ Salesforce::Config.stubs(:session_id).returns('boyahh_session_id')
93
+ Salesforce::Config.stubs(:soap_enterprise_namespace).returns('soap:enterprise:namespace')
94
+
95
+ soap_mock = mock
96
+ soap_mock.expects(:body=).with(:soap_body)
97
+ soap_mock.expects(:header=).with({ "ns1:SessionHeader" => { "ns1:sessionId" => 'boyahh_session_id' }})
98
+
99
+
100
+ namespaces_mock = mock
101
+ namespaces_mock.expects(:[]=).with("xmlns:ns1", 'soap:enterprise:namespace')
102
+ soap_mock.expects(:namespaces).returns(namespaces_mock)
103
+
104
+ soap_client_mock.expects(:request).with(:wsdl, :notLogin).yields(soap_mock).returns(mock(:to_hash => result))
105
+
106
+ assert_raises Salesforce::Connection::SoapApi::Error do
107
+ Salesforce.connection.send(:invoke_soap, :notLogin, options)
108
+ end
109
+ end
110
+
111
+ def test_invoke_soap__login
112
+ options = {
113
+ :body => :soap_body,
114
+ :other_option => 'foo'
115
+ }
116
+
117
+ result = {
118
+ :login_response => {
119
+ :result => {
120
+ :stuff => "i like"
121
+ }
122
+ }
123
+ }
124
+
125
+ expected_result = result[:login_response][:result]
126
+
127
+ soap_client_mock = mock
128
+ Salesforce.connection.expects(:soap_client).with(options).returns(soap_client_mock)
129
+
130
+ soap_mock = mock
131
+ soap_mock.expects(:body=).with(:soap_body)
132
+ soap_client_mock.expects(:request).with(:wsdl, :login).yields(soap_mock).returns(mock(:to_hash => result))
133
+
134
+ assert_equal expected_result, Salesforce.connection.send(:invoke_soap, :login, options)
135
+ end
136
+
137
+ def test_soap_client
138
+ options = {
139
+ :namespace => 'my:name:space',
140
+ :endpoint_url => 'https://my.endpoint.url.com'
141
+ }
142
+
143
+ client = Salesforce.connection.send(:soap_client, options)
144
+ assert_equal 'https://my.endpoint.url.com', client.wsdl.endpoint
145
+ assert_equal 'my:name:space', client.wsdl.namespace
146
+ end
147
+
148
+ end