spec/receipt_spec.rb in venice-0.5.0 vs spec/receipt_spec.rb in venice-0.6.0

- old
+ new

@@ -1,62 +1,10 @@ require 'spec_helper' describe Venice::Receipt do describe 'parsing the response' do - let(:response) do - { - 'status' => 0, - 'environment' => 'Production', - 'receipt' => { - 'receipt_type' => 'Production', - 'adam_id' => 7654321, - 'bundle_id' => 'com.foo.bar', - 'application_version' => '2', - 'download_id' => 1234567, - 'receipt_creation_date' => '2014-06-04 23:20:47 Etc/GMT', - 'receipt_creation_date_ms' => '1401924047883', - 'receipt_creation_date_pst' => '2014-06-04 16:20:47 America/Los_Angeles', - 'request_date' => '2014-06-04 23:20:47 Etc/GMT', - 'request_date_ms' => '1401924047883', - 'request_date_pst' => '2014-06-04 16:20:47 America/Los_Angeles', - 'original_purchase_date' => '2014-05-17 02:09:45 Etc/GMT', - 'original_purchase_date_ms' => '1400292585000', - 'original_purchase_date_pst' => '2014-05-16 19:09:45 America/Los_Angeles', - 'original_application_version' => '1', - 'expiration_date' => '1401924047883', - 'in_app' => [ - { - 'quantity' => '1', - 'product_id' => 'com.foo.product1', - 'transaction_id' => '1000000070107111', - 'original_transaction_id' => '1000000061051111', - 'web_order_line_item_id' => '1000000026812043', - 'purchase_date' => '2014-05-28 14:47:53 Etc/GMT', - 'purchase_date_ms' => '1401288473000', - 'purchase_date_pst' => '2014-05-28 07:47:53 America/Los_Angeles', - 'original_purchase_date' => '2014-05-28 14:47:53 Etc/GMT', - 'original_purchase_date_ms' => '1401288473000', - 'original_purchase_date_pst' => '2014-05-28 07:47:53 America/Los_Angeles', - 'expires_date' => '2014-06-28 14:47:53 Etc/GMT', - 'is_trial_period' => 'false' - } - ], - 'original_json_response' => { - 'pending_renewal_info' => [ - { - 'auto_renew_product_id' => 'com.foo.product1', - 'original_transaction_id' => '37xxxxxxxxx89', - 'product_id' => 'com.foo.product1', - 'auto_renew_status' => '0', - 'is_in_billing_retry_period' => '0', - 'expiration_intent' => '1' - } - ] - } - } - } - end + let(:response) { JSON.parse(File.read('./spec/fixtures/receipt_not_expired_cancelled.json')) } subject { Venice::Receipt.new(response['receipt']) } its(:bundle_id) { 'com.foo.bar' } its(:application_version) { '2' } @@ -67,19 +15,122 @@ its(:receipt_type) { 'Production' } its(:receipt_created_at) { should be_instance_of DateTime } its(:adam_id) { 7654321 } its(:download_id) { 1234567 } its(:requested_at) { should be_instance_of DateTime } + its(:expiration_intent) { nil } - describe '#verify!' do + context 'response is for expired cancelled receipt' do + let(:response) { JSON.parse(File.read('./spec/fixtures/receipt_expired_cancelled.json')) } + + its(:expiration_intent) { 1 } + end + + describe '.verify!' do + subject { described_class.verify!('asdf') } + before do Venice::Client.any_instance.stub(:json_response_from_verifying_data).and_return(response) end - let(:receipt) { Venice::Receipt.verify('asdf') } + it 'creates the receipt' do + expect(subject).to be_an_instance_of(Venice::Receipt) + end - it 'should create the receipt' do - receipt.should_not be_nil + describe 'retrying VerificationError' do + let(:retryable_error_response) do + { + 'status' => 21000, + 'receipt' => {}, + 'is_retryable' => true + } + end + + let(:error_response) do + { + 'status' => 21000, + 'receipt' => {}, + 'is_retryable' => false + } + end + + context 'with a retryable error response' do + before do + Venice::Client.any_instance.stub(:json_response_from_verifying_data).and_return(retryable_error_response, response) + end + + it 'creates the receipt' do + expect(subject).to be_an_instance_of(Venice::Receipt) + end + end + + context 'with 4 retryable error responses' do + before do + Venice::Client.any_instance.stub(:json_response_from_verifying_data).and_return( + retryable_error_response, + retryable_error_response, + retryable_error_response, + retryable_error_response, + response + ) + end + + it { expect { subject }.to raise_error(Venice::Receipt::VerificationError) } + end + + context 'with a not retryable error response' do + before do + Venice::Client.any_instance.stub(:json_response_from_verifying_data).and_return(error_response, response) + end + + it { expect { subject }.to raise_error(Venice::Receipt::VerificationError) } + end + end + + describe 'retrying http error' do + def stub_json_response_from_verifying_data(returns) + counter = 0 + Venice::Client.any_instance.stub(:json_response_from_verifying_data) do + begin + returns[counter].call + ensure + counter += 1 + end + end + end + + context 'given 3 http errors' do + before do + returns = [ + -> { raise(Net::ReadTimeout) }, + -> { raise(OpenSSL::SSL::SSLError) }, + -> { raise(Errno::ECONNRESET) }, + -> { response } + ] + stub_json_response_from_verifying_data(returns) + end + + it 'creates the receipt' do + expect(subject).to be_an_instance_of(Venice::Receipt) + end + end + + context 'given 4 Net::ReadTimeout' do + before do + returns = [ + -> { raise(Net::ReadTimeout) }, + -> { raise(Net::ReadTimeout) }, + -> { raise(Net::ReadTimeout) }, + -> { raise(Net::ReadTimeout) }, + -> { response } + ] + stub_json_response_from_verifying_data(returns) + end + + it 'raises http error' do + expect { subject }.to raise_error(Net::ReadTimeout) + end + end end end it 'parses the pending rerenewal information' do expect(subject.to_hash[:pending_renewal_info]).to eql([{ expiration_intent: 1,