spec/api_spec.rb in clickatell-0.3.0 vs spec/api_spec.rb in clickatell-0.4.0

- old
+ new

@@ -29,123 +29,106 @@ url.should == URI.parse("https://api.clickatell.com/http/cmdname?param_one=abc&param_two=123") end end describe "Command executor" do - it "should create an API command and send it via HTTP get, returning the raw http response" do + it "should create an API command with auth params and send it via HTTP get, returning the raw http response" do + executor = API::CommandExecutor.new(:session_id => '12345') API::Command.should_receive(:new).with('cmdname').and_return(cmd=mock('command')) - cmd.should_receive(:with_params).with(:param_one => 'foo').and_return(uri=mock('uri')) + cmd.should_receive(:with_params).with(:param_one => 'foo', :session_id => '12345').and_return(uri=mock('uri')) Net::HTTP.should_receive(:get_response).with(uri).and_return(raw_response=mock('http response')) - API.send(:execute_command, 'cmdname', :param_one => 'foo').should == raw_response + executor.execute('cmdname', :param_one => 'foo').should == raw_response end end describe "API" do + before do + API.debug_mode = false + API::CommandExecutor.should_receive(:new).with({:session_id => '1234'}, false).and_return(@executor = mock('command executor')) + @api = API.new(:session_id => '1234') + end + it "should return session_id for successful authentication" do - API.should_receive(:execute_command).with('auth', + @executor.should_receive(:execute).with('auth', :api_id => '1234', :user => 'joebloggs', :password => 'superpass' ).and_return(response=mock('response')) Response.should_receive(:parse).with(response).and_return('OK' => 'new_session_id') - API.authenticate('1234', 'joebloggs', 'superpass').should == 'new_session_id' + @api.authenticate('1234', 'joebloggs', 'superpass').should == 'new_session_id' end it "should support ping" do - API.should_receive(:execute_command).with('ping', :session_id => 'abcdefg').and_return(response=mock('response')) - API.ping('abcdefg').should == response + @executor.should_receive(:execute).with('ping', :session_id => 'abcdefg').and_return(response=mock('response')) + @api.ping('abcdefg').should == response end - it "should support sending messages with authentication, returning the message id" do - API.should_receive(:execute_command).with('sendmsg', - :api_id => '1234', - :user => 'joebloggs', - :password => 'superpass', + it "should support sending messages, returning the message id" do + @executor.should_receive(:execute).with('sendmsg', :to => '4477791234567', :text => 'hello world' ).and_return(response=mock('response')) Response.should_receive(:parse).with(response).and_return('ID' => 'message_id') - API.send_message('4477791234567', 'hello world', - :username => 'joebloggs', :password => 'superpass', :api_key => '1234' - ).should == 'message_id' + @api.send_message('4477791234567', 'hello world').should == 'message_id' end - it "should support sending messages with pre-auth, returning the message id" do - API.should_receive(:execute_command).with('sendmsg', - :session_id => 'abcde', - :to => '4477791234567', - :text => 'hello world' - ).and_return(response=mock('response')) - Response.should_receive(:parse).with(response).and_return('ID' => 'message_id') - API.send_message('4477791234567', 'hello world', :session_id => 'abcde').should == 'message_id' - end - it "should support sending messages with custom from number, returning the message id" do - API.should_receive(:execute_command).with('sendmsg', - :session_id => 'abcde', + @executor.should_receive(:execute).with('sendmsg', :to => '4477791234567', :text => 'hello world', :from => 'LUKE' ).and_return(response=mock('response')) Response.should_receive(:parse).with(response).and_return('ID' => 'message_id') - API.send_message('4477791234567', 'hello world', {:session_id => 'abcde'}, :from => 'LUKE') + @api.send_message('4477791234567', 'hello world', :from => 'LUKE') end it "should ignore any invalid parameters when sending message" do - API.should_receive(:execute_command).with('sendmsg', - :session_id => 'abcde', + @executor.should_receive(:execute).with('sendmsg', :to => '4477791234567', :text => 'hello world', :from => 'LUKE' ).and_return(response=mock('response')) Response.stub!(:parse).and_return('ID' => 'foo') - API.send_message('4477791234567', 'hello world', {:session_id => 'abcde'}, :from => 'LUKE', :any_old_param => 'test') + @api.send_message('4477791234567', 'hello world', :from => 'LUKE', :any_old_param => 'test') end - it "should support message status query with authentication, returning message status" do - API.should_receive(:execute_command).with('querymsg', - :api_id => '1234', - :user => 'joebloggs', - :password => 'superpass', + it "should support message status query, returning message status" do + @executor.should_receive(:execute).with('querymsg', :apimsgid => 'messageid' ).and_return(response=mock('response')) Response.should_receive(:parse).with(response).and_return('ID' => 'message_id', 'Status' => 'message_status') - API.message_status('messageid', - :username => 'joebloggs', :password => 'superpass', :api_key => '1234' - ).should == 'message_status' + @api.message_status('messageid').should == 'message_status' end - - it "should support message status query with pre-auth" do - API.should_receive(:execute_command).with('querymsg', - :session_id => 'abcde', - :apimsgid => 'messageid' - ).and_return(response=mock('response')) - Response.should_receive(:parse).with(response).and_return('ID' => 'message_id', 'Status' => 'message_status') - API.message_status('messageid', :session_id => 'abcde').should == 'message_status' - end - it "should support balance query with authentication, returning number of credits as a float" do - API.should_receive(:execute_command).with('getbalance', - :api_id => '1234', - :user => 'joebloggs', - :password => 'superpass' - ).and_return(response=mock('response')) + it "should support balance query, returning number of credits as a float" do + @executor.should_receive(:execute).with('getbalance', {}).and_return(response=mock('response')) Response.should_receive(:parse).with(response).and_return('Credit' => '10.0') - API.account_balance(:username => 'joebloggs', :password => 'superpass', :api_key => '1234').should == 10.0 + @api.account_balance.should == 10.0 end - it "should support balance query with pre-auth, returning number of credits as a float" do - API.should_receive(:execute_command).with('getbalance', - :session_id => 'abcde' - ).and_return(response=mock('response')) - Response.should_receive(:parse).with(response).and_return('Credit' => '10.0') - API.account_balance(:session_id => 'abcde').should == 10.0 - end - it "should raise an API::Error if the response parser raises" do - API.stub!(:execute_command) + @executor.stub!(:execute) Response.stub!(:parse).and_raise(Clickatell::API::Error.new('', '')) - proc { API.account_balance({}) }.should raise_error(Clickatell::API::Error) + proc { @api.account_balance }.should raise_error(Clickatell::API::Error) + end + end + + describe API, ' when authenticating' do + it "should authenticate to retrieve a session_id and return a new API instance using that session id" do + API.stub!(:new).and_return(api=mock('api')) + api.should_receive(:authenticate).with('my_api_key', 'joebloggs', 'mypassword').and_return('new_session_id') + api.should_receive(:auth_options=).with(:session_id => 'new_session_id') + API.authenticate('my_api_key', 'joebloggs', 'mypassword') + end + end + + describe API, ' with no authentication options set' do + it "should build commands with no authentication options" do + API.debug_mode = false + api = API.new + API::CommandExecutor.should_receive(:new).with({}, false).and_return(executor=mock('command executor')) + executor.stub!(:execute) + api.ping('1234') end end describe "API Error" do it "should parse http response string to create error" do \ No newline at end of file