test/test_client.rb in ringcentral_sdk-1.3.4 vs test/test_client.rb in ringcentral_sdk-2.0.0

- old
+ new

@@ -3,57 +3,54 @@ require 'faraday' require 'oauth2' class RingCentralSdkPlatformTest < Test::Unit::TestCase def setup - @rcsdk = RingCentralSdk.new( - 'my_app_key', - 'my_app_secret', - RingCentralSdk::RC_SERVER_SANDBOX - ) + @rcsdk = RingCentralSdk::REST::Client.new do |config| + config.server_url = RingCentralSdk::RC_SERVER_SANDBOX + config.app_key = 'my_app_key' + config.app_secret = 'my_app_secret' + end end def test_main - assert_equal "bXlfYXBwX2tleTpteV9hcHBfc2VjcmV0", @rcsdk.send(:get_api_key) + assert_equal 'bXlfYXBwX2tleTpteV9hcHBfc2VjcmV0', @rcsdk.send(:api_key) end def test_config redirect_url = 'http://localhost:4567/oauth' - client = RingCentralSdk.new( - 'my_app_key', - 'my_app_secret', - RingCentralSdk::RC_SERVER_SANDBOX, - { - redirect_url: redirect_url - } - ) - assert_equal redirect_url, client.app_config.redirect_url + client = RingCentralSdk::REST::Client.new do |config| + config.server_url = RingCentralSdk::RC_SERVER_SANDBOX + config.app_key = 'my_app_key' + config.app_secret = 'my_app_secret' + config.redirect_url = redirect_url + end + assert_equal redirect_url, client.config.redirect_url end def test_set_client - rcsdk = new_client() + rcsdk = new_client assert_equal true, rcsdk.oauth2client.is_a?(OAuth2::Client) - rcsdk.set_oauth2_client() + rcsdk.set_oauth2_client assert_equal true, rcsdk.oauth2client.is_a?(OAuth2::Client) - rcsdk = new_client() + rcsdk = new_client oauth2client = OAuth2::Client.new( 'my_app_key', 'my_app_secret', site: RingCentralSdk::RC_SERVER_SANDBOX, - token_url: rcsdk.class::TOKEN_ENDPOINT) - rcsdk.set_oauth2_client(oauth2client) - assert_equal true, rcsdk.oauth2client.is_a?(OAuth2::Client) + token_url: rcsdk.class::TOKEN_ENDPOINT + ) + rcsdk.set_oauth2_client oauth2client + assert_equal true, rcsdk.oauth2client.is_a?(OAuth2::Client) - assert_raise do - @rcsdk.set_oauth2_client('test') - end + assert_raise { @rcsdk.set_oauth2_client('test') } end def test_set_token - token_data = {access_token: 'test_token'} + token_data = { access_token: 'test_token' } @rcsdk.set_token(token_data) token = @rcsdk.token @@ -64,30 +61,31 @@ @rcsdk.set_token('test') end end def test_authorize_url_default - rcsdk = RingCentralSdk.new( - 'my_app_key', - 'my_app_secret', - RingCentralSdk::RC_SERVER_PRODUCTION, - {:redirect_url => 'http://localhost:4567/oauth'} - ) - authorize_url = rcsdk.authorize_url() + rcsdk = RingCentralSdk::REST::Client.new do |config| + config.app_key = 'my_app_key' + config.app_secret = 'my_app_secret' + config.server_url = RingCentralSdk::RC_SERVER_PRODUCTION + config.redirect_url = 'http://localhost:4567/oauth' + end + authorize_url = rcsdk.authorize_url + puts authorize_url assert_equal true, authorize_url.is_a?(String) assert_equal 0, authorize_url.index(RingCentralSdk::RC_SERVER_PRODUCTION) - assert_equal true, (authorize_url.index('localhost') > 0) ? true : false + assert_equal true, authorize_url.index('localhost') > 0 ? true : false end def test_authorize_url_explicit - authorize_url = @rcsdk.authorize_url({:redirect_uri => 'http://localhost:4567/oauth'}) + authorize_url = @rcsdk.authorize_url(redirect_uri: 'http://localhost:4567/oauth') assert_equal 0, authorize_url.index(RingCentralSdk::RC_SERVER_SANDBOX) - assert_equal true, (authorize_url.index('localhost') > 0) ? true : false + assert_equal true, authorize_url.index('localhost') > 0 ? true : false end def test_create_url assert_equal '/restapi/v1.0/subscribe', @rcsdk.create_url('subscribe') assert_equal '/restapi/v1.0/subscribe', @rcsdk.create_url('/subscribe') @@ -104,80 +102,86 @@ @rcsdk.create_urls(nil) end end def test_authorize_code - rcsdk = new_client() - rcsdk.set_oauth2_client() + rcsdk = new_client + rcsdk.set_oauth2_client stub_token_hash = data_auth_token_with_refresh - stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash) + stub_token = OAuth2::AccessToken.from_hash(rcsdk.oauth2client, stub_token_hash) rcsdk.oauth2client.auth_code.stubs(:get_token).returns(stub_token) token = rcsdk.authorize_code('my_test_auth_code') assert_equal 'OAuth2::AccessToken', token.class.name assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name - rcsdk = new_client({:redirect_uri => 'http://localhost:4567/oauth'}) - rcsdk.set_oauth2_client() + rcsdk = RingCentralSdk::REST::Client.new do |config| + config.app_key = 'my_app_key' + config.app_secret = 'my_app_secret' + config.server_url = RingCentralSdk::RC_SERVER_SANDBOX + config.redirect_url = 'http://localhost:4567/oauth' + end + rcsdk.set_oauth2_client + stub_token_hash = data_auth_token_with_refresh - stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash) + stub_token = OAuth2::AccessToken.from_hash(rcsdk.oauth2client, stub_token_hash) rcsdk.oauth2client.auth_code.stubs(:get_token).returns(stub_token) token = rcsdk.authorize_code('my_test_auth_code') assert_equal 'OAuth2::AccessToken', token.class.name assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name - rcsdk = new_client() - rcsdk.set_oauth2_client() + rcsdk = new_client + rcsdk.set_oauth2_client stub_token_hash = data_auth_token - stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash) + stub_token = OAuth2::AccessToken.from_hash(rcsdk.oauth2client, stub_token_hash) rcsdk.oauth2client.auth_code.stubs(:get_token).returns(stub_token) token = rcsdk.authorize_code('my_test_auth_code') assert_equal 'OAuth2::AccessToken', token.class.name assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name - rcsdk = new_client() - rcsdk.set_oauth2_client() + rcsdk = new_client + rcsdk.set_oauth2_client stub_token_hash = data_auth_token - stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash) + stub_token = OAuth2::AccessToken.from_hash(rcsdk.oauth2client, stub_token_hash) rcsdk.oauth2client.auth_code.stubs(:get_token).returns(stub_token) - token = rcsdk.authorize_code('my_test_auth_code', {:redirect_uri => 'http://localhost:4567/oauth'}) + token = rcsdk.authorize_code('my_test_auth_code', redirect_uri: 'http://localhost:4567/oauth') assert_equal 'OAuth2::AccessToken', token.class.name assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name end def test_authorize_password_with_refresh - rcsdk = new_client() - rcsdk.set_oauth2_client() + rcsdk = new_client + rcsdk.set_oauth2_client stub_token_hash = data_auth_token_with_refresh - stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash) + stub_token = OAuth2::AccessToken.from_hash(rcsdk.oauth2client, stub_token_hash) rcsdk.oauth2client.password.stubs(:get_token).returns(stub_token) token = rcsdk.authorize('my_test_username', 'my_test_extension', 'my_test_password') assert_equal 'OAuth2::AccessToken', token.class.name assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name assert_equal 'my_test_access_token_with_refresh', rcsdk.token.token end def test_authorize_password_without_refresh - rcsdk = new_client() - rcsdk.set_oauth2_client() + rcsdk = new_client + rcsdk.set_oauth2_client stub_token_hash = data_auth_token_without_refresh - stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash) + stub_token = OAuth2::AccessToken.from_hash(rcsdk.oauth2client, stub_token_hash) rcsdk.oauth2client.password.stubs(:get_token).returns(stub_token) token = rcsdk.authorize('my_test_username', 'my_test_extension', 'my_test_password') assert_equal 'OAuth2::AccessToken', token.class.name @@ -185,45 +189,45 @@ assert_equal 'my_test_access_token_without_refresh', rcsdk.token.token end def test_request assert_raise do - @rcsdk.request() + @rcsdk.request end - client = new_client() - client.set_oauth2_client() + client = new_client + client.set_oauth2_client stub_token_hash = data_auth_token_with_refresh - stub_token = OAuth2::AccessToken::from_hash(client.oauth2client, stub_token_hash) + stub_token = OAuth2::AccessToken.from_hash(client.oauth2client, stub_token_hash) client.oauth2client.password.stubs(:get_token).returns(stub_token) client.authorize('my_test_username', 'my_test_extension', 'my_test_password') - #@rcsdk.client.stubs(:post).returns(Faraday::Response.new) + # @rcsdk.client.stubs(:post).returns(Faraday::Response.new) Faraday::Connection.any_instance.stubs(:post).returns(Faraday::Response.new) fax = RingCentralSdk::REST::Request::Fax.new( # phone numbers are in E.164 format with or without leading '+' - :to => '+16505551212', - :faxResolution => 'High', - :coverPageText => 'RingCentral fax demo using Ruby SDK!', - :text => 'RingCentral fax demo using Ruby SDK!' + to: '+16505551212', + faxResolution: 'High', + coverPageText: 'RingCentral fax demo using Ruby SDK!', + text: 'RingCentral fax demo using Ruby SDK!' ) res = client.send_request(fax) assert_equal 'Faraday::Response', res.class.name assert_raise do res = client.send_request('non-fax') end res = client.messages.fax.create( - :to => '+16505551212', - :faxResolution => 'High', - :coverPageText => 'RingCentral fax demo using Ruby SDK!', - :text => 'RingCentral fax demo using Ruby SDK!' + to: '+16505551212', + faxResolution: 'High', + coverPageText: 'RingCentral fax demo using Ruby SDK!', + text: 'RingCentral fax demo using Ruby SDK!' ) assert_equal 'Faraday::Response', res.class.name ###### # Simple Request - GET @@ -245,13 +249,11 @@ # Simple Request - PUT ###### request = RingCentralSdk::REST::Request::Simple.new( method: 'put', url: 'account/~/extension/~/message-store/11111111', - body: { - readStatus: 'Read' - } + body: { readStatus: 'Read' } ) assert_equal 'put', request.method res = client.send_request request assert_equal 'Faraday::Response', res.class.name @@ -259,24 +261,24 @@ ###### # Simple Request - DELETE ###### request = RingCentralSdk::REST::Request::Simple.new( method: 'delete', - url: 'account/~/extension/~/message-store/11111111', + url: 'account/~/extension/~/message-store/11111111' ) assert_equal 'delete', request.method res = client.send_request request assert_equal 'Faraday::Response', res.class.name end def test_sms - client = new_client() - client.set_oauth2_client() + client = new_client + client.set_oauth2_client stub_token_hash = data_auth_token_with_refresh - stub_token = OAuth2::AccessToken::from_hash(client.oauth2client, stub_token_hash) + stub_token = OAuth2::AccessToken.from_hash(client.oauth2client, stub_token_hash) client.oauth2client.password.stubs(:get_token).returns(stub_token) client.authorize('my_test_username', 'my_test_extension', 'my_test_password') @@ -289,25 +291,24 @@ res = client.send_request( method: 'post', url: 'account/~/extension/~/sms', body: { - from: {phoneNumber: '+16505551212'}, - to: {phoneNumber: '+14155551212'}, + from: { phoneNumber: '+16505551212' }, + to: { phoneNumber: '+14155551212' }, text: 'Hi there!' } ) assert_equal 'Faraday::Response', res.class.name end - def new_client(opts={}) - return RingCentralSdk.new( - 'my_app_key', - 'my_app_secret', - RingCentralSdk::RC_SERVER_PRODUCTION, - opts - ) + def new_client + RingCentralSdk::REST::Client.new do |config| + config.app_key = 'my_app_key' + config.app_secret = 'my_app_secret' + config.server_url = RingCentralSdk::RC_SERVER_PRODUCTION + end end def data_auth_token_with_refresh json = '{ "access_token": "my_test_access_token_with_refresh", @@ -316,23 +317,21 @@ "refresh_token": "my_test_refresh_token", "refresh_token_expires_in": 604799, "scope": "ReadCallLog DirectRingOut EditCallLog ReadAccounts Contacts EditExtensions ReadContacts SMS EditPresence RingOut EditCustomData ReadPresence EditPaymentInfo Interoperability Accounts NumberLookup InternalMessages ReadCallRecording EditAccounts Faxes EditReportingSettings ReadClientInfo EditMessages VoipCalling ReadMessages", "owner_id": "1234567890" }' - data = JSON.parse(json, :symbolize_names=>true) - return data + JSON.parse(json, symbolize_names: true) end def data_auth_token_without_refresh json = '{ "access_token": "my_test_access_token_without_refresh", "token_type": "bearer", "expires_in": 3599, "scope": "ReadCallLog DirectRingOut EditCallLog ReadAccounts Contacts EditExtensions ReadContacts SMS EditPresence RingOut EditCustomData ReadPresence EditPaymentInfo Interoperability Accounts NumberLookup InternalMessages ReadCallRecording EditAccounts Faxes EditReportingSettings ReadClientInfo EditMessages VoipCalling ReadMessages", "owner_id": "1234567890" }' - data = JSON.parse(json, :symbolize_names=>true) - return data + JSON.parse(json, symbolize_names: true) end - alias_method :data_auth_token, :data_auth_token_with_refresh + alias data_auth_token data_auth_token_with_refresh end