amee 4.3.2 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/CHANGELOG.txt +6 -0
  2. data/Gemfile +7 -1
  3. data/Rakefile +2 -0
  4. data/VERSION +1 -1
  5. data/amee.gemspec +57 -6
  6. data/amee_test_credentials.example.yml +11 -0
  7. data/cassettes/AMEE_Connection/v2/raising_unhandled_errors.yml +36 -0
  8. data/cassettes/AMEE_Connection/v3/retries/502.yml +36 -0
  9. data/cassettes/AMEE_Connection/v3/retries/503.yml +36 -0
  10. data/cassettes/AMEE_Connection/v3/retries/504.yml +36 -0
  11. data/cassettes/AMEE_Connection/v3/retries/AMEE_TimeOut.yml +36 -0
  12. data/cassettes/AMEE_Connection/v3/should_be_able_to_get_from_meta_server.yml +30 -0
  13. data/cassettes/AMEE_Connection/v3/should_be_able_to_handle_failed_gets_from_meta_server.yml +30 -0
  14. data/cassettes/AMEE_Connection/v3/should_be_able_to_post_to_meta_server.yml +59 -0
  15. data/cassettes/AMEE_Connection/v3/should_have_a_connection_to_meta_server.yml +36 -0
  16. data/cassettes/AMEE_Connection/v3/should_login_and_know_the_path_to_the_server.yml +36 -0
  17. data/cassettes/AMEE_Connection_Caching_Off/authenticating.yml +36 -0
  18. data/cassettes/AMEE_Connection_Caching_Off/first_request.yml +40 -0
  19. data/cassettes/AMEE_Connection_Caching_Off/second_request.yml +40 -0
  20. data/cassettes/AMEE_Connection_Caching_On/authenticating.yml +36 -0
  21. data/cassettes/AMEE_Connection_Caching_On/first_request.yml +40 -0
  22. data/cassettes/AMEE_Connection_Caching_clear_all/second_request.yml +40 -0
  23. data/cassettes/AMEE_Connection_Caching_clear_manually/second_request.yml +40 -0
  24. data/cassettes/AMEE_Connection_Caching_further_down_tree/second_request.yml +79 -0
  25. data/cassettes/AMEE_Connection_with_authentication/handling_404s.yml +69 -0
  26. data/cassettes/AMEE_Connection_with_authentication/hitting_private_urls.yml +75 -0
  27. data/cassettes/AMEE_Connection_with_authentication/raising_errors_if_permission_denied.yml +69 -0
  28. data/cassettes/AMEE_Connection_with_authentication/should_re-authenticate_and_refresh_authtoken_when_authtoken_expires.yml +104 -0
  29. data/cassettes/AMEE_Connection_with_authentication/should_refresh_authtoken_when_authtoken_is_changed.yml +114 -0
  30. data/cassettes/AMEE_Connection_with_authentication/using_a_v1_key.yml +71 -0
  31. data/cassettes/AMEE_Connection_with_authentication/using_a_v2_key/detects_the_API_version_for_JSON.yml +36 -0
  32. data/cassettes/AMEE_Connection_with_authentication/using_a_v2_key/detects_the_API_version_for_XML.yml +36 -0
  33. data/cassettes/AMEE_Connection_with_authentication_doing_write-requests.yml +75 -0
  34. data/cassettes/AMEE_Connection_with_authentication_doing_write-requests/working_with_an_existing_profile/deleting_existing_items.yml +108 -0
  35. data/cassettes/AMEE_Connection_with_authentication_doing_write-requests/working_with_an_existing_profile/sending_updates_to_existing_items.yml +108 -0
  36. data/cassettes/AMEE_Connection_with_bad_authentication_information/hitting_a_private_url.yml +29 -0
  37. data/cassettes/AMEE_Connection_with_bad_authentication_information/hitting_a_public_url.yml +32 -0
  38. data/cassettes/AMEE_Connection_with_incorrect_server_name.yml +16 -0
  39. data/cassettes/AMEE_Connection_with_retry_enabled.yml +36 -0
  40. data/lib/amee/connection.rb +234 -110
  41. data/lib/amee/data_category.rb +2 -2
  42. data/lib/amee/data_item.rb +45 -2
  43. data/lib/amee/data_item_value.rb +1 -1
  44. data/lib/amee/exceptions.rb +5 -2
  45. data/lib/amee/parse_helper.rb +2 -0
  46. data/lib/amee/profile_item.rb +6 -2
  47. data/lib/amee/v3/connection.rb +77 -70
  48. data/lib/amee/v3/item_value_definition.rb +1 -1
  49. data/lib/amee/v3/return_value_definition.rb +1 -1
  50. data/spec/cache_spec.rb +107 -48
  51. data/spec/connection_spec.rb +224 -183
  52. data/spec/data_item_spec.rb +12 -0
  53. data/spec/data_item_value_history_spec.rb +4 -4
  54. data/spec/data_item_value_spec.rb +2 -2
  55. data/spec/fixtures/AD63A83B4D41.json +1 -1
  56. data/spec/fixtures/AD63A83B4D41.xml +1 -1
  57. data/spec/profile_item_spec.rb +14 -10
  58. data/spec/spec_helper.rb +29 -0
  59. data/spec/v3/connection_spec.rb +77 -65
  60. data/spec/v3/item_value_definition_spec.rb +1 -0
  61. data/spec/v3/return_value_definition_spec.rb +1 -1
  62. metadata +140 -24
  63. data/Gemfile.lock +0 -63
@@ -6,34 +6,32 @@ require 'spec_helper.rb'
6
6
  describe AMEE::Connection do
7
7
 
8
8
  it "requires server name, username, and password" do
9
- flexmock(Net::HTTP).new_instances.should_receive(:start => nil)
9
+
10
10
  lambda{AMEE::Connection.new(nil, nil, nil)}.should raise_error
11
11
  lambda{AMEE::Connection.new(nil, 'username', nil)}.should raise_error
12
12
  lambda{AMEE::Connection.new(nil, nil, 'password')}.should raise_error
13
13
  lambda{AMEE::Connection.new(nil, 'username', 'password')}.should raise_error
14
- lambda{AMEE::Connection.new('server.example.com', nil, nil)}.should raise_error
15
- lambda{AMEE::Connection.new('server.example.com', 'username', nil)}.should raise_error
16
- lambda{AMEE::Connection.new('server.example.com', nil, 'password')}.should raise_error
17
- c = AMEE::Connection.new('server.example.com', 'username', 'password')
14
+ lambda{AMEE::Connection.new('stage.amee.com', nil, nil)}.should raise_error
15
+ lambda{AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, nil)}.should raise_error
16
+ lambda{AMEE::Connection.new('stage.amee.com', nil, 'password')}.should raise_error
17
+ c = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
18
18
  c.should be_valid
19
19
  end
20
20
 
21
21
  it "has default timeout of 60 seconds" do
22
- flexmock(Net::HTTP).new_instances.should_receive(:start => nil)
23
- c = AMEE::Connection.new('server.example.com', 'username', 'password')
22
+
23
+ c = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
24
24
  c.timeout.should be(60)
25
25
  end
26
26
 
27
27
  it "can set timeout" do
28
- flexmock(Net::HTTP).new_instances.should_receive(:start => nil)
29
- c = AMEE::Connection.new('server.example.com', 'username', 'password')
28
+ c = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
30
29
  c.timeout = 30
31
30
  c.timeout.should be(30)
32
31
  end
33
32
 
34
33
  it "can set timeout on creation" do
35
- flexmock(Net::HTTP).new_instances.should_receive(:start => nil)
36
- c = AMEE::Connection.new('server.example.com', 'username', 'password', :timeout => 30)
34
+ c = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD, :timeout => 30)
37
35
  c.timeout.should be(30)
38
36
  end
39
37
 
@@ -41,199 +39,210 @@ end
41
39
 
42
40
  describe AMEE::Connection, "with authentication" do
43
41
 
42
+ describe "using a version 1 api key" do
43
+ use_vcr_cassette "AMEE_Connection_with_authentication/using a v1 key", :record => :new_episodes
44
+ it "detects the API version (1)" do
45
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V1_API_KEY, AMEE_V1_PASSWORD)
46
+ amee.authenticate
47
+ amee.authenticated?.should be_true
48
+ amee.version.should == 1.0
49
+ end
50
+ end
51
+
52
+ describe "using a version 2 api key" do
53
+
44
54
  it "should start out unauthenticated" do
45
- flexmock(Net::HTTP).new_instances.should_receive(:start => nil)
46
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
55
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
47
56
  amee.authenticated?.should be_false
48
57
  end
49
58
 
50
- it "detects the API version (1)" do
51
- flexmock(Net::HTTP).new_instances do |mock|
52
- mock.should_receive(:start => nil)
53
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => '0', :'[]' => 'dummy_auth_token_data'))
54
- mock.should_receive(:finish => nil)
59
+ it "detects the API version (2 - XML) normally" do
60
+ VCR.use_cassette('AMEE_Connection_with_authentication/using_a_v2_key/detects the API version for XML') do
61
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
62
+ amee.authenticate
63
+ amee.authenticated?.should be_true
64
+ amee.version.should == 2.0
55
65
  end
56
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
57
- amee.authenticate
58
- amee.authenticated?.should be_true
59
- amee.version.should == 1.0
60
66
  end
61
67
 
62
- it "detects the API version (2 - XML)" do
63
- flexmock(Net::HTTP).new_instances do |mock|
64
- mock.should_receive(:start => nil)
65
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => '<?xml version="1.0" encoding="UTF-8"?><Resources><SignInResource><Next>/auth</Next><User uid="DB2C6DA7EAA7"><Status>ACTIVE</Status><Type>STANDARD</Type><GroupNames><GroupName>amee</GroupName><GroupName>Standard</GroupName><GroupName>All</GroupName></GroupNames><ApiVersion>2.0</ApiVersion></User></SignInResource></Resources>', :'[]' => 'dummy_auth_token_data'))
66
- mock.should_receive(:finish => nil)
68
+ it "detects the API version (2 - JSON)" do
69
+ VCR.use_cassette('AMEE_Connection_with_authentication/using_a_v2_key/detects the API version for JSON') do
70
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
71
+ amee.authenticate
72
+ amee.authenticated?.should be_true
73
+ amee.version.should == 2.0
67
74
  end
68
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
69
- amee.authenticate
70
- amee.authenticated?.should be_true
71
- amee.version.should == 2.0
72
75
  end
73
76
 
74
- it "detects the API version (2 - JSON)" do
75
- flexmock(Net::HTTP).new_instances do |mock|
76
- mock.should_receive(:start => nil)
77
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => '{ "next" : "/auth","user" : { "apiVersion" : "2.0","groupNames" : [ "amee","Standard","All"],"status" : "ACTIVE","type" : "STANDARD","uid" : "DB2C6DA7EAA7"}}', :'[]' => 'dummy_auth_token_data'))
78
- mock.should_receive(:finish => nil)
79
- end
80
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
81
- amee.authenticate
82
- amee.authenticated?.should be_true
83
- amee.version.should == 2.0
84
77
  end
85
78
 
86
- it "should be able to get private URLs" do
87
- flexmock(Net::HTTP).new_instances do |mock|
88
- mock.should_receive(:start => nil)
89
- mock.should_receive(:request).and_return(flexmock(:code => '401', :body => '')).once
90
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => '', :'[]' => 'dummy_auth_token_data')).once
91
- mock.should_receive(:request).and_return { |request|
92
- if request['authToken'] == 'dummy_auth_token_data'
93
- flexmock(:code => '200', :body => '<?xml version="1.0" encoding="UTF-8"?><Resources><DataCategoryResource><Path/><DataCategory created="2007-07-27 09:30:44.0" modified="2007-07-27 09:30:44.0" uid="CD310BEBAC52"><Name>Root</Name><Path/><Environment uid="5F5887BCF726"/></DataCategory><Children><DataCategories><DataCategory uid="BBA3AC3E795E"><Name>Home</Name><Path>home</Path></DataCategory><DataCategory uid="9E5362EAB0E7"><Name>Metadata</Name><Path>metadata</Path></DataCategory><DataCategory uid="6153F468BE05"><Name>Test</Name><Path>test</Path></DataCategory><DataCategory uid="263FC0186834"><Name>Transport</Name><Path>transport</Path></DataCategory><DataCategory uid="2957AE9B6E6B"><Name>User</Name><Path>user</Path></DataCategory></DataCategories></Children></DataCategoryResource></Resources>')
94
- else
95
- flexmock(:code => '401', :body => '')
96
- end
97
- }.once
98
- mock.should_receive(:finish => nil)
99
- end
100
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
101
- amee.get('/data') do |response|
102
- response.should_not be_empty
79
+ describe "hitting_private_urls" do
80
+ use_vcr_cassette
81
+
82
+ it "should be able to get private URLs" do
83
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
84
+ amee.get('/data') do |response|
85
+ response.should_not be_empty
86
+ end
87
+ amee.authenticated?.should be_true
103
88
  end
104
- amee.authenticated?.should be_true
105
89
  end
106
90
 
107
- it "should handle 404s gracefully" do
108
- flexmock(Net::HTTP).new_instances.should_receive(:start => nil, :request => flexmock(:code => '404', :body => ""), :finish => nil)
109
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
110
- lambda{amee.get('/missing_url')}.should raise_error(AMEE::NotFound, "The URL was not found on the server.\nRequest: GET /missing_url")
91
+ describe "handling 404s" do
92
+ use_vcr_cassette
93
+ it "should handle 404s gracefully" do
94
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
95
+ lambda{amee.get('/missing_url')}.should raise_error(AMEE::NotFound, "The URL was not found on the server.\nRequest: GET /missing_url")
96
+ end
111
97
  end
112
98
 
113
- it "should raise error if permission for operation is denied" do
114
- flexmock(Net::HTTP).new_instances do |mock|
115
- mock.should_receive(:start => nil)
116
- mock.should_receive(:request).and_return(flexmock(:code => '403', :body => '{}'))
117
- mock.should_receive(:finish => nil)
99
+ describe "raising errors if permission denied" do
100
+ use_vcr_cassette
101
+
102
+ it "should raise error if permission for operation is denied" do
103
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
104
+ lambda {
105
+ amee.get('/data/lca/ecoinvent')
106
+ }.should raise_error(AMEE::PermissionDenied)
118
107
  end
119
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
120
- lambda {
121
- amee.get('/data')
122
- }.should raise_error(AMEE::PermissionDenied,"You do not have permission to perform the requested operation.
123
- Request: GET /data
124
- Response: {}")
125
108
  end
126
109
 
127
- it "should raise error if authentication succeeds, but permission for operation is denied" do
128
- flexmock(Net::HTTP).new_instances do |mock|
129
- mock.should_receive(:start => nil)
130
- mock.should_receive(:request).and_return(flexmock(:code => '401', :body => ''),
131
- flexmock(:code => '200', :body => '', :'[]' => 'dummy_auth_token_data'),
132
- flexmock(:code => '403', :body => '{}'))
133
- mock.should_receive(:finish => nil)
110
+ describe "raising unhandled errors" do
111
+ it "should raise error if unhandled errors occur in connection" do
112
+
113
+ @error_response = {
114
+ :status => 500,
115
+ :body => ""
116
+ }
117
+
118
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
119
+
120
+ VCR.use_cassette("AMEE_Connection/v2/raising unhandled errors") do
121
+ amee.authenticate
122
+ end
123
+ stub_request(:any, "https://stage.amee.com/data").to_return(@error_response)
124
+
125
+ lambda {
126
+ amee.get('/data')
127
+ }.should raise_error(AMEE::UnknownError)
134
128
  end
135
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
136
- lambda {
137
- amee.get('/data')
138
- }.should raise_error(AMEE::PermissionDenied,"You do not have permission to perform the requested operation.
139
- Request: GET /data
140
- Response: {}")
141
- amee.authenticated?.should be_true
142
129
  end
143
130
 
144
- it "should raise error if unhandled errors occur in connection" do
145
- flexmock(Net::HTTP).new_instances do |mock|
146
- mock.should_receive(:start => nil)
147
- mock.should_receive(:request).and_return(flexmock(:code => '500', :body => '{}'))
148
- mock.should_receive(:finish => nil)
131
+ it "should re-authenticate and refresh authtoken when authtoken expires" do
132
+ VCR.use_cassette('AMEE_Connection_with_authentication/should re-authenticate and refresh authtoken when authtoken expires') do
133
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
134
+ amee.auth_token = 'old_token'
135
+ amee.get('/profiles')
136
+ amee.auth_token.should_not eql 'old_token'
137
+ end
138
+ end
139
+
140
+ it "should refresh authtoken when authtoken is changed" do
141
+ VCR.use_cassette('AMEE_Connection_with_authentication/should refresh authtoken when authtoken is changed') do
142
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
143
+ amee.get('/profiles')
144
+ old_token = amee.auth_token
145
+ #sleep(65) # Wait 65 seconds so platform changes auth token. This is only required when generating the cassette.
146
+ amee.get('/profiles')
147
+ amee.auth_token.should_not eql old_token
149
148
  end
150
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
151
- lambda {
152
- amee.get('/data')
153
- }.should raise_error(AMEE::UnknownError,"An error occurred while talking to AMEE: HTTP response code 500.
154
- Request: GET /data
155
- Response: {}")
156
149
  end
157
150
 
158
151
  end
159
152
 
160
153
  describe AMEE::Connection, "with retry enabled" do
161
-
154
+ use_vcr_cassette
162
155
  [
163
- Timeout::Error,
164
- Errno::EINVAL,
165
- Errno::ECONNRESET,
166
- EOFError,
167
- Net::HTTPBadResponse,
168
- Net::HTTPHeaderSyntaxError,
169
- Net::ProtocolError
156
+ AMEE::TimeOut,
170
157
  ].each do |e|
171
158
 
172
- it "should retry after #{e.name} the correct number of times" do
173
- flexmock(Net::HTTP).new_instances do |mock|
174
- mock.should_receive(:start => nil)
175
- mock.should_receive(:request).and_raise(e.new).twice
176
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => '{}')).once
177
- mock.should_receive(:finish => nil)
159
+ before(:each) do
160
+
161
+ @amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD, :retries => 2)
162
+
163
+ VCR.use_cassette("AMEE_Connection/v2/raising unhandled errors") do
164
+ @amee.authenticate
178
165
  end
179
- amee = AMEE::Connection.new('server.example.com', 'username', 'password', :retries => 2)
166
+
167
+ end
168
+
169
+ it "should retry after #{e.name} the correct number of times" do
170
+
171
+ @error_response = {
172
+ :status => 408,
173
+ :body => ""
174
+ }
175
+
176
+
177
+ stub_request(:any, "https://stage.amee.com/data").to_return(@error_response).times(2).
178
+ then.to_return(:status => 200, :body => {})
179
+
180
180
  lambda {
181
- amee.get('/data')
182
- }.should_not raise_error
181
+ @amee.get('/data')
182
+ }.should_not raise_error
183
183
  end
184
184
 
185
185
  it "should retry #{e.name} the correct number of times and raise error on failure" do
186
- flexmock(Net::HTTP).new_instances do |mock|
187
- mock.should_receive(:start => nil)
188
- mock.should_receive(:request).and_raise(e.new).times(3)
189
- mock.should_receive(:finish => nil)
190
- end
191
- amee = AMEE::Connection.new('server.example.com', 'username', 'password', :retries => 2)
186
+
187
+ @error_response = {
188
+ :status => 408,
189
+ :body => ""
190
+ }
191
+ stub_request(:any, "https://stage.amee.com/data").to_return(@error_response).times(3)
192
+
192
193
  lambda {
193
- amee.get('/data')
194
+ @amee.get('/data')
194
195
  }.should raise_error(e)
195
196
  end
196
197
  end
197
-
198
+
198
199
  [
199
- '502',
200
- '503',
201
- '504'
200
+ 502,
201
+ 503,
202
+ 504
202
203
  ].each do |e|
203
204
 
205
+ # binding.pry
206
+
207
+ before(:each) do
208
+ VCR.use_cassette("AMEE_Connection/v2/raising unhandled errors") do
209
+ @amee.authenticate
210
+ end
211
+
212
+ @error_response = {
213
+ :status => e,
214
+ :body => {}
215
+ }
216
+ @amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD, :retries => 2)
217
+
218
+ end
219
+
204
220
  it "should retry after #{e} the correct number of times" do
205
- flexmock(Net::HTTP).new_instances do |mock|
206
- mock.should_receive(:start => nil)
207
- mock.should_receive(:request).and_return(flexmock(:code => e, :body => '{}')).twice
208
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => '{}')).once
209
- mock.should_receive(:finish => nil)
210
- end
211
- amee = AMEE::Connection.new('server.example.com', 'username', 'password', :retries => 2)
221
+
222
+ stub_request(:any, "https://stage.amee.com/data").to_return(:status => e, :body => {}).times(2).
223
+ then.to_return(:status => 200, :body => {})
224
+
212
225
  lambda {
213
- amee.get('/data')
214
- }.should_not raise_error
226
+ @amee.get('/data')
227
+ }.should_not raise_error
215
228
  end
216
229
 
217
230
  it "should retry #{e} the correct number of times and raise error on failure" do
218
- flexmock(Net::HTTP).new_instances do |mock|
219
- mock.should_receive(:start => nil)
220
- mock.should_receive(:request).and_return(flexmock(:code => e, :body => '{}')).times(3)
221
- mock.should_receive(:finish => nil)
222
- end
223
- amee = AMEE::Connection.new('server.example.com', 'username', 'password', :retries => 2)
231
+
232
+ stub_request(:any, "https://stage.amee.com/data").to_return(@error_response)
224
233
  lambda {
225
- amee.get('/data')
234
+ @amee.get('/data')
226
235
  }.should raise_error(AMEE::ConnectionFailed)
227
236
  end
228
- end
229
-
237
+ end
238
+
230
239
  end
231
240
 
232
241
  describe AMEE::Connection, "with incorrect server name" do
233
242
 
243
+ use_vcr_cassette
234
244
  it "should raise a useful error" do
235
- flexmock(Net::HTTP).new_instances.should_receive(:start).and_raise(SocketError.new)
236
- amee = AMEE::Connection.new('badservername.example.com', 'username', 'password')
245
+ amee = AMEE::Connection.new('badservername.example.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
237
246
  lambda{
238
247
  amee.get('/')
239
248
  }.should raise_error(AMEE::ConnectionFailed, "Connection failed. Check server name or network connection.")
@@ -243,56 +252,88 @@ end
243
252
 
244
253
  describe AMEE::Connection, "with bad authentication information" do
245
254
 
246
- it "should be capable of making requests for public URLs" do
247
- flexmock(Net::HTTP).new_instances.should_receive(:start => nil, :request => flexmock(:code => '200', :body => ""), :finish => nil)
248
- amee = AMEE::Connection.new('server.example.com', 'wrong', 'details')
249
- lambda{amee.get('/')}.should_not raise_error
255
+ describe "hitting a private url" do
256
+ use_vcr_cassette
257
+ it "should get an authentication failure" do
258
+ amee = AMEE::Connection.new('stage.amee.com', 'wrong', 'details')
259
+ lambda{
260
+ amee.get('/data')
261
+ }.should raise_error(AMEE::AuthFailed, "Authentication failed. Please check your username and password. (tried wrong,details)")
262
+ end
250
263
  end
251
264
 
252
- it "should get an authentication failure when accessing private URLs" do
253
- flexmock(Net::HTTP).new_instances.should_receive(:start => nil, :request => flexmock(:code => '401', :body => "", :'[]' => nil), :finish => nil)
254
- amee = AMEE::Connection.new('server.example.com', 'wrong', 'details')
255
- lambda{amee.get('/data')}.should raise_error(AMEE::AuthFailed, "Authentication failed. Please check your username and password. (tried wrong,details)")
265
+ # according to the docs, public urls should not be accessible without
266
+ # an api key either
267
+ # http://www.amee.com/developer/docs/apc.php#auth-reference
268
+ describe "hitting a public url" do
269
+ use_vcr_cassette
270
+ it "should not be capable of making requests for public URLs" do
271
+ amee = AMEE::Connection.new('stage.amee.com', 'wrong', 'details')
272
+ lambda{
273
+ amee.get('/')
274
+ }.should raise_error
275
+ end
256
276
  end
257
277
 
278
+
279
+
258
280
  end
259
281
 
260
282
  describe AMEE::Connection, "with authentication , doing write-requests" do
261
-
283
+ use_vcr_cassette
262
284
  it "should be able to send post requests" do
263
- flexmock(Net::HTTP).new_instances do |mock|
264
- mock.should_receive(:start => nil)
265
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => ''))
266
- mock.should_receive(:finish => nil)
267
- end
268
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
269
- amee.post('/profiles', :test => 1, :test2 => 2) do |response|
270
- response.should be_empty
285
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
286
+ amee.post('/profiles', {:profile => true}) do |response|
287
+ response.code.should be_200
271
288
  end
272
289
  end
273
290
 
274
- it "should be able to send put requests" do
275
- flexmock(Net::HTTP).new_instances do |mock|
276
- mock.should_receive(:start => nil)
277
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => ''))
278
- mock.should_receive(:finish => nil)
279
- end
280
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
281
- amee.put('/profiles/ABC123', :test => 1, :test2 => 2) do |response|
282
- response.should be_empty
283
- end
284
- end
291
+ describe "working with an existing profile" do
292
+
293
+ describe "sending updates to existing items" do
294
+ use_vcr_cassette
295
+ it "should be possible" do
285
296
 
286
- it "should be able to send delete requests" do
287
- flexmock(Net::HTTP).new_instances do |mock|
288
- mock.should_receive(:start => nil)
289
- mock.should_receive(:request).and_return(flexmock(:code => '200', :body => ''))
290
- mock.should_receive(:finish => nil)
297
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
298
+
299
+ profile_creation_post = amee.post('/profiles', {:profile => true})
300
+ profile = JSON.parse(profile_creation_post.body)['profile']['uid']
301
+
302
+ # fetch the new_item type uid to create
303
+ new_item_drill = amee.get('/data/test/apitests/drill')
304
+ new_item_uid = JSON.parse(new_item_drill.body)['choices']['choices'].first['value']
305
+ # create a new item
306
+ new_profile = amee.post("/profiles/#{profile}/test/apitests", :dataItemUid => new_item_uid)
307
+ # update the new item
308
+ path = new_profile.headers_hash['location'].gsub('https://stage.amee.com', '')
309
+ amee.put(path, :dataItemUid => new_item_uid) do |response|
310
+ response.should be_empty
311
+ end
312
+ end
291
313
  end
292
- amee = AMEE::Connection.new('server.example.com', 'username', 'password')
293
- amee.delete('/profiles/ABC123') do |response|
294
- response.should be_empty
314
+
315
+ describe "deleting existing items" do
316
+ use_vcr_cassette
317
+ it "should also be possible" do
318
+
319
+ amee = AMEE::Connection.new('stage.amee.com', AMEE_V2_API_KEY, AMEE_V2_PASSWORD)
320
+ profile_creation_post = amee.post('/profiles', {:profile => true})
321
+ profile = JSON.parse(profile_creation_post.body)['profile']['uid']
322
+
323
+ # fetch the new_item type uid to create
324
+ new_item_drill = amee.get('/data/test/apitests/drill')
325
+ new_item_uid = JSON.parse(new_item_drill.body)['choices']['choices'].first['value']
326
+ # create a new item
327
+ new_profile = amee.post("/profiles/#{profile}/test/apitests", :dataItemUid => new_item_uid)
328
+ # delete the new item
329
+ path = new_profile.headers_hash['location'].gsub('https://stage.amee.com', '')
330
+ amee.delete(path) do |response|
331
+ response.should be_empty
332
+ end
333
+ end
295
334
  end
335
+
336
+
296
337
  end
297
338
 
298
339
  end