spec/unit/connection_spec.rb in syncano-4.0.0.alpha4 vs spec/unit/connection_spec.rb in syncano-4.0.0.pre
- old
+ new
@@ -5,70 +5,110 @@
context 'api_key' do
specify { expect(described_class.new(api_key: 'fafarafa')).to be_authenticated }
specify { expect(described_class.new).to_not be_authenticated }
end
- describe '#authenticate' do
- subject { described_class.new email: email, password: password }
+ describe '#request' do
+ let(:api_key) { '87a7da987da98sd7a98' }
- let(:authenticate_uri) { endpoint_uri('account/auth/') }
- let(:email) { 'kiszka@koza.com' }
- let(:password) { 'kiszonka' }
- let(:success_status) { 200 }
- let(:unauthorized_status) { 401 }
+ subject { described_class.new(api_key: api_key) }
- context 'successful' do
+ context 'called with unsupported method' do
+ specify do
+ expect { subject.request :koza, 'fafarafa' }.
+ to raise_error(RuntimeError, 'Unsupported method "koza"')
+ end
+ end
+
+ context 'called with supported method' do
before do
- expect(subject).to receive(:request).
- with(:post, described_class::AUTH_PATH, email: email, password: password).
- and_return('account_key' => 'kEy')
+ stub_request(:get, endpoint_uri('somepath/')).
+ with(headers: {'X-Api-Key'=>'87a7da987da98sd7a98'}).
+ to_return(body: generate_body(some: 'response'))
end
specify do
- expect { subject.authenticate }.to change { subject.authenticated? }
+ expect(subject.request(:get, 'somepath/')).to eq('some' => 'response')
end
end
- context 'failed' do
+ context 'called with supported method returning an error' do
before do
- expect(subject).to receive(:request).and_raise('auth failed')
+ stub_request(:post, endpoint_uri('instances/')).
+ with(body: { 'name' => 'koza' },
+ headers: {'X-Api-Key'=>'87a7da987da98sd7a98'}).
+ to_return(body: generate_body({name: ['This field can not be "koza"']}),
+ status: 400)
+ end
+ specify do
+ expect { subject.request(:post, '/v1/instances/', { name: "koza" }) }.
+ to raise_error(Syncano::ClientError)
end
+ end
+ context 'successful returning empty body' do
+ before do
+ stub_request(:delete, endpoint_uri('instances/kiszonka/')).
+ with(headers: {'X-Api-Key'=>'87a7da987da98sd7a98'}).
+ to_return(body: nil, status: 204)
+ end
+
specify do
- expect { subject.authenticate }.to raise_error('auth failed')
+ expect { subject.request(:delete, '/v1/instances/kiszonka/', {}) }.
+ to_not raise_error
end
+
end
end
- describe '#request' do
- let(:headers) { { 'X-Api-Key'=>'87a7da987da98sd7a98', 'User-Agent' => "Syncano Ruby Gem #{Syncano::VERSION}" } }
- let(:api_key) { '87a7da987da98sd7a98' }
- let(:connection_params) { { api_key: api_key, user_key: 'Us3rK3y' } }
- let(:raw_response) { { body: 'koza' } }
- let(:handled_response) { double :handled_response }
+ describe '#authenticate' do
+ subject { described_class.new }
- subject { described_class.new(connection_params) }
+ let(:authenticate_uri) { endpoint_uri('account/auth/') }
+ let(:email) { 'kiszka@koza.com' }
+ let(:password) { 'kiszonka' }
+ let(:success_status) { 200 }
+ let(:unauthorized_status) { 401 }
- context 'with supported method' do
+ context 'successful' do
before do
- stub_request(:get, endpoint_uri('user/method/')).
- with(headers: headers).to_return(raw_response)
+ stub_request(:post, authenticate_uri).
+ with(body: { 'email' => email, 'password' => password } ).
+ to_return(body: successful_body, status: success_status)
+ end
- expect(Syncano::Response).
- to receive(:handle) { |raw_response| expect(raw_response.body).to eq('koza') }.
- and_return(handled_response)
+ it 'should get an API key' do
+ expect { subject.authenticate(email, password) }.to change { subject.authenticated? }
end
- specify do
- expect(subject.request(:get, 'user/method/')).to eq(handled_response)
+ def successful_body
+ generate_body id: 15,
+ email: email,
+ first_name: '',
+ last_name: '',
+ account_key: 'kozakoza123'
end
end
- context 'with unsupported method' do
- specify do
- expect { subject.request :koza, 'fafarafa' }.
- to raise_error(RuntimeError, 'Unsupported method "koza"')
+ context 'failed' do
+ before do
+ stub_request(:post, authenticate_uri).
+ with(body: { 'email' => email, 'password' => password }).
+ to_return(body: failed_body, status: unauthorized_status)
+
end
+
+ it 'should raise an exception' do
+ expect { subject.authenticate(email, password) }.to raise_error(Syncano::ClientError)
+ end
+
+ def failed_body
+ generate_body detail: 'Invalid email or password.'
+ end
end
+ end
+
+ def generate_body(params)
+ JSON.generate params
end
end