fake_bsmobil 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63b52174e5d7028294318290e33ba350999a2f39
4
- data.tar.gz: b661647384395a509587483e4f6af6601e396226
3
+ metadata.gz: 489e0c4b34aaf26dc581bdbb32ad10cb24dbe09f
4
+ data.tar.gz: ac433145feb14af030130c204b07200c08005d05
5
5
  SHA512:
6
- metadata.gz: 25074d725cea8cc8c5d871df6c3989ac2b6627e05bc8c009bbf6b5a2c61ea49c3d62287131ce4abd900ea112cc791910cae52f8e5c50c3c49a67687ce3931ba6
7
- data.tar.gz: 16512f9b1b09b8e989bb9a4d69a7b8e952d495cfcf7a0605f43517bdc1a92ef930f3c912da2aa1c6d7fa175c9c6e3e1489feda5896269fc2ee5994dd6972405b
6
+ metadata.gz: c7c9d9cc1412650c5842c4c5e241afe39aabb333a3935087f8b580b6813e406172af560edaed38b9285ce782c7203a25aea1adf3b343aea3d65de370f7e03065
7
+ data.tar.gz: 6784c6c83f0f12b1d39ffb76d67bd751fea8f1f190f38112c90e2a98e028d95e18d94bc565901003dafb05d8279a14cceb888e91e12cc5ca1f1195d4e3ae2558
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --format documentation
2
1
  --color
2
+ --require spec_helper
@@ -0,0 +1 @@
1
+ ruby-2.2
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.9"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'rack-test'
24
25
 
25
26
  spec.add_runtime_dependency 'roda', '~> 2.1'
26
27
  spec.add_runtime_dependency 'json-schema'
@@ -1,201 +1,44 @@
1
1
  require 'fake_bsmobil/version'
2
- require 'json-schema'
3
2
  require 'logger'
4
- require 'securerandom'
3
+
4
+
5
+ require 'json-schema'
5
6
 
6
7
  module FakeBsmobil
7
- autoload :Datastore, 'fake_bsmobil/datastore'
8
+ autoload :DataStore, 'fake_bsmobil/data_store'
8
9
  autoload :Api, 'fake_bsmobil/api'
9
10
  autoload :Schema, 'fake_bsmobil/schema'
10
11
  autoload :Generator, 'fake_bsmobil/generator'
12
+ autoload :Bank, 'fake_bsmobil/bank'
11
13
 
12
- module_function
13
-
14
- def user
15
- @user
16
- end
17
-
18
- def user=(user)
19
- @user = user
20
- end
21
-
22
- def datastore
23
- @datastore ||= Datastore.new
24
- end
25
-
26
- def validator
27
- JSON::Validator
28
- end
29
14
 
30
15
  ValidationError = JSON::Schema::ValidationError
31
16
 
32
- def logger
33
- @logger = Logger.new($stdout)
34
- end
35
-
36
- def generate_session_id
37
- @sessions ||= Set.new
38
- session_id = SecureRandom.base64(18) + '.pmobil3'
39
- @sessions.add(session_id)
40
- session_id
41
- end
42
-
43
- def generate_user
44
- {
45
- dni: Generator.dni,
46
- name: Generator.name,
47
- sex: Generator.sex,
48
- phoneNumber: Generator.phone_number,
49
- contractNumber: Generator.contract_number,
50
- hasMoreContracts: false,
51
- customization: '1H4XX000000032010011010211301211000000000010110011',
52
- isNewUser: false,
53
- pendingSignOperations: 0,
54
- personNumber: Generator.person_number,
55
- idType: '01',
56
- loginType: 5,
57
- promoLayer: {code: '', url: 'resources/mobapp/layers//', nShows: 0, exist: false},
58
- cardId: Generator.card_id,
59
- refreshActiveAgent: 10,
60
- signatureType: 'tpc',
61
- vTPCInscriptionKey: ''
62
- }
63
- end
64
-
65
- def session(info)
66
- logger.info info
67
- validator.validate!(Schema::SESSION, info)
68
-
69
- {
70
- user: self.user ||= generate_user,
71
- isInputTypeNumberSupported: true
17
+ class InvalidInputError < StandardError
18
+ def to_json
19
+ { errorMessage: message, code: '' }.to_json
20
+ end
72
21
 
73
- }
22
+ def status
23
+ 500
24
+ end
74
25
  end
75
26
 
76
- def products
77
- {
78
- accountProduct: {
79
- amount: amount = Generator.amount,
80
- accounts: [ Generator.account(amount: amount) ]
81
- },
82
- cardProduct: {
83
- amount: {value: "0,00", currency: "EUR"},
84
- cards: [Generator.debit_card]
85
- },
86
- investmentProduct: {
87
- amount: {:value => "", :currency => ""},
88
- securities: {:amount => {:value => "", :currency => ""}, :accounts => [], :type => ""},
89
- pensionPlan: {amount: {:value => "", :currency => ""}, accounts: [], type: ""},
90
- insuredPlanForecast: {amount: {:value => "", :currency => ""}, policies: []},
91
- investmentFund: {:amount => {:value => "", :currency => ""}, :accounts => [], :type => ""},
92
- depositProduct: {:amount => {:value => "", :currency => ""}, :accounts => []},
93
- unknowns: []
94
- },
95
- financialProduct: {
96
- amount: {:value => "0,00", :currency => "EUR"},
97
- loan: {:amount => {:value => "", :currency => ""}, :accounts => []},
98
- credit: {:amount => {:value => "", :currency => ""}, :accounts => []},
99
- expansionLineGP: {amount: {:value => "0,00", :currency => "EUR"},
100
- expansionLines: []},
101
- unknowns: []
102
- },
103
- other: {amount: {value: "", currency: ""}, unknowns: []}
104
- }
105
- end
27
+ class << self
28
+ def logger
29
+ @logger ||= Logger.new($stdout)
30
+ end
106
31
 
107
- def account_movements(json)
108
- logger.info json
109
- validator.validate!(Schema::ACCOUNT, json)
32
+ def bank
33
+ @bank ||= FakeBsmobil::Bank.new
34
+ end
110
35
 
111
- account_info = JSON.parse(json).fetch('account') # FIXME: string keys :(
112
- account = Generator.full_account(account_info)
36
+ def validator
37
+ JSON::Validator
38
+ end
113
39
 
114
- movements(account)
40
+ def method_missing(*args, &block)
41
+ bank.public_send(*args, &block)
42
+ end
115
43
  end
116
-
117
- def movements(account)
118
- Generator.movements(account)
119
- end
120
-
121
- def accounts
122
- {
123
- accounts: [Generator.full_account],
124
- }.merge(paginator)
125
- end
126
-
127
- def cards(_filter)
128
- {
129
- cards: [ Generator.debit_card ]
130
- }.merge(paginator)
131
- end
132
-
133
- def unconfirmed_card_movements(json)
134
- card = load_card(json)
135
-
136
- card_movements(card).merge(cardMovements: Generator.unconfirmed_card_movements)
137
- end
138
-
139
- def card_movements(card)
140
- {
141
- contractNumber: Generator.contract_number(length: 15),
142
- contractOwner: name = Generator.contract_name,
143
- paymentType: "",
144
- ptorete: "",
145
- previousBalance: {value: "0,00", currency: "EUR"},
146
- currentMonthBalance: {:value => "0,00", :currency => "EUR"},
147
- totalAmount: Generator.amount,
148
- pendingLiquidationBalance: {:value => "0,00", :currency => "EUR"},
149
- availableBalance: {:value => "0,00", :currency => "EUR"},
150
- willingBalance: {:value => "0,00", :currency => "EUR"},
151
- chargeAccount: {
152
- description: "",
153
- availability: "",
154
- owner: name,
155
- product: "",
156
- productType: "",
157
- entityCode: "",
158
- contractCode: "",
159
- bic: "",
160
- number: Generator.account_number,
161
- iban: "",
162
- amount: {:value => "", :currency => ""},
163
- numOwners: 0,
164
- isOwner: false,
165
- isSBPManaged: false,
166
- isIberSecurities: false,
167
- joint: "",
168
- mobileWarning: ""
169
- },
170
- card: card,
171
- hasUnconfirmedMovements: true
172
- }.merge(paginator)
173
- end
174
-
175
- def confirmed_card_movements(json)
176
- card = load_card(json)
177
-
178
- card_movements(card).merge(cardMovements: Generator.confirmed_card_movements)
179
- end
180
-
181
- def paginator
182
- {
183
- paginator: {
184
- page: 0,
185
- itemsPerPage: 20,
186
- order: 'desc',
187
- totalItems: 1
188
- }
189
- }
190
- end
191
-
192
- def load_card(json)
193
- logger.info json
194
- validator.validate!(Schema::CARD, json)
195
-
196
- card_info = JSON.parse(json).fetch('card') # FIXME: string keys :(
197
-
198
- Generator.debit_card(card_info)
199
- end
200
-
201
44
  end
@@ -26,18 +26,31 @@ module FakeBsmobil
26
26
  }
27
27
  end
28
28
 
29
- def session_id
30
-
31
- end
32
-
33
29
  plugin :param_matchers
34
30
  plugin :header_matchers
35
31
  plugin :hash_matcher
32
+ plugin :cookies
33
+ plugin :error_handler
34
+
35
+
36
+ def handle_error(exception)
37
+ case exception
38
+ when FakeBsmobil::InvalidInputError
39
+ response.status = exception.status
40
+ response['Content-Type'] = 'application/json'
41
+ response.write exception.to_json
42
+ else raise exception
43
+ end
44
+ end
36
45
 
37
46
  hash_matcher :content_type do |v|
38
47
  self.content_type == v
39
48
  end
40
49
 
50
+ def session_id
51
+ request.cookies['JSESSIONID']
52
+ end
53
+
41
54
  JSON_TYPE = 'application/json'.freeze
42
55
 
43
56
  route do |r|
@@ -49,7 +62,6 @@ module FakeBsmobil
49
62
 
50
63
  r.on 'bsmobil/api', accept: 'application/vnd.idk.bsmobil-v2+json', header: 'Content-Type' do |content_type|
51
64
 
52
-
53
65
  response.headers.merge!(DEFAULT_HEADERS)
54
66
 
55
67
  r.post 'session' do
@@ -57,14 +69,24 @@ module FakeBsmobil
57
69
 
58
70
  begin
59
71
  user = FakeBsmobil.session(request.body.read)
60
- session = FakeBsmobil.generate_session_id
61
- response.headers['Set-Cookie'] = "JSESSIONID=#{session}; Path=/bsmobil"
62
- user
72
+
73
+ if user
74
+ response.set_cookie 'JSESSIONID', value: FakeBsmobil.session_id, path: '/bsmobil'
75
+ user
76
+ else
77
+ response.status = 500
78
+ { errorMessage: 'Z23226: The USERNAME or ACCESS CODE is incorrect. Please enter details again.', code: '' }
79
+ end
63
80
  rescue FakeBsmobil::ValidationError
64
81
  validation_error
65
82
  end
66
83
  end
67
84
 
85
+ unless FakeBsmobil.find_session(session_id)
86
+ response.status = 403
87
+ break { errorMessage: 'Session time out for security. If you wish to re/connect.', code: '' }
88
+ end
89
+
68
90
  r.post 'activeagent' do
69
91
  {
70
92
  name: name = Generator.name,
@@ -0,0 +1,226 @@
1
+ require 'forwardable'
2
+
3
+ require 'securerandom'
4
+
5
+
6
+ module FakeBsmobil
7
+ class Bank
8
+ extend Forwardable
9
+
10
+ def initialize
11
+ @session_id = nil
12
+ @sessions = Set.new
13
+ end
14
+
15
+ def user
16
+ @user
17
+ end
18
+
19
+ def user=(user)
20
+ @user = user
21
+ end
22
+
23
+ def session_id
24
+ @session_id || generate_session_id
25
+ end
26
+
27
+ def use_session(id)
28
+ _session_id, @session_id = @session_id, id
29
+ yield if block_given?
30
+ ensure
31
+ @session_id = _session_id
32
+ end
33
+
34
+ def find_session(id)
35
+ @sessions.include?(id)
36
+ end
37
+
38
+
39
+ def generate_user
40
+ {
41
+ dni: Generator.dni,
42
+ name: Generator.name,
43
+ sex: Generator.sex,
44
+ phoneNumber: Generator.phone_number,
45
+ contractNumber: Generator.contract_number,
46
+ hasMoreContracts: false,
47
+ customization: '1H4XX000000032010011010211301211000000000010110011',
48
+ isNewUser: false,
49
+ pendingSignOperations: 0,
50
+ personNumber: Generator.person_number,
51
+ idType: '01',
52
+ loginType: 5,
53
+ promoLayer: {code: '', url: 'resources/mobapp/layers//', nShows: 0, exist: false},
54
+ cardId: Generator.card_id,
55
+ refreshActiveAgent: 10,
56
+ signatureType: 'tpc',
57
+ vTPCInscriptionKey: ''
58
+ }
59
+ end
60
+
61
+ def session(info)
62
+ logger.info info
63
+ validator.validate!(Schema::SESSION, info)
64
+
65
+ login = JSON.parse(info)
66
+
67
+ login.fetch('password'){ return }.empty? and return
68
+ login.fetch('userName') { return }.empty? and return
69
+
70
+ {
71
+ user: self.user ||= generate_user,
72
+ isInputTypeNumberSupported: true
73
+
74
+ }
75
+ end
76
+
77
+ def products
78
+ {
79
+ accountProduct: {
80
+ amount: amount = Generator.amount,
81
+ accounts: [ Generator.account(amount: amount) ]
82
+ },
83
+ cardProduct: {
84
+ amount: {value: "0,00", currency: "EUR"},
85
+ cards: [Generator.debit_card]
86
+ },
87
+ investmentProduct: {
88
+ amount: {:value => "", :currency => ""},
89
+ securities: {:amount => {:value => "", :currency => ""}, :accounts => [], :type => ""},
90
+ pensionPlan: {amount: {:value => "", :currency => ""}, accounts: [], type: ""},
91
+ insuredPlanForecast: {amount: {:value => "", :currency => ""}, policies: []},
92
+ investmentFund: {:amount => {:value => "", :currency => ""}, :accounts => [], :type => ""},
93
+ depositProduct: {:amount => {:value => "", :currency => ""}, :accounts => []},
94
+ unknowns: []
95
+ },
96
+ financialProduct: {
97
+ amount: {:value => "0,00", :currency => "EUR"},
98
+ loan: {:amount => {:value => "", :currency => ""}, :accounts => []},
99
+ credit: {:amount => {:value => "", :currency => ""}, :accounts => []},
100
+ expansionLineGP: {amount: {:value => "0,00", :currency => "EUR"},
101
+ expansionLines: []},
102
+ unknowns: []
103
+ },
104
+ other: {amount: {value: "", currency: ""}, unknowns: []}
105
+ }
106
+ end
107
+
108
+ def account_movements(json)
109
+ logger.info json
110
+ validator.validate!(Schema::ACCOUNT, json)
111
+
112
+ query = JSON.parse(json)
113
+
114
+ begin
115
+ Date.strptime(query.fetch('dateFrom'), '%d-%m-%Y')
116
+ Date.strptime(query.fetch('dateTo'), '%d-%m-%Y')
117
+ rescue ArgumentError
118
+ raise InvalidInputError, 'CDSO006: Wrong date. '
119
+ end
120
+
121
+ account_info = JSON.parse(json).fetch('account') # FIXME: string keys :(
122
+ account = Generator.full_account(account_info)
123
+
124
+ Generator.movements(account)
125
+ end
126
+
127
+ def use_account(account = {})
128
+ datastore.account = account
129
+ end
130
+
131
+ def accounts
132
+ {
133
+ accounts: [datastore.account || Generator.full_account],
134
+ }.merge(paginator)
135
+ end
136
+
137
+ def cards(_filter)
138
+ {
139
+ cards: [ Generator.debit_card ]
140
+ }.merge(paginator)
141
+ end
142
+
143
+ def unconfirmed_card_movements(json)
144
+ card = load_card(json)
145
+
146
+ card_movements(card).merge(cardMovements: Generator.unconfirmed_card_movements)
147
+ end
148
+
149
+ def card_movements(card)
150
+ {
151
+ contractNumber: Generator.contract_number(length: 15),
152
+ contractOwner: name = Generator.contract_name,
153
+ paymentType: "",
154
+ ptorete: "",
155
+ previousBalance: {value: "0,00", currency: "EUR"},
156
+ currentMonthBalance: {:value => "0,00", :currency => "EUR"},
157
+ totalAmount: Generator.amount,
158
+ pendingLiquidationBalance: {:value => "0,00", :currency => "EUR"},
159
+ availableBalance: {:value => "0,00", :currency => "EUR"},
160
+ willingBalance: {:value => "0,00", :currency => "EUR"},
161
+ chargeAccount: {
162
+ description: "",
163
+ availability: "",
164
+ owner: name,
165
+ product: "",
166
+ productType: "",
167
+ entityCode: "",
168
+ contractCode: "",
169
+ bic: "",
170
+ number: Generator.account_number,
171
+ iban: "",
172
+ amount: {:value => "", :currency => ""},
173
+ numOwners: 0,
174
+ isOwner: false,
175
+ isSBPManaged: false,
176
+ isIberSecurities: false,
177
+ joint: "",
178
+ mobileWarning: ""
179
+ },
180
+ card: card,
181
+ hasUnconfirmedMovements: true
182
+ }.merge(paginator)
183
+ end
184
+
185
+ def confirmed_card_movements(json)
186
+ card = load_card(json)
187
+
188
+ card_movements(card).merge(cardMovements: Generator.confirmed_card_movements)
189
+ end
190
+
191
+ def load_card(json)
192
+ logger.info json
193
+ validator.validate!(Schema::CARD, json)
194
+
195
+ card_info = JSON.parse(json).fetch('card') # FIXME: string keys :(
196
+
197
+ Generator.debit_card(card_info)
198
+ end
199
+
200
+ def datastore
201
+ @datastore ||= DataStore.new
202
+ end
203
+
204
+ protected
205
+
206
+ def generate_session_id
207
+ session_id = SecureRandom.base64(18) + '.pmobil3'
208
+ @sessions.add(session_id)
209
+ session_id
210
+ end
211
+
212
+ def paginator
213
+ {
214
+ paginator: {
215
+ page: 0,
216
+ itemsPerPage: 20,
217
+ order: 'desc',
218
+ totalItems: 1
219
+ }
220
+ }
221
+ end
222
+
223
+ def_delegators FakeBsmobil, :logger, :validator
224
+
225
+ end
226
+ end
@@ -0,0 +1,11 @@
1
+ module FakeBsmobil
2
+ class DataStore
3
+
4
+ attr_reader :account
5
+
6
+ # TODO: when merging, raise on unknown keys
7
+ def account=(attributes)
8
+ @account = FakeBsmobil::Generator.full_account.merge(attributes)
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module FakeBsmobil
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fake_bsmobil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Cichra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-06 00:00:00.000000000 Z
11
+ date: 2015-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: roda
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -89,6 +103,7 @@ extra_rdoc_files: []
89
103
  files:
90
104
  - ".gitignore"
91
105
  - ".rspec"
106
+ - ".ruby-version"
92
107
  - ".travis.yml"
93
108
  - Gemfile
94
109
  - LICENSE.txt
@@ -100,7 +115,8 @@ files:
100
115
  - fake_bsmobil.gemspec
101
116
  - lib/fake_bsmobil.rb
102
117
  - lib/fake_bsmobil/api.rb
103
- - lib/fake_bsmobil/datastore.rb
118
+ - lib/fake_bsmobil/bank.rb
119
+ - lib/fake_bsmobil/data_store.rb
104
120
  - lib/fake_bsmobil/generator.rb
105
121
  - lib/fake_bsmobil/generator/movements.rb
106
122
  - lib/fake_bsmobil/schema.rb
@@ -130,3 +146,4 @@ signing_key:
130
146
  specification_version: 4
131
147
  summary: Ruby server faking Bank Sabadell mobile API
132
148
  test_files: []
149
+ has_rdoc:
@@ -1,5 +0,0 @@
1
- module FakeBsmobil
2
- class Datastore
3
-
4
- end
5
- end