auction_fun_core 0.8.9 → 0.8.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/auction_fun_core.gemspec +1 -1
  4. data/lib/auction_fun_core/version.rb +1 -1
  5. data/spec/auction_fun_core/contracts/auction_context/create_contract_spec.rb +158 -0
  6. data/spec/auction_fun_core/contracts/auction_context/post_auction/participant_contract_spec.rb +77 -0
  7. data/spec/auction_fun_core/contracts/auction_context/post_auction/winner_contract_spec.rb +76 -0
  8. data/spec/auction_fun_core/contracts/auction_context/pre_auction/auction_start_reminder_contract_spec.rb +48 -0
  9. data/spec/auction_fun_core/contracts/auction_context/processor/finish/closed_contract_spec.rb +59 -0
  10. data/spec/auction_fun_core/contracts/auction_context/processor/finish/penny_contract_spec.rb +59 -0
  11. data/spec/auction_fun_core/contracts/auction_context/processor/finish/standard_contract_spec.rb +59 -0
  12. data/spec/auction_fun_core/contracts/auction_context/processor/pause_contract_spec.rb +39 -0
  13. data/spec/auction_fun_core/contracts/auction_context/processor/start_contract_spec.rb +59 -0
  14. data/spec/auction_fun_core/contracts/auction_context/processor/unpause_contract_spec.rb +39 -0
  15. data/spec/auction_fun_core/contracts/bid_context/create_bid_closed_contract_spec.rb +130 -0
  16. data/spec/auction_fun_core/contracts/bid_context/create_bid_penny_contract_spec.rb +118 -0
  17. data/spec/auction_fun_core/contracts/bid_context/create_bid_standard_contract_spec.rb +154 -0
  18. data/spec/auction_fun_core/contracts/staff_context/authentication_contract_spec.rb +89 -0
  19. data/spec/auction_fun_core/contracts/staff_context/registration_contract_spec.rb +24 -0
  20. data/spec/auction_fun_core/contracts/user_context/authentication_contract_spec.rb +89 -0
  21. data/spec/auction_fun_core/contracts/user_context/email_confirmation_contract_spec.rb +54 -0
  22. data/spec/auction_fun_core/contracts/user_context/phone_confirmation_contract_spec.rb +54 -0
  23. data/spec/auction_fun_core/contracts/user_context/registration_contract_spec.rb +27 -0
  24. data/spec/auction_fun_core/entities/auction_spec.rb +75 -0
  25. data/spec/auction_fun_core/entities/bid_spec.rb +7 -0
  26. data/spec/auction_fun_core/entities/staff_spec.rb +38 -0
  27. data/spec/auction_fun_core/entities/user_spec.rb +106 -0
  28. data/spec/auction_fun_core/operations/auction_context/create_operation_spec.rb +126 -0
  29. data/spec/auction_fun_core/operations/auction_context/post_auction/participant_operation_spec.rb +88 -0
  30. data/spec/auction_fun_core/operations/auction_context/post_auction/winner_operation_spec.rb +76 -0
  31. data/spec/auction_fun_core/operations/auction_context/pre_auction/auction_start_reminder_operation_spec.rb +98 -0
  32. data/spec/auction_fun_core/operations/auction_context/processor/finish/closed_operation_spec.rb +107 -0
  33. data/spec/auction_fun_core/operations/auction_context/processor/finish/penny_operation_spec.rb +107 -0
  34. data/spec/auction_fun_core/operations/auction_context/processor/finish/standard_operation_spec.rb +106 -0
  35. data/spec/auction_fun_core/operations/auction_context/processor/pause_operation_spec.rb +83 -0
  36. data/spec/auction_fun_core/operations/auction_context/processor/start_operation_spec.rb +147 -0
  37. data/spec/auction_fun_core/operations/auction_context/processor/unpause_operation_spec.rb +80 -0
  38. data/spec/auction_fun_core/operations/bid_context/create_bid_closed_operation_spec.rb +95 -0
  39. data/spec/auction_fun_core/operations/bid_context/create_bid_penny_operation_spec.rb +133 -0
  40. data/spec/auction_fun_core/operations/bid_context/create_bid_standard_operation_spec.rb +95 -0
  41. data/spec/auction_fun_core/operations/staff_context/authentication_operation_spec.rb +74 -0
  42. data/spec/auction_fun_core/operations/staff_context/registration_operation_spec.rb +93 -0
  43. data/spec/auction_fun_core/operations/user_context/authentication_operation_spec.rb +74 -0
  44. data/spec/auction_fun_core/operations/user_context/email_confirmation_operation_spec.rb +76 -0
  45. data/spec/auction_fun_core/operations/user_context/phone_confirmation_operation_spec.rb +76 -0
  46. data/spec/auction_fun_core/operations/user_context/registration_operation_spec.rb +100 -0
  47. data/spec/auction_fun_core/relations/auctions_spec.rb +471 -0
  48. data/spec/auction_fun_core/repos/auction_context/auction_repository_spec.rb +64 -0
  49. data/spec/auction_fun_core/repos/bid_context/bid_repository_spec.rb +64 -0
  50. data/spec/auction_fun_core/repos/staff_context/staff_repository_spec.rb +118 -0
  51. data/spec/auction_fun_core/repos/user_context/user_repository_spec.rb +117 -0
  52. data/spec/auction_fun_core/services/mail/auction_context/post_auction/participant_mailer_spec.rb +48 -0
  53. data/spec/auction_fun_core/services/mail/auction_context/post_auction/winner_mailer_spec.rb +48 -0
  54. data/spec/auction_fun_core/services/mail/auction_context/pre_auction/auction_start_reminder_mailer_spec.rb +40 -0
  55. data/spec/auction_fun_core/services/mail/user_context/registration_mailer_spec.rb +33 -0
  56. data/spec/auction_fun_core/workers/operations/auction_context/post_auction/participation_operation_job_spec.rb +54 -0
  57. data/spec/auction_fun_core/workers/operations/auction_context/post_auction/winner_operation_job_spec.rb +48 -0
  58. data/spec/auction_fun_core/workers/operations/auction_context/pre_auction/auction_start_reminder_operation_job_spec.rb +53 -0
  59. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/closed_operation_job_spec.rb +47 -0
  60. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/penny_operation_job_spec.rb +47 -0
  61. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/standard_operation_job_spec.rb +47 -0
  62. data/spec/auction_fun_core/workers/operations/auction_context/processor/start_operation_job_spec.rb +47 -0
  63. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/participant_mailer_job_spec.rb +62 -0
  64. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/pre_auction/auction_start_reminder_mailer_job_spec.rb +59 -0
  65. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/winner_mailer_job_spec.rb +62 -0
  66. data/spec/auction_fun_core/workers/services/mail/user_context/registration_mailer_job_spec.rb +53 -0
  67. data/spec/auction_fun_core_spec.rb +7 -0
  68. data/spec/spec_helper.rb +61 -0
  69. data/spec/support/background_job.rb +7 -0
  70. data/spec/support/factories/auctions.rb +143 -0
  71. data/spec/support/factories/bids.rb +6 -0
  72. data/spec/support/factories/staffs.rb +18 -0
  73. data/spec/support/factories/users.rb +43 -0
  74. data/spec/support/faker.rb +6 -0
  75. data/spec/support/mail.rb +13 -0
  76. data/spec/support/shared_examples/validate_email_contract.rb +25 -0
  77. data/spec/support/shared_examples/validate_name_contract.rb +19 -0
  78. data/spec/support/shared_examples/validate_password_confirmation_contract.rb +16 -0
  79. data/spec/support/shared_examples/validate_password_contract.rb +18 -0
  80. data/spec/support/shared_examples/validate_phone_contract.rb +25 -0
  81. data/spec/support/shared_examples/validate_stopwatch_contract.rb +32 -0
  82. metadata +79 -2
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::AuctionContext::Processor::UnpauseOperation, type: :operation do
6
+ let(:auction_repository) { AuctionFunCore::Repos::AuctionContext::AuctionRepository.new }
7
+ let(:auction) { Factory[:auction, :default_paused_standard] }
8
+
9
+ describe ".call(attributes, &block)" do
10
+ let(:operation) { described_class }
11
+
12
+ context "when block is given" do
13
+ context "when operation happens with success" do
14
+ let(:attributes) { {auction_id: auction.id} }
15
+
16
+ it "expect result success matching block" do
17
+ matched_success = nil
18
+ matched_failure = nil
19
+
20
+ operation.call(attributes) do |o|
21
+ o.success { |v| matched_success = v }
22
+ o.failure { |f| matched_failure = f }
23
+ end
24
+
25
+ expect(matched_success).to eq(auction.id)
26
+ expect(matched_failure).to be_nil
27
+ end
28
+ end
29
+
30
+ context "when operation happens with failure" do
31
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
32
+
33
+ it "expect result matching block" do
34
+ matched_success = nil
35
+ matched_failure = nil
36
+
37
+ operation.call(attributes) do |o|
38
+ o.success { |v| matched_success = v }
39
+ o.failure { |f| matched_failure = f }
40
+ end
41
+
42
+ expect(matched_success).to be_nil
43
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "#call(attributes)" do
50
+ subject(:operation) { described_class.new.call(attributes) }
51
+
52
+ context "when contract is invalid" do
53
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
54
+
55
+ it "expect not change auction status" do
56
+ expect { operation }.not_to change { auction_repository.by_id(auction.id).status }
57
+ end
58
+
59
+ it "expect return failure with error messages" do
60
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
61
+ end
62
+ end
63
+
64
+ context "when contract is valid" do
65
+ let(:attributes) { {auction_id: auction.id} }
66
+
67
+ it "expect update status auction record on database" do
68
+ expect { operation }.to change { auction_repository.by_id(auction.id).status }.from("paused").to("running")
69
+ end
70
+
71
+ it "expect publish the auction unpause event" do
72
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
73
+
74
+ operation
75
+
76
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::BidContext::CreateBidClosedOperation, type: :operation do
6
+ let(:bid_repository) { AuctionFunCore::Repos::BidContext::BidRepository.new }
7
+
8
+ describe ".call(attributes, &block)" do
9
+ let(:operation) { described_class }
10
+
11
+ context "when block is given" do
12
+ context "when operation happens with success" do
13
+ let(:auction) { Factory[:auction, :default_scheduled_closed] }
14
+ let(:user) { Factory[:user] }
15
+ let(:attributes) do
16
+ Factory.structs[:bid, user: user, auction: auction]
17
+ .to_h.except(:id, :created_at, :updated_at, :admin, :user)
18
+ .merge!(value_cents: auction.initial_bid_cents * 2)
19
+ end
20
+
21
+ it "expect result success matching block" do
22
+ matched_success = nil
23
+ matched_failure = nil
24
+
25
+ operation.call(attributes) do |o|
26
+ o.success { |v| matched_success = v }
27
+ o.failure { |f| matched_failure = f }
28
+ end
29
+
30
+ expect(matched_success).to be_a(AuctionFunCore::Entities::Bid)
31
+ expect(matched_failure).to be_nil
32
+ end
33
+ end
34
+
35
+ context "when operation happens with failure" do
36
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
37
+
38
+ it "expect result matching block" do
39
+ matched_success = nil
40
+ matched_failure = nil
41
+
42
+ operation.call(attributes) do |o|
43
+ o.success { |v| matched_success = v }
44
+ o.failure { |f| matched_failure = f }
45
+ end
46
+
47
+ expect(matched_success).to be_nil
48
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "#call" do
55
+ subject(:operation) { described_class.new.call(attributes) }
56
+
57
+ context "when contract are not valid" do
58
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
59
+
60
+ it "expect not persist new bid on database" do
61
+ expect(bid_repository.count).to be_zero
62
+
63
+ expect { operation }.not_to change(bid_repository, :count)
64
+ end
65
+
66
+ it "expect return failure with error messages" do
67
+ expect(operation).to be_failure
68
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
69
+ end
70
+ end
71
+
72
+ context "when contract are valid" do
73
+ let(:auction) { Factory[:auction, :default_scheduled_closed] }
74
+ let(:user) { Factory[:user] }
75
+ let(:attributes) do
76
+ Factory.structs[:bid, user: user, auction: auction]
77
+ .to_h.except(:id, :created_at, :updated_at, :staff, :user)
78
+ .merge!(value_cents: auction.initial_bid_cents * 2)
79
+ end
80
+
81
+ it "expect return success without error messages" do
82
+ expect(operation).to be_success
83
+ expect(operation.failure).to be_blank
84
+ end
85
+
86
+ it "expect persist new bid on database and dispatch event" do
87
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
88
+
89
+ expect { operation }.to change(bid_repository, :count).from(0).to(1)
90
+
91
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::BidContext::CreateBidPennyOperation, type: :operation do
6
+ let(:auction_repository) { AuctionFunCore::Repos::AuctionContext::AuctionRepository.new }
7
+ let(:bid_repository) { AuctionFunCore::Repos::BidContext::BidRepository.new }
8
+
9
+ describe ".call(attributes, &block)" do
10
+ let(:operation) { described_class }
11
+
12
+ context "when block is given" do
13
+ context "when operation happens with success" do
14
+ let(:auction) { Factory[:auction, :default_scheduled_penny] }
15
+ let(:user) { Factory[:user] }
16
+ let(:attributes) do
17
+ Factory.structs[:bid, user: user, auction: auction]
18
+ .to_h.except(:id, :created_at, :updated_at, :staff, :user)
19
+ end
20
+
21
+ it "expect result success matching block" do
22
+ matched_success = nil
23
+ matched_failure = nil
24
+
25
+ operation.call(attributes) do |o|
26
+ o.success { |v| matched_success = v }
27
+ o.failure { |f| matched_failure = f }
28
+ end
29
+
30
+ expect(matched_success).to be_a(AuctionFunCore::Entities::Bid)
31
+ expect(matched_failure).to be_nil
32
+ end
33
+ end
34
+
35
+ context "when operation happens with failure" do
36
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
37
+
38
+ it "expect result matching block" do
39
+ matched_success = nil
40
+ matched_failure = nil
41
+
42
+ operation.call(attributes) do |o|
43
+ o.success { |v| matched_success = v }
44
+ o.failure { |f| matched_failure = f }
45
+ end
46
+
47
+ expect(matched_success).to be_nil
48
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "#call" do
55
+ subject(:operation) { described_class.new.call(attributes) }
56
+
57
+ context "when contract are not valid" do
58
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
59
+
60
+ it "expect not persist new bid on database" do
61
+ expect(bid_repository.count).to be_zero
62
+
63
+ expect { operation }.not_to change(bid_repository, :count)
64
+ end
65
+
66
+ it "expect return failure with error messages" do
67
+ expect(operation).to be_failure
68
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
69
+ end
70
+ end
71
+
72
+ context "when contract are valid" do
73
+ let(:auction) { Factory[:auction, :default_scheduled_penny] }
74
+ let(:user) { Factory[:user] }
75
+ let(:attributes) do
76
+ Factory.structs[:bid, user: user, auction: auction]
77
+ .to_h.except(:id, :created_at, :updated_at, :staff, :user)
78
+ end
79
+
80
+ it "expect return success without error messages" do
81
+ expect(operation).to be_success
82
+ expect(operation.failure).to be_blank
83
+ end
84
+
85
+ it "expect persist new bid on database" do
86
+ expect { operation }.to change(bid_repository, :count).from(0).to(1)
87
+ end
88
+
89
+ it "expect dispatch event" do
90
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
91
+
92
+ operation
93
+
94
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
95
+ end
96
+
97
+ context "when an auction has not started" do
98
+ it "expects not to update the auction's 'finished_at' field" do
99
+ expect { operation }.not_to change { auction_repository.by_id(auction.id).finished_at }
100
+ end
101
+
102
+ it "expect not reschedule the end of the auction" do
103
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::PennyOperationJob)
104
+ .to receive(:perform_at).with(Time, auction.id)
105
+
106
+ operation
107
+
108
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::PennyOperationJob)
109
+ .not_to have_received(:perform_at).with(Time, auction.id)
110
+ end
111
+ end
112
+
113
+ context "when an auction was started" do
114
+ let(:auction) { Factory[:auction, :default_running_penny] }
115
+ let(:user) { Factory[:user, :with_balance] }
116
+
117
+ it "expects to update the auction's 'finished_at' field and reschedule the end of the auction" do
118
+ expect { operation }.to change { auction_repository.by_id(auction.id).finished_at }
119
+ end
120
+
121
+ it "expect reschedule the end of the auction" do
122
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::PennyOperationJob)
123
+ .to receive(:perform_at).with(Time, auction.id)
124
+
125
+ operation
126
+
127
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::PennyOperationJob)
128
+ .to have_received(:perform_at).with(Time, auction.id).once
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::BidContext::CreateBidStandardOperation, type: :operation do
6
+ let(:bid_repository) { AuctionFunCore::Repos::BidContext::BidRepository.new }
7
+
8
+ describe ".call(attributes, &block)" do
9
+ let(:operation) { described_class }
10
+
11
+ context "when block is given" do
12
+ context "when operation happens with success" do
13
+ let(:auction) { Factory[:auction, :default_scheduled_standard] }
14
+ let(:user) { Factory[:user] }
15
+ let(:attributes) do
16
+ Factory.structs[:bid, user: user, auction: auction]
17
+ .to_h.except(:id, :created_at, :updated_at, :staff, :user)
18
+ .merge!(value_cents: auction.initial_bid_cents * 2)
19
+ end
20
+
21
+ it "expect result success matching block" do
22
+ matched_success = nil
23
+ matched_failure = nil
24
+
25
+ operation.call(attributes) do |o|
26
+ o.success { |v| matched_success = v }
27
+ o.failure { |f| matched_failure = f }
28
+ end
29
+
30
+ expect(matched_success).to be_a(AuctionFunCore::Entities::Bid)
31
+ expect(matched_failure).to be_nil
32
+ end
33
+ end
34
+
35
+ context "when operation happens with failure" do
36
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
37
+
38
+ it "expect result matching block" do
39
+ matched_success = nil
40
+ matched_failure = nil
41
+
42
+ operation.call(attributes) do |o|
43
+ o.success { |v| matched_success = v }
44
+ o.failure { |f| matched_failure = f }
45
+ end
46
+
47
+ expect(matched_success).to be_nil
48
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "#call" do
55
+ subject(:operation) { described_class.new.call(attributes) }
56
+
57
+ context "when contract are not valid" do
58
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
59
+
60
+ it "expect return failure with error messages" do
61
+ expect(operation).to be_failure
62
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
63
+ end
64
+
65
+ it "expect not persist new bid on database" do
66
+ expect(bid_repository.count).to be_zero
67
+
68
+ expect { operation }.not_to change(bid_repository, :count)
69
+ end
70
+ end
71
+
72
+ context "when contract are valid" do
73
+ let(:auction) { Factory[:auction, :default_scheduled_standard] }
74
+ let(:user) { Factory[:user] }
75
+ let(:attributes) do
76
+ Factory.structs[:bid, user: user, auction: auction]
77
+ .to_h.except(:id, :created_at, :updated_at, :staff, :user)
78
+ .merge!(value_cents: auction.initial_bid_cents * 2)
79
+ end
80
+
81
+ it "expect persist new bid on database" do
82
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
83
+
84
+ expect { operation }.to change(bid_repository, :count).from(0).to(1)
85
+
86
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
87
+ end
88
+
89
+ it "expect return success without error messages" do
90
+ expect(operation).to be_success
91
+ expect(operation.failure).to be_blank
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::StaffContext::AuthenticationOperation, type: :operation do
6
+ describe ".call(attributes, &block)" do
7
+ let(:operation) { described_class }
8
+
9
+ context "when block is given" do
10
+ context "when operation happens with success" do
11
+ let(:staff) { Factory[:staff] }
12
+ let(:attributes) { {login: staff.email, password: "password"} }
13
+
14
+ it "expect result success matching block" do
15
+ matched_success = nil
16
+ matched_failure = nil
17
+
18
+ operation.call(attributes) do |o|
19
+ o.success { |v| matched_success = v }
20
+ o.failure { |f| matched_failure = f }
21
+ end
22
+
23
+ expect(matched_success).to be_a(AuctionFunCore::Entities::Staff)
24
+ expect(matched_failure).to be_nil
25
+ end
26
+ end
27
+
28
+ context "when operation happens with failure" do
29
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
30
+
31
+ it "expect result matching block" do
32
+ matched_success = nil
33
+ matched_failure = nil
34
+
35
+ operation.call(attributes) do |o|
36
+ o.success { |v| matched_success = v }
37
+ o.failure { |f| matched_failure = f }
38
+ end
39
+
40
+ expect(matched_success).to be_nil
41
+ expect(matched_failure[:login]).to include(I18n.t("contracts.errors.key?"))
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#call(attributes)" do
48
+ subject(:operation) { described_class.new.call(attributes) }
49
+
50
+ context "when contract are invalid" do
51
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
52
+
53
+ it "expect return failure with error messages" do
54
+ expect(operation).to be_failure
55
+ expect(operation.failure[:login]).to include(I18n.t("contracts.errors.key?"))
56
+ end
57
+ end
58
+
59
+ context "when contract are valid" do
60
+ let(:staff) { Factory[:staff] }
61
+ let(:attributes) { {login: staff.email, password: "password"} }
62
+
63
+ before do
64
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
65
+ end
66
+
67
+ it "expect return success" do
68
+ expect(operation).to be_success
69
+
70
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::StaffContext::RegistrationOperation, type: :operation do
6
+ let(:staff_repository) { AuctionFunCore::Repos::StaffContext::StaffRepository.new }
7
+
8
+ describe ".call(attributes, &block)" do
9
+ let(:operation) { described_class }
10
+
11
+ context "when block is given" do
12
+ context "when operation happens with success" do
13
+ let(:attributes) do
14
+ Factory.structs[:staff]
15
+ .to_h
16
+ .except(:id, :created_at, :updated_at, :password_digest)
17
+ end
18
+
19
+ it "expect result success matching block" do
20
+ matched_success = nil
21
+ matched_failure = nil
22
+
23
+ operation.call(attributes) do |o|
24
+ o.success { |v| matched_success = v }
25
+ o.failure { |f| matched_failure = f }
26
+ end
27
+
28
+ expect(matched_success).to be_a(AuctionFunCore::Entities::Staff)
29
+ expect(matched_failure).to be_nil
30
+ end
31
+ end
32
+
33
+ context "when operation happens with failure" do
34
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
35
+
36
+ it "expect result matching block" do
37
+ matched_success = nil
38
+ matched_failure = nil
39
+
40
+ operation.call(attributes) do |o|
41
+ o.success { |v| matched_success = v }
42
+ o.failure { |f| matched_failure = f }
43
+ end
44
+
45
+ expect(matched_success).to be_nil
46
+ expect(matched_failure[:name]).to include(I18n.t("contracts.errors.key?"))
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#call" do
53
+ subject(:operation) { described_class.new.call(attributes) }
54
+
55
+ context "when contract are not valid" do
56
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
57
+
58
+ it "expect not persist new staff on database" do
59
+ expect(staff_repository.count).to be_zero
60
+
61
+ expect { operation }.not_to change(staff_repository, :count)
62
+ end
63
+
64
+ it "expect return failure with error messages" do
65
+ expect(operation).to be_failure
66
+ expect(operation.failure[:name]).to include(I18n.t("contracts.errors.key?"))
67
+ end
68
+ end
69
+
70
+ context "when contract are valid" do
71
+ let(:attributes) do
72
+ Factory.structs[:staff]
73
+ .to_h
74
+ .except(:id, :created_at, :updated_at, :password_digest)
75
+ end
76
+
77
+ before do
78
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
79
+ end
80
+
81
+ it "expect persist new staff on database and dispatch event registration" do
82
+ expect { operation }.to change(staff_repository, :count).from(0).to(1)
83
+
84
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
85
+ end
86
+
87
+ it "expect return success without error messages" do
88
+ expect(operation).to be_success
89
+ expect(operation.failure).to be_blank
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::UserContext::AuthenticationOperation, type: :operation do
6
+ describe ".call(attributes, &block)" do
7
+ let(:operation) { described_class }
8
+
9
+ context "when block is given" do
10
+ context "when operation happens with success" do
11
+ let(:user) { Factory[:user] }
12
+ let(:attributes) { {login: user.email, password: "password"} }
13
+
14
+ it "expect result success matching block" do
15
+ matched_success = nil
16
+ matched_failure = nil
17
+
18
+ operation.call(attributes) do |o|
19
+ o.success { |v| matched_success = v }
20
+ o.failure { |f| matched_failure = f }
21
+ end
22
+
23
+ expect(matched_success).to be_a(AuctionFunCore::Entities::User)
24
+ expect(matched_failure).to be_nil
25
+ end
26
+ end
27
+
28
+ context "when operation happens with failure" do
29
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
30
+
31
+ it "expect result matching block" do
32
+ matched_success = nil
33
+ matched_failure = nil
34
+
35
+ operation.call(attributes) do |o|
36
+ o.success { |v| matched_success = v }
37
+ o.failure { |f| matched_failure = f }
38
+ end
39
+
40
+ expect(matched_success).to be_nil
41
+ expect(matched_failure[:login]).to include(I18n.t("contracts.errors.key?"))
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#call(attributes)" do
48
+ subject(:operation) { described_class.new.call(attributes) }
49
+
50
+ context "when contract are invalid" do
51
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
52
+
53
+ it "expect return failure with error messages" do
54
+ expect(operation).to be_failure
55
+ expect(operation.failure[:login]).to include(I18n.t("contracts.errors.key?"))
56
+ end
57
+ end
58
+
59
+ context "when contract are valid" do
60
+ let(:user) { Factory[:user] }
61
+ let(:attributes) { {login: user.email, password: "password"} }
62
+
63
+ before do
64
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
65
+ end
66
+
67
+ it "expect return success" do
68
+ expect(operation).to be_success
69
+
70
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
71
+ end
72
+ end
73
+ end
74
+ end