require File.expand_path('../spec_helper', __FILE__) describe Yammer::Client do before :all do @configuration = { :site_url => 'https://www.yammer.com', :client_id => 'PRbTcg9qjgKsp4jjpm1pw', :client_secret => 'Xn7kp7Ly0TCY4GtZWkmSsqGEPg10DmMADyjWkf2U', :access_token => 'TolNOFka9Uls2DxahNi78A' } end subject { Yammer::Client.new(@configuration) } context "with module configuration" do before :each do Yammer.configure do |config| Yammer::Configurable.keys.each do |key| config.send("#{key}=", key) end end end after do Yammer.reset! end it "inherits the module configuration" do client = Yammer::Client.new Yammer::Configurable.keys.each do |key| expect(client.instance_variable_get(:"@#{key}")).to eq key end end context "with initialization options" do before(:all) do @conf = { :site_url => 'https://www.yammer.com', :client_id => 'PRbTcg9qjgKsp4jjpm1pw', :client_secret => 'Xn7kp7Ly0TCY4GtZWkmSsqGEPg10DmMADyjWkf2U', :access_token => 'TolNOFka9Uls2DxahNi78A', :http_adapter => Struct.new('CustomHttpAdapter', :site_url, :conn_options), :connection_options => { :max_redirects => 5, :use_ssl => true } } end context "during initialization" do it "overrides the module configuration" do client = Yammer::Client.new(@conf) Yammer::Configurable.keys.each do |key| expect(client.instance_variable_get(:"@#{key}")).to eq @conf[key] end end end context "after initialization" do it "overrides the module configuration" do client = Yammer::Client.new client.configure do |config| @conf.each do |key, value| config.send("#{key}=", value) end end Yammer::Configurable.keys.each do |key| expect(client.instance_variable_get(:"@#{key}")).to eq @conf[key] end end end end end describe "connection" do it "looks like an http connection" do conn = subject.instance_eval{ connection } expect(conn).to respond_to(:send_request) end it "gets memoized" do c1, c2 = subject.instance_eval{ connection }, subject.instance_eval{ connection } expect(c1.object_id).to eq c2.object_id end end describe "site_url" do it "returns site_url" do expect(subject.site_url).to eq 'https://www.yammer.com' end end describe "site_url=" do before do subject.site_url = 'elpmaxe.com' end it "sets the connection to nil" do expect(subject.instance_variable_get(:'@connection')).to eq nil end it "sets new site_url on client" do expect(subject.site_url).to eq 'elpmaxe.com' end end describe "#connection_options" do context "with default connection options" do it "returns empty hash" do expect(subject.connection_options).to eq ({ :max_redirects => 5, :use_ssl => true }) end end context "with custom connection options" do it "returns default options" do subject.connection_options = { :max_redirects => 5, :use_ssl => true } expect(subject.connection_options).to eq ({:max_redirects => 5, :use_ssl => true}) end end end context 'http request' do describe "#post" do it "makes an http POST request" do stub_post('/users').with( :headers => { 'Accept'=>'application/json', 'Authorization'=>'Bearer TolNOFka9Uls2DxahNi78A', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>"Yammer Ruby Gem #{Yammer::Version}" }, :body => { :name => 'alice' }) subject.post('/users', { :name => 'alice'}) end end describe "#put" do it "makes an http PUT request" do stub_put('/users/1').with( :headers => { 'Accept'=>'application/json', 'Authorization'=>'Bearer TolNOFka9Uls2DxahNi78A', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=> "Yammer Ruby Gem #{Yammer::Version}" }, :body => { :name => 'bob' }) subject.put('/users/1', { :name => 'bob'}) end end describe "#get" do it "makes an http GET request" do stub_get('/users/1').with(:headers => { 'Authorization' => 'Bearer TolNOFka9Uls2DxahNi78A' }) subject.get('/users/1') end end describe "#delete" do it "makes an http DELETE request" do stub_delete('/users/1').with(:headers => { 'Authorization' => 'Bearer TolNOFka9Uls2DxahNi78A' }) subject.delete('/users/1') end end describe 'with invalid token' do it 'raises exception' do stub_get('/users/1').with(:headers => { 'Authorization' => 'Bearer TolNOFka9Uls2DxahNi78A' }).to_return( :body => '{ "response": { "message": "Token not found.", "code": 16, "stat": "fail" } }', :status => 401 ) expect { subject.get('/users/1') }.to raise_error(Yammer::Error::Unauthorized, 'Token not found.') end end describe 'with too many requests' do it 'raises exception' do stub_get('/users/1').with(:headers => { 'Authorization' => 'Bearer TolNOFka9Uls2DxahNi78A' }).to_return( :body => '{ "response": { "message": "Rate limited due to excessive requests.", "code": 33, "stat": "fail" } }', :status => 429 ) expect { subject.get('/users/1') }.to raise_error(Yammer::Error::RateLimitExceeded, 'Rate limited due to excessive requests.') end end end describe "#connection" do context "with default connection options" do it "returns HttpConnection" do expect(subject.send(:connection)).to be_instance_of(Yammer::HttpConnection) end end context "with custom connection options" do it "returns custom connection" do custom_http = Struct.new('CustomHttpClient', :site_url, :connection_options) client = Yammer::Client.new( :site_url => 'www.example.com', :client_id => 'PRbTcg9qjgKsp4jjpm1pw', :client_secret => 'Xn7kp7Ly0TCY4GtZWkmSsqGEPg10DmMADyjWkf2U', :http_adapter => custom_http, :connection_options => { :max_redirects => 5, :use_ssl => true } ) connection = client.send(:connection) expect(connection.site_url).to eq "www.example.com" expect(connection.connection_options).to eq({ :max_redirects => 5, :use_ssl => true }) end end end end