kaui 0.6.6 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/Gemfile.lock +51 -37
  4. data/README.md +5 -0
  5. data/Rakefile +1 -1
  6. data/app/controllers/kaui/account_emails_controller.rb +26 -44
  7. data/app/controllers/kaui/account_tags_controller.rb +26 -38
  8. data/app/controllers/kaui/account_timelines_controller.rb +47 -50
  9. data/app/controllers/kaui/accounts_controller.rb +68 -148
  10. data/app/controllers/kaui/bundle_tags_controller.rb +32 -21
  11. data/app/controllers/kaui/bundles_controller.rb +44 -69
  12. data/app/controllers/kaui/chargebacks_controller.rb +31 -61
  13. data/app/controllers/kaui/charges_controller.rb +19 -21
  14. data/app/controllers/kaui/credits_controller.rb +22 -36
  15. data/app/controllers/kaui/custom_fields_controller.rb +15 -13
  16. data/app/controllers/kaui/engine_controller.rb +8 -10
  17. data/app/controllers/kaui/invoice_items_controller.rb +24 -41
  18. data/app/controllers/kaui/invoices_controller.rb +36 -46
  19. data/app/controllers/kaui/payment_methods_controller.rb +84 -28
  20. data/app/controllers/kaui/payments_controller.rb +64 -26
  21. data/app/controllers/kaui/refunds_controller.rb +27 -107
  22. data/app/controllers/kaui/subscriptions_controller.rb +88 -132
  23. data/app/controllers/kaui/tag_definitions_controller.rb +32 -79
  24. data/app/controllers/kaui/tags_controller.rb +16 -14
  25. data/app/controllers/kaui/transactions_controller.rb +25 -0
  26. data/app/helpers/kaui/money_helper.rb +8 -0
  27. data/app/helpers/kaui/subscription_helper.rb +115 -0
  28. data/app/helpers/kaui/uuid_helper.rb +9 -0
  29. data/app/models/kaui/ability.rb +2 -2
  30. data/app/models/kaui/account.rb +25 -42
  31. data/app/models/kaui/account_email.rb +11 -48
  32. data/app/models/kaui/account_timeline.rb +1 -5
  33. data/app/models/kaui/audit_log.rb +1 -15
  34. data/app/models/kaui/base.rb +1 -126
  35. data/app/models/kaui/bundle.rb +47 -14
  36. data/app/models/kaui/catalog.rb +2 -0
  37. data/app/models/kaui/chargeback.rb +24 -36
  38. data/app/models/kaui/credit.rb +5 -20
  39. data/app/models/kaui/custom_field.rb +9 -5
  40. data/app/models/kaui/invoice.rb +9 -53
  41. data/app/models/kaui/invoice_item.rb +8 -18
  42. data/app/models/kaui/invoice_payment.rb +24 -0
  43. data/app/models/kaui/payment.rb +14 -32
  44. data/app/models/kaui/payment_method.rb +36 -0
  45. data/app/models/kaui/rails_methods.rb +7 -9
  46. data/app/models/kaui/refund.rb +5 -20
  47. data/app/models/kaui/subscription.rb +7 -27
  48. data/app/models/kaui/tag.rb +10 -6
  49. data/app/models/kaui/tag_definition.rb +26 -34
  50. data/app/models/kaui/transaction.rb +30 -0
  51. data/app/models/kaui/user.rb +2 -0
  52. data/app/views/kaui/account_emails/_form.html.erb +8 -28
  53. data/app/views/kaui/account_emails/show.html.erb +10 -10
  54. data/app/views/kaui/account_tags/_account_tags_table.html.erb +20 -18
  55. data/app/views/kaui/account_tags/_form.html.erb +42 -0
  56. data/app/views/kaui/account_tags/edit.html.erb +1 -41
  57. data/app/views/kaui/account_tags/show.html.erb +6 -3
  58. data/app/views/kaui/account_timelines/show.html.erb +273 -302
  59. data/app/views/kaui/accounts/_form.html.erb +116 -0
  60. data/app/views/kaui/accounts/index.html.erb +13 -11
  61. data/app/views/kaui/accounts/new.html.erb +5 -0
  62. data/app/views/kaui/accounts/show.html.erb +101 -99
  63. data/app/views/kaui/bundle_tags/_bundle_tags_table.html.erb +29 -9
  64. data/app/views/kaui/bundle_tags/_form.html.erb +41 -0
  65. data/app/views/kaui/bundle_tags/edit.html.erb +2 -34
  66. data/app/views/kaui/bundle_tags/show.html.erb +6 -0
  67. data/app/views/kaui/bundles/index.html.erb +9 -9
  68. data/app/views/kaui/bundles/show.html.erb +17 -15
  69. data/app/views/kaui/bundles/transfer.html.erb +25 -24
  70. data/app/views/kaui/chargebacks/_form.html.erb +44 -0
  71. data/app/views/kaui/chargebacks/new.html.erb +3 -115
  72. data/app/views/kaui/charges/_form.html.erb +73 -0
  73. data/app/views/kaui/charges/new.html.erb +2 -91
  74. data/app/views/kaui/credits/_form.html.erb +73 -0
  75. data/app/views/kaui/credits/new.html.erb +2 -85
  76. data/app/views/kaui/invoice_items/edit.html.erb +77 -78
  77. data/app/views/kaui/invoices/show.html.erb +3 -10
  78. data/app/views/kaui/layouts/kaui_application.html.erb +0 -2
  79. data/app/views/kaui/payment_methods/_form.html.erb +30 -0
  80. data/app/views/kaui/payment_methods/_new_creditcard_payment_method.html.erb +19 -18
  81. data/app/views/kaui/payment_methods/_payment_methods_table.html.erb +8 -5
  82. data/app/views/kaui/payment_methods/index.html.erb +10 -10
  83. data/app/views/kaui/payment_methods/new.html.erb +2 -20
  84. data/app/views/kaui/payment_methods/show.html.erb +2 -1
  85. data/app/views/kaui/payments/_payments_table.html.erb +30 -22
  86. data/app/views/kaui/payments/index.html.erb +1 -1
  87. data/app/views/kaui/payments/new.html.erb +5 -5
  88. data/app/views/kaui/payments/show.html.erb +3 -0
  89. data/app/views/kaui/refunds/new.html.erb +96 -104
  90. data/app/views/kaui/subscriptions/_form.html.erb +36 -0
  91. data/app/views/kaui/subscriptions/_subscriptions_table.html.erb +39 -44
  92. data/app/views/kaui/subscriptions/edit.html.erb +24 -35
  93. data/app/views/kaui/subscriptions/index.html.erb +13 -12
  94. data/app/views/kaui/subscriptions/new.html.erb +6 -43
  95. data/app/views/kaui/subscriptions/show.html.erb +32 -25
  96. data/app/views/kaui/tag_definitions/_form.html.erb +10 -37
  97. data/app/views/kaui/tag_definitions/index.html.erb +3 -5
  98. data/app/views/kaui/tag_definitions/show.html.erb +1 -2
  99. data/app/views/kaui/transactions/_form.html.erb +77 -0
  100. data/app/views/kaui/transactions/new.html.erb +3 -0
  101. data/config/initializers/kilbill-client-init.rb +1 -1
  102. data/config/routes.rb +4 -15
  103. data/kaui.gemspec +8 -6
  104. data/lib/kaui/engine.rb +4 -1
  105. data/lib/kaui/version.rb +1 -1
  106. data/lib/kaui.rb +2 -2
  107. data/test/functional/kaui/account_emails_controller_test.rb +28 -0
  108. data/test/functional/kaui/account_tags_controller_test.rb +29 -0
  109. data/test/functional/kaui/account_timelines_controller_test.rb +21 -0
  110. data/test/functional/kaui/accounts_controller_test.rb +56 -50
  111. data/test/functional/kaui/bundle_tags_controller_test.rb +29 -0
  112. data/test/functional/kaui/bundles_controller_test.rb +58 -13
  113. data/test/functional/kaui/chargebacks_controller_test.rb +16 -5
  114. data/test/functional/kaui/charges_controller_test.rb +39 -0
  115. data/test/functional/kaui/credits_controller_test.rb +35 -0
  116. data/test/functional/kaui/custom_fields_controller_test.rb +21 -0
  117. data/test/functional/kaui/functional_test_helper.rb +61 -0
  118. data/test/functional/kaui/invoice_items_controller_test.rb +24 -10
  119. data/test/functional/kaui/invoices_controller_test.rb +41 -13
  120. data/test/functional/kaui/payment_methods_controller_test.rb +49 -0
  121. data/test/functional/kaui/payments_controller_test.rb +32 -5
  122. data/test/functional/kaui/refunds_controller_test.rb +40 -5
  123. data/test/functional/kaui/subscriptions_controller_test.rb +82 -0
  124. data/test/functional/kaui/tag_definitions_controller_test.rb +27 -53
  125. data/test/functional/kaui/tags_controller_test.rb +21 -0
  126. data/test/functional/kaui/transactions_controller_test.rb +32 -0
  127. data/test/integration/kaui/integration_test_helper.rb +31 -0
  128. data/test/integration/kaui/navigation_test.rb +43 -0
  129. data/test/killbill_test_helper.rb +143 -0
  130. data/test/test_helper.rb +10 -227
  131. data/test/unit/kaui/account_email_test.rb +19 -0
  132. data/test/unit/kaui/account_test.rb +7 -35
  133. data/test/unit/kaui/base_test.rb +5 -46
  134. data/test/unit/kaui/date_helper_test.rb +6 -5
  135. data/test/unit/kaui/invoice_item_test.rb +5 -17
  136. data/test/unit/kaui/invoice_payment_test.rb +42 -0
  137. data/test/unit/kaui/invoice_test.rb +15 -15
  138. data/test/unit/kaui/money_helper_test.rb +10 -0
  139. data/test/unit/kaui/payment_test.rb +42 -0
  140. data/test/unit/kaui/tag_definition_test.rb +5 -28
  141. data/test/unit/kaui/uuid_helper_test.rb +10 -0
  142. metadata +134 -159
  143. data/app/controllers/kaui/analytics_controller.rb +0 -28
  144. data/app/helpers/kaui/killbill_helper.rb +0 -611
  145. data/app/models/kaui/analytics.rb +0 -19
  146. data/app/models/kaui/analytics_sanity.rb +0 -7
  147. data/app/models/kaui/business_account.rb +0 -12
  148. data/app/models/kaui/business_field.rb +0 -6
  149. data/app/models/kaui/business_invoice.rb +0 -31
  150. data/app/models/kaui/business_invoice_item.rb +0 -17
  151. data/app/models/kaui/business_invoice_payment.rb +0 -25
  152. data/app/models/kaui/business_overdue_status.rb +0 -8
  153. data/app/models/kaui/business_snapshot.rb +0 -9
  154. data/app/models/kaui/business_subscription_transition.rb +0 -53
  155. data/app/models/kaui/business_tag.rb +0 -5
  156. data/app/models/kaui/charge.rb +0 -14
  157. data/app/models/kaui/entitlement.rb +0 -2
  158. data/app/models/kaui/event.rb +0 -13
  159. data/app/models/kaui/external_payment.rb +0 -15
  160. data/app/models/kaui/overdue_state.rb +0 -10
  161. data/app/models/kaui/payment_attempt.rb +0 -27
  162. data/app/models/kaui/product.rb +0 -24
  163. data/app/models/kaui/time_series_data.rb +0 -8
  164. data/app/views/kaui/analytics/account_snapshot.html.erb +0 -211
  165. data/app/views/kaui/analytics/index.html.erb +0 -17
  166. data/app/views/kaui/chargebacks/index.html.erb +0 -14
  167. data/app/views/kaui/chargebacks/show.html.erb +0 -19
  168. data/app/views/kaui/credits/index.html.erb +0 -14
  169. data/app/views/kaui/credits/show.html.erb +0 -19
  170. data/app/views/kaui/invoice_items/index.html.erb +0 -20
  171. data/app/views/kaui/invoice_items/show.html.erb +0 -31
  172. data/app/views/kaui/refunds/_refunds_table.html.erb +0 -31
  173. data/app/views/kaui/refunds/index.html.erb +0 -43
  174. data/app/views/kaui/refunds/show.html.erb +0 -33
  175. data/app/views/kaui/tag_definitions/edit.html.erb +0 -3
  176. data/test/dummy/test/fixtures/accounts.yml +0 -32
  177. data/test/dummy/test/fixtures/bill_cycle_days.yml +0 -3
  178. data/test/dummy/test/fixtures/bundles.yml +0 -4
  179. data/test/dummy/test/fixtures/invoice_items.yml +0 -13
  180. data/test/dummy/test/fixtures/invoices.yml +0 -11
  181. data/test/dummy/test/fixtures/overdue_states.yml +0 -8
  182. data/test/dummy/test/fixtures/payment_methods.yml +0 -5
  183. data/test/dummy/test/fixtures/plugin_info_properties.yml +0 -59
  184. data/test/dummy/test/fixtures/plugin_infos.yml +0 -2
  185. data/test/dummy/test/fixtures/refunds.yml +0 -9
  186. data/test/dummy/test/fixtures/tag_definitions.yml +0 -20
  187. data/test/functional/kaui/account_timeline_controller_test.rb +0 -9
  188. data/test/functional/kaui/extpayments_controller_test.rb +0 -9
  189. data/test/functional/kaui/home_controller_test.rb +0 -11
  190. data/test/integration/navigation_test.rb +0 -10
  191. data/test/kaui_test.rb +0 -7
  192. data/test/unit/helpers/kaui/account_timeline_helper_test.rb +0 -6
  193. data/test/unit/helpers/kaui/bundles_helper_test.rb +0 -6
  194. data/test/unit/helpers/kaui/chargebacks_helper_test.rb +0 -6
  195. data/test/unit/helpers/kaui/extpayments_helper_test.rb +0 -6
  196. data/test/unit/helpers/kaui/home_helper_test.rb +0 -6
  197. data/test/unit/helpers/kaui/invoices_helper_test.rb +0 -6
  198. data/test/unit/helpers/kaui/payments_helper_test.rb +0 -6
  199. data/test/unit/helpers/kaui/refunds_helper_test.rb +0 -6
  200. data/test/unit/kaui/overdue_state_test.rb +0 -18
  201. data/test/unit/kaui/refund_test.rb +0 -20
  202. data/test/unit/model_test.rb +0 -151
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ module Kaui
4
+ class NavigationTest < IntegrationTestHelper
5
+
6
+ test 'Browse one account' do
7
+ get BASE_PATH
8
+ assert_redirected_to SIGN_IN_PATH
9
+
10
+ # Verify log-in
11
+ post_via_redirect SIGN_IN_PATH, {:user => {:kb_username => USERNAME, :password => PASSWORD}}
12
+ assert_equal BASE_PATH + '/', path
13
+ assert_equal 'Signed in successfully.', flash[:notice]
14
+
15
+ # User goes to search for the account
16
+ get ACCOUNTS_PATH
17
+ assert_response :success
18
+
19
+ # Assumes he found it on the listing
20
+ get ACCOUNTS_PATH + '/' + @account.account_id
21
+ assert_response :success
22
+ check_no_flash_error
23
+ assert_not_nil assigns(:tags)
24
+ assert_not_nil assigns(:account_emails)
25
+ assert_not_nil assigns(:overdue_state)
26
+ assert_not_nil assigns(:payment_methods)
27
+ assert_not_nil assigns(:bundles)
28
+
29
+ # He now clicks on the timeline link
30
+ get ACCOUNT_TIMELINE_PATH + '/' + @account.account_id
31
+ assert_response :success
32
+ check_no_flash_error
33
+ assert_not_nil assigns(:account)
34
+ assert_not_nil assigns(:timeline)
35
+
36
+ # Verify log-out
37
+ delete_via_redirect SIGN_OUT_PATH
38
+ assert_equal SIGN_IN_PATH, path
39
+ assert_equal 'You need to sign in or sign up before continuing.', flash[:alert]
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,143 @@
1
+ module Kaui
2
+ module KillbillTestHelper
3
+
4
+ USERNAME = 'admin'
5
+ PASSWORD = 'password'
6
+
7
+ #
8
+ # Rails helpers
9
+ #
10
+
11
+ def check_no_flash_error
12
+ unless flash.now.nil?
13
+ assert_nil flash.now[:alert], 'Found flash alert: ' + flash.now[:alert].to_s
14
+ assert_nil flash.now[:error], 'Found flash error: ' + flash.now[:error].to_s
15
+ end
16
+ assert_nil flash[:alert]
17
+ assert_nil flash[:error]
18
+ end
19
+
20
+ #
21
+ # Kill Bill specific helpers
22
+ #
23
+
24
+ def setup_test_data
25
+ @tenant = create_tenant
26
+ @account = create_account(@tenant)
27
+ @account2 = create_account(@tenant)
28
+ @bundle = create_bundle(@account, @tenant)
29
+ @payment_method = create_payment_method(true, @account, @tenant)
30
+ @invoice_item = create_charge(@account, @tenant)
31
+ @cba = create_cba(@invoice_item.invoice_id, @account, @tenant)
32
+ @paid_invoice_item = create_charge(@account, @tenant)
33
+ @payment = create_payment(@paid_invoice_item, @account, @tenant)
34
+ end
35
+
36
+ # Return a new test account
37
+ def create_account(tenant = nil, username = USERNAME, password = PASSWORD, user = 'Kaui test', reason = nil, comment = nil)
38
+ tenant = create_tenant if tenant.nil?
39
+ external_key = SecureRandom.uuid.to_s
40
+
41
+ account = KillBillClient::Model::Account.new
42
+ account.name = 'Kaui'
43
+ account.external_key = external_key
44
+ account.email = 'kill@bill.com'
45
+ account.currency = 'USD'
46
+ account.time_zone = 'UTC'
47
+ account.address1 = '5, ruby road'
48
+ account.address2 = 'Apt 4'
49
+ account.postal_code = 10293
50
+ account.company = 'KillBill, Inc.'
51
+ account.city = 'SnakeCase'
52
+ account.state = 'Awesome'
53
+ account.country = 'LalaLand'
54
+ account.locale = 'fr_FR'
55
+ account.is_notified_for_invoices = false
56
+
57
+ account.create(user, reason, comment, build_options(tenant, username, password))
58
+ end
59
+
60
+ # Return the created bundle
61
+ def create_bundle(account = nil, tenant = nil, username = USERNAME, password = PASSWORD, user = 'Kaui test', reason = nil, comment = nil)
62
+ tenant = create_tenant(user, reason, comment) if tenant.nil?
63
+ account = create_account(tenant, username, password, user, reason, comment) if account.nil?
64
+
65
+ entitlement = KillBillClient::Model::Subscription.new(:account_id => account.account_id,
66
+ :external_key => SecureRandom.uuid,
67
+ :product_name => 'Sports', # Sports, so we can add addons
68
+ :product_category => 'BASE',
69
+ :billing_period => 'MONTHLY',
70
+ :price_list => 'DEFAULT')
71
+ entitlement = entitlement.create(user, reason, comment, build_options(tenant, username, password))
72
+
73
+ KillBillClient::Model::Bundle.find_by_id(entitlement.bundle_id, build_options(tenant, username, password))
74
+ end
75
+
76
+ # Return a new test payment method
77
+ def create_payment_method(set_default = false, account = nil, tenant = nil, username = USERNAME, password = PASSWORD, user = 'Kaui test', reason = nil, comment = nil)
78
+ account = create_account(tenant, username, password, user, reason, comment) if account.nil?
79
+
80
+ payment_method = Kaui::PaymentMethod.new(:account_id => account.account_id, :plugin_name => '__EXTERNAL_PAYMENT__', :is_default => set_default)
81
+ payment_method.create(user, reason, comment, build_options(tenant, username, password))
82
+ end
83
+
84
+ # Return the created external charge
85
+ def create_charge(account = nil, tenant = nil, username = USERNAME, password = PASSWORD, user = 'Kaui test', reason = nil, comment = nil)
86
+ tenant = create_tenant(user, reason, comment) if tenant.nil?
87
+ account = create_account(tenant, username, password, user, reason, comment) if account.nil?
88
+
89
+ invoice_item = KillBillClient::Model::InvoiceItem.new
90
+ invoice_item.account_id = account.account_id
91
+ invoice_item.currency = account.currency
92
+ invoice_item.amount = 123.98
93
+
94
+ invoice_item.create(user, reason, comment, build_options(tenant, username, password))
95
+ end
96
+
97
+ # Return the created credit
98
+ def create_cba(invoice_id = nil, account = nil, tenant = nil, username = USERNAME, password = PASSWORD, user = 'Kaui test', reason = nil, comment = nil)
99
+ tenant = create_tenant(user, reason, comment) if tenant.nil?
100
+ account = create_account(tenant, username, password, user, reason, comment) if account.nil?
101
+
102
+ credit = KillBillClient::Model::Credit.new(:invoice_id => invoice_id, :account_id => account.account_id, :credit_amount => 23.22)
103
+ credit = credit.create(user, reason, comment, build_options(tenant, username, password))
104
+
105
+ invoice = KillBillClient::Model::Invoice.find_by_id_or_number(credit.invoice_id, true, 'NONE', build_options(tenant, username, password))
106
+ invoice.items.find { |ii| ii.amount == -credit.credit_amount }
107
+ end
108
+
109
+ def create_payment(invoice_item = nil, account = nil, tenant = nil, username = USERNAME, password = PASSWORD, user = 'Kaui test', reason = nil, comment = nil)
110
+ tenant = create_tenant(user, reason, comment) if tenant.nil?
111
+ account = create_account(tenant, username, password, user, reason, comment) if account.nil?
112
+ invoice_item = create_charge(account, tenant, username, password, user, reason, comment) if invoice_item.nil?
113
+
114
+ payment = Kaui::InvoicePayment.new({:account_id => account.account_id, :target_invoice_id => invoice_item.invoice_id, :purchased_amount => invoice_item.amount})
115
+ payment.create(true, user, reason, comment, build_options(tenant, username, password))
116
+ end
117
+
118
+ # Return a new test tenant
119
+ def create_tenant(user = 'Kaui test', reason = nil, comment = nil)
120
+ api_key = SecureRandom.uuid.to_s
121
+ api_secret = 'S4cr3333333t!!!!!!lolz'
122
+
123
+ tenant = KillBillClient::Model::Tenant.new
124
+ tenant.api_key = api_key
125
+ tenant.api_secret = api_secret
126
+
127
+ tenant.create(user, reason, comment, build_options)
128
+ end
129
+
130
+ def build_options(tenant = nil, username = USERNAME, password = PASSWORD)
131
+ {
132
+ :api_key => tenant.nil? ? nil : tenant.api_key,
133
+ :api_secret => tenant.nil? ? nil : tenant.api_secret,
134
+ :username => username,
135
+ :password => password
136
+ }
137
+ end
138
+
139
+ def options
140
+ build_options(@tenant)
141
+ end
142
+ end
143
+ end
data/test/test_helper.rb CHANGED
@@ -1,232 +1,15 @@
1
- require 'fakeweb'
1
+ require 'simplecov'
2
+ SimpleCov.start 'rails'
2
3
 
3
- # Configure Rails Environment
4
- ENV["RAILS_ENV"] = "test"
4
+ # Configure the Rails Environment
5
+ ENV['RAILS_ENV'] = 'test'
5
6
 
6
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
7
- require "rails/test_help"
8
- require 'securerandom'
7
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
8
+ require 'rails/test_help'
9
9
 
10
10
  Rails.backtrace_cleaner.remove_silencers!
11
11
 
12
- # Load support files
13
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
14
-
15
- # Load helpers
16
- Dir["#{File.dirname(__FILE__)}/unit/helpers/kaui/*.rb"].each { |f| require f }
17
-
18
- # Setup Kill Bill stubs
19
- FakeWeb.allow_net_connect = false
20
- FakeWeb.register_uri(:get, "http://127.0.0.1:8080/1.0/kb/security/subject", body: "{\"is_authenticated\":true}", :content_type => "application/json")
21
- FakeWeb.register_uri(:any, %r|http://127.0.0.1:8080|, body: "{}", :content_type => "application/json")
22
-
23
- # Include Devise helpers
24
- class ActionController::TestCase
25
- include Devise::TestHelpers
26
-
27
- setup do
28
- @user = Kaui::User.create!({
29
- :kb_tenant_id => "tenant_id_test",
30
- :kb_username => "username_test",
31
- :kb_session_id => "session_id_test"
32
- })
33
- sign_in @user
34
- end
35
- end
36
-
37
- module Kaui
38
- class User < ActiveRecord::Base
39
-
40
- private
41
-
42
- def self.do_find_permissions(options = {})
43
- ["*:*"]
44
- end
45
- end
46
- end
47
-
48
- # Mock killbill-server and serve data from the fixtures
49
- module Kaui::KillbillHelper
50
- @@fixtures ||= {}
51
-
52
- def self.get_account_by_key_with_balance_and_cba(key, options = {})
53
- self.get_account_by_key(key, false, true, options)
54
- end
55
-
56
- def self.get_account(account_id, with_balance = false, with_balance_and_cba = false, options = {})
57
- find_among_fixtures(Kaui::Account, account_id)
58
- end
59
-
60
- def self.get_account_emails(account_id, options = {})
61
- []
62
- end
63
-
64
- def self.get_account_by_external_key(account_id, with_balance = false, with_balance_and_cba = false, options = {})
65
- find_among_fixtures(Kaui::Account, account_id)
66
- end
67
-
68
- def self.get_overdue_state_for_account(account_id, options = {})
69
- find_among_fixtures(Kaui::OverdueState, account_id)
70
- end
71
-
72
- def self.get_invoice(invoice_id, with_items = true, audit= "NONE", options = {})
73
- find_among_fixtures(Kaui::Invoice, invoice_id)
74
- end
75
-
76
- def self.get_invoice_item(invoice_id, invoice_item_id, options = {})
77
- find_among_fixtures(Kaui::InvoiceItem, invoice_item_id)
78
- end
79
-
80
- def self.get_bundle(bundle_id, options = {})
81
- find_among_fixtures(Kaui::Bundle, bundle_id)
82
- end
83
-
84
- def self.get_payments(invoice_id, options = {})
85
- []
86
- end
87
-
88
- def self.get_non_external_payment_methods(account_id, options = {})
89
- []
90
- end
91
-
92
- def self.get_payment_methods(account_id, options = {})
93
- []
94
- end
95
-
96
- def self.get_bundles_for_account(account_id, options = {})
97
- []
98
- end
99
-
100
- def self.get_subscriptions_for_bundle(bundle_id, options = {})
101
- []
102
- end
103
-
104
- def self.get_tags_for_account(account_id, included_deleted = false, audit = "NONE", options = {})
105
- []
106
- end
107
-
108
- def self.get_tag_definitions(options = {})
109
- find_all_among_fixtures(Kaui::TagDefinition)
110
- end
111
-
112
- def self.get_tag_definition(tag_definition_id, options = {})
113
- find_among_fixtures(Kaui::TagDefinition, tag_definition_id, 'id')
114
- end
115
-
116
- def self.create_tag_definition(tag_definition, user, reason, comment, options = {})
117
- tag_definition.id = SecureRandom.uuid
118
- add_fixture(tag_definition, Kaui::TagDefinition)
119
- end
120
-
121
- def self.delete_tag_definition(tag_definition_id, user, reason, comment, options = {})
122
- delete_fixture(Kaui::TagDefinition, tag_definition_id, 'id')
123
- end
124
-
125
- def self.find_among_fixtures(clazz, id, type_id=nil)
126
- results = find_all_among_fixtures(clazz, id, type_id)
127
- results.length > 0 ? results[0] : nil
128
- end
129
-
130
- def self.find_all_among_fixtures(clazz, id=nil, type_id=nil)
131
- results = []
132
- type = clazz.name.demodulize.underscore
133
- type_id = "#{clazz.name.demodulize.camelize}Id".uncapitalize unless type_id
134
- @@fixtures.each do |k,v|
135
- next unless k == "#{type}s"
136
- v.each do |w,u|
137
- results << clazz.new(u.fixture) if u.fixture[type_id] == id or !id.present?
138
- end
139
- end
140
-
141
- return results
142
- end
143
-
144
- def self.add_fixture(fixture, clazz)
145
- type = clazz.name.demodulize.underscore
146
- @@fixtures["#{type}s"].fixtures ||= {}
147
- @@fixtures["#{type}s"].fixtures["auto_generated_fixture_#{rand(100)}"] = ActiveRecord::Fixture.new(fixture.to_hash, clazz)
148
- fixture
149
- end
150
-
151
- def self.delete_fixture(clazz, id, type_id=nil)
152
- type = clazz.name.demodulize.underscore
153
- type_id = "#{clazz.name.demodulize.camelize}Id".uncapitalize unless type_id
154
- @@fixtures.each do |k,v|
155
- next unless k == "#{type}s"
156
- v.each do |w,u|
157
- v.fixtures.delete(w) if u.fixture[type_id] == id
158
- end
159
- end
160
- end
161
-
162
- def self.set_fixtures(f)
163
- @@fixtures = f
164
- end
165
- end
166
-
167
- class ActiveRecord::Fixtures
168
- require 'json'
169
-
170
- class File
171
- def rows
172
- return @rows if @rows
173
-
174
- begin
175
- # Note the .to_json! We want to enforce JSON-type data here,
176
- # which is what we get over the network when querying Killbill
177
- data = JSON.parse(YAML.load(render(IO.read(@file))).to_json)
178
- rescue *RESCUE_ERRORS => error
179
- raise Fixture::FormatError, "a YAML error occurred parsing #{@file}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace
180
- end
181
- # Skip validation since we modified the data
182
- @rows = data ? data.to_a : []
183
- end
184
- end
185
-
186
- # Monkey-patch the create_fixtures method not to rely on a database
187
- def self.create_fixtures(fixtures_directory, table_names, class_names = {})
188
- table_names = [table_names].flatten.map { |n| n.to_s }
189
- table_names.each { |n|
190
- class_names[n.tr('/', '_').to_sym] = n.classify if n.include?('/')
191
- }
192
-
193
- unless table_names.empty?
194
- fixtures_map = {}
195
- table_names.map do |path|
196
- table_name = path.tr '/', '_'
197
- fixtures_map[path] = ActiveRecord::Fixtures.new(nil,
198
- table_name,
199
- class_names[table_name.to_sym] || table_name.classify,
200
- ::File.join(fixtures_directory, path))
201
- end
202
-
203
- all_loaded_fixtures.update(fixtures_map)
204
- cache_fixtures(nil, fixtures_map)
205
-
206
- Kaui::KillbillHelper.set_fixtures fixtures_map
207
- end
208
- cached_fixtures(nil, table_names)
209
- end
210
- end
211
-
212
- class ActiveRecord::Fixture
213
- def find
214
- @fixture
215
- end
216
- end
217
-
218
- class String
219
- def uncapitalize
220
- self[0, 1].downcase + self[1..-1]
221
- end
222
- end
223
-
224
- # See http://jira.codehaus.org/browse/JRUBY-6176
225
- module SecureRandom
226
- def self.uuid
227
- ary = self.random_bytes(16).unpack("NnnnnN")
228
- ary[2] = (ary[2] & 0x0fff) | 0x4000
229
- ary[3] = (ary[3] & 0x3fff) | 0x8000
230
- "%08x-%04x-%04x-%04x-%04x%08x" % ary
231
- end unless respond_to?(:uuid)
232
- end
12
+ # Include helpers
13
+ require 'killbill_test_helper'
14
+ require 'functional/kaui/functional_test_helper'
15
+ require 'integration/kaui/integration_test_helper'
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class Kaui::AccountEmailTest < ActiveSupport::TestCase
4
+
5
+ test 'can compare emails' do
6
+ email1 = Kaui::AccountEmail.new(:account_id => SecureRandom.uuid, :email => 'abc@bar.com')
7
+ email2 = Kaui::AccountEmail.new(:account_id => SecureRandom.uuid, :email => 'bcd@bar.com')
8
+ email3 = Kaui::AccountEmail.new(:account_id => SecureRandom.uuid, :email => nil)
9
+
10
+ assert_equal -1, email1 <=> email2
11
+ assert_equal 1, email2 <=> email1
12
+ assert_equal 0, email1 <=> email1
13
+ assert_equal 0, email2 <=> email2
14
+ assert_equal 0, email3 <=> email3
15
+ assert_equal 1, email1 <=> email3
16
+ assert_equal -1, email3 <=> email1
17
+ assert_equal -1, email1 <=> nil
18
+ end
19
+ end
@@ -1,42 +1,14 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Kaui::AccountTest < ActiveSupport::TestCase
4
- fixtures :accounts
5
4
 
6
- test "can serialize from json" do
7
- as_json = accounts(:pierre)
8
- pierre = Kaui::Account.new(as_json)
5
+ test 'can convert to money' do
6
+ account = Kaui::Account.new(:account_balance => 12.42, :account_cba => 54.32, :currency => 'USD')
9
7
 
10
- assert_equal as_json["accountId"], pierre.account_id
11
- assert_equal as_json["address1"], pierre.address1
12
- assert_equal as_json["address2"], pierre.address2
13
- assert_equal as_json["company"], pierre.company
14
- assert_equal as_json["country"], pierre.country
15
- assert_equal as_json["currency"], pierre.currency
16
- assert_equal as_json["email"], pierre.email
17
- assert_equal as_json["externalKey"], pierre.external_key
18
- assert_equal as_json["name"], pierre.name
19
- assert_equal as_json["paymentMethodId"], pierre.payment_method_id
20
- assert_equal as_json["phone"], pierre.phone
21
- assert_equal as_json["state"], pierre.state
22
- assert_equal as_json["timeZone"], pierre.timezone
23
- end
24
-
25
- test "can read correct positive balance" do
26
- as_json = accounts(:account_with_positive_balance)
27
- t = Kaui::Account.new(as_json)
28
- assert(t.balance > 0)
29
- end
30
-
31
- test "can read correct negative balance" do
32
- as_json = accounts(:account_with_negative_balance)
33
- t = Kaui::Account.new(as_json)
34
- assert(t.balance < 0)
35
- end
8
+ assert_equal 1242, account.balance_to_money.cents
9
+ assert_equal 'USD', account.balance_to_money.currency_as_string
36
10
 
37
- test "can read correct zero balance" do
38
- as_json = accounts(:account_with_zero_balance)
39
- t = Kaui::Account.new(as_json)
40
- assert(t.balance == 0)
11
+ assert_equal 5432, account.cba_to_money.cents
12
+ assert_equal 'USD', account.cba_to_money.currency_as_string
41
13
  end
42
- end
14
+ end
@@ -2,58 +2,17 @@ require 'test_helper'
2
2
 
3
3
  class Kaui::BaseTest < ActiveSupport::TestCase
4
4
 
5
- class Kaui::SomeKlass < Kaui::Base
6
- define_attr :attribute_id
7
-
8
- has_many :klasses, Kaui::SomeKlass
9
- end
10
-
11
- test "has_many association should return [] by default" do
12
- klass = Kaui::SomeKlass.new
13
- assert_equal [], klass.klasses
14
- end
15
-
16
- test "can serialize from hash" do
17
- klass = Kaui::SomeKlass.new(:attribute_id => 12)
18
- assert_equal 12, klass.attribute_id
19
- end
20
-
21
- test "can serialize from json" do
22
- klass = Kaui::SomeKlass.new(:attributeId => 12)
23
- assert_equal 12, klass.attribute_id
24
- end
25
-
26
- test "convert camelcase hash into snake case hash" do
27
- # Happy path
28
- new_hash = Kaui::Base.convert_hash_keys({:accountId=>12, :data_id=>14})
29
- assert_equal 12, new_hash[:account_id]
30
- assert_equal 14, new_hash[:data_id]
31
- assert_nil new_hash[:accountId]
32
-
33
- # Edge cases
34
- assert_nil Kaui::Base.convert_hash_keys(nil)
35
- assert_equal [], Kaui::Base.convert_hash_keys([])
36
- assert_equal Hash.new, Kaui::Base.convert_hash_keys({})
37
- assert_equal 1, Kaui::Base.convert_hash_keys(1)
38
- end
39
-
40
- test "convert nested snake case into camel case" do
41
- new_hash = Kaui::Base.camelize({:account_id=>12, :my_nested_field=> {:not_good => 32}})
42
- assert_equal 12, new_hash[:accountId]
43
- assert_equal 32, new_hash[:myNestedField][:notGood]
44
- end
45
-
46
- test "can convert to money" do
5
+ test 'can convert to money' do
47
6
  # Happy path
48
- ['GBP', 'MXN', 'BRL', 'EUR', 'AUD', 'USD', 'CAD', 'JPY'].each do |currency|
7
+ %w(GBP MXN BRL EUR AUD USD CAD JPY).each do |currency|
49
8
  money = Kaui::Base.to_money(12.42, currency)
50
9
  assert_equal 1242, money.cents
51
10
  assert_equal currency, money.currency.iso_code
52
11
  end
53
12
 
54
13
  # Edge cases
55
- bad_money = Kaui::Base.to_money(12.42, "blahblah")
14
+ bad_money = Kaui::Base.to_money(12.42, 'blahblah')
56
15
  assert_equal 1242, bad_money.cents
57
- assert_equal "USD", bad_money.currency.iso_code
16
+ assert_equal 'USD', bad_money.currency.iso_code
58
17
  end
59
- end
18
+ end
@@ -1,13 +1,14 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Kaui::DateHelperTest < ActiveSupport::TestCase
4
+
4
5
  include Kaui::DateHelper
5
6
 
6
- test "can parse from string" do
7
- assert_equal "2012-07-01", format_date("2012-07-01T12:55:44Z")
7
+ test 'can parse from string' do
8
+ assert_equal '2012-07-01', format_date('2012-07-01T12:55:44Z')
8
9
  end
9
10
 
10
- test "can parse from date" do
11
- assert_equal "2012-07-01", format_date(Date.new(2012, 7, 1))
11
+ test 'can parse from date' do
12
+ assert_equal '2012-07-01', format_date(Date.new(2012, 7, 1))
12
13
  end
13
- end
14
+ end
@@ -1,23 +1,11 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Kaui::InvoiceItemTest < ActiveSupport::TestCase
4
- fixtures :invoice_items
5
4
 
6
- test "can serialize from json" do
7
- as_json = invoice_items(:recurring_item_for_pierre)
8
- invoice = Kaui::InvoiceItem.new(as_json)
5
+ test 'can convert to money' do
6
+ invoice_item = Kaui::InvoiceItem.new(:amount => 12.42, :currency => 'USD')
9
7
 
10
- assert_equal as_json["invoiceItemId"], invoice.invoice_item_id
11
- assert_equal as_json["invoiceId"], invoice.invoice_id
12
- assert_equal as_json["accountId"], invoice.account_id
13
- assert_equal as_json["bundleId"], invoice.bundle_id
14
- assert_equal as_json["subscriptionId"], invoice.subscription_id
15
- assert_equal as_json["planName"], invoice.plan_name
16
- assert_equal as_json["phaseName"], invoice.phase_name
17
- assert_equal as_json["description"], invoice.description
18
- assert_equal as_json["startDate"], invoice.start_date
19
- assert_equal as_json["endDate"], invoice.end_date
20
- assert_equal as_json["amount"], invoice.amount
21
- assert_equal as_json["currency"], invoice.currency
8
+ assert_equal 1242, invoice_item.amount_to_money.cents
9
+ assert_equal 'USD', invoice_item.amount_to_money.currency_as_string
22
10
  end
23
- end
11
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class Kaui::InvoicePaymentTest < ActiveSupport::TestCase
4
+
5
+ test 'can convert to money' do
6
+ payment = Kaui::InvoicePayment.new(:auth_amount => 12.42,
7
+ :captured_amount => 10.2,
8
+ :purchased_amount => 12,
9
+ :refunded_amount => 9,
10
+ :credited_amount => 5,
11
+ :currency => 'USD')
12
+
13
+ assert_equal 1242, payment.auth_amount_to_money.cents
14
+ assert_equal 'USD', payment.auth_amount_to_money.currency_as_string
15
+
16
+ assert_equal 1020, payment.captured_amount_to_money.cents
17
+ assert_equal 'USD', payment.captured_amount_to_money.currency_as_string
18
+
19
+ assert_equal 1200, payment.purchased_amount_to_money.cents
20
+ assert_equal 'USD', payment.purchased_amount_to_money.currency_as_string
21
+
22
+ assert_equal 900, payment.refunded_amount_to_money.cents
23
+ assert_equal 'USD', payment.refunded_amount_to_money.currency_as_string
24
+
25
+ assert_equal 500, payment.credited_amount_to_money.cents
26
+ assert_equal 'USD', payment.credited_amount_to_money.currency_as_string
27
+
28
+ assert_equal 2220, payment.paid_amount_to_money.cents
29
+ assert_equal 'USD', payment.paid_amount_to_money.currency_as_string
30
+
31
+ assert_equal 1400, payment.returned_amount_to_money.cents
32
+ assert_equal 'USD', payment.returned_amount_to_money.currency_as_string
33
+ end
34
+
35
+ test 'can check for full refunds' do
36
+ assert Kaui::InvoicePayment.new(:purchased_amount => 10.2, :refunded_amount => 10.20, :currency => 'USD').is_fully_refunded?
37
+ assert !Kaui::InvoicePayment.new(:purchased_amount => 10.2, :refunded_amount => 9, :currency => 'USD').is_fully_refunded?
38
+
39
+ assert Kaui::InvoicePayment.new(:captured_amount => 10.2, :refunded_amount => 10.20, :currency => 'USD').is_fully_refunded?
40
+ assert !Kaui::InvoicePayment.new(:captured_amount => 10.2, :refunded_amount => 9, :currency => 'USD').is_fully_refunded?
41
+ end
42
+ end