require 'spec_helper' RSpec.describe Base::APIClient::ClientSecret do subject { Base::APIClient::ClientSecret.new } let(:secret_file) { FIXTURES_PATH / 'files' / 'client_secret.json' } let(:data) { MultiJson.load(File.open(secret_file, 'r').read) } before(:each) do allow(Time).to receive(:now) { Time.new(2016) } allow_any_instance_of(Base::APIClient::ClientSecret) .to receive(:fetch_token) do { access_token: 'fake_access_token', refresh_token: 'fake_refresh_token', expires_in: 3600 } end end describe '#load_file' do it 'reads client_secret.json' do obj = instance_double('ClientSecret') allow(obj).to receive(:load_file) { data } expect(obj.send(:load_file)).to eq data end end describe '::new' do describe 'existence of instance variables' do it 'has @client_id' do expect(subject).to respond_to(:client_id) end it 'has @client_secret' do expect(subject).to respond_to(:client_secret) end it 'has @code' do expect(subject).to respond_to(:code) end it 'has @expires_at' do expect(subject).to respond_to(:expires_at) end it 'has @redirect_uri' do expect(subject).to respond_to(:redirect_uri) end it 'has @search_client_id' do expect(subject).to respond_to(:search_client_id) end it 'has @search_client_secret' do expect(subject).to respond_to(:search_client_secret) end end describe 'without parameters' do it 'instance variables have correct values' do expect(subject.client_id).to eq \ 'fake_client_id' expect(subject.client_secret).to eq \ 'fake_client_secret' expect(subject.code).to eq \ nil expect(subject.redirect_uri).to eq \ 'http://fake_redirect_uri.com' expect(subject.search_client_id).to eq \ 'fake_search_client_id' expect(subject.search_client_secret).to eq \ 'fake_search_client_secret' end end describe 'with parameters' do subject do Base::APIClient::ClientSecret.new( 'client_id' => 'optional_client_id', 'client_secret' => 'optional_client_secret', 'code' => 'optional_code', 'redirect_uri' => 'optional_redirect_uri', 'search_client_id' => 'optional_search_client_id', 'search_client_secret' => 'optional_search_client_secret') end describe 'content of variables' do it 'set the variable by given value' do expect(subject.client_id).to eq 'optional_client_id' expect(subject.client_secret).to eq 'optional_client_secret' expect(subject.code).to eq 'optional_code' expect(subject.redirect_uri).to eq 'optional_redirect_uri' expect(subject.search_client_id).to eq 'optional_search_client_id' expect(subject.search_client_secret).to eq \ 'optional_search_client_secret' end end end end describe '#update!' do before(:each) do subject.update!( client_id: 'updated_client_id', client_secret: 'updated_client_secret', code: 'updated_code', redirect_uri: 'updated_redirect_uri', search_client_id: 'updated_search_client_id', search_client_secret: 'updated_search_client_secret') end it 'update values of instance variables' do expect(subject.client_id).to eq 'updated_client_id' expect(subject.client_secret).to eq 'updated_client_secret' expect(subject.code).to eq 'updated_code' expect(subject.redirect_uri).to eq 'updated_redirect_uri' expect(subject.search_client_id).to eq 'updated_search_client_id' expect(subject.search_client_secret).to eq \ 'updated_search_client_secret' end end describe '#header_parameter' do it 'returns correct parameter' do subject.instance_variable_set(:@access_token, 'fake_access_token') expect(subject.header_parameter).to eq( 'Authorization' => 'Bearer fake_access_token') end end describe '#to_hash' do it 'returns values of instance variables in hash' do expect(subject.send(:to_hash)).to eq( client_id: 'fake_client_id', client_secret: 'fake_client_secret', code: nil, expires_at: nil, redirect_uri: 'http://fake_redirect_uri.com', access_token: nil, refresh_token: nil, search_client_id: 'fake_search_client_id', search_client_secret: 'fake_search_client_secret') end end describe '#to_json' do it 'returns values of instance variables in json' do expect(subject.send(:to_json)).to eq \ <<"EOS""{\"client_id\":\"fake_client_id\",\"client_secret\":\"fake_client_secret\",\"code\":null,\"expires_at\":null,\"redirect_uri\":\"http://fake_redirect_uri.com\",\"access_token\":null,\"refresh_token\":null,\"search_client_id\":\"fake_search_client_id\",\"search_client_secret\":\"fake_search_client_secret\"}" EOS end end describe '#generate_authorize_parameters' do it 'returns parameters' do expect(subject.send(:generate_authorize_parameters)).to eq \ <<"EOS""?client_id=fake_client_id&redirect_uri=http://fake_redirect_uri.com&state=&response_type=code&scope=read_items read_orders read_savings read_users read_users_mail write_items write_orders" EOS end end describe '#generate_code_uri' do it 'generates URI for fetching code' do expect(subject.generate_code_uri).to eq \ 'https://api.thebase.in/1/oauth/authorize?client_id=fake_client_id&redirect_uri=http://fake_redirect_uri.com&state=&response_type=code&scope=read_items read_orders read_savings read_users read_users_mail write_items write_orders' end end describe '#grant_type' do context '@grant_type is not nil' do it 'returns @grant_type value authorization_code' do subject.instance_variable_set(:@grant_type, nil) subject.instance_variable_set(:@code, true) subject.send(:grant_type) expect(subject.instance_variable_get(:@grant_type)).to eq \ 'authorization_code' end end context '@code and @redirect_uri exist' do it 'sets @grant_type "authorization_code"' do subject.instance_variable_set(:@grant_type, nil) subject.instance_variable_set(:@code, true) subject.instance_variable_set(:@redirect_uri, true) subject.send(:grant_type) expect(subject.instance_variable_get(:@grant_type)).to eq \ 'authorization_code' end end context 'only @refresh_token exists' do it 'sets @grant_type "authorization_code"' do subject.instance_variable_set(:@grant_type, nil) subject.instance_variable_set(:@code, nil) subject.instance_variable_set(:@redirect_uri, nil) subject.instance_variable_set(:@refresh_token, true) subject.send(:grant_type) expect(subject.instance_variable_get(:@grant_type)).to eq \ 'refresh_token' end end end describe '#fetch_token' do context 'grant_type is "authorization_code"' do it 'returns access and refresh tokens' do stub_request( :post, URI.parse(Base::APIClient::ClientSecret::TOKEN_URI.to_s)) .with(body: subject .send(:to_hash) .merge(grant_type: 'authorization_code')) expect(subject.send(:fetch_token)).to eq( access_token: 'fake_access_token', refresh_token: 'fake_refresh_token', expires_in: 3600) end end context 'grant_type is "refresh_token"' do it 'returns access and refresh tokens' do stub_request( :post, URI.parse(Base::APIClient::ClientSecret::TOKEN_URI.to_s)) .with(body: subject .send(:to_hash) .merge(grant_type: 'refresh_token')) expect(subject.send(:fetch_token)).to eq( access_token: 'fake_access_token', refresh_token: 'fake_refresh_token', expires_in: 3600) end end end describe '#set_tokens!' do let(:expected_expires_at) do Date.parse(Time.new(2016).to_s).to_time + 3600 end before do allow(subject).to receive(:fetch_token) do { 'access_token' => 'stubbed_access_token', 'refresh_token' => 'stubbed_refresh_token', 'expires_in' => 3600 } end end context 'fetch_token called with grant_type "authorization_code"' do context 'grant_type is authorization_code' do before do allow(subject).to receive(:grant_type) do 'authorization_code' end end it 'set access and refresh tokens' do subject.send(:set_tokens!) expect(subject.access_token).to eq 'stubbed_access_token' expect(subject.refresh_token).to eq 'stubbed_refresh_token' expect(subject.expires_at).to eq expected_expires_at end it 'returns hased properties' do expect(subject.send(:set_tokens!)).to eq( access_token: 'stubbed_access_token', client_id: 'fake_client_id', client_secret: 'fake_client_secret', code: nil, expires_at: expected_expires_at, redirect_uri: 'http://fake_redirect_uri.com', refresh_token: 'stubbed_refresh_token', search_client_id: 'fake_search_client_id', search_client_secret: 'fake_search_client_secret') end end end context 'fetch_token called with grant_type "refresh_token"' do context 'grant_type is refresh_token' do before do allow(subject).to receive(:grant_type) { 'refresh_token' } end it 'set access and refresh tokens' do subject.send(:set_tokens!) expect(subject.access_token).to eq 'stubbed_access_token' expect(subject.refresh_token).to eq 'stubbed_refresh_token' expect(subject.expires_at).to eq expected_expires_at end it 'returns hased properties' do expect(subject.send(:set_tokens!)).to eq( access_token: 'stubbed_access_token', client_id: 'fake_client_id', client_secret: 'fake_client_secret', code: nil, expires_at: expected_expires_at, redirect_uri: 'http://fake_redirect_uri.com', refresh_token: 'stubbed_refresh_token', search_client_id: 'fake_search_client_id', search_client_secret: 'fake_search_client_secret') end end end describe '#expires_at=' do let(:time) { Date.parse(Time.new(2016).to_s).to_time + 3600 } context 'an arg given' do it 'returns expires_at' do subject.send(:expires_at=, time) expect(subject.expires_at).to eq time end end context 'none is given' do it 'returns expires_at' do allow(Time).to receive(:now) do Date.parse(Time.new(2016).to_s).to_time end subject.send(:expires_at=) expect(subject.expires_at).to eq time end end end end describe '#my_info' do let(:response) do VCR.use_cassette('ClientSecret_my_info') { subject.my_info } end it 'contains code "200"' do expect(response.code).to eq '200' end it '@body contains information of the user' do expect(response.body).to match(/user/) expect(response.body).to match(/shop_id/) expect(response.body).to match(/shop_name/) expect(response.body).to match(/shop_introduction/) expect(response.body).to match(/shop_url/) expect(response.body).to match(/twitter_id/) expect(response.body).to match(/facebook_id/) expect(response.body).to match(/ameba_id/) expect(response.body).to match(/instagram_id/) expect(response.body).to match(/background/) expect(response.body).to match(/display_background/) expect(response.body).to match(/repeat_background/) expect(response.body).to match(/logo/) expect(response.body).to match(/display_logo/) expect(response.body).to match(/mail_address/) end end describe '#my_items' do let(:response) do VCR.use_cassette('ClientSecret_my_items') { subject.my_items } end it 'contains code "200"' do expect(response.code).to eq '200' end it '@body contains information of the items' do expect(response.body).to match(/items/) expect(response.body).to match(/item_id/) expect(response.body).to match(/title/) expect(response.body).to match(/detail/) expect(response.body).to match(/price/) expect(response.body).to match(/stock/) expect(response.body).to match(/visible/) expect(response.body).to match(/list_order/) expect(response.body).to match(/identifier/) end end end