cookie_jar 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.
@@ -0,0 +1,195 @@
1
+ require 'spec_helper'
2
+
3
+ describe CookieJar::Wallet::WalletBase do
4
+ let!(:client_id) { "sample-client-id" }
5
+ let!(:user_id) { "sample-user-id" }
6
+ let!(:client_secret) { "sample-client-secret" }
7
+ let!(:username) { "sample@user.com" }
8
+ let!(:password) { "password" }
9
+ let!(:url) { "http://localhost:3000" }
10
+ let!(:args) { {:param1 => "param1"} }
11
+ let!(:new_args) { {"param1" => "new_param"} }
12
+ let!(:result) { { "response" => "sample response body" } }
13
+ let!(:invalid_url) { "http://invlaid.url"}
14
+ let!(:invalid_result) { {:code => 404, :message => "404 Resource Not Found"} }
15
+ let!(:unauthorized_result) { {:code => 401, :message => "401 Unauthorized"}}
16
+ let!(:token) { "sample token" }
17
+ let!(:access_token) { double OAuth2::AccessToken, :token => token }
18
+
19
+ before do
20
+ CookieJar::Wallet::WalletBase.any_instance.stub(:init_token).and_return(double OAuth2::AccessToken)
21
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_user_id).and_return(user_id)
22
+ end
23
+
24
+ context "#initialize" do
25
+ it "should initialize class attributes" do
26
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
27
+
28
+ wallet.client.id.should eql(client_id)
29
+ wallet.client.secret.should eql(client_secret)
30
+ wallet.client.site.should eql(CookieJar::Wallet::AA_HOST)
31
+ end
32
+ end
33
+
34
+ context "#get" do
35
+ context "successful" do
36
+ before do
37
+ stub_request(:get, "#{url}?param1=#{args[:param1]}&access_token=#{token}").
38
+ with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}).
39
+ to_return(:status => 200, :body => result.to_json, :headers => {})
40
+
41
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
42
+ end
43
+
44
+ it "should get resource from url" do
45
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
46
+ wallet.url = url
47
+
48
+ wallet.get(args).should eql(result)
49
+ end
50
+ end
51
+
52
+ context "on failure" do
53
+ context "when invalid url" do
54
+ before do
55
+ stub_request(:get, "#{invalid_url}?param1=#{args[:param1]}&access_token=#{token}").
56
+ with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}).
57
+ to_return(:status => 404, :body => "", :headers => {})
58
+
59
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
60
+ end
61
+
62
+ it "should return 404 error code" do
63
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
64
+ wallet.url = invalid_url
65
+ wallet.get(args).should eql(invalid_result)
66
+ end
67
+ end
68
+
69
+ context "when unauthorized" do
70
+ before do
71
+ stub_request(:get, "#{url}?param1=#{args[:param1]}&access_token=#{token}").
72
+ with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}).
73
+ to_return(:status => 401, :body => "", :headers => {})
74
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
75
+ end
76
+
77
+ it "should get 401 error code" do
78
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
79
+ wallet.url = url
80
+ wallet.get(args).should eql(unauthorized_result)
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ context "#post" do
87
+ context "successful" do
88
+ before do
89
+ stub_request(:post, url).
90
+ with(:body => {"access_token"=>token, "param1"=>"param1"},
91
+ :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
92
+ to_return(:status => 200, :body => result.to_json, :headers => {})
93
+
94
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
95
+ end
96
+
97
+ it "should post resource to url" do
98
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
99
+ wallet.url = url
100
+ wallet.post(args).should eql(result)
101
+ end
102
+ end
103
+
104
+ context "on failure" do
105
+ context "when invalid url" do
106
+ before do
107
+ stub_request(:post, invalid_url).
108
+ with(:body => {"access_token"=>token, "param1"=>"param1"},
109
+ :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
110
+ to_return(:status => 404, :body => "", :headers => {})
111
+
112
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
113
+ end
114
+
115
+ it "should return 404 error code" do
116
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
117
+ wallet.url = invalid_url
118
+ wallet.post(args).should eql(invalid_result)
119
+ end
120
+ end
121
+
122
+ context "when unauthorized" do
123
+ before do
124
+ stub_request(:post, url).
125
+ with(:body => {"access_token"=>token, "param1"=>"param1"},
126
+ :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
127
+ to_return(:status => 401, :body => "", :headers => {})
128
+
129
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
130
+ end
131
+
132
+ it "should return 401 error code" do
133
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
134
+ wallet.url = url
135
+ wallet.post(args).should eql(unauthorized_result)
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ context "#put" do
142
+ context "successful" do
143
+ before do
144
+ stub_request(:put, url).
145
+ with(:body => {"access_token"=>token, "param1"=>"new_param"},
146
+ :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
147
+ to_return(:status => 200, :body => result.to_json, :headers => {})
148
+
149
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
150
+ end
151
+
152
+ it "should update resource from the server" do
153
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
154
+ wallet.url = url
155
+ wallet.put(new_args).should eql(result)
156
+ end
157
+ end
158
+
159
+ context "on failure" do
160
+ context "when url is invalid" do
161
+ before do
162
+ stub_request(:put, invalid_url).
163
+ with(:body => {"access_token"=>token, "param1"=>"new_param"},
164
+ :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
165
+ to_return(:status => 404, :body => "", :headers => {})
166
+
167
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
168
+ end
169
+
170
+ it "should return 404 error code" do
171
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
172
+ wallet.url = invalid_url
173
+ wallet.put(new_args).should eql(invalid_result)
174
+ end
175
+ end
176
+
177
+ context "when unauthorized" do
178
+ before do
179
+ stub_request(:put, url).
180
+ with(:body => {"access_token"=>token, "param1"=>"new_param"},
181
+ :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
182
+ to_return(:status => 401, :body => "", :headers => {})
183
+
184
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double(OAuth2::AccessToken, :token => token))
185
+ end
186
+
187
+ it "should return 404 error code" do
188
+ wallet = CookieJar::Wallet::WalletBase.new(client_id, client_secret, username, password)
189
+ wallet.url = url
190
+ wallet.put(new_args).should eql(unauthorized_result)
191
+ end
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,190 @@
1
+ require 'spec_helper'
2
+
3
+ describe CookieJar::Wallet::Transfer do
4
+ let!(:user_id){ "some-user-id" }
5
+ let!(:token) { "sample token" }
6
+ let!(:transfer_id){ "some-transfer-id" }
7
+ let!(:access_denied) { {"message" => "You're not authorized to access the resource"} }
8
+ let!(:transfer_not_found_id) { "invalid-transfer-id" }
9
+ let!(:transfer_not_found_message) { {"message" => "Couldn't find Transfer id =#{transfer_not_found_id}"} }
10
+ let!(:transfer_permission_denied) { {"message" => "You do not have permission to access the transfer."} }
11
+ let!(:account_permission_denied) { {"message" => "You do not have permission to access the account."} }
12
+ let!(:transfer_already_initiated) { {"message" => "Transfer is already initiated."} }
13
+ let!(:invalid_withdraw_amount) { {"message" => "Invalid withdraw amount."} }
14
+
15
+ let!(:successful_response){
16
+ {
17
+ :id => "some-id", :user_id => "some-user-id", :state => "some-state", :from_account_balance => 1000, :to_account_balance => 500, :created_at => Time.now, :updated_at => Time.now
18
+ }
19
+ }
20
+
21
+ before do
22
+ CookieJar::Wallet::WalletBase.any_instance.stub(:init_token).and_return(double OAuth2::AccessToken)
23
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double OAuth2::AccessToken, :token => token)
24
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_user_id).and_return(user_id)
25
+ end
26
+
27
+ let!(:user){ CookieJar::Wallet::User.new(*INITIALIZE_VALUES) }
28
+
29
+ context "#create_transfer" do
30
+ context "successful" do
31
+ let!(:successful_json_response){ JSON.dump(successful_response) }
32
+
33
+ before do
34
+ user.stub(:post).and_return(successful_response)
35
+ end
36
+
37
+ it "should create a transfer" do
38
+ user.create_transfer.should eql(successful_response)
39
+ end
40
+ end
41
+
42
+ context "failure" do
43
+ context "when unauthorized" do
44
+ before do
45
+ user.stub(:post).and_return(access_denied)
46
+ end
47
+
48
+ it "should raise ACCESS DENIED error" do
49
+ expect {
50
+ user.create_transfer
51
+ }.to raise_error(CookieJar::Error::AccessDenied)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ context "#get_transfer" do
58
+ context "successful" do
59
+ let!(:successful_json_response){ JSON.dump(successful_response) }
60
+
61
+ before do
62
+ user.stub(:get).and_return(successful_response)
63
+ end
64
+
65
+ it "should get a transfer" do
66
+ user.get_transfer(transfer_id).should eql(successful_response)
67
+ end
68
+
69
+ end
70
+
71
+ context "on failure" do
72
+ context "when transfer not found" do
73
+ before do
74
+ user.stub(:get).and_return(transfer_not_found_message)
75
+ end
76
+
77
+ it "should raise TRANSFER_NOT_FOUND error" do
78
+ expect {
79
+ user.get_transfer(transfer_not_found_id)
80
+ }.to raise_error(CookieJar::Error::TransferNotFound)
81
+ end
82
+ end
83
+
84
+ context "when unauthorized" do
85
+ before do
86
+ user.stub(:get).and_return(access_denied)
87
+ end
88
+
89
+ it "should raise ACCESS DENIED error" do
90
+ expect {
91
+ user.get_transfer(transfer_id)
92
+ }.to raise_error(CookieJar::Error::AccessDenied)
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ context "#all_transfers" do
99
+ context "successful" do
100
+ before do
101
+ user.stub(:get).and_return([successful_response])
102
+ end
103
+
104
+ it "should return all transfers created" do
105
+ user.all_transfers.should eql([successful_response])
106
+ end
107
+ end
108
+
109
+ context "failure" do
110
+ context "when unauthorized" do
111
+ before do
112
+ user.stub(:get).and_return({:code => 401})
113
+ end
114
+
115
+ it "should raise ACCESS_DENIED error" do
116
+ expect {
117
+ user.all_transfers
118
+ }.to raise_error(CookieJar::Error::AccessDenied)
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ context "#activate_transfer" do
125
+ context "successful" do
126
+
127
+ let!(:successful_json_response){ JSON.dump(successful_response) }
128
+ let!(:hash_params){ ["some-id", "some_user_id", 100, 80] }
129
+
130
+ before do
131
+ user.stub(:put).and_return(successful_response)
132
+ end
133
+
134
+ it "should transfer zing to another account" do
135
+ user.activate_transfer(*hash_params).should eql(successful_response)
136
+ end
137
+ end
138
+
139
+ context "on failure" do
140
+ let!(:hash_params){ ["some-id", "some_user_id", 100, 80] }
141
+ context "when unauthorized" do
142
+ before do
143
+ user.stub(:put).and_return(account_permission_denied)
144
+ end
145
+
146
+ it "should raise ACCESS_DENIED error" do
147
+ expect {
148
+ user.activate_transfer(*hash_params)
149
+ }.to raise_error(CookieJar::Error::AccountPermissionDenied)
150
+ end
151
+ end
152
+
153
+ context "when transfer is already initiated" do
154
+ before do
155
+ user.stub(:put).and_return(transfer_already_initiated)
156
+ end
157
+
158
+ it "should raise TriggerAlreadyInitiated error" do
159
+ expect {
160
+ user.activate_transfer(*hash_params)
161
+ }.to raise_error(CookieJar::Error::TransferAlreadyInitiated)
162
+ end
163
+ end
164
+
165
+ context "when transfer permission is denied" do
166
+ before do
167
+ user.stub(:put).and_return(transfer_permission_denied)
168
+ end
169
+
170
+ it "should raise TriggerAlreadyInitiated error" do
171
+ expect {
172
+ user.activate_transfer(*hash_params)
173
+ }.to raise_error(CookieJar::Error::TransferPermissionDenied)
174
+ end
175
+ end
176
+
177
+ context "when withdraw amount is invalid" do
178
+ before do
179
+ user.stub(:put).and_return(invalid_withdraw_amount)
180
+ end
181
+
182
+ it "should raise TriggerAlreadyInitiated error" do
183
+ expect {
184
+ user.activate_transfer(*hash_params)
185
+ }.to raise_error(CookieJar::Error::InvalidWithdrawAmount)
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ describe CookieJar::Wallet::User do
4
+ let!(:url){ "http://localhost:3000/v1/users" }
5
+ let!(:hash){ { "access_token" => "sample" } }
6
+ let!(:client_id) { "sample-client-id" }
7
+ let!(:client_secret) { "sample-secret" }
8
+ let!(:user_id) { "user-id" }
9
+
10
+ before do
11
+ CookieJar::Wallet::WalletBase.any_instance.stub(:init_token).and_return(double OAuth2::AccessToken)
12
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_user_id).and_return(user_id)
13
+ end
14
+
15
+ let!(:user){ CookieJar::Wallet::User.new(*INITIALIZE_VALUES) }
16
+
17
+ context "#all_users" do
18
+ context "successful" do
19
+ let(:successful_response){
20
+ [ { "id" => 1, "first_name" => "Sample", "last_name" => "User", "email" => "user@sample.com" },
21
+ { "id" => 2, "first_name" => "Sample2", "last_name" => "User2", "email" => "user2@sample.com" } ]
22
+ }
23
+
24
+ let(:successful_json_response){ JSON.dump(successful_response) }
25
+
26
+ before do
27
+ user.stub(:get).and_return(successful_response)
28
+ end
29
+
30
+ it "should return a success response json if the operation is successful" do
31
+ user.all_users.should eql(successful_response)
32
+ end
33
+
34
+ it "should call the proper API" do
35
+ user.should_receive(:get)
36
+ user.all_users
37
+ end
38
+ end
39
+
40
+ context "on failure" do
41
+ context "when user is not admin" do
42
+ before do
43
+ user.stub(:get).and_return({"message" => "You're not authorized to access the resource"})
44
+ end
45
+
46
+ it "should raise access_denied error" do
47
+ expect {
48
+ user.all_users
49
+ }.to raise_error(CookieJar::Error::AccessDenied)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ context "#show_user" do
56
+ context "successful" do
57
+ let!(:successful_response) { {"id" => "user-id", "first_name" => "SAMPLE", "last_name" => "USER", "email" => "sample@user.com" }}
58
+
59
+ before do
60
+ CookieJar::Wallet::User.any_instance.stub(:get).and_return(successful_response)
61
+ end
62
+
63
+ it "should return user information" do
64
+ user = CookieJar::Wallet::User.new(*INITIALIZE_VALUES)
65
+ user.get_user(user_id).should eql(successful_response)
66
+ end
67
+ end
68
+
69
+ context "on failure" do
70
+ context "when unauthorized user" do
71
+ before do
72
+ CookieJar::Wallet::User.any_instance.stub(:get).and_return({"message" => "You're not authorized to access the resource"})
73
+ end
74
+
75
+ it "should raise access denied error" do
76
+ expect {
77
+ user.get_user(user_id)
78
+ }.to raise_error(CookieJar::Error::AccessDenied)
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ context "#get_balance" do
85
+ let(:successful_balance) {
86
+ {:user_id => "user-id", :account_id => "account-id",
87
+ :balance_in_cents => 1000, :currency => "ZING"}
88
+ }
89
+
90
+ context "successful" do
91
+ before do
92
+ CookieJar::Wallet::User.any_instance.stub(:get).and_return(successful_balance)
93
+ end
94
+
95
+ it "should get current default account balance" do
96
+ user = CookieJar::Wallet::User.new(*INITIALIZE_VALUES)
97
+ user.get_balance.should eql(successful_balance)
98
+ end
99
+ end
100
+
101
+ context "on failure" do
102
+ context "when unauthorized user" do
103
+ before do
104
+ CookieJar::Wallet::User.any_instance.stub(:get).and_return({"message" => "You're not authorized to access the resource"})
105
+ end
106
+
107
+ it "should raise access denied error" do
108
+ expect {
109
+ user.get_balance
110
+ }.to raise_error(CookieJar::Error::AccessDenied)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end