spec/zeppelin_spec.rb in zeppelin-0.7.0 vs spec/zeppelin_spec.rb in zeppelin-0.8.1

- old
+ new

@@ -549,9 +549,116 @@ subject.remove_tag_from_device(device_token, tag_name) }.to raise_error(Zeppelin::ResourceNotFound) end end + describe '#tags' do + let(:uri) { '/api/tags/' } + + let(:response_body) { { :tags => %w[foo bar baz] } } + + it 'is true when request is successful' do + stub_requests do |stub| + stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(response_body)] } + end + + subject.tags + end + + it 'raises an error when the request fails' do + stub_requests do |stub| + stub.get(uri) { [500, {}, ''] } + end + + expect { + subject.tags + }.to raise_error(Zeppelin::ClientError) + end + end + + describe '#register_pin' do + let(:pin) { '12345678' } + + let(:uri) { "/api/device_pins/#{pin}/" } + + it 'is true when the request is successful' do + stub_requests do |stub| + stub.put(uri) { [201, {}, ''] } + end + + subject.register_pin(pin).should be_true + end + + it 'raises an error when the request fails' do + stub_requests do |stub| + stub.put(uri) { [500, {}, ''] } + end + + expect { + subject.register_pin(pin) + }.to raise_error(Zeppelin::ClientError) + end + end + + describe '#pin' do + let(:pin) { '12345678' } + + let(:uri) { "/api/device_pins/#{pin}/" } + + let(:response_body) { + { + :device_pin => pin, + :alias => 'my alias', + :last_registration => Time.mktime(2009, 11, 6, 20, 41, 6), + :created => Time.mktime(2009, 11, 6, 20, 41, 6), + :active => true, + :tags => %w[one two] + } + } + + it 'responds with information about a device with a PIN when successful' do + stub_requests do |stub| + stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, response_body] } + end + + subject.pin(pin).should eql(response_body) + end + + it 'raises an error when the PIN resource cannot be found' do + stub_requests do |stub| + stub.get(uri) { [404, {}, ''] } + end + + expect { + subject.pin(pin) + }.to raise_error(Zeppelin::ResourceNotFound) + end + end + + describe '#delete_pin' do + let(:pin) { '12345678' } + + let(:uri) { "/api/device_pins/#{pin}/" } + + it 'is true when the request succeeds' do + stub_requests do |stub| + stub.delete(uri) { [204, {}, ''] } + end + + subject.delete_pin(pin).should be_true + end + + it 'raises an error when the request fails' do + stub_requests do |stub| + stub.delete(uri) { [404, {}, ''] } + end + + expect { + subject.delete_pin(pin) + }.to raise_error(Zeppelin::ResourceNotFound) + end + end + def stub_requests subject.connection.builder.handlers.delete(Faraday::Adapter::NetHttp) subject.connection.adapter :test do |stubs| yield(stubs) end