auction_fun_core 0.8.7 → 0.8.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.standard.yml +2 -0
  3. data/CHANGELOG.md +29 -0
  4. data/Procfile +1 -0
  5. data/README.md +34 -32
  6. data/Rakefile +1 -1
  7. data/auction_fun_core.gemspec +1 -1
  8. data/i18n/en-US/contracts/contracts.en-US.yml +3 -0
  9. data/i18n/en-US/mail/application.en-US.yml +7 -0
  10. data/i18n/en-US/mail/auction_context/pre_auction/auction_start_reminder.en-US.yml +11 -0
  11. data/i18n/pt-BR/contracts/contracts.pt-BR.yml +3 -0
  12. data/i18n/pt-BR/mail/application.pt-BR.yml +7 -0
  13. data/i18n/pt-BR/mail/auction_context/pre_auction/auction_start_reminder.pt-BR.yml +11 -0
  14. data/lib/auction_fun_core/business/configuration.rb +31 -0
  15. data/lib/auction_fun_core/business/token_generator.rb +19 -1
  16. data/lib/auction_fun_core/contracts/application_contract.rb +9 -1
  17. data/lib/auction_fun_core/contracts/auction_context/create_contract.rb +35 -20
  18. data/lib/auction_fun_core/contracts/auction_context/post_auction/participant_contract.rb +23 -1
  19. data/lib/auction_fun_core/contracts/auction_context/post_auction/winner_contract.rb +22 -1
  20. data/lib/auction_fun_core/contracts/auction_context/pre_auction/auction_start_reminder_contract.rb +48 -0
  21. data/lib/auction_fun_core/contracts/auction_context/processor/finish/closed_contract.rb +19 -7
  22. data/lib/auction_fun_core/contracts/auction_context/processor/finish/penny_contract.rb +19 -7
  23. data/lib/auction_fun_core/contracts/auction_context/processor/finish/standard_contract.rb +19 -7
  24. data/lib/auction_fun_core/contracts/auction_context/processor/pause_contract.rb +16 -4
  25. data/lib/auction_fun_core/contracts/auction_context/processor/start_contract.rb +17 -5
  26. data/lib/auction_fun_core/contracts/auction_context/processor/unpause_contract.rb +16 -4
  27. data/lib/auction_fun_core/contracts/bid_context/create_bid_closed_contract.rb +20 -11
  28. data/lib/auction_fun_core/contracts/bid_context/create_bid_penny_contract.rb +18 -9
  29. data/lib/auction_fun_core/contracts/bid_context/create_bid_standard_contract.rb +19 -10
  30. data/lib/auction_fun_core/contracts/staff_context/authentication_contract.rb +18 -4
  31. data/lib/auction_fun_core/contracts/staff_context/registration_contract.rb +20 -8
  32. data/lib/auction_fun_core/contracts/user_context/authentication_contract.rb +18 -4
  33. data/lib/auction_fun_core/contracts/user_context/email_confirmation_contract.rb +17 -2
  34. data/lib/auction_fun_core/contracts/user_context/phone_confirmation_contract.rb +17 -2
  35. data/lib/auction_fun_core/contracts/user_context/registration_contract.rb +26 -8
  36. data/lib/auction_fun_core/entities/auction.rb +48 -4
  37. data/lib/auction_fun_core/entities/bid.rb +3 -2
  38. data/lib/auction_fun_core/entities/staff.rb +15 -2
  39. data/lib/auction_fun_core/entities/user.rb +31 -2
  40. data/lib/auction_fun_core/events/app.rb +8 -2
  41. data/lib/auction_fun_core/events/listener.rb +19 -16
  42. data/lib/auction_fun_core/operations/auction_context/create_operation.rb +25 -9
  43. data/lib/auction_fun_core/operations/auction_context/post_auction/participant_operation.rb +36 -3
  44. data/lib/auction_fun_core/operations/auction_context/post_auction/winner_operation.rb +36 -2
  45. data/lib/auction_fun_core/operations/auction_context/pre_auction/auction_start_reminder_operation.rb +96 -0
  46. data/lib/auction_fun_core/operations/auction_context/processor/finish/closed_operation.rb +82 -10
  47. data/lib/auction_fun_core/operations/auction_context/processor/finish/penny_operation.rb +81 -10
  48. data/lib/auction_fun_core/operations/auction_context/processor/finish/standard_operation.rb +81 -12
  49. data/lib/auction_fun_core/operations/auction_context/processor/pause_operation.rb +36 -1
  50. data/lib/auction_fun_core/operations/auction_context/processor/unpause_operation.rb +36 -1
  51. data/lib/auction_fun_core/relations/auctions.rb +178 -97
  52. data/lib/auction_fun_core/relations/bids.rb +18 -0
  53. data/lib/auction_fun_core/repos/auction_context/auction_repository.rb +40 -11
  54. data/lib/auction_fun_core/repos/bid_context/bid_repository.rb +27 -5
  55. data/lib/auction_fun_core/repos/staff_context/staff_repository.rb +63 -21
  56. data/lib/auction_fun_core/repos/user_context/user_repository.rb +69 -25
  57. data/lib/auction_fun_core/services/mail/auction_context/post_auction/participant_mailer.rb +7 -1
  58. data/lib/auction_fun_core/services/mail/auction_context/post_auction/winner_mailer.rb +7 -1
  59. data/lib/auction_fun_core/services/mail/auction_context/pre_auction/auction_start_reminder_mailer.rb +35 -0
  60. data/lib/auction_fun_core/services/mail/templates/auction_context/post_auction/participant.html.erb +1 -1
  61. data/lib/auction_fun_core/services/mail/templates/auction_context/post_auction/winner.html.erb +1 -1
  62. data/lib/auction_fun_core/services/mail/templates/auction_context/pre_auction/auction_start_reminder.html.erb +192 -0
  63. data/lib/auction_fun_core/services/mail/user_context/registration_mailer.rb +6 -0
  64. data/lib/auction_fun_core/version.rb +1 -1
  65. data/lib/auction_fun_core/workers/application_job.rb +10 -0
  66. data/lib/auction_fun_core/workers/operations/auction_context/post_auction/participant_operation_job.rb +7 -2
  67. data/lib/auction_fun_core/workers/operations/auction_context/post_auction/winner_operation_job.rb +6 -2
  68. data/lib/auction_fun_core/workers/operations/auction_context/pre_auction/auction_start_reminder_operation_job.rb +44 -0
  69. data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/closed_operation_job.rb +6 -3
  70. data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/penny_operation_job.rb +6 -3
  71. data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/standard_operation_job.rb +6 -2
  72. data/lib/auction_fun_core/workers/operations/auction_context/processor/start_operation_job.rb +6 -3
  73. data/lib/auction_fun_core/workers/services/mail/auction_context/post_auction/participant_mailer_job.rb +12 -7
  74. data/lib/auction_fun_core/workers/services/mail/auction_context/post_auction/winner_mailer_job.rb +11 -5
  75. data/lib/auction_fun_core/workers/services/mail/auction_context/pre_auction/auction_start_reminder_mailer_job.rb +48 -0
  76. data/lib/auction_fun_core/workers/services/mail/user_context/registration_mailer_job.rb +8 -6
  77. data/spec/auction_fun_core/contracts/auction_context/create_contract_spec.rb +158 -0
  78. data/spec/auction_fun_core/contracts/auction_context/post_auction/participant_contract_spec.rb +77 -0
  79. data/spec/auction_fun_core/contracts/auction_context/post_auction/winner_contract_spec.rb +76 -0
  80. data/spec/auction_fun_core/contracts/auction_context/pre_auction/auction_start_reminder_contract_spec.rb +48 -0
  81. data/spec/auction_fun_core/contracts/auction_context/processor/finish/closed_contract_spec.rb +59 -0
  82. data/spec/auction_fun_core/contracts/auction_context/processor/finish/penny_contract_spec.rb +59 -0
  83. data/spec/auction_fun_core/contracts/auction_context/processor/finish/standard_contract_spec.rb +59 -0
  84. data/spec/auction_fun_core/contracts/auction_context/processor/pause_contract_spec.rb +39 -0
  85. data/spec/auction_fun_core/contracts/auction_context/processor/start_contract_spec.rb +59 -0
  86. data/spec/auction_fun_core/contracts/auction_context/processor/unpause_contract_spec.rb +39 -0
  87. data/spec/auction_fun_core/contracts/bid_context/create_bid_closed_contract_spec.rb +130 -0
  88. data/spec/auction_fun_core/contracts/bid_context/create_bid_penny_contract_spec.rb +118 -0
  89. data/spec/auction_fun_core/contracts/bid_context/create_bid_standard_contract_spec.rb +154 -0
  90. data/spec/auction_fun_core/contracts/staff_context/authentication_contract_spec.rb +89 -0
  91. data/spec/auction_fun_core/contracts/staff_context/registration_contract_spec.rb +24 -0
  92. data/spec/auction_fun_core/contracts/user_context/authentication_contract_spec.rb +89 -0
  93. data/spec/auction_fun_core/contracts/user_context/email_confirmation_contract_spec.rb +54 -0
  94. data/spec/auction_fun_core/contracts/user_context/phone_confirmation_contract_spec.rb +54 -0
  95. data/spec/auction_fun_core/contracts/user_context/registration_contract_spec.rb +27 -0
  96. data/spec/auction_fun_core/entities/auction_spec.rb +75 -0
  97. data/spec/auction_fun_core/entities/bid_spec.rb +7 -0
  98. data/spec/auction_fun_core/entities/staff_spec.rb +38 -0
  99. data/spec/auction_fun_core/entities/user_spec.rb +106 -0
  100. data/spec/auction_fun_core/operations/auction_context/create_operation_spec.rb +126 -0
  101. data/spec/auction_fun_core/operations/auction_context/post_auction/participant_operation_spec.rb +88 -0
  102. data/spec/auction_fun_core/operations/auction_context/post_auction/winner_operation_spec.rb +76 -0
  103. data/spec/auction_fun_core/operations/auction_context/pre_auction/auction_start_reminder_operation_spec.rb +98 -0
  104. data/spec/auction_fun_core/operations/auction_context/processor/finish/closed_operation_spec.rb +107 -0
  105. data/spec/auction_fun_core/operations/auction_context/processor/finish/penny_operation_spec.rb +107 -0
  106. data/spec/auction_fun_core/operations/auction_context/processor/finish/standard_operation_spec.rb +106 -0
  107. data/spec/auction_fun_core/operations/auction_context/processor/pause_operation_spec.rb +83 -0
  108. data/spec/auction_fun_core/operations/auction_context/processor/start_operation_spec.rb +147 -0
  109. data/spec/auction_fun_core/operations/auction_context/processor/unpause_operation_spec.rb +80 -0
  110. data/spec/auction_fun_core/operations/bid_context/create_bid_closed_operation_spec.rb +95 -0
  111. data/spec/auction_fun_core/operations/bid_context/create_bid_penny_operation_spec.rb +133 -0
  112. data/spec/auction_fun_core/operations/bid_context/create_bid_standard_operation_spec.rb +95 -0
  113. data/spec/auction_fun_core/operations/staff_context/authentication_operation_spec.rb +74 -0
  114. data/spec/auction_fun_core/operations/staff_context/registration_operation_spec.rb +93 -0
  115. data/spec/auction_fun_core/operations/user_context/authentication_operation_spec.rb +74 -0
  116. data/spec/auction_fun_core/operations/user_context/email_confirmation_operation_spec.rb +76 -0
  117. data/spec/auction_fun_core/operations/user_context/phone_confirmation_operation_spec.rb +76 -0
  118. data/spec/auction_fun_core/operations/user_context/registration_operation_spec.rb +100 -0
  119. data/spec/auction_fun_core/relations/auctions_spec.rb +471 -0
  120. data/spec/auction_fun_core/repos/auction_context/auction_repository_spec.rb +64 -0
  121. data/spec/auction_fun_core/repos/bid_context/bid_repository_spec.rb +64 -0
  122. data/spec/auction_fun_core/repos/staff_context/staff_repository_spec.rb +118 -0
  123. data/spec/auction_fun_core/repos/user_context/user_repository_spec.rb +117 -0
  124. data/spec/auction_fun_core/services/mail/auction_context/post_auction/participant_mailer_spec.rb +48 -0
  125. data/spec/auction_fun_core/services/mail/auction_context/post_auction/winner_mailer_spec.rb +48 -0
  126. data/spec/auction_fun_core/services/mail/auction_context/pre_auction/auction_start_reminder_mailer_spec.rb +40 -0
  127. data/spec/auction_fun_core/services/mail/user_context/registration_mailer_spec.rb +33 -0
  128. data/spec/auction_fun_core/workers/operations/auction_context/post_auction/participation_operation_job_spec.rb +54 -0
  129. data/spec/auction_fun_core/workers/operations/auction_context/post_auction/winner_operation_job_spec.rb +48 -0
  130. data/spec/auction_fun_core/workers/operations/auction_context/pre_auction/auction_start_reminder_operation_job_spec.rb +53 -0
  131. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/closed_operation_job_spec.rb +47 -0
  132. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/penny_operation_job_spec.rb +47 -0
  133. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/standard_operation_job_spec.rb +47 -0
  134. data/spec/auction_fun_core/workers/operations/auction_context/processor/start_operation_job_spec.rb +47 -0
  135. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/participant_mailer_job_spec.rb +62 -0
  136. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/pre_auction/auction_start_reminder_mailer_job_spec.rb +59 -0
  137. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/winner_mailer_job_spec.rb +62 -0
  138. data/spec/auction_fun_core/workers/services/mail/user_context/registration_mailer_job_spec.rb +53 -0
  139. data/spec/auction_fun_core_spec.rb +7 -0
  140. data/spec/spec_helper.rb +61 -0
  141. data/spec/support/background_job.rb +7 -0
  142. data/spec/support/factories/auctions.rb +143 -0
  143. data/spec/support/factories/bids.rb +6 -0
  144. data/spec/support/factories/staffs.rb +18 -0
  145. data/spec/support/factories/users.rb +43 -0
  146. data/spec/support/faker.rb +6 -0
  147. data/spec/support/mail.rb +13 -0
  148. data/spec/support/shared_examples/validate_email_contract.rb +25 -0
  149. data/spec/support/shared_examples/validate_name_contract.rb +19 -0
  150. data/spec/support/shared_examples/validate_password_confirmation_contract.rb +16 -0
  151. data/spec/support/shared_examples/validate_password_contract.rb +18 -0
  152. data/spec/support/shared_examples/validate_phone_contract.rb +25 -0
  153. data/spec/support/shared_examples/validate_stopwatch_contract.rb +32 -0
  154. data/system/providers/background_job.rb +6 -0
  155. metadata +87 -2
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Contracts::BidContext::CreateBidClosedContract, type: :contract do
6
+ describe "#call" do
7
+ subject(:contract) { described_class.new.call(attributes) }
8
+
9
+ context "when params are blank" do
10
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
11
+
12
+ it "expect failure with error messages" do
13
+ expect(contract).to be_failure
14
+
15
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.key?"))
16
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.key?"))
17
+ end
18
+ end
19
+
20
+ context "with foreign key user_id param" do
21
+ context "when is a invalid type" do
22
+ let(:attributes) { {user_id: "wrongvalue"} }
23
+
24
+ it "expect failure with error messages" do
25
+ expect(contract).to be_failure
26
+
27
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.int?"))
28
+ end
29
+ end
30
+
31
+ context "when is not found on database" do
32
+ let(:attributes) { {user_id: rand(10_000..1_000_000)} }
33
+
34
+ it "expect failure with error messages" do
35
+ expect(contract).to be_failure
36
+
37
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.custom.not_found"))
38
+ end
39
+ end
40
+
41
+ context "when user has already placed a bid on the auction" do
42
+ let(:user) { Factory[:user] }
43
+ let(:auction) { Factory[:auction, :default_running_closed] }
44
+ let(:bid) { Factory[:bid, user: user, auction: auction, value_cents: auction.initial_bid_cents] }
45
+ let(:attributes) { {auction_id: auction.id, user_id: user.id, value_cents: (bid.value_cents * 2)} }
46
+
47
+ it "expect failure with error messages" do
48
+ expect(contract).to be_failure
49
+
50
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.custom.bids.already_bidded"))
51
+ end
52
+ end
53
+ end
54
+
55
+ context "with foreign key auction_id param" do
56
+ context "when a invalid type" do
57
+ let(:attributes) { {auction_id: "wrongvalue"} }
58
+
59
+ it "expect failure with error messages" do
60
+ expect(contract).to be_failure
61
+
62
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.int?"))
63
+ end
64
+ end
65
+
66
+ context "when is not found on database" do
67
+ let(:attributes) { {auction_id: rand(10_000..1_000_000)} }
68
+
69
+ it "expect failure with error messages" do
70
+ expect(contract).to be_failure
71
+
72
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.custom.not_found"))
73
+ end
74
+ end
75
+
76
+ context 'when auction kind is different of "closed"' do
77
+ let(:auction) { Factory[:auction, :default_scheduled_penny] }
78
+ let(:attributes) { {auction_id: auction.id} }
79
+
80
+ it "expect failure with error messages" do
81
+ expect(contract).to be_failure
82
+
83
+ expect(contract.errors[:auction_id]).to include(
84
+ I18n.t("contracts.errors.custom.bids.invalid_kind", kind: "closed")
85
+ )
86
+ end
87
+ end
88
+
89
+ context 'when auction status is not "scheduled" or "running"' do
90
+ let(:auction) { Factory[:auction, :default_finished_standard] }
91
+ let(:attributes) { {auction_id: auction.id} }
92
+
93
+ it "expect failure with error messages" do
94
+ expect(contract).to be_failure
95
+
96
+ expect(contract.errors[:auction_id]).to include(
97
+ I18n.t("contracts.errors.custom.bids.invalid_status", status: auction.status)
98
+ )
99
+ end
100
+ end
101
+ end
102
+
103
+ context "with value_cents param" do
104
+ let(:user) { Factory[:user] }
105
+ let(:auction) { Factory[:auction, :default_running_closed] }
106
+
107
+ context "when value_cents is less than to auction initial bid" do
108
+ let(:value) { Money.new(auction.initial_bid_cents - 1) }
109
+ let(:attributes) { {auction_id: auction.id, user_id: user.id, value_cents: value.cents} }
110
+
111
+ it "expect failure with error messages" do
112
+ expect(contract).to be_failure
113
+
114
+ expect(contract.errors[:value_cents]).to include(
115
+ I18n.t("contracts.errors.gteq?", num: Money.new(auction.initial_bid_cents).to_f)
116
+ )
117
+ end
118
+ end
119
+
120
+ context "when value_cents is greater than or equal to auction initial bid" do
121
+ let(:value) { Money.new(auction.initial_bid_cents) }
122
+ let(:attributes) { {auction_id: auction.id, user_id: user.id, value_cents: value.cents} }
123
+
124
+ it "expect return success without error messages" do
125
+ expect(contract).to be_success
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Contracts::BidContext::CreateBidPennyContract, type: :contract do
6
+ describe "#call" do
7
+ subject(:contract) { described_class.new.call(attributes) }
8
+
9
+ context "when params are blank" do
10
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
11
+
12
+ it "expect failure with error messages" do
13
+ expect(contract).to be_failure
14
+
15
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.key?"))
16
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.key?"))
17
+ end
18
+ end
19
+
20
+ context "with foreign key user_id param" do
21
+ context "when is a invalid type" do
22
+ let(:attributes) { {user_id: "wrongvalue"} }
23
+
24
+ it "expect failure with error messages" do
25
+ expect(contract).to be_failure
26
+
27
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.int?"))
28
+ end
29
+ end
30
+
31
+ context "when is not found on database" do
32
+ let(:attributes) { {user_id: rand(10_000..1_000_000)} }
33
+
34
+ it "expect failure with error messages" do
35
+ expect(contract).to be_failure
36
+
37
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.custom.not_found"))
38
+ end
39
+ end
40
+ end
41
+
42
+ context "with foreign key auction_id param" do
43
+ context "when a invalid type" do
44
+ let(:attributes) { {auction_id: "wrongvalue"} }
45
+
46
+ it "expect failure with error messages" do
47
+ expect(contract).to be_failure
48
+
49
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.int?"))
50
+ end
51
+ end
52
+
53
+ context "when is not found on database" do
54
+ let(:attributes) { {auction_id: rand(10_000..1_000_000)} }
55
+
56
+ it "expect failure with error messages" do
57
+ expect(contract).to be_failure
58
+
59
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.custom.not_found"))
60
+ end
61
+ end
62
+
63
+ context 'when auction kind is different of "penny"' do
64
+ let(:auction) { Factory[:auction, :default_scheduled_standard] }
65
+ let(:attributes) { {auction_id: auction.id} }
66
+
67
+ it "expect failure with error messages" do
68
+ expect(contract).to be_failure
69
+
70
+ expect(contract.errors[:auction_id]).to include(
71
+ I18n.t("contracts.errors.custom.bids.invalid_kind", kind: "penny")
72
+ )
73
+ end
74
+ end
75
+
76
+ context 'when auction status is not "scheduled" or "running"' do
77
+ let(:auction) { Factory[:auction, :default_finished_standard] }
78
+ let(:attributes) { {auction_id: auction.id} }
79
+
80
+ it "expect failure with error messages" do
81
+ expect(contract).to be_failure
82
+
83
+ expect(contract.errors[:auction_id]).to include(
84
+ I18n.t("contracts.errors.custom.bids.invalid_status", status: auction.status)
85
+ )
86
+ end
87
+ end
88
+ end
89
+
90
+ context "with user balance" do
91
+ let(:bid_cents) { 10_000 }
92
+ let(:user) { Factory[:user] }
93
+ let(:auction) { Factory[:auction, :default_running_penny, initial_bid_cents: bid_cents] }
94
+ let(:attributes) { {auction_id: auction.id, user_id: user.id} }
95
+
96
+ context "when does not have enough balance to bid" do
97
+ it "expect failure with error messages" do
98
+ expect(contract).to be_failure
99
+ expect(contract.errors[:user_id]).to include(
100
+ I18n.t("contracts.errors.custom.bids.insufficient_balance")
101
+ )
102
+ end
103
+ end
104
+
105
+ context "when has enough balance to bid" do
106
+ before do
107
+ AuctionFunCore::Repos::UserContext::UserRepository.new.update(
108
+ user.id, balance_cents: (bid_cents * 10)
109
+ )
110
+ end
111
+
112
+ it "expect return success" do
113
+ expect(contract).to be_success
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Contracts::BidContext::CreateBidStandardContract, type: :contract do
6
+ let(:auction_repo) { described_class.new.auction_repo }
7
+
8
+ describe "#call" do
9
+ subject(:contract) { described_class.new.call(attributes) }
10
+
11
+ context "when params are blank" do
12
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
13
+
14
+ it "expect failure with error messages" do
15
+ expect(contract).to be_failure
16
+
17
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.key?"))
18
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.key?"))
19
+ expect(contract.errors[:value_cents]).to include(I18n.t("contracts.errors.key?"))
20
+ end
21
+ end
22
+
23
+ context "with foreign key user_id param" do
24
+ context "when is a invalid type" do
25
+ let(:attributes) { {user_id: "wrongvalue"} }
26
+
27
+ it "expect failure with error messages" do
28
+ expect(contract).to be_failure
29
+
30
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.int?"))
31
+ end
32
+ end
33
+
34
+ context "when is not found on database" do
35
+ let(:attributes) { {user_id: rand(10_000..1_000_000)} }
36
+
37
+ it "expect failure with error messages" do
38
+ expect(contract).to be_failure
39
+
40
+ expect(contract.errors[:user_id]).to include(I18n.t("contracts.errors.custom.not_found"))
41
+ end
42
+ end
43
+ end
44
+
45
+ context "with foreign key auction_id param" do
46
+ context "when a invalid type" do
47
+ let(:attributes) { {auction_id: "wrongvalue"} }
48
+
49
+ it "expect failure with error messages" do
50
+ expect(contract).to be_failure
51
+
52
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.int?"))
53
+ end
54
+ end
55
+
56
+ context "when is not found on database" do
57
+ let(:attributes) { {auction_id: rand(10_000..1_000_000)} }
58
+
59
+ it "expect failure with error messages" do
60
+ expect(contract).to be_failure
61
+
62
+ expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.custom.not_found"))
63
+ end
64
+ end
65
+
66
+ context 'when auction kind is different of "standard"' do
67
+ let(:auction) { Factory[:auction, :default_scheduled_penny] }
68
+ let(:attributes) { {auction_id: auction.id} }
69
+
70
+ it "expect failure with error messages" do
71
+ expect(contract).to be_failure
72
+
73
+ expect(contract.errors[:auction_id]).to include(
74
+ I18n.t("contracts.errors.custom.bids.invalid_kind", kind: "standard")
75
+ )
76
+ end
77
+ end
78
+
79
+ context 'when auction status is not "scheduled" or "running"' do
80
+ let(:auction) { Factory[:auction, :default_finished_standard] }
81
+ let(:attributes) { {auction_id: auction.id} }
82
+
83
+ it "expect failure with error messages" do
84
+ expect(contract).to be_failure
85
+
86
+ expect(contract.errors[:auction_id]).to include(
87
+ I18n.t("contracts.errors.custom.bids.invalid_status", status: auction.status)
88
+ )
89
+ end
90
+ end
91
+ end
92
+
93
+ context "with value_cents param" do
94
+ let(:user) { Factory[:user] }
95
+ let(:auction) { Factory[:auction, :default_running_standard] }
96
+
97
+ context "when auction has no bids" do
98
+ context "when value_cents is less than to auction minimal initial bid" do
99
+ let(:value) { Money.new(auction.initial_bid_cents - 1) }
100
+ let(:attributes) { {auction_id: auction.id, value_cents: value.cents} }
101
+
102
+ it "expect failure with error messages" do
103
+ expect(contract).to be_failure
104
+
105
+ expect(contract.errors[:value_cents]).to include(
106
+ I18n.t("contracts.errors.gteq?", num: Money.new(auction.initial_bid_cents).to_f)
107
+ )
108
+ end
109
+ end
110
+
111
+ context "when value_cents is greather than or equal to to minimal bid" do
112
+ let(:value) { Money.new(auction.initial_bid_cents) }
113
+ let(:attributes) { {auction_id: auction.id, user_id: user.id, value_cents: value.cents} }
114
+
115
+ it "expect return success" do
116
+ expect(contract).to be_success
117
+ end
118
+ end
119
+ end
120
+
121
+ context "when auction has bids" do
122
+ let(:last_bid) { Money.new(auction.minimal_bid_cents) }
123
+ let!(:bid) { Factory[:bid, auction: auction, value_cents: last_bid.cents] }
124
+ let(:required_minimal_bid) { (last_bid.cents + (last_bid.cents * 0.1)) }
125
+
126
+ before do
127
+ auction_repo.update(auction.id, {minimal_bid_cents: required_minimal_bid.to_i})
128
+ end
129
+
130
+ context "when value_cents is less than to auction minimal bid value" do
131
+ let(:value_cents) { last_bid.cents - 1 }
132
+ let(:attributes) { {auction_id: auction.id, value_cents: value_cents} }
133
+ let(:required_minimal_bid) { (last_bid.cents + (last_bid.cents * 0.1)) }
134
+
135
+ it "expect failure with error messages" do
136
+ expect(contract).to be_failure
137
+ expect(contract.errors[:value_cents]).to include(
138
+ I18n.t("contracts.errors.gteq?", num: Money.new(required_minimal_bid).to_f)
139
+ )
140
+ end
141
+ end
142
+
143
+ context "when value_cents is greather than or equal to ten percent of auction minimal bid value" do
144
+ let(:value_cents) { 1_100 }
145
+ let(:attributes) { {auction_id: auction.id, user_id: user.id, value_cents: value_cents} }
146
+
147
+ it "expect return success" do
148
+ expect(contract).to be_success
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Contracts::StaffContext::AuthenticationContract, type: :contract do
6
+ describe "#call" do
7
+ subject(:contract) { described_class.new.call(attributes) }
8
+
9
+ context "when params are blank" do
10
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
11
+
12
+ it "expect failure with error messages" do
13
+ expect(contract).to be_failure
14
+ expect(contract.errors[:login]).to include(I18n.t("contracts.errors.key?"))
15
+ expect(contract.errors[:password]).to include(I18n.t("contracts.errors.key?"))
16
+ end
17
+ end
18
+
19
+ context "when login params are invalid" do
20
+ context "when email is invalid" do
21
+ let(:attributes) { {login: "invalid_email"} }
22
+
23
+ it "expect failure with error messages" do
24
+ expect(contract).to be_failure
25
+ expect(contract.errors[:login]).to include(
26
+ I18n.t("contracts.errors.custom.macro.login_format")
27
+ )
28
+ end
29
+ end
30
+
31
+ context "when phone is invalid" do
32
+ let(:attributes) { {login: "12345"} }
33
+
34
+ it "expect failure with error messages" do
35
+ expect(contract).to be_failure
36
+ expect(contract.errors[:login]).to include(
37
+ I18n.t("contracts.errors.custom.macro.login_format")
38
+ )
39
+ end
40
+ end
41
+ end
42
+
43
+ context "with database" do
44
+ context "when login is not found on database" do
45
+ let(:attributes) { {login: "notfound@staff.com", password: "example"} }
46
+
47
+ it "expect failure with error messages" do
48
+ expect(contract).to be_failure
49
+ expect(contract.errors[:base]).to include(
50
+ I18n.t("contracts.errors.custom.default.login_not_found")
51
+ )
52
+ end
53
+ end
54
+
55
+ context "when password doesn't match with storage password on database" do
56
+ let(:staff) { Factory[:staff] }
57
+ let(:attributes) { {login: staff.email, password: "invalid"} }
58
+
59
+ it "expect failure with error messages" do
60
+ expect(contract).to be_failure
61
+ expect(contract.errors[:base]).to include(
62
+ I18n.t("contracts.errors.custom.default.login_not_found")
63
+ )
64
+ end
65
+ end
66
+
67
+ context "when credentials are valid but user is inactive" do
68
+ let(:staff) { Factory[:staff, :inactive] }
69
+ let(:attributes) { {login: staff.email, password: "password"} }
70
+
71
+ it "expect failure with error messages" do
72
+ expect(contract).to be_failure
73
+ expect(contract.errors[:base]).to include(
74
+ I18n.t("contracts.errors.custom.default.inactive_account")
75
+ )
76
+ end
77
+ end
78
+ end
79
+
80
+ context "when credentials are valid" do
81
+ let(:staff) { Factory[:staff] }
82
+ let(:attributes) { {login: staff.email, password: "password"} }
83
+
84
+ it "expect return success" do
85
+ expect(contract).to be_success
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Contracts::StaffContext::RegistrationContract, type: :contract do
6
+ describe "#call" do
7
+ subject(:contract) { described_class.new.call(attributes) }
8
+
9
+ context "when params are blank" do
10
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
11
+
12
+ it "expect failure with error messages" do
13
+ expect(contract).to be_failure
14
+ expect(contract.errors[:name]).to include(I18n.t("contracts.errors.key?"))
15
+ expect(contract.errors[:email]).to include(I18n.t("contracts.errors.key?"))
16
+ expect(contract.errors[:phone]).to include(I18n.t("contracts.errors.key?"))
17
+ end
18
+ end
19
+
20
+ it_behaves_like "validate_name_contract", :staff
21
+ it_behaves_like "validate_email_contract", :staff
22
+ it_behaves_like "validate_phone_contract", :staff
23
+ end
24
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Contracts::UserContext::AuthenticationContract, type: :contract do
6
+ describe "#call" do
7
+ subject(:contract) { described_class.new.call(attributes) }
8
+
9
+ context "when params are blank" do
10
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
11
+
12
+ it "expect failure with error messages" do
13
+ expect(contract).to be_failure
14
+ expect(contract.errors[:login]).to include(I18n.t("contracts.errors.key?"))
15
+ expect(contract.errors[:password]).to include(I18n.t("contracts.errors.key?"))
16
+ end
17
+ end
18
+
19
+ context "when login params are invalid" do
20
+ context "when email is invalid" do
21
+ let(:attributes) { {login: "invalid_email"} }
22
+
23
+ it "expect failure with error messages" do
24
+ expect(contract).to be_failure
25
+ expect(contract.errors[:login]).to include(
26
+ I18n.t("contracts.errors.custom.macro.login_format")
27
+ )
28
+ end
29
+ end
30
+
31
+ context "when phone is invalid" do
32
+ let(:attributes) { {login: "12345"} }
33
+
34
+ it "expect failure with error messages" do
35
+ expect(contract).to be_failure
36
+ expect(contract.errors[:login]).to include(
37
+ I18n.t("contracts.errors.custom.macro.login_format")
38
+ )
39
+ end
40
+ end
41
+ end
42
+
43
+ context "with database" do
44
+ context "when login is not found on database" do
45
+ let(:attributes) { {login: "notfound@user.com", password: "example"} }
46
+
47
+ it "expect failure with error messages" do
48
+ expect(contract).to be_failure
49
+ expect(contract.errors[:base]).to include(
50
+ I18n.t("contracts.errors.custom.default.login_not_found")
51
+ )
52
+ end
53
+ end
54
+
55
+ context "when password doesn't match with storage password on database" do
56
+ let(:user) { Factory[:user] }
57
+ let(:attributes) { {login: user.email, password: "invalid"} }
58
+
59
+ it "expect failure with error messages" do
60
+ expect(contract).to be_failure
61
+ expect(contract.errors[:base]).to include(
62
+ I18n.t("contracts.errors.custom.default.login_not_found")
63
+ )
64
+ end
65
+ end
66
+
67
+ context "when credentials are valid but user is inactive" do
68
+ let(:user) { Factory[:user, :inactive] }
69
+ let(:attributes) { {login: user.email, password: "password"} }
70
+
71
+ it "expect failure with error messages" do
72
+ expect(contract).to be_failure
73
+ expect(contract.errors[:base]).to include(
74
+ I18n.t("contracts.errors.custom.default.inactive_account")
75
+ )
76
+ end
77
+ end
78
+ end
79
+
80
+ context "when credentials are valid" do
81
+ let(:user) { Factory[:user] }
82
+ let(:attributes) { {login: user.email, password: "password"} }
83
+
84
+ it "expect return success" do
85
+ expect(contract).to be_success
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Contracts::UserContext::EmailConfirmationContract, type: :contract do
6
+ let(:generate_email_confirmation_token) { AuctionFunCore::Business::TokenService.generate_email_token }
7
+
8
+ describe "#call" do
9
+ subject(:contract) { described_class.new.call(attributes) }
10
+
11
+ context "when params are blank" do
12
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
13
+
14
+ it "expect failure with error messages" do
15
+ expect(contract).to be_failure
16
+ expect(contract.errors[:email_confirmation_token]).to include(I18n.t("contracts.errors.key?"))
17
+ end
18
+ end
19
+
20
+ context "when email_confirmation_token is present" do
21
+ context "when token is not found on database" do
22
+ let(:attributes) { {email_confirmation_token: SecureRandom.hex(3)} }
23
+
24
+ it "expect failure with error messages" do
25
+ expect(contract).to be_failure
26
+ expect(contract.errors[:email_confirmation_token]).to include(
27
+ I18n.t("contracts.errors.custom.default.not_found")
28
+ )
29
+ end
30
+ end
31
+
32
+ context "when credentials are valid but user is inactive" do
33
+ let(:user) { Factory[:user, :inactive, :with_email_confirmation_token] }
34
+ let(:attributes) { {email_confirmation_token: user.email_confirmation_token} }
35
+
36
+ it "expect failure with error messages" do
37
+ expect(contract).to be_failure
38
+ expect(contract.errors[:base]).to include(
39
+ I18n.t("contracts.errors.custom.default.inactive_account")
40
+ )
41
+ end
42
+ end
43
+ end
44
+
45
+ context "when email_confirmation_token is valid" do
46
+ let(:user) { Factory[:user, :unconfirmed, :with_email_confirmation_token] }
47
+ let(:attributes) { {email_confirmation_token: user.email_confirmation_token} }
48
+
49
+ it "expect return success" do
50
+ expect(contract).to be_success
51
+ end
52
+ end
53
+ end
54
+ end