tinypass 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.DS_Store +0 -0
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +19 -0
- data/lib/.DS_Store +0 -0
- data/lib/tinypass.rb +48 -0
- data/lib/tinypass/.DS_Store +0 -0
- data/lib/tinypass/builder.rb +7 -0
- data/lib/tinypass/builder/client_builder.rb +40 -0
- data/lib/tinypass/builder/client_parser.rb +48 -0
- data/lib/tinypass/builder/cookie_parser.rb +25 -0
- data/lib/tinypass/builder/json_msg_builder.rb +115 -0
- data/lib/tinypass/builder/open_encoder.rb +11 -0
- data/lib/tinypass/builder/secure_encoder.rb +15 -0
- data/lib/tinypass/builder/security_utils.rb +67 -0
- data/lib/tinypass/gateway.rb +104 -0
- data/lib/tinypass/offer.rb +23 -0
- data/lib/tinypass/policies.rb +5 -0
- data/lib/tinypass/policies/discount_policy.rb +25 -0
- data/lib/tinypass/policies/policy.rb +25 -0
- data/lib/tinypass/policies/pricing_policy.rb +13 -0
- data/lib/tinypass/policies/restriction_policy.rb +14 -0
- data/lib/tinypass/price_option.rb +66 -0
- data/lib/tinypass/resource.rb +17 -0
- data/lib/tinypass/token.rb +6 -0
- data/lib/tinypass/token/access_token.rb +163 -0
- data/lib/tinypass/token/access_token_list.rb +71 -0
- data/lib/tinypass/token/access_token_store.rb +59 -0
- data/lib/tinypass/token/meter.rb +76 -0
- data/lib/tinypass/token/meter_helper.rb +82 -0
- data/lib/tinypass/token/token_data.rb +72 -0
- data/lib/tinypass/ui.rb +2 -0
- data/lib/tinypass/ui/html_widget.rb +29 -0
- data/lib/tinypass/ui/purchase_request.rb +34 -0
- data/lib/tinypass/utils.rb +34 -0
- data/lib/tinypass/version.rb +3 -0
- data/spec/.DS_Store +0 -0
- data/spec/acceptance/basic_workflow_spec.rb +81 -0
- data/spec/integration/.DS_Store +0 -0
- data/spec/integration/cases/.DS_Store +0 -0
- data/spec/integration/cases/basic_spec.rb +53 -0
- data/spec/integration/cases/combo_spec.rb +43 -0
- data/spec/integration/cases/metered_reminder_spec.rb +42 -0
- data/spec/integration/cases/metered_strict_spec.rb +54 -0
- data/spec/integration/cases/metered_views_spec.rb +92 -0
- data/spec/integration/client_builder_and_parser_spec.rb +21 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/support/.DS_Store +0 -0
- data/spec/support/acceptance.rb +13 -0
- data/spec/support/tinypass_factories.rb +25 -0
- data/spec/unit/.DS_Store +0 -0
- data/spec/unit/builder/.DS_Store +0 -0
- data/spec/unit/builder/client_builder_spec.rb +23 -0
- data/spec/unit/builder/client_parser_spec.rb +27 -0
- data/spec/unit/builder/cookie_parser_spec.rb +39 -0
- data/spec/unit/builder/json_msg_builder_spec.rb +73 -0
- data/spec/unit/builder/open_encoder_spec.rb +15 -0
- data/spec/unit/builder/secure_encoder_spec.rb +23 -0
- data/spec/unit/builder/security_utils_spec.rb +42 -0
- data/spec/unit/gateway_spec.rb +80 -0
- data/spec/unit/offer_spec.rb +31 -0
- data/spec/unit/policies/.DS_Store +0 -0
- data/spec/unit/policies/discount_policy_spec.rb +40 -0
- data/spec/unit/policies/pricing_policy_spec.rb +23 -0
- data/spec/unit/policies/restriction_policy_spec.rb +35 -0
- data/spec/unit/price_option_spec.rb +109 -0
- data/spec/unit/resource_spec.rb +24 -0
- data/spec/unit/tinypass_spec.rb +51 -0
- data/spec/unit/token/.DS_Store +0 -0
- data/spec/unit/token/access_token_list_spec.rb +94 -0
- data/spec/unit/token/access_token_spec.rb +267 -0
- data/spec/unit/token/access_token_store_spec.rb +93 -0
- data/spec/unit/token/meter_helper_spec.rb +103 -0
- data/spec/unit/token/meter_spec.rb +66 -0
- data/spec/unit/token/token_data_spec.rb +66 -0
- data/spec/unit/ui/.DS_Store +0 -0
- data/spec/unit/ui/html_widget_spec.rb +89 -0
- data/spec/unit/ui/purchase_request_spec.rb +46 -0
- data/spec/unit/utils_spec.rb +57 -0
- data/tinypass.gemspec +35 -0
- metadata +330 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Tinypass::RestrictionPolicy do
|
4
|
+
describe '#limit_purchases_in_period_by_amount' do
|
5
|
+
let(:data) { Tinypass::RestrictionPolicy.limit_purchases_in_period_by_amount('10', '1 week') }
|
6
|
+
let(:hash) { data.to_hash }
|
7
|
+
|
8
|
+
it 'returns a RestrictionPolicy' do
|
9
|
+
expect(data).to be_kind_of Tinypass::RestrictionPolicy
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'sets the type' do
|
13
|
+
expect(hash[Tinypass::Policy::POLICY_TYPE]).to eq Tinypass::Policy::RESTRICT_MAX_PURCHASES
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'sets the values' do
|
17
|
+
expect(hash['amount']).to eq '10'
|
18
|
+
expect(hash['withinPeriod']).to eq '1 week'
|
19
|
+
end
|
20
|
+
|
21
|
+
context "with details" do
|
22
|
+
let(:data) { Tinypass::RestrictionPolicy.limit_purchases_in_period_by_amount('10', '1 week', 'details') }
|
23
|
+
|
24
|
+
it 'sets the type' do
|
25
|
+
expect(hash[Tinypass::Policy::POLICY_TYPE]).to eq Tinypass::Policy::RESTRICT_MAX_PURCHASES
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'sets the values' do
|
29
|
+
expect(hash['amount']).to eq '10'
|
30
|
+
expect(hash['withinPeriod']).to eq '1 week'
|
31
|
+
expect(hash['linkWithDetails']).to eq 'details'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Tinypass::PriceOption do
|
4
|
+
describe "#initialize" do
|
5
|
+
it "accepts a price and an access period" do
|
6
|
+
option = Tinypass::PriceOption.new('.50', '24 hours')
|
7
|
+
expect(option.price).to eq '.50'
|
8
|
+
expect(option.access_period).to eq '24 hours'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "can set price" do
|
13
|
+
option = Tinypass::PriceOption.new('.50', '24 hours')
|
14
|
+
option.price = '1.50'
|
15
|
+
expect(option.price).to eq '1.50'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "can set access period" do
|
19
|
+
option = Tinypass::PriceOption.new('.50', '24 hours')
|
20
|
+
option.access_period = '1 week'
|
21
|
+
expect(option.access_period).to eq '1 week'
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#access_period_in_msecs" do
|
25
|
+
it "passes the happy case" do
|
26
|
+
option = Tinypass::PriceOption.new('.50', '321 msecs')
|
27
|
+
expect(option.access_period_in_msecs).to eq 321
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#access_period_in_secs" do
|
32
|
+
it "passes the happy case" do
|
33
|
+
option = Tinypass::PriceOption.new('.50', '321 secs')
|
34
|
+
expect(option.access_period_in_secs).to eq 321
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "can set start date" do
|
39
|
+
option = Tinypass::PriceOption.new('.50', '24 hours')
|
40
|
+
option.start_date_in_secs = 1234
|
41
|
+
expect(option.start_date_in_secs).to eq 1234
|
42
|
+
end
|
43
|
+
|
44
|
+
it "can set end date" do
|
45
|
+
option = Tinypass::PriceOption.new('.50', '24 hours')
|
46
|
+
option.end_date_in_secs = 1234
|
47
|
+
expect(option.end_date_in_secs).to eq 1234
|
48
|
+
end
|
49
|
+
|
50
|
+
it "can set caption" do
|
51
|
+
option = Tinypass::PriceOption.new('.50', '24 hours')
|
52
|
+
option.caption = 'caption'
|
53
|
+
expect(option.caption).to eq 'caption'
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#active?" do
|
57
|
+
it "returns true when no start or end date" do
|
58
|
+
expect(Tinypass::PriceOption.new('.50', '1 day').active?).to be_true
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns false when before start date" do
|
62
|
+
expect(Tinypass::PriceOption.new('.50', '1 day', Time.now.to_i + 10).active?).to be_false
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns false when after end date" do
|
66
|
+
expect(Tinypass::PriceOption.new('.50', '1 day', nil, Time.now.to_i - 1).active?).to be_false
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns true when after start and no end" do
|
70
|
+
expect(Tinypass::PriceOption.new('.50', '1 day', Time.now.to_i - 1).active?).to be_true
|
71
|
+
end
|
72
|
+
|
73
|
+
it "returns true when before end and no start" do
|
74
|
+
expect(Tinypass::PriceOption.new('.50', '1 day', nil, Time.now.to_i + 10).active?).to be_true
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns true when between start and end dates" do
|
78
|
+
expect(Tinypass::PriceOption.new('.50', '1 day', Time.now.to_i - 1, Time.now.to_i + 10).active?).to be_true
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when passed a value" do
|
82
|
+
it "uses the value instead of the current time" do
|
83
|
+
option = Tinypass::PriceOption.new('.50', '1 day', Time.now.to_i - 1, Time.now.to_i + 10)
|
84
|
+
expect(option.active?(Time.now.to_i - 2)).to be_false
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it "can add split pays" do
|
90
|
+
price_option = Tinypass::PriceOption.new('.50', '1 day')
|
91
|
+
price_option.add_split_pay('email@example.org', '.25')
|
92
|
+
price_option.add_split_pay('another_email@another_host.net', '5%')
|
93
|
+
expect(price_option.split_pays).not_to be_nil
|
94
|
+
expect(price_option.split_pays['email@example.org']).to eq 0.25
|
95
|
+
expect(price_option.split_pays['another_email@another_host.net']).to eq 0.05
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "#to_s" do
|
99
|
+
it "translates the price option as expected" do
|
100
|
+
price_option = Tinypass::PriceOption.new('.50', '1 day',
|
101
|
+
Date.new(2013, 9, 21).to_time.to_i,
|
102
|
+
Date.new(2013, 7, 3).to_time.to_i)
|
103
|
+
price_option.add_split_pay('email@example.org', '.25')
|
104
|
+
price_option.add_split_pay('another_email@another_host.net', '5%')
|
105
|
+
|
106
|
+
expect(price_option.to_s).to eq "Price:.50\tPeriod:1 day\tTrial Period:1 day\tStart:1379736000:Sat, 21 Sep 2013 00 00 00\tEnd:1372824000:Wed, 03 Jul 2013 00 00 00\tSplit:email@example.org:0.25\tSplit:another_email@another_host.net:0.05"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Tinypass::Resource do
|
4
|
+
describe "#initialize" do
|
5
|
+
it "accepts an rid and a name" do
|
6
|
+
resource = Tinypass::Resource.new('rid', 'name')
|
7
|
+
expect(resource.rid).to eq 'rid'
|
8
|
+
expect(resource.name).to eq 'name'
|
9
|
+
end
|
10
|
+
|
11
|
+
it "accepts an rid, name and url" do
|
12
|
+
resource = Tinypass::Resource.new('rid', 'name', 'url')
|
13
|
+
expect(resource.rid).to eq 'rid'
|
14
|
+
expect(resource.name).to eq 'name'
|
15
|
+
expect(resource.url).to eq 'url'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can set name" do
|
20
|
+
resource = Tinypass::Resource.new('rid', 'name')
|
21
|
+
resource.name = 'NEW'
|
22
|
+
expect(resource.name).to eq 'NEW'
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Tinypass do
|
4
|
+
it "can set and get sandbox" do
|
5
|
+
Tinypass.sandbox = true
|
6
|
+
expect(Tinypass.sandbox).to be_true
|
7
|
+
end
|
8
|
+
|
9
|
+
it "can set and get the aid" do
|
10
|
+
Tinypass.aid = 'this is my aid'
|
11
|
+
expect(Tinypass.aid).to eq 'this is my aid'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can set and get the private key" do
|
15
|
+
Tinypass.private_key = 'this is my private key'
|
16
|
+
expect(Tinypass.private_key).to eq 'this is my private key'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Tinypass::Config do
|
21
|
+
it "has the expected version number" do
|
22
|
+
expect(Tinypass::Config::VERSION).to eq '2.0.7'
|
23
|
+
end
|
24
|
+
|
25
|
+
it "has the expected context" do
|
26
|
+
expect(Tinypass::Config::CONTEXT).to eq '/v2'
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#endpoint" do
|
30
|
+
after do
|
31
|
+
if Tinypass.const_defined?('API_ENDPOINT_DEV')
|
32
|
+
Tinypass.send(:remove_const, 'API_ENDPOINT_DEV')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns the live url when live" do
|
37
|
+
Tinypass.sandbox = false
|
38
|
+
expect(Tinypass::Config.endpoint).to eq "https://api.tinypass.com"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns the sandbox url when sandboxed" do
|
42
|
+
Tinypass.sandbox = true
|
43
|
+
expect(Tinypass::Config.endpoint).to eq "https://sandbox.tinypass.com"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "returns the development url if set" do
|
47
|
+
Tinypass.const_set('API_ENDPOINT_DEV', 'http://test.com')
|
48
|
+
expect(Tinypass::Config.endpoint).to eq 'http://test.com'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
Binary file
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Tinypass::AccessTokenList do
|
4
|
+
let(:list) { Tinypass::AccessTokenList.new }
|
5
|
+
let(:token) { Tinypass::AccessToken.new('present') }
|
6
|
+
let(:other_token) { Tinypass::AccessToken.new(123456) }
|
7
|
+
let(:tokens) { [token, other_token] }
|
8
|
+
|
9
|
+
describe "#initialize" do
|
10
|
+
it "accepts an access token" do
|
11
|
+
list = Tinypass::AccessTokenList.new(token)
|
12
|
+
expect(list[token.rid]).not_to be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "accepts an array of access tokens" do
|
16
|
+
list = Tinypass::AccessTokenList.new(tokens)
|
17
|
+
expect(list[token.rid]).not_to be_nil
|
18
|
+
expect(list[other_token.rid]).not_to be_nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#tokens, by default" do
|
23
|
+
it "is not null" do
|
24
|
+
expect(list.tokens).not_to be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "is empty" do
|
28
|
+
expect(list.tokens.size).to eq 0
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#contains" do
|
33
|
+
context "when empty" do
|
34
|
+
it "returns false" do
|
35
|
+
expect(list.empty?).to be_true
|
36
|
+
expect(list.contains?(123)).to be_false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when not empty" do
|
41
|
+
let(:list) { Tinypass::AccessTokenList.new(tokens) }
|
42
|
+
|
43
|
+
before do
|
44
|
+
Tinypass::AccessToken.new('absent')
|
45
|
+
expect(list.empty?).to be_false
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns true for present token RIDs" do
|
49
|
+
expect(list.include?(token.rid)).to be_true #test alias
|
50
|
+
expect(list.contains?(other_token.rid)).to be_true
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns false for absent token RIDs" do
|
54
|
+
expect(list.contains?('absent')).to be_false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#<<" do
|
60
|
+
before do
|
61
|
+
stub_const('Tinypass::AccessTokenList::MAX', 1)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "adds the token to the list if we haven't reached the max" do
|
65
|
+
list << token
|
66
|
+
expect(list.length).to eq 1
|
67
|
+
expect(list.first).to eq token
|
68
|
+
end
|
69
|
+
|
70
|
+
it "drops the oldest tokens if we reach the max" do
|
71
|
+
list << token
|
72
|
+
list << other_token
|
73
|
+
expect(list.length).to eq 1
|
74
|
+
expect(list.first).to eq other_token
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#add_all" do
|
79
|
+
it "adds all the tokens" do
|
80
|
+
list.add_all(tokens)
|
81
|
+
expect(list.length).to eq 2
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#empty?" do
|
86
|
+
it "returns true when empty" do
|
87
|
+
expect(list).to be_empty
|
88
|
+
end
|
89
|
+
|
90
|
+
it "returns false when populated" do
|
91
|
+
expect(Tinypass::AccessTokenList.new(token)).not_to be_empty
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,267 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Tinypass::AccessToken do
|
4
|
+
let(:token) { Tinypass::AccessToken.new('123456', 3000) }
|
5
|
+
|
6
|
+
it "creates token data" do
|
7
|
+
expect(token.token_data.values).not_to be_nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "sets the rid" do
|
11
|
+
expect(token.rid).to eq '123456'
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#access_id" do
|
15
|
+
it "can be retrieved" do
|
16
|
+
token.token_data[Tinypass::TokenData::ACCESS_ID] = 'value'
|
17
|
+
expect(token.access_id).to eq 'value'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#uid" do
|
22
|
+
it "can be retrieved" do
|
23
|
+
token.token_data[Tinypass::TokenData::UID] = 'value'
|
24
|
+
expect(token.uid).to eq 'value'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#expiration_in_seconds" do
|
29
|
+
it "can be retrieved" do
|
30
|
+
expect(token.expiration_in_seconds).to eq 3000
|
31
|
+
end
|
32
|
+
|
33
|
+
it "is aliased as #expiration_in_secs" do
|
34
|
+
expect(token.expiration_in_secs).to eq 3000
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#early_expiration_in_seconds" do
|
39
|
+
it "defaults to zero" do
|
40
|
+
expect(token.early_expiration_in_seconds).to eq 0
|
41
|
+
end
|
42
|
+
|
43
|
+
it "is aliased as #expiration_in_secs" do
|
44
|
+
expect(token.early_expiration_in_secs).to eq 0
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
it "supports trial_end_time_secs" do
|
49
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = 4000
|
50
|
+
expect(token.trial_end_time_secs).to eq 4000
|
51
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = 1000
|
52
|
+
expect(token.trial_end_time_secs).to eq 1000
|
53
|
+
end
|
54
|
+
|
55
|
+
it "supports lockout_end_time_secs" do
|
56
|
+
token.token_data[Tinypass::TokenData::METER_LOCKOUT_ENDTIME] = 4000
|
57
|
+
expect(token.lockout_end_time_secs).to eq 4000
|
58
|
+
token.token_data[Tinypass::TokenData::METER_LOCKOUT_ENDTIME] = 1000
|
59
|
+
expect(token.lockout_end_time_secs).to eq 1000
|
60
|
+
end
|
61
|
+
|
62
|
+
it "supports trial_view_count" do
|
63
|
+
expect(token.trial_view_count).to eq 0
|
64
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ACCESS_ATTEMPTS] = 1000
|
65
|
+
expect(token.trial_view_count).to eq 1000
|
66
|
+
end
|
67
|
+
|
68
|
+
it "supports trial_view_limit" do
|
69
|
+
expect(token.trial_view_limit).to eq 0
|
70
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_MAX_ACCESS_ATTEMPTS] = 1000
|
71
|
+
expect(token.trial_view_limit).to eq 1000
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#access_state" do
|
75
|
+
it "returns ACCESS_GRANTED by default" do
|
76
|
+
token = Tinypass::AccessToken.new('RID', 0)
|
77
|
+
expect(token.access_state).to eq Tinypass::AccessState::ACCESS_GRANTED
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#access_granted?" do
|
82
|
+
it "returns true when not expired" do
|
83
|
+
token = Tinypass::AccessToken.new('RID', Time.now.to_i + 4)
|
84
|
+
expect(token.access_granted?).to be_true
|
85
|
+
expect(token.access_state).to eq Tinypass::AccessState::ACCESS_GRANTED
|
86
|
+
end
|
87
|
+
|
88
|
+
it "returns true when expiration is zero" do
|
89
|
+
token = Tinypass::AccessToken.new('RID', 0)
|
90
|
+
expect(token.access_granted?).to be_true
|
91
|
+
expect(token.access_state).to eq Tinypass::AccessState::ACCESS_GRANTED
|
92
|
+
end
|
93
|
+
|
94
|
+
it "returns true when expiration is nil" do
|
95
|
+
token = Tinypass::AccessToken.new('RID', nil)
|
96
|
+
expect(token.access_granted?).to be_true
|
97
|
+
expect(token.access_state).to eq Tinypass::AccessState::ACCESS_GRANTED
|
98
|
+
end
|
99
|
+
|
100
|
+
it "returns false when expired" do
|
101
|
+
token = Tinypass::AccessToken.new('RID', Time.now.to_i - 1)
|
102
|
+
expect(token.access_granted?).to be_false
|
103
|
+
expect(token.expired?).to be_true
|
104
|
+
expect(token.access_state).to eq Tinypass::AccessState::EXPIRED
|
105
|
+
end
|
106
|
+
|
107
|
+
it "returns false when early expired" do
|
108
|
+
token = Tinypass::AccessToken.new('RID', 0, Time.now.to_i - 1)
|
109
|
+
expect(token.access_granted?).to be_false
|
110
|
+
expect(token.access_state).to eq Tinypass::AccessState::EXPIRED
|
111
|
+
end
|
112
|
+
|
113
|
+
context "when passed an ip" do
|
114
|
+
let(:token) { Tinypass::AccessToken.new('RID', 0) }
|
115
|
+
it "ignores the ip if no ips set" do
|
116
|
+
expect(token.access_granted?('1.1.1.1')).to be_true
|
117
|
+
expect(token.access_state).to eq Tinypass::AccessState::ACCESS_GRANTED
|
118
|
+
end
|
119
|
+
|
120
|
+
it "ignores the ip if ip included" do
|
121
|
+
token.token_data[Tinypass::TokenData::IPS] = ['1.1.1.1']
|
122
|
+
expect(token.access_granted?('1.1.1.1')).to be_true
|
123
|
+
expect(token.access_state).to eq Tinypass::AccessState::ACCESS_GRANTED
|
124
|
+
end
|
125
|
+
|
126
|
+
it "returns false when ip not included" do
|
127
|
+
token.token_data[Tinypass::TokenData::IPS] = ['1.1.1.1']
|
128
|
+
expect(token.access_granted?('1.1.1.2')).to be_false
|
129
|
+
expect(token.access_state).to eq Tinypass::AccessState::CLIENT_IP_DOES_NOT_MATCH_TOKEN
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "timed meter" do
|
134
|
+
let(:token) { Tinypass::AccessToken.new('RID', 0) }
|
135
|
+
|
136
|
+
before do
|
137
|
+
token.token_data[Tinypass::TokenData::METER_TYPE] = Tinypass::TokenData::METER_STRICT
|
138
|
+
end
|
139
|
+
|
140
|
+
it "returns true when not expired" do
|
141
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i + 1000
|
142
|
+
expect(token.access_granted?).to be_true
|
143
|
+
expect(token.metered?).to be_true
|
144
|
+
expect(token.trial_period_active?).to be_true
|
145
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_IN_TRIAL
|
146
|
+
end
|
147
|
+
|
148
|
+
it "returns false when expired" do
|
149
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i - 1
|
150
|
+
expect(token.access_granted?).to be_false
|
151
|
+
expect(token.metered?).to be_true
|
152
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_TRIAL_DEAD
|
153
|
+
end
|
154
|
+
|
155
|
+
it "returns false when null" do
|
156
|
+
expect(token.access_granted?).to be_false
|
157
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_TRIAL_DEAD
|
158
|
+
end
|
159
|
+
|
160
|
+
it "returns false with special state if locked out" do
|
161
|
+
token.token_data[Tinypass::TokenData::METER_LOCKOUT_ENDTIME] = Time.now.to_i + 1000
|
162
|
+
expect(token.access_granted?).to be_false
|
163
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_IN_LOCKOUT
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "view meter" do
|
168
|
+
let(:token) { Tinypass::AccessToken.new('RID', 0) }
|
169
|
+
|
170
|
+
before do
|
171
|
+
token.token_data[Tinypass::TokenData::METER_TYPE] = Tinypass::TokenData::METER_REMINDER
|
172
|
+
end
|
173
|
+
|
174
|
+
context "not expired" do
|
175
|
+
before do
|
176
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i + 1000
|
177
|
+
end
|
178
|
+
|
179
|
+
it "returns true when views not exceeded" do
|
180
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_MAX_ACCESS_ATTEMPTS] = 1
|
181
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ACCESS_ATTEMPTS] = 0
|
182
|
+
expect(token.access_granted?).to be_true
|
183
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_IN_TRIAL
|
184
|
+
end
|
185
|
+
|
186
|
+
it "returns false when views exceeded" do
|
187
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_MAX_ACCESS_ATTEMPTS] = 1
|
188
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ACCESS_ATTEMPTS] = 2 # NOTE: probable off-by-one error
|
189
|
+
expect(token.access_granted?).to be_false
|
190
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_TRIAL_DEAD
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context "expired" do
|
195
|
+
before do
|
196
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i - 1
|
197
|
+
end
|
198
|
+
|
199
|
+
it "returns false despite views not exceeded" do
|
200
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_MAX_ACCESS_ATTEMPTS] = 1
|
201
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ACCESS_ATTEMPTS] = 0
|
202
|
+
expect(token.access_granted?).to be_false
|
203
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_TRIAL_DEAD
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
context "unknown meter" do
|
209
|
+
let(:token) { Tinypass::AccessToken.new('RID', 0) }
|
210
|
+
|
211
|
+
before do
|
212
|
+
token.token_data[Tinypass::TokenData::METER_TYPE] = 'unknown meter type'
|
213
|
+
end
|
214
|
+
|
215
|
+
it "returns true when not expired" do
|
216
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i + 1000
|
217
|
+
expect(token.access_granted?).to be_true
|
218
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_IN_TRIAL
|
219
|
+
end
|
220
|
+
|
221
|
+
it "returns false when expired" do
|
222
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i - 1
|
223
|
+
expect(token.access_granted?).to be_false
|
224
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_TRIAL_DEAD
|
225
|
+
end
|
226
|
+
|
227
|
+
it "returns true when null" do
|
228
|
+
expect(token.access_granted?).to be_true
|
229
|
+
expect(token.access_state).to eq Tinypass::AccessState::METERED_IN_TRIAL
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe "#trial_dead?" do
|
235
|
+
let(:token) { Tinypass::AccessToken.new('RID', 0) }
|
236
|
+
|
237
|
+
it "returns true when not metered" do
|
238
|
+
expect(token.trial_dead?).to be_true
|
239
|
+
end
|
240
|
+
|
241
|
+
context "when metered" do
|
242
|
+
before do
|
243
|
+
token.token_data[Tinypass::TokenData::METER_TYPE] = Tinypass::TokenData::METER_STRICT
|
244
|
+
end
|
245
|
+
|
246
|
+
it "returns false when trial is active" do
|
247
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i + 1000
|
248
|
+
expect(token.trial_dead?).to be_false
|
249
|
+
end
|
250
|
+
|
251
|
+
it "returns false when lockout is active" do
|
252
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i - 1
|
253
|
+
token.token_data[Tinypass::TokenData::METER_LOCKOUT_ENDTIME] = Time.now.to_i + 1000
|
254
|
+
expect(token.trial_dead?).to be_false
|
255
|
+
expect(token.lockout_period_active?).to be_true
|
256
|
+
end
|
257
|
+
|
258
|
+
it "returns true when lockout and trial expired" do
|
259
|
+
token.token_data[Tinypass::TokenData::METER_TRIAL_ENDTIME] = Time.now.to_i - 1
|
260
|
+
token.token_data[Tinypass::TokenData::METER_LOCKOUT_ENDTIME] = Time.now.to_i - 1
|
261
|
+
expect(token.trial_dead?).to be_true
|
262
|
+
expect(token.lockout_period_active?).to be_false
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|