spec/unit/connection_spec.rb in syncano-4.0.0.alpha1 vs spec/unit/connection_spec.rb in syncano-4.0.0.alpha2
- old
+ new
@@ -5,133 +5,70 @@
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 '#request' do
- let(:headers) { { 'X-Api-Key'=>'87a7da987da98sd7a98', 'User-Agent' => "Syncano Ruby Gem #{Syncano::VERSION}" } }
- let(:api_key) { '87a7da987da98sd7a98' }
+ describe '#authenticate' do
+ subject { described_class.new email: email, password: password }
- subject { described_class.new(api_key: api_key) }
+ let(:authenticate_uri) { endpoint_uri('account/auth/') }
+ let(:email) { 'kiszka@koza.com' }
+ let(:password) { 'kiszonka' }
+ let(:success_status) { 200 }
+ let(:unauthorized_status) { 401 }
- 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
+ context 'successful' do
before do
- stub_request(:get, endpoint_uri('somepath/')).
- with(headers: headers).
- to_return(body: generate_body(some: 'response'))
+ expect(subject).to receive(:request).
+ with(:post, described_class::AUTH_PATH, email: email, password: password).
+ and_return('account_key' => 'kEy')
end
specify do
- expect(subject.request(:get, 'somepath/')).to eq('some' => 'response')
+ expect { subject.authenticate }.to change { subject.authenticated? }
end
end
- context 'called with supported method returning a a client error' do
+ context 'failed' do
before do
- stub_request(:post, endpoint_uri('instances/')).
- with(body: { 'name' => 'koza' },
- headers: headers).
- to_return(body: generate_body({name: ['This field can not be "koza"']}),
- status: 400)
- end
+ expect(subject).to receive(:request).and_raise('auth failed')
- specify do
- expect { subject.request(:post, '/v1/instances/', { name: "koza" }) }.
- to raise_error(Syncano::ClientError)
end
- end
- context 'returning a server error' do
- before do
- stub_request(:get, endpoint_uri('error_prone/')).
- to_return(body: 'An error occured', status: 500)
- end
-
specify do
- expect { subject.request(:get, endpoint_uri('error_prone/'), nil) }.
- to raise_error(Syncano::ServerError)
+ expect { subject.authenticate }.to raise_error('auth failed')
end
end
-
- context 'returning unsupported status code' do
- before do
- stub_request(:get, endpoint_uri('weird/')).to_return(body: 'O HAI!', status: 101)
- end
-
- specify do
- expect { subject.request(:get, endpoint_uri('weird/')) }.to raise_error(Syncano::UnsupportedStatusError)
- end
- end
-
- context 'successful returning empty body' do
- before do
- stub_request(:delete, endpoint_uri('instances/kiszonka/')).
- with(headers: headers).
- to_return(body: nil, status: 204)
- end
-
- specify do
- expect { subject.request(:delete, '/v1/instances/kiszonka/', {}) }.
- to_not raise_error
- end
-
- end
end
- describe '#authenticate' do
- subject { described_class.new }
+ 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 }
- 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(connection_params) }
- context 'successful' do
+ context 'with supported method' do
before do
- stub_request(:post, authenticate_uri).
- with(body: { 'email' => email, 'password' => password } ).
- to_return(body: successful_body, status: success_status)
- end
+ stub_request(:get, endpoint_uri('user/method/')).
+ with(headers: headers).to_return(raw_response)
- it 'should get an API key' do
- expect { subject.authenticate(email, password) }.to change { subject.authenticated? }
+ expect(Syncano::Response).
+ to receive(:handle) { |raw_response| expect(raw_response.body).to eq('koza') }.
+ and_return(handled_response)
end
- def successful_body
- generate_body id: 15,
- email: email,
- first_name: '',
- last_name: '',
- account_key: 'kozakoza123'
+ specify do
+ expect(subject.request(:get, 'user/method/')).to eq(handled_response)
end
end
- context 'failed' do
- before do
- stub_request(:post, authenticate_uri).
- with(body: { 'email' => email, 'password' => password }).
- to_return(body: failed_body, status: unauthorized_status)
-
+ context 'with unsupported method' do
+ specify do
+ expect { subject.request :koza, 'fafarafa' }.
+ to raise_error(RuntimeError, 'Unsupported method "koza"')
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