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,42 @@
1
+ require 'cookie_jar/account'
2
+ require 'cookie_jar/transfer'
3
+ require 'cookie_jar/banker'
4
+
5
+ module CookieJar
6
+ module Wallet
7
+ class User < WalletBase
8
+ include CookieJar::Wallet::Account
9
+ include CookieJar::Wallet::Transfer
10
+
11
+ def all_users
12
+ self.url = "#{CookieJar::Wallet::WALLET_HOST}/v1/users"
13
+ response = self.get
14
+
15
+ #for successful response, self.get will return array. unless it will return error hash
16
+ if response.is_a?(Hash)
17
+ raise CookieJar::Error::AccessDenied if response["message"] == "You're not authorized to access the resource" or response[:code] == 401
18
+ end
19
+
20
+ response
21
+ end
22
+
23
+ def get_user(user_id=self.user_id)
24
+ self.url = "#{CookieJar::Wallet::WALLET_HOST}/v1/users/#{user_id}"
25
+ response = self.get
26
+
27
+ raise CookieJar::Error::AccessDenied if response["message"] == "You're not authorized to access the resource" or response[:code] == 401
28
+
29
+ response
30
+ end
31
+
32
+ def get_balance
33
+ self.url = "#{CookieJar::Wallet::WALLET_HOST}/v1/users/#{user_id}/balance"
34
+ response = self.get
35
+
36
+ raise CookieJar::Error::AccessDenied if response["message"] == "You're not authorized to access the resource" or response[:code] == 401
37
+
38
+ response
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module CookieJar
2
+ module Wallet
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,67 @@
1
+ require "cookie_jar/version"
2
+ require 'rest-client'
3
+ require 'json'
4
+ require 'oauth2'
5
+
6
+ module CookieJar
7
+ module Wallet
8
+ WALLET_HOST = "http://localhost:3003"
9
+ AA_HOST = "http://localhost:3002"
10
+
11
+ class WalletBase
12
+ attr_accessor :url, :client, :token, :user_id, :account_id
13
+
14
+ def initialize(client_id, client_secret, username, password)
15
+ self.client = OAuth2::Client.new(client_id, client_secret, :site => CookieJar::Wallet::AA_HOST)
16
+ self.token = init_token(username, password)
17
+ self.user_id = get_user_id
18
+ end
19
+
20
+ def init_token(username, password)
21
+ self.client.password.get_token(username, password)
22
+ end
23
+
24
+ def get_user_id
25
+ JSON.parse(self.token.get('/get_user').body)["id"]
26
+ end
27
+
28
+ def get_token
29
+ if self.token.expired?
30
+ self.token = token.refresh!
31
+ end
32
+
33
+ self.token
34
+ end
35
+
36
+ def get(args={})
37
+ access_token = get_token.token
38
+ begin
39
+ response = RestClient.get self.url, :params => args.merge!({:access_token => access_token})
40
+ JSON.parse(response)
41
+ rescue => exception
42
+ {:code => exception.response.code, :message => exception.message}
43
+ end
44
+ end
45
+
46
+ def post(args={})
47
+ access_token = get_token.token
48
+ begin
49
+ response = RestClient.post self.url, args.merge!(:access_token => access_token)
50
+ JSON.parse(response)
51
+ rescue => exception
52
+ {:code => exception.response.code, :message => exception.message}
53
+ end
54
+ end
55
+
56
+ def put(args)
57
+ access_token = get_token.token
58
+ begin
59
+ response = RestClient.put self.url, args.merge!(:access_token => access_token)
60
+ JSON.parse(response)
61
+ rescue => exception
62
+ {:code => exception.response.code, :message => exception.message}
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,198 @@
1
+ require 'spec_helper'
2
+
3
+ describe CookieJar::Wallet::Account do
4
+ let!(:user_id) { "index-user-id" }
5
+ let!(:account_id) { "some-account-id" }
6
+ let!(:account_not_found_id) { "some-account-id" }
7
+ let!(:invalid_user_id) { "invalid-user-id" }
8
+ let!(:invalid_user_result) { {"message" => "Couldn't find User id =#{invalid_user_id}"} }
9
+ let!(:unauthorized_message) { {"message" => "You're not authorized to access the resource"} }
10
+ let!(:account_not_found_message) { {"message" => "Couldn't find Account id =#{account_not_found_id}"} }
11
+ let!(:insufficient_params_message) { {"message" => "Insufficient parameters."} }
12
+
13
+ let!(:index_result) {
14
+ {
15
+ "id" => "index-id",
16
+ "account_type_id" => "savings",
17
+ "user_id" => user_id,
18
+ "description" => "Default savings account",
19
+ "created_at" => Time.now,
20
+ "updated_at" => Time.now
21
+ }
22
+ }
23
+
24
+ before do
25
+ CookieJar::Wallet::WalletBase.any_instance.stub(:init_token).and_return(double OAuth2::AccessToken)
26
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_token).and_return(double OAuth2::AccessToken, :token => "sample1234")
27
+ CookieJar::Wallet::WalletBase.any_instance.stub(:get_user_id).and_return(user_id)
28
+ end
29
+
30
+ let!(:account){ CookieJar::Wallet::User.new(*INITIALIZE_VALUES) }
31
+
32
+ context "#all_accounts" do
33
+ context "successful" do
34
+ before do
35
+ account.stub(:get).and_return([index_result])
36
+ end
37
+
38
+ it "should retrieve all user's account" do
39
+ account.all_accounts.should eql([index_result])
40
+ end
41
+ end
42
+
43
+ context "on failure" do
44
+ context "when user's not found" do
45
+ before do
46
+ account.stub(:get).and_return(invalid_user_result)
47
+ end
48
+
49
+ it "should raise USER_NOT_FOUND error" do
50
+ expect {
51
+ account.all_accounts
52
+ }.to raise_error(CookieJar::Error::UserNotFound)
53
+ end
54
+ end
55
+
56
+ context "when unauthorized" do
57
+ before do
58
+ account.stub(:get).and_return(unauthorized_message)
59
+ end
60
+
61
+ it "should raise access denied error" do
62
+ expect {
63
+ account.all_accounts
64
+ }.to raise_error(CookieJar::Error::AccessDenied)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ context "#get_account" do
71
+ let!(:user_account) {
72
+ {
73
+ "id" => "index-id",
74
+ "account_type_id" => "savings",
75
+ "user_id" => user_id,
76
+ "description" => "Default savings account",
77
+ "created_at" => Time.now,
78
+ "updated_at" => Time.now
79
+ }
80
+ }
81
+ context "successful" do
82
+ before do
83
+ account.stub(:get).and_return(user_account)
84
+ end
85
+
86
+ it "should retrieve the user's account" do
87
+ account.get_account(account_id).should eql(user_account)
88
+ end
89
+ end
90
+
91
+ context "failure" do
92
+ context "when unauthorized" do
93
+ before do
94
+ account.stub(:get).and_return(unauthorized_message)
95
+ end
96
+
97
+ it "should raise access denied error" do
98
+ expect {
99
+ account.get_account(account_id)
100
+ }.to raise_error(CookieJar::Error::AccessDenied)
101
+ end
102
+ end
103
+
104
+ context "when account is not found" do
105
+ before do
106
+ account.stub(:get).and_return(account_not_found_message)
107
+ end
108
+
109
+ it "should raise access denied error" do
110
+ expect {
111
+ account.get_account(account_not_found_id)
112
+ }.to raise_error(CookieJar::Error::AccountNotFound)
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ context "#create_account" do
119
+ let!(:account_description) { "default account description" }
120
+ context "successful" do
121
+ before do
122
+ account.stub(:post).and_return(index_result)
123
+ end
124
+
125
+ it "should create account" do
126
+ account.create_account(account_description)
127
+ account.account_id.should eql(index_result["id"])
128
+ end
129
+ end
130
+
131
+ context "failure" do
132
+ context "when params are insufficient" do
133
+
134
+ before do
135
+ account.stub(:post).and_return(insufficient_params_message)
136
+ end
137
+
138
+ it "should raise INSUFFICIENT_PARAMS error" do
139
+ expect {
140
+ account.create_account("")
141
+ }.to raise_error(CookieJar::Error::InsufficientParams)
142
+ end
143
+ end
144
+
145
+ context "when unauthorized" do
146
+ before do
147
+ account.stub(:post).and_return({:code => 401})
148
+ end
149
+
150
+ it "should raise ACCESS_DENIED error" do
151
+ expect {
152
+ account.create_account("oh shit")
153
+ }.to raise_error(CookieJar::Error::AccessDenied)
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ context "#update_account" do
160
+ let!(:account_description) { "default account description" }
161
+ context "successful" do
162
+ before do
163
+ account.stub(:put).and_return(index_result)
164
+ end
165
+
166
+ it "should update account" do
167
+ account.update_account(account_id, account_description)
168
+ account.account_id.should eql(index_result["id"])
169
+ end
170
+ end
171
+
172
+ context "failure" do
173
+ context "when params are insufficient" do
174
+ before do
175
+ account.stub(:put).and_return(insufficient_params_message)
176
+ end
177
+
178
+ it "should raise INSUFFICIENT_PARAMS error" do
179
+ expect {
180
+ account.update_account("","")
181
+ }.to raise_error(CookieJar::Error::InsufficientParams)
182
+ end
183
+ end
184
+
185
+ context "when unauthorized" do
186
+ before do
187
+ account.stub(:put).and_return({:code => 401})
188
+ end
189
+
190
+ it "should raise ACCESS_DENIED error" do
191
+ expect {
192
+ account.update_account("oh shit", "oh shit")
193
+ }.to raise_error(CookieJar::Error::AccessDenied)
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
@@ -0,0 +1,273 @@
1
+ require 'spec_helper'
2
+
3
+ describe CookieJar::Wallet::Banker do
4
+ let!(:circulation) {
5
+ {
6
+ "id" => "circulation-id",
7
+ "amount" => 1000,
8
+ "state" => "initiated",
9
+ "created_at" => Time.now,
10
+ "updated_at" => Time.now
11
+ }
12
+ }
13
+
14
+ let!(:user_id) { "user-id" }
15
+ let!(:circulation_id) { circulation["id"] }
16
+ let!(:amount) { 1000 }
17
+ let!(:passcode) { 1000 }
18
+ let!(:invalid_circulation_id) { "invalid-circulation-id" }
19
+ let!(:user_ip) { "127.0.0.1" }
20
+ let!(:invalid_ip) { "invalid-ip" }
21
+ let!(:user_url) { "http://biscuit-aa.com/users/#{user_id}" }
22
+ let!(:user_agent) { "user-agent" }
23
+ let!(:insufficient_params_message) { {"message" => "Insufficient parameters."} }
24
+ let!(:unauthorized_message) { {"message" => "You're not authorized to access the resource"} }
25
+ let!(:circulation_not_found_message) { {"message" => "Couldn't find Transfer id=#{invalid_circulation_id}"} }
26
+ let!(:circulation_already_activated_message) { {"message" => "Circulation is already activated."} }
27
+ let!(:creation_failed_message) { {"message" => "Creation of circulation failed."} }
28
+
29
+ before do
30
+ CookieJar::Wallet::Banker.any_instance.stub(:init_token).and_return(double OAuth2::AccessToken)
31
+ CookieJar::Wallet::Banker.any_instance.stub(:get_token).and_return(double OAuth2::AccessToken, :token => "sample1234")
32
+ CookieJar::Wallet::Banker.any_instance.stub(:get_user_id).and_return(user_id)
33
+ end
34
+
35
+ let!(:user) { CookieJar::Wallet::Banker.new(*INITIALIZE_BANKER_VALUES) }
36
+
37
+ context "#all_circulations" do
38
+ context "successful" do
39
+ before do
40
+ user.stub(:get).and_return([circulation])
41
+ end
42
+
43
+ it "should return list of circulations" do
44
+ user.all_circulations(user_url, user_ip, user_agent).should eql([circulation])
45
+ end
46
+ end
47
+
48
+ context "on failure" do
49
+ context "when params are not sufficient" do
50
+ before do
51
+ user.stub(:get).and_return(insufficient_params_message)
52
+ end
53
+
54
+ it "should return INSUFFICIENT_PARAMS error message." do
55
+ expect {
56
+ user.all_circulations("", user_ip, "")
57
+ }.to raise_error(CookieJar::Error::InsufficientParams)
58
+ end
59
+ end
60
+
61
+ context "when user_ip is not valid" do
62
+ it "should return INVALID_IP_ADDRESS error message." do
63
+ expect {
64
+ user.all_circulations(user_url, invalid_ip, user_agent)
65
+ }.to raise_error(CookieJar::Error::InvalidUserIP)
66
+ end
67
+ end
68
+
69
+ context "when unauthorized" do
70
+ before do
71
+ user.stub(:get).and_return(unauthorized_message)
72
+ end
73
+
74
+ it "should raise ACCESS_DENIED error" do
75
+ expect {
76
+ user.all_circulations(user_url, user_ip, user_agent)
77
+ }.to raise_error(CookieJar::Error::AccessDenied)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ context "#get_circulation" do
84
+ context "successful" do
85
+ before do
86
+ user.stub(:get).and_return(circulation)
87
+ end
88
+
89
+ it "should return circulation's information" do
90
+ user.get_circulation(circulation_id, user_url, user_ip, user_agent).should eql(circulation)
91
+ end
92
+ end
93
+
94
+ context "on failure" do
95
+ context "when unauthorized" do
96
+ before do
97
+ user.stub(:get).and_return(unauthorized_message)
98
+ end
99
+
100
+ it "should raise ACCESS_DENIED error" do
101
+ expect {
102
+ user.get_circulation(circulation_id, user_url, user_ip, user_agent)
103
+ }.to raise_error(CookieJar::Error::AccessDenied)
104
+ end
105
+ end
106
+
107
+ context "when unauthorized with 401 error code" do
108
+ before do
109
+ user.stub(:get).and_return({:code => 401, :message => "You're not authorized to access the resource"})
110
+ end
111
+
112
+ it "should raise ACCESS_DENIED error" do
113
+ expect {
114
+ user.get_circulation(circulation_id, user_url, user_ip, user_agent)
115
+ }.to raise_error(CookieJar::Error::AccessDenied)
116
+ end
117
+ end
118
+
119
+ context "when user_ip is not valid" do
120
+ it "should raise USER IP NOT VALID error" do
121
+ expect {
122
+ user.get_circulation(circulation_id, user_url, invalid_ip, user_agent)
123
+ }.to raise_error(CookieJar::Error::InvalidUserIP)
124
+ end
125
+ end
126
+
127
+ context "when insufficient parameters" do
128
+ before do
129
+ user.stub(:get).and_return(insufficient_params_message)
130
+ end
131
+ it "should raise INSUFFICIENT parameters" do
132
+ expect {
133
+ user.get_circulation(circulation_id, "", user_ip, "")
134
+ }.to raise_error(CookieJar::Error::InsufficientParams)
135
+ end
136
+ end
137
+
138
+ context "when invalid circulation id" do
139
+ before do
140
+ user.stub(:get).and_return(circulation_not_found_message)
141
+ end
142
+ it "should raise CIRCULATION NOT FOUND error" do
143
+ expect {
144
+ user.get_circulation(invalid_circulation_id, user_url, user_ip, user_agent)
145
+ }.to raise_error(CookieJar::Error::CirculationNotFound)
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ context "#create_circulation" do
152
+ context "successful" do
153
+ before do
154
+ user.stub(:post).and_return(circulation)
155
+ end
156
+ it "should create circulation" do
157
+ user.create_circulation(amount, user_url, user_ip, user_agent).should eql(circulation)
158
+ end
159
+ end
160
+
161
+ context "on failure" do
162
+ context "when insufficient parameters" do
163
+ before do
164
+ user.stub(:post).and_return(insufficient_params_message)
165
+ end
166
+ it "should raise INSUFFICIENT parameters" do
167
+ expect {
168
+ user.create_circulation(circulation_id, "", user_ip, "")
169
+ }.to raise_error(CookieJar::Error::InsufficientParams)
170
+ end
171
+ end
172
+
173
+ context "when unauthorized" do
174
+ before do
175
+ user.stub(:post).and_return(unauthorized_message)
176
+ end
177
+
178
+ it "should raise ACCESS_DENIED error" do
179
+ expect {
180
+ user.create_circulation(circulation_id, user_url, user_ip, user_agent)
181
+ }.to raise_error(CookieJar::Error::AccessDenied)
182
+ end
183
+ end
184
+
185
+ context "when creation of circulation failed" do
186
+ before do
187
+ user.stub(:post).and_return(creation_failed_message)
188
+ end
189
+
190
+ it "should raise CREATION_FAILED error message" do
191
+ expect {
192
+ user.create_circulation(invalid_circulation_id, user_url, user_ip, user_agent)
193
+ }.to raise_error(CookieJar::Error::CreateError)
194
+ end
195
+ end
196
+
197
+ context "when user_ip is not valid" do
198
+ it "should raise USER IP NOT VALID error" do
199
+ expect {
200
+ user.create_circulation(invalid_circulation_id, user_url, invalid_ip, user_agent)
201
+ }.to raise_error(CookieJar::Error::InvalidUserIP)
202
+ end
203
+ end
204
+ end
205
+ end
206
+
207
+ context "#activate_circulation" do
208
+ context "successful" do
209
+ before do
210
+ user.stub(:put).and_return(circulation)
211
+ end
212
+
213
+ it "should activate circulation" do
214
+ user.activate_circulation(circulation_id, user_url, user_ip, user_agent).should eql(circulation)
215
+ end
216
+ end
217
+
218
+ context "on failure" do
219
+ context "when unauthorized" do
220
+ before do
221
+ user.stub(:put).and_return(unauthorized_message)
222
+ end
223
+
224
+ it "should raise ACCESS_DENIED error" do
225
+ expect {
226
+ user.activate_circulation(circulation_id, user_url, user_ip, user_agent)
227
+ }.to raise_error(CookieJar::Error::AccessDenied)
228
+ end
229
+ end
230
+
231
+ context "when insufficient parameter" do
232
+ before do
233
+ user.stub(:put).and_return(insufficient_params_message)
234
+ end
235
+ it "should raise INSUFFICIENT parameters" do
236
+ expect {
237
+ user.activate_circulation(circulation_id, "", user_ip, "")
238
+ }.to raise_error(CookieJar::Error::InsufficientParams)
239
+ end
240
+ end
241
+
242
+ context "when invalid circulation id" do
243
+ before do
244
+ user.stub(:put).and_return(circulation_not_found_message)
245
+ end
246
+ it "should raise CIRCULATION NOT FOUND error" do
247
+ expect {
248
+ user.activate_circulation(invalid_circulation_id, user_url, user_ip, user_agent)
249
+ }.to raise_error(CookieJar::Error::CirculationNotFound)
250
+ end
251
+ end
252
+
253
+ context "when circulation is already activated" do
254
+ before do
255
+ user.stub(:put).and_return(circulation_already_activated_message)
256
+ end
257
+ it "should raise CIRCULATION NOT FOUND error" do
258
+ expect {
259
+ user.activate_circulation(invalid_circulation_id, user_url, user_ip, user_agent)
260
+ }.to raise_error(CookieJar::Error::CirculationAlreadyActivated)
261
+ end
262
+ end
263
+
264
+ context "when invalid USER IP" do
265
+ it "should raise USER IP NOT VALID error" do
266
+ expect {
267
+ user.activate_circulation(invalid_circulation_id, user_url, invalid_ip, user_agent)
268
+ }.to raise_error(CookieJar::Error::InvalidUserIP)
269
+ end
270
+ end
271
+ end
272
+ end
273
+ end