From c3ba4a85c1bb9f620b7d6cd344d6d8477e567608 Mon Sep 17 00:00:00 2001 From: JesusGautamah Date: Fri, 25 Nov 2022 07:04:06 -0700 Subject: [PATCH] refactored some files, created ticket open api feature with tests, refactored some api methdos --- CHANGELOG.md | 11 +- README.md | 8 +- .../api/v1/application_controller.rb | 26 + .../api/v1/ticket_manager_controller.rb | 65 + app/workers/create_ticket_worker.rb | 1 + config/initializers/sidekiq.rb | 1 + config/routes/api.rb | 2 +- coverage/.last_run.json | 2 +- coverage/.resultset.json | 219 ++- coverage/index.html | 1725 +++++++++++++---- osbc.gemspec | 2 +- .../api/v1/ticket_manager_controller_spec.rb | 71 + 12 files changed, 1693 insertions(+), 440 deletions(-) create mode 100644 config/initializers/sidekiq.rb create mode 100644 spec/requests/api/v1/ticket_manager_controller_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c22575..de639eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## [Unreleased] -## [0.1.0] - 2022-10-18 - -- Initial release +## [0.1.5] +### Added +- Initial Version +## [0.1.6] +### Added +- Docker Compose Installation Recognition +- Refactored some tests +- Added lib tasks helper to created rake tasks for blockchain diff --git a/README.md b/README.md index e48be28..1d8ab0a 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ #### Rspec is used to test the blockchain. - ## Build Version: 0.1.6 ### Last Update: #### Added Docker Compose Installation Recognition and Refactored some tests @@ -26,10 +25,7 @@ ###### Add more documentation ###### Add NFT Generator ###### Increase the environment variables for autoconfig -###### Add features of autogeneration of blockchain for better configuration - - - +###### Add features to autogeneration of blockchain for better configuration ## System dependencies 1. Docker @@ -195,7 +191,7 @@ The timestamps of the signatures will be usefull to version the block, checking **The miners will be rewarded with the block reward distributed by the number of signatures** ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/outerspace-coding/outerspace-blockchain +Bug reports and pull requests are welcome on GitHub at https://github.com/JesusGautamah/outerspace-blockchain ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/app/controllers/api/v1/application_controller.rb b/app/controllers/api/v1/application_controller.rb index c77d4fb..72ba831 100644 --- a/app/controllers/api/v1/application_controller.rb +++ b/app/controllers/api/v1/application_controller.rb @@ -11,6 +11,8 @@ def ticket_founded? end private + attr_accessor :user + def find_user_by_header return unless request.headers["X-API-KEY"].present? api_key = request.headers["X-API-KEY"] @@ -38,4 +40,28 @@ def ticket_not_found_response def unauthorized_response render json: { error: "Unauthorized" }, status: :unauthorized end + + def current_block + @current_block = Block.find_by(master_hash: nil) + end + + def current_pool + @current_pool = Pool.find_by(block_id: current_block.id) + end + + def ticket + @ticket = Ticket.find_by(user_id: user.id, status: :active) + end + + def block_transactions + @block_transactions = Transaction.where(block_id: current_block.id) + end + + def block_transactions_empty? + return no_transactions_response unless block_transactions.present? + end + + def no_transactions_response + render json: { error: "No transactions in the block" }, status: :not_found + end end diff --git a/app/controllers/api/v1/ticket_manager_controller.rb b/app/controllers/api/v1/ticket_manager_controller.rb index e69de29..d14f32f 100644 --- a/app/controllers/api/v1/ticket_manager_controller.rb +++ b/app/controllers/api/v1/ticket_manager_controller.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +class Api::V1::TicketManagerController < Api::V1::ApplicationController + before_action :unauthorized? + before_action :block_transactions_empty?, only: [:open_ticket] + + def open_ticket + return ticket_already_opened_response unless ticket_nil? + return ticket_already_opened_response if ticket_active? + return terms_not_confirmed_response unless ticket_params_confirmed? + create_ticket if acceptable_create? + ticket_opened_response + end + + + private + def ticket_nil? + ticket.nil? + end + + def ticket_active? + return false if ticket_nil? + ticket.status == "active" + end + + def acceptable_create? + ticket_nil? && ticket_params_confirmed? || ticket_params_confirmed? && ticket_active? == false + end + + def ticket_params_confirmed? + open_ticket_params[:ticket_terms] == "confirmed" + end + + def ticket_already_opened_response + render json: { error: "Ticket already opened" }, status: :not_found + end + + def ticket_opened_response + render json: { message: "Ticket opened" }, status: :ok + end + + def terms_not_confirmed_response + render json: { error: "Ticket terms not confirmed" }, status: :not_found + end + + def open_ticket_params + params.permit(:ticket_terms) + end + + def time_ref + @time_ref = block_transactions.first.created_at.to_s + end + + def user_id + @user_id = user.id.to_s + end + + def current_pool_id + @current_pool_id = current_pool.id.to_s + end + + def create_ticket + CreateTicketWorker.perform_async(user_id, current_pool_id, time_ref) + end +end diff --git a/app/workers/create_ticket_worker.rb b/app/workers/create_ticket_worker.rb index 6c29a14..f63f9f6 100644 --- a/app/workers/create_ticket_worker.rb +++ b/app/workers/create_ticket_worker.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class CreateTicketWorker < ApplicationWorker + include Sidekiq::Worker sidekiq_options retry: false def perform(user_id, pool_id, time_ref) diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb new file mode 100644 index 0000000..ae17d27 --- /dev/null +++ b/config/initializers/sidekiq.rb @@ -0,0 +1 @@ +Sidekiq.strict_args! \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index d701a1b..c2bbbdf 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -3,7 +3,7 @@ namespace :api do namespace :v1 do post "confirm_block", to: "block_confirmations#confirm_block" - # get "tr_to_mine", to: "block_confirmations#transactions_to_mine" + post "open_ticket", to: "ticket_manager#open_ticket" get "info_to_mine", to: "block_confirmations#info_to_mine" end end diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 6e527da..a017211 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "line": 81.24 + "line": 83.46 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 1e45b1a..5ffead8 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -23,28 +23,28 @@ null, 1, 1, - 71, + 83, null, null, 1, - 71, - 71, - 71, + 83, + 83, + 83, null, - 71, + 83, null, - 71, + 83, null, - 71, + 83, null, - 71, - 71, - 71, + 83, + 83, + 83, null, null, 1, - 71, - 71, + 83, + 83, null, null ] @@ -566,7 +566,7 @@ null, 1, 1, - 68, + 72, null, null ] @@ -660,7 +660,7 @@ null, null, 1, - 42, + 57, null, null ] @@ -671,11 +671,11 @@ null, 1, 1, - 52, + 67, 13, null, null, - 52, + 67, 13, null, null, @@ -701,19 +701,19 @@ 1, null, 1, - 59, - 57, - 57, + 71, + 69, + 69, null, null, - 59, - 59, + 71, + 71, null, - 59, + 71, 1, null, - 58, - 58, + 70, + 70, null, null, null @@ -813,7 +813,7 @@ null, 1, 1, - 8, + 12, null, null, 1, @@ -823,11 +823,13 @@ null, 1, 1, - 8, - 8, - 8, - 8, - 6, + null, + 1, + 12, + 12, + 12, + 12, + 10, null, null, 1, @@ -849,6 +851,99 @@ 1, 3, null, + null, + 1, + 6, + null, + null, + 1, + 1, + null, + null, + 1, + 6, + null, + null, + 1, + 5, + null, + null, + 1, + 4, + null, + null, + 1, + 1, + null, + null + ] + }, + "/myapp/app/controllers/api/v1/ticket_manager_controller.rb": { + "lines": [ + null, + null, + 1, + 1, + 1, + null, + 1, + 3, + 2, + 2, + 1, + 1, + null, + null, + null, + 1, + 1, + 6, + null, + null, + 1, + 2, + 0, + null, + null, + 1, + 1, + null, + null, + 1, + 3, + null, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 3, + null, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 1, + null, + null, + 1, + 1, + null, null ] }, @@ -1153,7 +1248,7 @@ null, null, 1, - 12, + 10, 2, 2, 2, @@ -1183,7 +1278,7 @@ null, null, 1, - 87, + 85, null, null, 1, @@ -1608,12 +1703,33 @@ null ] }, - "/myapp/app/controllers/api/v1/ticket_manager_controller.rb": { + "/myapp/app/workers/create_ticket_worker.rb": { "lines": [ + null, + null, + 1, + 1, + 1, + null, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + null, + null, + null, + 1, + 1, + null, + 1, + 0, + null, null - ], - "branches": { - } + ] }, "/myapp/app/models/concerns/sequences_validator.rb": { "lines": [ @@ -1743,37 +1859,8 @@ ], "branches": { } - }, - "/myapp/app/workers/create_ticket_worker.rb": { - "lines": [ - null, - null, - 0, - 0, - null, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - null, - 0, - 0, - null, - 0, - 0, - 0, - 0 - ], - "branches": { - } } }, - "timestamp": 1669378473 + "timestamp": 1669384840 } } diff --git a/coverage/index.html b/coverage/index.html index 921d703..c26fb85 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -14,7 +14,7 @@ loading
-
Generated 2022-11-25T12:14:33+00:00
+
Generated 2022-11-25T14:00:40+00:00
@@ -23,14 +23,14 @@

All Files ( - 81.24% + 83.46% covered at - 7.05 + 7.15 hits/line ) @@ -43,11 +43,11 @@

- 741 relevant lines, - 602 lines covered and - 139 lines missed. + 786 relevant lines, + 656 lines covered and + 130 lines missed. ( - 81.24% + 83.46% )
@@ -73,11 +73,11 @@

app/controllers/api/v1/application_controller.rb 100.00 % - 41 - 23 - 23 + 67 + 36 + 36 0 - 3.35 + 3.64 @@ -94,12 +94,12 @@

app/controllers/api/v1/ticket_manager_controller.rb - 100.00 % + 97.14 % + 65 + 35 + 34 1 - 0 - 0 - 0 - 0.00 + 1.37 @@ -429,7 +429,7 @@

12 12 0 - 6.58 + 6.92 @@ -462,7 +462,7 @@

6 6 0 - 22.00 + 27.00 @@ -517,7 +517,7 @@

16 16 0 - 3.69 + 4.63 @@ -528,7 +528,7 @@

27 27 0 - 32.11 + 37.44 @@ -539,7 +539,7 @@

18 18 0 - 26.44 + 31.78 @@ -561,7 +561,7 @@

98 98 0 - 25.94 + 25.90 @@ -622,12 +622,12 @@

app/workers/create_ticket_worker.rb - 0.00 % - 23 - 18 - 0 - 18 - 0.00 + 46.67 % + 24 + 15 + 7 + 8 + 0.47 @@ -720,14 +720,14 @@

Controllers ( - 93.69% + 94.44% covered at - 3.26 + 3.06 hits/line ) @@ -740,11 +740,11 @@

- 222 relevant lines, - 208 lines covered and - 14 lines missed. + 270 relevant lines, + 255 lines covered and + 15 lines missed. ( - 93.69% + 94.44% )
@@ -770,11 +770,11 @@

app/controllers/api/v1/application_controller.rb 100.00 % - 41 - 23 - 23 + 67 + 36 + 36 0 - 3.35 + 3.64 @@ -791,12 +791,12 @@

app/controllers/api/v1/ticket_manager_controller.rb - 100.00 % + 97.14 % + 65 + 35 + 34 1 - 0 - 0 - 0 - 0.00 + 1.37 @@ -1041,7 +1041,7 @@

covered at - 12.53 + 14.74 hits/line ) @@ -1132,7 +1132,7 @@

12 12 0 - 6.58 + 6.92 @@ -1165,7 +1165,7 @@

6 6 0 - 22.00 + 27.00 @@ -1220,7 +1220,7 @@

16 16 0 - 3.69 + 4.63 @@ -1231,7 +1231,7 @@

27 27 0 - 32.11 + 37.44 @@ -1242,7 +1242,7 @@

18 18 0 - 26.44 + 31.78 @@ -1482,15 +1482,15 @@

Jobs ( - - 78.07% + + 86.49% covered at - 1.35 + 1.45 hits/line ) @@ -1503,11 +1503,11 @@

- 114 relevant lines, - 89 lines covered and - 25 lines missed. - ( - 78.07% + 111 relevant lines, + 96 lines covered and + 15 lines missed. + ( + 86.49% )
@@ -1554,12 +1554,12 @@

app/workers/create_ticket_worker.rb - 0.00 % - 23 - 18 - 0 - 18 - 0.00 + 46.67 % + 24 + 15 + 7 + 8 + 0.47 @@ -1716,7 +1716,7 @@

covered at - 11.76 + 11.74 hits/line ) @@ -1774,7 +1774,7 @@

98 98 0 - 25.94 + 25.90 @@ -1821,15 +1821,15 @@

Workers ( - - 78.07% + + 86.49% covered at - 1.35 + 1.45 hits/line ) @@ -1842,11 +1842,11 @@

- 114 relevant lines, - 89 lines covered and - 25 lines missed. - ( - 78.07% + 111 relevant lines, + 96 lines covered and + 15 lines missed. + ( + 86.49% )
@@ -1893,12 +1893,12 @@

app/workers/create_ticket_worker.rb - 0.00 % - 23 - 18 - 0 - 18 - 0.00 + 46.67 % + 24 + 15 + 7 + 8 + 0.47 @@ -2183,8 +2183,8 @@

- 23 relevant lines. - 23 lines covered and + 36 relevant lines. + 36 lines covered and 0 lines missed.
@@ -2240,8 +2240,8 @@

-
  • - 8 +
  • + 12 @@ -2345,13 +2345,35 @@

    + attr_accessor :user +

  • +
    + +
    +
  • + + + + + + +
  • +
    + +
    +
  • + 1 + + + + def find_user_by_header
  • -
  • - 8 +
  • + 12 @@ -2361,8 +2383,8 @@

  • -
  • - 8 +
  • + 12 @@ -2372,8 +2394,8 @@

  • -
  • - 8 +
  • + 12 @@ -2383,8 +2405,8 @@

  • -
  • - 8 +
  • + 12 @@ -2394,8 +2416,8 @@

  • -
  • - 6 +
  • + 10 @@ -2405,7 +2427,7 @@

  • -
  • +
  • @@ -2416,7 +2438,7 @@

  • -
  • +
  • @@ -2427,7 +2449,7 @@

  • -
  • +
  • 1 @@ -2438,7 +2460,7 @@

  • -
  • +
  • 3 @@ -2449,7 +2471,7 @@

  • -
  • +
  • @@ -2460,7 +2482,7 @@

  • -
  • +
  • @@ -2471,7 +2493,7 @@

  • -
  • +
  • 1 @@ -2482,7 +2504,7 @@

  • -
  • +
  • 5 @@ -2493,7 +2515,7 @@

  • -
  • +
  • @@ -2504,7 +2526,7 @@

  • -
  • +
  • @@ -2515,7 +2537,7 @@

  • -
  • +
  • 1 @@ -2526,7 +2548,7 @@

  • -
  • +
  • 1 @@ -2537,7 +2559,7 @@

  • -
  • +
  • @@ -2548,7 +2570,7 @@

  • -
  • +
  • @@ -2559,7 +2581,7 @@

  • -
  • +
  • 1 @@ -2570,7 +2592,7 @@

  • -
  • +
  • 1 @@ -2581,7 +2603,7 @@

  • -
  • +
  • @@ -2592,7 +2614,7 @@

  • -
  • +
  • @@ -2603,7 +2625,7 @@

  • -
  • +
  • 1 @@ -2614,7 +2636,7 @@

  • -
  • +
  • 3 @@ -2625,7 +2647,7 @@

  • -
  • +
  • @@ -2636,60 +2658,95 @@

  • -
  • +
  • - end +
  • - - - +
    +
  • + 1 + + + + def current_block +
  • +
    -
    -
    -

    app/controllers/api/v1/block_confirmations_controller.rb

    -

    - - 100.0% - +
    +
  • + 6 + - lines covered -
  • + - + @current_block = Block.find_by(master_hash: nil) + +
    + +
    +
  • + + -
    - 25 relevant lines. - 25 lines covered and - 0 lines missed. -
    + - + end +
  • +
    + +
    +
  • + + -
  • + -
    -    
      + + +
    -
  • +
  • + 1 + + + + def current_pool +
  • +
    + +
    +
  • + 1 - # frozen_string_literal: true + @current_pool = Pool.find_by(block_id: current_block.id)
  • -
  • +
  • + + + + + + end +
  • +
    + +
    +
  • @@ -2700,106 +2757,1103 @@

  • -
  • +
  • 1 - class Api::V1::BlockConfirmationsController < Api::V1::ApplicationController + def ticket
  • -
  • - 1 +
  • + 6 - before_action :unauthorized? + @ticket = Ticket.find_by(user_id: user.id, status: :active)
  • -
  • - 1 +
  • + - before_action :ticket_founded?, only: [:confirm_block] + end
  • -
  • +
  • + + + + + + +
  • +
    + +
    +
  • 1 - before_action :confirmation_hash_founded?, only: [:confirm_block] + def block_transactions
  • -
  • +
  • + 5 + + @block_transactions = Transaction.where(block_id: current_block.id) +
  • +
    + +
    +
  • + - + + + end +
  • +
    + +
    +
  • + + + + + + +
  • +
    + +
    +
  • + 1 + + + + + def block_transactions_empty? +
  • +
    + +
    +
  • + 4 + + + + + return no_transactions_response unless block_transactions.present? +
  • +
    + +
    +
  • + + + + + + end +
  • +
    + +
    +
  • + + + + + + +
  • +
    + +
    +
  • + 1 + + + + + def no_transactions_response +
  • +
    + +
    +
  • + 1 + + + + + render json: { error: "No transactions in the block" }, status: :not_found +
  • +
    + +
    +
  • + + + + + + end +
  • +
    + +
    +
  • + + + + + + end +
  • +
    + + + + + + +
    +
    +

    app/controllers/api/v1/block_confirmations_controller.rb

    +

    + + 100.0% + + + lines covered +

    + + + +
    + 25 relevant lines. + 25 lines covered and + 0 lines missed. +
    + + + +
    + +
    +    
      + +
      +
    1. + + + + + + # frozen_string_literal: true +
    2. +
      + +
      +
    3. + + + + + + +
    4. +
      + +
      +
    5. + 1 + + + + + class Api::V1::BlockConfirmationsController < Api::V1::ApplicationController +
    6. +
      + +
      +
    7. + 1 + + + + + before_action :unauthorized? +
    8. +
      + +
      +
    9. + 1 + + + + + before_action :ticket_founded?, only: [:confirm_block] +
    10. +
      + +
      +
    11. + 1 + + + + + before_action :confirmation_hash_founded?, only: [:confirm_block] +
    12. +
      + +
      +
    13. + + + + + + +
    14. +
      + +
      +
    15. + 1 + + + + + def confirm_block +
    16. +
      + +
      +
    17. + 2 + + + + + return not_valid_confirmation_hash_response unless hash_confirmed? +
    18. +
      + +
      +
    19. + 1 + + + + + assign_contract +
    20. +
      + +
      +
    21. + + + + + + end +
    22. +
      + +
      +
    23. + + + + + + +
    24. +
      + +
      +
    25. + 1 + + + + + def info_to_mine +
    26. +
      + +
      +
    27. + 1 + + + + + render json: { words: words, number_sequences: number_sequences, symbol_sequences: symbol_sequences }, status: :ok +
    28. +
      + +
      +
    29. + + + + + + end +
    30. +
      + +
      +
    31. + + + + + + +
    32. +
      + +
      +
    33. + 1 + + + + + private +
    34. +
      + +
      +
    35. + 1 + + + + + def hash_confirmed? +
    36. +
      + +
      +
    37. + 2 + + + + + @ticket.user_acceptable_hash == confirmation_hash +
    38. +
      + +
      +
    39. + + + + + + end +
    40. +
      + +
      +
    41. + + + + + + +
    42. +
      + +
      +
    43. + 1 + + + + + def assign_contract +
    44. +
      + +
      +
    45. + 1 + + + + + AssignContractWorker.perform_async(@ticket.id) +
    46. +
      + +
      +
    47. + 1 + + + + + render json: { message: "Contract assigned" }, status: :ok +
    48. +
      + +
      +
    49. + + + + + + end +
    50. +
      + +
      +
    51. + + + + + + +
    52. +
      + +
      +
    53. + 1 + + + + + def not_valid_confirmation_hash_response +
    54. +
      + +
      +
    55. + 1 + + + + + render json: { error: "Not valid confirmation hash" }, status: :not_found +
    56. +
      + +
      +
    57. + + + + + + end +
    58. +
      + +
      +
    59. + + + + + + +
    60. +
      + +
      +
    61. + 1 + + + + + def words +
    62. +
      + +
      +
    63. + 1 + + + + + @words = @user.acceptable_words +
    64. +
      + +
      +
    65. + + + + + + end +
    66. +
      + +
      +
    67. + + + + + + +
    68. +
      + +
      +
    69. + 1 + + + + + def number_sequences +
    70. +
      + +
      +
    71. + 1 + + + + + @number_sequences = @user.acceptable_number_sequences +
    72. +
      + +
      +
    73. + + + + + + end +
    74. +
      + +
      +
    75. + + + + + + +
    76. +
      + +
      +
    77. + 1 + + + + + def symbol_sequences +
    78. +
      + +
      +
    79. + 1 + + + + + @symbol_sequences = @user.acceptable_symbol_sequences +
    80. +
      + +
      +
    81. + + + + + + end +
    82. +
      + +
      +
    83. + + + + + + +
    84. +
      + +
      +
    85. + 1 + + + + + def block_confirmation_params +
    86. +
      + +
      +
    87. + 3 + + + + + params.permit(:user_confirmation_hash) +
    88. +
      + +
      +
    89. + + + + + + end +
    90. +
      + +
      +
    91. + + + + + + end +
    92. +
      + +
    +
    +
    + + +
    +
    +

    app/controllers/api/v1/ticket_manager_controller.rb

    +

    + + 97.14% + + + lines covered +

    + + + +
    + 35 relevant lines. + 34 lines covered and + 1 lines missed. +
    + + + +
    + +
    +    
      + +
      +
    1. + + + + + + # frozen_string_literal: true +
    2. +
      + +
      +
    3. + + + + + + +
    4. +
      + +
      +
    5. + 1 + + + + + class Api::V1::TicketManagerController < Api::V1::ApplicationController +
    6. +
      + +
      +
    7. + 1 + + + + + before_action :unauthorized? +
    8. +
      + +
      +
    9. + 1 + + + + + before_action :block_transactions_empty?, only: [:open_ticket] +
    10. +
      + +
      +
    11. + + + + + + +
    12. +
      + +
      +
    13. + 1 + + + + + def open_ticket +
    14. +
      + +
      +
    15. + 3 + + + + + return ticket_already_opened_response unless ticket_nil? +
    16. +
      + +
      +
    17. + 2 + + + + + return ticket_already_opened_response if ticket_active? +
    18. +
      + +
      +
    19. + 2 + + + + + return terms_not_confirmed_response unless ticket_params_confirmed? +
    20. +
      + +
      +
    21. + 1 + + + + + create_ticket if acceptable_create? +
    22. +
      + +
      +
    23. + 1 + + + + + ticket_opened_response +
    24. +
      + +
      +
    25. + + + + + + end +
    26. +
      + +
      +
    27. + + + + + + +
    28. +
      + +
      +
    29. + + + + + + +
    30. +
      + +
      +
    31. + 1 + + + + + private +
    32. +
      + +
      +
    33. + 1 + + + + + def ticket_nil? +
    34. +
      + +
      +
    35. + 6 + + + + + ticket.nil? +
    36. +
      + +
      +
    37. + + + + + + end +
    38. +
      + +
      +
    39. + + + + + + +
    40. +
      + +
      +
    41. + 1 + + + + + def ticket_active? +
    42. +
      + +
      +
    43. + 2 + + + + + return false if ticket_nil? +
    44. +
      + +
      +
    45. + + + + + + ticket.status == "active" +
    46. +
      + +
      +
    47. + + + + + + end +
    48. +
      + +
      +
    49. + + + + + + +
    50. +
      + +
      +
    51. + 1 + + + + + def acceptable_create? +
    52. +
      + +
      +
    53. + 1 + + + + + ticket_nil? && ticket_params_confirmed? || ticket_params_confirmed? && ticket_active? == false +
    54. +
      + +
      +
    55. + + + + + + end
    56. -
    57. - 1 +
    58. + - def confirm_block +
    59. -
    60. - 2 +
    61. + 1 - return not_valid_confirmation_hash_response unless hash_confirmed? + def ticket_params_confirmed?
    62. -
    63. - 1 +
    64. + 3 - assign_contract + open_ticket_params[:ticket_terms] == "confirmed"
    65. -
    66. +
    67. - end + end
    68. -
    69. +
    70. @@ -2810,40 +3864,40 @@

    71. -
    72. +
    73. 1 - def info_to_mine + def ticket_already_opened_response
    74. -
    75. +
    76. 1 - render json: { words: words, number_sequences: number_sequences, symbol_sequences: symbol_sequences }, status: :ok + render json: { error: "Ticket already opened" }, status: :not_found
    77. -
    78. +
    79. - end + end
    80. -
    81. +
    82. @@ -2854,40 +3908,29 @@

    83. -
    84. +
    85. 1 - private + def ticket_opened_response
    86. -
    87. +
    88. 1 - def hash_confirmed? -
    89. -
      - -
      -
    90. - 2 - - - - - @ticket.user_acceptable_hash == confirmation_hash + render json: { message: "Ticket opened" }, status: :ok
    91. -
    92. +
    93. @@ -2898,7 +3941,7 @@

    94. -
    95. +
    96. @@ -2909,40 +3952,29 @@

    97. -
    98. - 1 - - - - - def assign_contract -
    99. -
      - -
      -
    100. +
    101. 1 - AssignContractWorker.perform_async(@ticket.id) + def terms_not_confirmed_response
    102. -
    103. +
    104. 1 - render json: { message: "Contract assigned" }, status: :ok + render json: { error: "Ticket terms not confirmed" }, status: :not_found
    105. -
    106. +
    107. @@ -2953,7 +3985,7 @@

    108. -
    109. +
    110. @@ -2964,29 +3996,29 @@

    111. -
    112. +
    113. 1 - def not_valid_confirmation_hash_response + def open_ticket_params
    114. -
    115. - 1 +
    116. + 3 - render json: { error: "Not valid confirmation hash" }, status: :not_found + params.permit(:ticket_terms)
    117. -
    118. +
    119. @@ -2997,7 +4029,7 @@

    120. -
    121. +
    122. @@ -3008,29 +4040,29 @@

    123. -
    124. +
    125. 1 - def words + def time_ref
    126. -
    127. +
    128. 1 - @words = @user.acceptable_words + @time_ref = block_transactions.first.created_at.to_s
    129. -
    130. +
    131. @@ -3041,7 +4073,7 @@

    132. -
    133. +
    134. @@ -3052,29 +4084,29 @@

    135. -
    136. +
    137. 1 - def number_sequences + def user_id
    138. -
    139. +
    140. 1 - @number_sequences = @user.acceptable_number_sequences + @user_id = user.id.to_s
    141. -
    142. +
    143. @@ -3085,7 +4117,7 @@

    144. -
    145. +
    146. @@ -3096,29 +4128,29 @@

    147. -
    148. +
    149. 1 - def symbol_sequences + def current_pool_id
    150. -
    151. +
    152. 1 - @symbol_sequences = @user.acceptable_symbol_sequences + @current_pool_id = current_pool.id.to_s
    153. -
    154. +
    155. @@ -3129,7 +4161,7 @@

    156. -
    157. +
    158. @@ -3140,29 +4172,29 @@

    159. -
    160. +
    161. 1 - def block_confirmation_params + def create_ticket
    162. -
    163. - 3 +
    164. + 1 - params.permit(:user_confirmation_hash) + CreateTicketWorker.perform_async(user_id, current_pool_id, time_ref)
    165. -
    166. +
    167. @@ -3173,7 +4205,7 @@

    168. -
    169. +
    170. @@ -3188,48 +4220,6 @@

    171. -
      -
      -

      app/controllers/api/v1/ticket_manager_controller.rb

      -

      - - 100.0% - - - lines covered -

      - - - -
      - 0 relevant lines. - 0 lines covered and - 0 lines missed. -
      - - - -
      - -
      -    
        - -
        -
      1. - - - - - - # frozen string_literal: true -
      2. -
        - -
      -
      -
      - -

      app/controllers/application_controller.rb

      @@ -11031,8 +12021,8 @@

      -
    172. - 68 +
    173. + 72 @@ -11641,8 +12631,8 @@

    174. -
    175. - 52 +
    176. + 67 @@ -11685,8 +12675,8 @@

    177. -
    178. - 52 +
    179. + 67 @@ -12522,8 +13512,8 @@

    180. -
    181. - 42 +
    182. + 57 @@ -12806,8 +13796,8 @@

    183. -
    184. - 71 +
    185. + 83 @@ -12850,8 +13840,8 @@

    186. -
    187. - 71 +
    188. + 83 @@ -12861,8 +13851,8 @@

    189. -
    190. - 71 +
    191. + 83 @@ -12872,8 +13862,8 @@

    192. -
    193. - 71 +
    194. + 83 @@ -12894,8 +13884,8 @@

    195. -
    196. - 71 +
    197. + 83 @@ -12916,8 +13906,8 @@

    198. -
    199. - 71 +
    200. + 83 @@ -12938,8 +13928,8 @@

    201. -
    202. - 71 +
    203. + 83 @@ -12960,8 +13950,8 @@

    204. -
    205. - 71 +
    206. + 83 @@ -12971,8 +13961,8 @@

    207. -
    208. - 71 +
    209. + 83 @@ -12982,8 +13972,8 @@

    210. -
    211. - 71 +
    212. + 83 @@ -13026,8 +14016,8 @@

    213. -
    214. - 71 +
    215. + 83 @@ -13037,8 +14027,8 @@

    216. -
    217. - 71 +
    218. + 83 @@ -13288,8 +14278,8 @@

    219. -
    220. - 59 +
    221. + 71 @@ -13299,8 +14289,8 @@

    222. -
    223. - 57 +
    224. + 69 @@ -13310,8 +14300,8 @@

    225. -
    226. - 57 +
    227. + 69 @@ -13343,8 +14333,8 @@

    228. -
    229. - 59 +
    230. + 71 @@ -13354,8 +14344,8 @@

    231. -
    232. - 59 +
    233. + 71 @@ -13376,8 +14366,8 @@

    234. -
    235. - 59 +
    236. + 71 @@ -13409,8 +14399,8 @@

    237. -
    238. - 58 +
    239. + 70 @@ -13420,8 +14410,8 @@

    240. -
    241. - 58 +
    242. + 70 @@ -14285,8 +15275,8 @@

    243. -
    244. - 12 +
    245. + 10 @@ -14615,8 +15605,8 @@

    246. -
    247. - 87 +
    248. + 85 @@ -18783,7 +19773,7 @@

      app/workers/create_ticket_worker.rb

      - 0.0% + 46.67% lines covered @@ -18792,9 +19782,9 @@

      - 18 relevant lines. - 0 lines covered and - 18 lines missed. + 15 relevant lines. + 7 lines covered and + 8 lines missed.
      @@ -18827,8 +19817,8 @@

    249. -
    250. - +
    251. + 1 @@ -18838,8 +19828,19 @@

    252. -
    253. +
    254. + 1 + + + + include Sidekiq::Worker +
    255. +
      + +
      +
    256. + 1 @@ -18849,7 +19850,7 @@

    257. -
    258. +
    259. @@ -18860,8 +19861,8 @@

    260. -
    261. - +
    262. + 1 @@ -18871,7 +19872,7 @@

    263. -
    264. +
    265. @@ -18882,7 +19883,7 @@

    266. -
    267. +
    268. @@ -18893,7 +19894,7 @@

    269. -
    270. +
    271. @@ -18904,7 +19905,7 @@

    272. -
    273. +
    274. @@ -18915,7 +19916,7 @@

    275. -
    276. +
    277. @@ -18926,7 +19927,7 @@

    278. -
    279. +
    280. @@ -18937,7 +19938,7 @@

    281. -
    282. +
    283. @@ -18948,7 +19949,7 @@

    284. -
    285. +
    286. @@ -18959,7 +19960,7 @@

    287. -
    288. +
    289. @@ -18970,7 +19971,7 @@

    290. -
    291. +
    292. @@ -18981,8 +19982,8 @@

    293. -
    294. - +
    295. + 1 @@ -18992,8 +19993,8 @@

    296. -
    297. - +
    298. + 1 @@ -19003,7 +20004,7 @@

    299. -
    300. +
    301. @@ -19014,8 +20015,8 @@

    302. -
    303. - +
    304. + 1 @@ -19025,7 +20026,7 @@

    305. -
    306. +
    307. @@ -19036,7 +20037,7 @@

    308. -
    309. +
    310. @@ -19047,7 +20048,7 @@

    311. -
    312. +
    313. diff --git a/osbc.gemspec b/osbc.gemspec index afedcc7..f9c445c 100644 --- a/osbc.gemspec +++ b/osbc.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = "https://github.com/JesusGautamah/outerspace-blockchain" - spec.metadata["changelog_uri"] = "https://github.com/outerspace-coding/outerspace-blockchain/blob/main/CHANGELOG.md" + spec.metadata["changelog_uri"] = "https://github.com/JesusGautamah/outerspace-blockchain/blob/main/CHANGELOG.md" spec.files = Dir.chdir(File.expand_path(__dir__)) do `git ls-files -z`.split("\x0").reject do |f| (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)}) diff --git a/spec/requests/api/v1/ticket_manager_controller_spec.rb b/spec/requests/api/v1/ticket_manager_controller_spec.rb new file mode 100644 index 0000000..fe03e7e --- /dev/null +++ b/spec/requests/api/v1/ticket_manager_controller_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe Api::V1::TicketManagerController, type: :request do + let(:chain) { create(:chain) } + let(:block) { create(:block, chain: chain) } + let(:sender_user) { create(:user) } + let(:receiver_user) { create(:user, email: "receiver@mail.com", username: "receiver") } + let(:miner_user) { create(:user, email: "miner@email.com", username: "miner") } + let(:miner_wallet) { create(:wallet, user: miner_user) } + let(:sender_wallet) { create(:wallet, user: sender_user, pr_key: "123454321", pv_key: "123454321") } + let(:receiver_wallet) { create(:wallet, user: receiver_user, pr_key: "123454321", pv_key: "123454321") } + + describe "POST /api/v1/open_ticket" do + before(:each) do + chain + block + miner_wallet.update(balance: 1000) + sender_wallet.update(balance: 1000) + receiver_wallet.update(balance: 1000) + end + + context "when the request is valid" do + it "creates a ticket" do + 5.times do + create(:transaction, sender_key: sender_wallet.pv_key, receiver_key: receiver_wallet.pr_key, amount: 100, block: block) + end + create(:ticket, user: miner_user, pool: block.pool, status: "inactive", user_acceptable_hash: "a" * 64) + headers = { "X-API-KEY": miner_user.api_key, "X-API-SECRET": miner_user.api_secret } + post "/api/v1/open_ticket", params: { ticket_terms: "confirmed" }, headers: headers + expect(response.body).to eq({ message: "Ticket opened" }.to_json) + expect(response).to have_http_status(200) + end + end + + context "when the ticket active already" do + it "returns a ticket already opened response" do + 5.times do + create(:transaction, sender_key: sender_wallet.pv_key, receiver_key: receiver_wallet.pr_key, amount: 100, block: block) + end + create(:ticket, user: miner_user, pool: block.pool, status: "active", user_acceptable_hash: "a" * 64) + headers = { "X-API-KEY": miner_user.api_key, "X-API-SECRET": miner_user.api_secret } + post "/api/v1/open_ticket", params: { ticket_terms: "confirmed" }, headers: headers + expect(response.body).to eq({ error: "Ticket already opened" }.to_json) + expect(response).to have_http_status(404) + end + end + + context "when the request is invalid" do + it "returns status code 422" do + 5.times do + create(:transaction, sender_key: sender_wallet.pv_key, receiver_key: receiver_wallet.pr_key, amount: 100, block: block) + end + headers = { "X-API-KEY": miner_user.api_key, "X-API-SECRET": miner_user.api_secret } + post "/api/v1/open_ticket", params: { ticket_terms: "not confirmed" }, headers: headers + expect(response.body).to eq({ error: "Ticket terms not confirmed" }.to_json) + expect(response).to have_http_status(404) + end + end + + context "when has no transactions in the block" do + it "returns status code 422" do + headers = { "X-API-KEY": miner_user.api_key, "X-API-SECRET": miner_user.api_secret } + post "/api/v1/open_ticket", params: { ticket_terms: "confirmed" }, headers: headers + expect(response.body).to eq({ error: "No transactions in the block" }.to_json) + expect(response).to have_http_status(404) + end + end + end +end