This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
receipt_spec.rb
189 lines (159 loc) · 6.27 KB
/
receipt_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
require 'spec_helper'
describe Venice::Receipt do
describe 'parsing the response' do
let(:response) { JSON.parse(File.read('./spec/fixtures/receipt_not_expired_cancelled.json')) }
subject { Venice::Receipt.new(response) }
its(:bundle_id) { 'com.foo.bar' }
its(:application_version) { '2' }
its(:in_app) { should be_instance_of Array }
its(:original_application_version) { '1' }
its(:original_purchase_date) { should be_instance_of DateTime }
its(:expires_at) { should be_instance_of DateTime }
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 }
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
allow_any_instance_of(Venice::Client).to receive(:json_response_from_verifying_data).and_return(response)
end
def stub_json_response_from_verifying_data(returns)
counter = 0
allow_any_instance_of(Venice::Client).to receive(:json_response_from_verifying_data) do
begin
returns[counter].call
ensure
counter += 1
end
end
end
it 'creates the receipt' do
expect(subject).to be_an_instance_of(Venice::Receipt)
end
its(:environment) { is_expected.to eq 'production' }
describe 'retrying VerificationError' do
let(:retryable_error_response) do
{
'status' => 21000,
'receipt' => {},
'is_retryable' => true
}
end
context 'with a retryable error response' do
before do
allow_any_instance_of(Venice::Client).to receive(: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
its(:environment) { is_expected.to eq 'production' }
its(:production?) { is_expected.to be true }
its(:development?) { is_expected.to be false }
end
context 'with 4 retryable error responses' do
before do
allow_any_instance_of(Venice::Client).to receive(: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
let(:error_response) do
{
'status' => 21000,
'receipt' => {},
'is_retryable' => false
}
end
before do
allow_any_instance_of(Venice::Client).to receive(:json_response_from_verifying_data).and_return(error_response, response)
end
it { expect { subject }.to raise_error(Venice::Receipt::VerificationError) }
end
context 'with production response' do
let(:retryable_error_response) do
{
'status' => 21007,
'receipt' => {},
'is_retryable' => true
}
end
before do
returns = [
-> { retryable_error_response },
-> { response }
]
stub_json_response_from_verifying_data(returns)
end
it 'creates the receipt' do
expect(subject).to be_an_instance_of(Venice::Receipt)
end
its(:environment) { is_expected.to eq 'development' }
its(:production?) { is_expected.to be false }
its(:development?) { is_expected.to be true }
end
end
describe 'retrying http error' do
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 renewal information' do
expect(subject.to_hash[:pending_renewal_info]).to eql([{ expiration_intent: 1,
auto_renew_status: 0,
auto_renew_product_id: 'com.foo.product1',
is_in_billing_retry_period: false,
product_id: 'com.foo.product1',
price_consent_status: nil,
cancellation_reason: nil,
grace_period_expires_at: nil,
original_transaction_id: "37xxxxxxxxx89",
}])
end
end
describe 'initialize with no argument' do
subject { Venice::Receipt.new }
it 'does not raise error' do
expect { subject }.not_to raise_error
end
end
end