From 3f7c03437db9a6edd163439b0a9e00a35bca0388 Mon Sep 17 00:00:00 2001 From: JesusGautamah Date: Sat, 28 Jan 2023 20:20:28 +0000 Subject: [PATCH] correct tests, 98.87% ccoverage --- .env_sample | 3 +- .gitignore | 5 +- .../api/v1/application_controller.rb | 1 + app/controllers/chains_controller.rb | 3 +- app/controllers/signatures_controller.rb | 19 +-- app/controllers/tickets_controller.rb | 10 ++ app/models/concerns/sequences_validator.rb | 30 ----- app/models/contract.rb | 4 + app/services/application_service.rb | 3 + app/services/create_ticket_service.rb | 7 -- app/workers/application_worker.rb | 2 + config/routes.rb | 2 - docker-compose.yml | 4 +- ...ncept.ipynb => mining_concept_sample.ipynb | 0 .../application_controller_spec.rb | 11 ++ spec/controllers/pools_controller_spec.rb | 25 ++++ spec/controllers/tickets_controller_spec.rb | 112 ++++++++++++++++++ spec/rails_helper.rb | 65 +--------- .../api/v1/ticket_manager_controller_spec.rb | 6 + spec/requests/signatures_spec.rb | 42 ------- spec/routing/chains_routing_spec.rb | 34 ------ spec/routing/signatures_routing_spec.rb | 34 ------ spec/services/application_service_spec.rb | 6 + spec/services/create_ticket_service_spec.rb | 25 ++++ spec/spec_helper.rb | 83 ------------- spec/support/database_cleaner.rb | 17 +++ spec/support/shoulda_matchers.rb | 8 ++ spec/views/blocks/index.html.slim_spec.rb | 3 +- spec/views/chains/edit.html.slim_spec.rb | 35 ------ spec/views/chains/index.html.slim_spec.rb | 39 ------ spec/views/chains/new.html.slim_spec.rb | 31 ----- spec/views/chains/show.html.slim_spec.rb | 24 ---- spec/views/signatures/edit.html.slim_spec.rb | 29 ----- spec/views/signatures/index.html.slim_spec.rb | 42 ------- spec/views/signatures/new.html.slim_spec.rb | 25 ---- spec/views/signatures/show.html.slim_spec.rb | 35 ------ spec/workers/application_worker_spec.rb | 6 + spec/workers/assign_contract_worker_spec.rb | 48 ++++++++ spec/workers/create_ticket_worker_spec.rb | 32 +++++ 39 files changed, 330 insertions(+), 580 deletions(-) delete mode 100644 app/models/concerns/sequences_validator.rb rename mining_concept.ipynb => mining_concept_sample.ipynb (100%) create mode 100644 spec/controllers/pools_controller_spec.rb create mode 100644 spec/controllers/tickets_controller_spec.rb create mode 100644 spec/services/create_ticket_service_spec.rb create mode 100644 spec/support/database_cleaner.rb create mode 100644 spec/support/shoulda_matchers.rb delete mode 100644 spec/views/chains/edit.html.slim_spec.rb delete mode 100755 spec/views/chains/index.html.slim_spec.rb delete mode 100644 spec/views/chains/new.html.slim_spec.rb delete mode 100644 spec/views/chains/show.html.slim_spec.rb delete mode 100644 spec/views/signatures/edit.html.slim_spec.rb delete mode 100644 spec/views/signatures/index.html.slim_spec.rb delete mode 100644 spec/views/signatures/new.html.slim_spec.rb delete mode 100644 spec/views/signatures/show.html.slim_spec.rb create mode 100644 spec/workers/assign_contract_worker_spec.rb create mode 100644 spec/workers/create_ticket_worker_spec.rb diff --git a/.env_sample b/.env_sample index a62177b..9ee5e48 100644 --- a/.env_sample +++ b/.env_sample @@ -11,4 +11,5 @@ GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX FIRESTORE_PROJECT_ID=YOUR_FIRESTORE_PROJECT_ID FIRESTORE_KEY_FILE=YOUR_FIRESTORE_KEY_FILE FIRESTORE_KEY_FILE_PATH=YOUR_FIRESTORE_KEY_FILE_PATH - +POSTGRES_USER=YOUR_POSTGRES_USER +POSTGRES_PASSWORD=YOUR_POSTGRES_PASSWORD \ No newline at end of file diff --git a/.gitignore b/.gitignore index ff004ea..130278d 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,7 @@ osbc-*.gem .env /.env -/.env.* \ No newline at end of file +/.env.* +/mining_concept.ipynb +/mining_concept.ipynb_checkpoints +/hyperminer.py \ No newline at end of file diff --git a/app/controllers/api/v1/application_controller.rb b/app/controllers/api/v1/application_controller.rb index 72ba831..3f2925a 100644 --- a/app/controllers/api/v1/application_controller.rb +++ b/app/controllers/api/v1/application_controller.rb @@ -50,6 +50,7 @@ def current_pool end def ticket + return nil unless user.present? @ticket = Ticket.find_by(user_id: user.id, status: :active) end diff --git a/app/controllers/chains_controller.rb b/app/controllers/chains_controller.rb index 0b513b6..7a58415 100644 --- a/app/controllers/chains_controller.rb +++ b/app/controllers/chains_controller.rb @@ -1,4 +1,3 @@ # frozen_string_literal: true -class ChainsController < ApplicationController -end +class ChainsController < ApplicationController; end diff --git a/app/controllers/signatures_controller.rb b/app/controllers/signatures_controller.rb index 32f4d18..bc0a143 100644 --- a/app/controllers/signatures_controller.rb +++ b/app/controllers/signatures_controller.rb @@ -1,20 +1,3 @@ # frozen_string_literal: true -class SignaturesController < ApplicationController - before_action :set_signature, only: %i[ show edit update destroy ] - - # GET /signatures or /signatures.json - def index - @signatures = Signature.all - end - - # GET /signatures/1 or /signatures/1.json - def show - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_signature - @signature = Signature.find(params[:id]) - end -end +class SignaturesController < ApplicationController; end diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index b88dff0..9fea06b 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -21,11 +21,21 @@ def new # POST /tickets or /tickets.json def create return no_transactions_response if block_transactions.empty? + return you_already_have_a_ticket_in_this_pool if user_ticket_on_this_pool? create_ticket redirect_to tickets_path, notice: "Processing ticket, please wait, wait a minute and refresh the page" end private + + def user_ticket_on_this_pool? + current_user.tickets.where(status: 'active').any? + end + + def you_already_have_a_ticket_in_this_pool + redirect_to root_path, notice: "You already have a ticket in this pool" + end + def create_ticket CreateTicketWorker.perform_async(current_user.id, current_pool.id, time_ref) end diff --git a/app/models/concerns/sequences_validator.rb b/app/models/concerns/sequences_validator.rb deleted file mode 100644 index 77d066b..0000000 --- a/app/models/concerns/sequences_validator.rb +++ /dev/null @@ -1,30 +0,0 @@ -# frozen_string_literal: true - -class SequencesValidator < ActiveModel::Validator - def validate(record) - @record = record - @word = record.common_word - @symbol_seq = record.symbol_sequence - @number_seq = record.number_sequence - record.errors.add(:base, "Invalid Hashable Content") unless valid_checker - end - - private - attr_reader :record, :word, :symbol_seq, :number_seq - - def valid_checker - acceptable_word? && acceptable_symbol_sequence? && acceptable_number_sequence? - end - - def acceptable_word? - AcceptableWord.find_by(word: word).present? - end - - def acceptable_symbol_sequence? - AcceptableSymbolSequence.find_by(seq: symbol_seq).present? - end - - def acceptable_number_sequence? - AcceptableNumberSequence.find_by(seq: number_seq).present? - end -end diff --git a/app/models/contract.rb b/app/models/contract.rb index 6d0f2f6..22ac96b 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -4,4 +4,8 @@ class Contract < ApplicationRecord validates :blk_transaction, presence: true belongs_to :blk_transaction, class_name: "Transaction", foreign_key: "transaction_id", inverse_of: :contract has_many :signatures, inverse_of: :contract, dependent: :destroy + + def confirmed + signatures_count == ENV["SIGNATURES_LIMIT"].to_i + end end diff --git a/app/services/application_service.rb b/app/services/application_service.rb index e197579..71f24db 100644 --- a/app/services/application_service.rb +++ b/app/services/application_service.rb @@ -2,6 +2,8 @@ class ApplicationService private + attr_reader :user_id, :wallet_id, :wallet_key, :wallet_method + def dev_error(e) if env_acceptable? puts "#{e.class}: #{e.message}" @@ -10,6 +12,7 @@ def dev_error(e) end def user_exists? + return false unless user_id User.find_by(id: user_id).present? end diff --git a/app/services/create_ticket_service.rb b/app/services/create_ticket_service.rb index f9aa075..a85f899 100644 --- a/app/services/create_ticket_service.rb +++ b/app/services/create_ticket_service.rb @@ -50,21 +50,14 @@ def generate_transaction_id_list end def shuffled_string - puts "Word: #{word}" - puts "Number sequence: #{number_sequence}" - puts "Symbol sequence: #{symbol_sequence}" string_sum = (word + number_sequence + symbol_sequence).split("").shuffle.join - puts "Shuffled string: #{string_sum}" string_sum end def generate_user_acceptable_hash hash = Digest::SHA256.hexdigest(shuffled_string) - puts "Hash: #{hash}" block_hash = Digest::SHA256.hexdigest(@transactions.to_json) - puts "Block hash: #{block_hash}" master_hash = Digest::SHA256.hexdigest(hash + block_hash) - puts "Master hash: #{master_hash}" [hash, block_hash, master_hash] end diff --git a/app/workers/application_worker.rb b/app/workers/application_worker.rb index abbcb3f..39122b6 100644 --- a/app/workers/application_worker.rb +++ b/app/workers/application_worker.rb @@ -64,6 +64,8 @@ def user_exists? def pool_exists? Pool.find_by(id: pool_id).present? + rescue NameError + false end def block_exists? diff --git a/config/routes.rb b/config/routes.rb index 3468577..8758d15 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,14 +4,12 @@ Rails.application.routes.draw do mount Sidekiq::Web => "/sidekiq" if Rails.env.development? - resources :signatures resources :tickets resources :pools resources :contracts resources :transactions resources :wallets resources :blocks, only: %i[index show] - resources :chains devise_for :users, controllers: { registrations: "users/registrations", sessions: "users/sessions" } devise_scope :user do diff --git a/docker-compose.yml b/docker-compose.yml index 6ef68e1..909d3fb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,8 +14,8 @@ services: - "5432:5432" volumes: - ./tmp/db:/var/lib/postgresql/data - environment: - POSTGRES_PASSWORD: secret + env_file: + - .env web: build: . command: bash -c "rm -f tmp/pids/server.pid && foreman start -f Procfile.dev" diff --git a/mining_concept.ipynb b/mining_concept_sample.ipynb similarity index 100% rename from mining_concept.ipynb rename to mining_concept_sample.ipynb diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index d839779..bba1218 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -5,6 +5,7 @@ RSpec.describe ApplicationController, type: :controller do let(:chain) { create(:chain) } let(:block) { create(:block, chain: chain) } + let (:user) { create(:user) } describe "GET #home" do it "returns http success" do chain @@ -13,4 +14,14 @@ expect(response).to have_http_status(:success) end end + + describe "GET #mining_profile" do + it "returns http success" do + chain + block + sign_in user + get :mining_profile + expect(response).to have_http_status(:success) + end + end end diff --git a/spec/controllers/pools_controller_spec.rb b/spec/controllers/pools_controller_spec.rb new file mode 100644 index 0000000..1570d46 --- /dev/null +++ b/spec/controllers/pools_controller_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe PoolsController, type: :controller do + let(:chain) { create(:chain) } + let(:block) { create(:block, chain: chain) } + let(:pool) { create(:pool, block: block) } + let(:user) { create(:user) } + let(:user_two) { create(:user, email: 'lorem@ipsum.com', username: 'lorem', password: 'loremaludias', password_confirmation: 'loremaludias') } + let(:wallet) { create(:wallet, user: user) } + let(:wallet_two) { create(:wallet, user: user_two) } + + context "#index" do + it "returns http success" do + chain + block + sign_in user + pool = Pool.find(3) + get :index + expect(response).to have_http_status(:success) + expect(assigns(:pools)).to eq([pool]) + end + end +end \ No newline at end of file diff --git a/spec/controllers/tickets_controller_spec.rb b/spec/controllers/tickets_controller_spec.rb new file mode 100644 index 0000000..5b15637 --- /dev/null +++ b/spec/controllers/tickets_controller_spec.rb @@ -0,0 +1,112 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe TicketsController, type: :controller do + let(:chain) { create(:chain) } + let(:block) { create(:block, chain: chain) } + let(:pool) { create(:pool, block: block) } + let(:user) { create(:user) } + let(:user_two) { create(:user, email: 'lorem@ipsum.com', username: 'lorem', password: 'loremaludias', password_confirmation: 'loremaludias') } + let(:wallet) { create(:wallet, user: user) } + let(:wallet_two) { create(:wallet, user: user_two) } + let(:transaction) { create(:transaction, sender_key: wallet.pv_key, receiver_key: wallet_two.pr_key, amount: 100 , fee: 10, block: block) } + let(:transaction_two) { create(:transaction, sender_key: wallet.pv_key, receiver_key: wallet_two.pr_key, amount: 100 , fee: 10, block: block) } + let(:contract) { create(:contract, blk_transaction: transaction) } + let(:contract_two) { create(:contract, blk_transaction: transaction_two) } + let(:word) { create(:acceptable_word) } + let(:number_sequence) { create(:acceptable_number_sequence) } + let(:symbol_sequence) { create(:acceptable_symbol_sequence) } + let(:ticket) { create(:ticket, pool: pool, user: user, transaction_id_list: [transaction.id, transaction_two.id]) } + + let(:ticket) { create(:ticket, user: user, pool: pool) } + describe "GET #index" do + it "returns http success" do + chain + block + ticket + sign_in user + get :index + expect(response).to have_http_status(:success) + end + end + + describe "GET #show" do + it "returns http success" do + chain + block + ticket + sign_in user + get :show, params: { id: ticket.id } + expect(response).to have_http_status(:success) + end + end + + describe "GET #new" do + it "returns http success" do + chain + block + ticket + sign_in user + get :new + expect(response).to have_http_status(:success) + end + end + + describe "GET #edit" do + it "returns http success" do + chain + block + ticket + sign_in user + get :edit, params: { id: ticket.id } + expect(response).to have_http_status(:success) + end + end + + describe "POST #create" do + it "returns http success but not create ticket" do + chain + block + Ticket.create(user_id: user.id, pool_id: pool.id, status: 'active', time_ref: Time.now, transaction_id_list: [transaction.id, transaction_two.id]) + sign_in user + transaction + transaction_two + contract + contract_two + post :create, params: { ticket: { block_id: block.id, user_id: user.id, pool_id: pool.id } } + #should redirect to tickets path with notice "Processing ticket, please wait, wait a minute and refresh the page" + + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(root_path) + expect(flash[:notice]).to eq("You already have a ticket in this pool") + # expect Tickets count + expect(Ticket.count).to eq(1) + end + + it "redirects to root path with notice no transactions yet" do + chain + block + sign_in user + post :create, params: { ticket: {user_id: user.id, pool_id: pool.id, status: 'pending', time_ref: Time.now, transaction_id_list: [] } } + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(root_path) + expect(flash[:notice]).to eq("No transactions yet") + end + + + it "returns http success and create ticket" do + chain + block + transaction + transaction_two + contract + contract_two + sign_in user + post :create, params: { ticket: { user_id: user.id, pool_id: pool.id, status: 'pending', time_ref: Time.now, transaction_id_list: [transaction.id, transaction_two.id] } } + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(tickets_path) + expect(flash[:notice]).to eq("Processing ticket, please wait, wait a minute and refresh the page") + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index fb59a6a..0c4320a 100755 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -# This file is copied to spec/ when you run "rails generate rspec:install" require "simplecov" SimpleCov.start "rails" do add_group "Services", "app/services" @@ -16,84 +15,24 @@ require "spec_helper" ENV["RAILS_ENV"] ||= "test" require_relative "../config/environment" -# Prevent database truncation if the environment is production + abort("The Rails environment is running in production mode!") if Rails.env.production? require "rspec/rails" -# Add additional requires below this line. Rails is not loaded until this point! -# Requires supporting ruby files with custom matchers and macros, etc, in -# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are -# run as spec files by default. This means that files in spec/support that end -# in _spec.rb will both be required and run as specs, causing the specs to be -# run twice. It is recommended that you do not name files matching this glob to -# end with _spec.rb. You can configure this pattern with the --pattern -# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. -# -# The following line is provided for convenience purposes. It has the downside -# of increasing the boot-up time by auto-requiring all files in the support -# directory. Alternatively, in the individual `*_spec.rb` files, manually -# require only the support files necessary. -# Dir[Rails.root.join("spec", "support", "**", "*.rb")].sort.each { |f| require f } -# Checks for pending migrations and applies them before tests are run. -# If you are not using ActiveRecord, you can remove these lines. begin ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e abort e.to_s.strip end -RSpec.configure do |config| - config.before(:suite) do - DatabaseCleaner.clean_with(:truncation) - end - - config.before(:each) do - DatabaseCleaner.strategy = :transaction - end - config.before(:each) do - DatabaseCleaner.start - end - - config.after(:each) do - DatabaseCleaner.clean - end - - Shoulda::Matchers.configure do |config| - config.integrate do |with| - with.test_framework :rspec - with.library :rails - end - end - # Remove this line if you"re not using ActiveRecord or ActiveRecord fixtures +RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" - # If you"re not using ActiveRecord, or you"d prefer not to run each of your - # examples within a transaction, remove the following line or assign false - # instead of true. config.use_transactional_fixtures = true - # You can uncomment this line to turn off ActiveRecord support entirely. - # config.use_active_record = false - - # RSpec Rails can automatically mix in different behaviours to your tests - # based on their file location, for example enabling you to call `get` and - # `post` in specs under `spec/controllers`. - # - # You can disable this behaviour by removing the line below, and instead - # explicitly tag your specs with their type, e.g.: - # - # RSpec.describe UsersController, type: :controller do - # # ... - # end - # - # The different available types are documented in the features, such as in - # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! - # Filter lines from Rails gems in backtraces. config.filter_rails_from_backtrace! - # arbitrary gems may also be filtered via: - # config.filter_gems_from_backtrace("gem name") end diff --git a/spec/requests/api/v1/ticket_manager_controller_spec.rb b/spec/requests/api/v1/ticket_manager_controller_spec.rb index fe03e7e..c11134d 100644 --- a/spec/requests/api/v1/ticket_manager_controller_spec.rb +++ b/spec/requests/api/v1/ticket_manager_controller_spec.rb @@ -67,5 +67,11 @@ expect(response).to have_http_status(404) end end + + context "ticket active check" do + it "receives ticket_active? method" do + expect(subject.send(:ticket_active?)).to eq(false) + end + end end end diff --git a/spec/requests/signatures_spec.rb b/spec/requests/signatures_spec.rb index 8321e78..ac3dfba 100644 --- a/spec/requests/signatures_spec.rb +++ b/spec/requests/signatures_spec.rb @@ -15,46 +15,4 @@ # sticking to rails and rspec-rails APIs to keep things simple and stable. RSpec.describe "/signatures", type: :request do - let(:chain) { create(:chain) } - let(:block) { create(:block, chain: chain) } - let(:pool) { create(:pool, block: block) } - let(:user) { create(:user) } - let(:user_2) { create(:user, email: "second@email.com", username: "second") } - let(:wallet) { create(:wallet, user: user) } - let(:wallet_2) { create(:wallet, user: user_2, pr_key: "second_pr_key", pv_key: "second_pv_key") } - let(:ticket) { create(:ticket, pool: pool, user: user) } - let(:transaction) { create(:transaction, block: block, sender_key: wallet.pr_key, receiver_key: wallet_2.pv_key) } - let(:contract) { create(:contract, blk_transaction: transaction) } - let(:acceptable_word) { create(:acceptable_word) } - let(:acceptable_symbol_sequence) { create(:acceptable_symbol_sequence) } - let(:acceptable_number_sequence) { create(:acceptable_number_sequence) } - let(:valid_attributes) { { signature: "a" * 64, contract_id: contract.id, user_id: user.id, ticket_id: ticket.id } } - - let(:invalid_attributes) { - { signature: "a" * 64, - contract_id: contract.id, - common_word: "0000", - symbol_sequence: "0000", - number_sequence: "invalid", - verify_sig: "Verify Sig", - block_hash: "Block Hash", - signature_hash: "Hashed", - time_ref: ticket.time_ref } - } - - describe "GET /index" do - it "renders a successful response" do - Signature.create! valid_attributes - get signatures_url - expect(response).to be_successful - end - end - - describe "GET /show" do - it "renders a successful response" do - signature = Signature.create! valid_attributes - get signature_url(signature) - expect(response).to be_successful - end - end end diff --git a/spec/routing/chains_routing_spec.rb b/spec/routing/chains_routing_spec.rb index 82ffdc1..486777d 100644 --- a/spec/routing/chains_routing_spec.rb +++ b/spec/routing/chains_routing_spec.rb @@ -3,38 +3,4 @@ require "rails_helper" RSpec.describe ChainsController, type: :routing do - describe "routing" do - it "routes to #index" do - expect(get: "/chains").to route_to("chains#index") - end - - it "routes to #new" do - expect(get: "/chains/new").to route_to("chains#new") - end - - it "routes to #show" do - expect(get: "/chains/1").to route_to("chains#show", id: "1") - end - - it "routes to #edit" do - expect(get: "/chains/1/edit").to route_to("chains#edit", id: "1") - end - - - it "routes to #create" do - expect(post: "/chains").to route_to("chains#create") - end - - it "routes to #update via PUT" do - expect(put: "/chains/1").to route_to("chains#update", id: "1") - end - - it "routes to #update via PATCH" do - expect(patch: "/chains/1").to route_to("chains#update", id: "1") - end - - it "routes to #destroy" do - expect(delete: "/chains/1").to route_to("chains#destroy", id: "1") - end - end end diff --git a/spec/routing/signatures_routing_spec.rb b/spec/routing/signatures_routing_spec.rb index 368709e..0f94380 100644 --- a/spec/routing/signatures_routing_spec.rb +++ b/spec/routing/signatures_routing_spec.rb @@ -3,38 +3,4 @@ require "rails_helper" RSpec.describe SignaturesController, type: :routing do - describe "routing" do - it "routes to #index" do - expect(get: "/signatures").to route_to("signatures#index") - end - - it "routes to #new" do - expect(get: "/signatures/new").to route_to("signatures#new") - end - - it "routes to #show" do - expect(get: "/signatures/1").to route_to("signatures#show", id: "1") - end - - it "routes to #edit" do - expect(get: "/signatures/1/edit").to route_to("signatures#edit", id: "1") - end - - - it "routes to #create" do - expect(post: "/signatures").to route_to("signatures#create") - end - - it "routes to #update via PUT" do - expect(put: "/signatures/1").to route_to("signatures#update", id: "1") - end - - it "routes to #update via PATCH" do - expect(patch: "/signatures/1").to route_to("signatures#update", id: "1") - end - - it "routes to #destroy" do - expect(delete: "/signatures/1").to route_to("signatures#destroy", id: "1") - end - end end diff --git a/spec/services/application_service_spec.rb b/spec/services/application_service_spec.rb index 7656afe..139de38 100644 --- a/spec/services/application_service_spec.rb +++ b/spec/services/application_service_spec.rb @@ -33,4 +33,10 @@ expect(service.send(:wallet_exists?, :user_id, 9000)).to be_falsey end end + + describe "#user_exists?" do + it "returns false if user does not exist" do + expect(service.send(:user_exists?)).to be_falsey + end + end end diff --git a/spec/services/create_ticket_service_spec.rb b/spec/services/create_ticket_service_spec.rb new file mode 100644 index 0000000..d3cea74 --- /dev/null +++ b/spec/services/create_ticket_service_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe CreateTicketService, type: :service do + let(:chain) { create(:chain) } + let(:block) { create(:block, chain: chain) } + let(:pool) { create(:pool, block: block) } + let(:user) { create(:user) } + let(:ticket) { create(:ticket, user: user, pool: pool) } + let(:ticket_params) { attributes_for(:ticket, user: user, pool: pool) } + let(:service) { CreateTicketService.new(user.id, pool.id, Time.now) } + let(:word) { create(:acceptable_word) } + let(:number_sequence) { create(:acceptable_number_sequence) } + let(:symbol_sequence) { create(:acceptable_symbol_sequence) } + + describe "#call" do + it "creates a ticket" do + word + number_sequence + symbol_sequence + expect { service.call }.to change(Ticket, :count).by(1) + end + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a0d4080..06e09a3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,94 +1,11 @@ -# This file was generated by the `rails generate rspec:install` command. Conventionally, all -# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. -# The generated `.rspec` file contains `--require spec_helper` which will cause -# this file to always be loaded, without a need to explicitly require it in any -# files. -# -# Given that it is always loaded, you are encouraged to keep this file as -# light-weight as possible. Requiring heavyweight dependencies from this file -# will add to the boot time of your test suite on EVERY test run, even for an -# individual file that may not need all of that loaded. Instead, consider making -# a separate helper file that requires the additional dependencies and performs -# the additional setup, and require it from the spec files that actually need -# it. -# -# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| - # rspec-expectations config goes here. You can use an alternate - # assertion/expectation library such as wrong or the stdlib/minitest - # assertions if you prefer. config.expect_with :rspec do |expectations| - # This option will default to `true` in RSpec 4. It makes the `description` - # and `failure_message` of custom matchers include text for helper methods - # defined using `chain`, e.g.: - # be_bigger_than(2).and_smaller_than(4).description - # # => "be bigger than 2 and smaller than 4" - # ...rather than: - # # => "be bigger than 2" expectations.include_chain_clauses_in_custom_matcher_descriptions = true end - # rspec-mocks config goes here. You can use an alternate test double - # library (such as bogus or mocha) by changing the `mock_with` option here. config.mock_with :rspec do |mocks| - # Prevents you from mocking or stubbing a method that does not exist on - # a real object. This is generally recommended, and will default to - # `true` in RSpec 4. mocks.verify_partial_doubles = true end - # This option will default to `:apply_to_host_groups` in RSpec 4 (and will - # have no way to turn it off -- the option exists only for backwards - # compatibility in RSpec 3). It causes shared context metadata to be - # inherited by the metadata hash of host groups and examples, rather than - # triggering implicit auto-inclusion in groups with matching metadata. config.shared_context_metadata_behavior = :apply_to_host_groups - -# The settings below are suggested to provide a good initial experience -# with RSpec, but feel free to customize to your heart's content. -=begin - # This allows you to limit a spec run to individual examples or groups - # you care about by tagging them with `:focus` metadata. When nothing - # is tagged with `:focus`, all examples get run. RSpec also provides - # aliases for `it`, `describe`, and `context` that include `:focus` - # metadata: `fit`, `fdescribe` and `fcontext`, respectively. - config.filter_run_when_matching :focus - - # Allows RSpec to persist some state between runs in order to support - # the `--only-failures` and `--next-failure` CLI options. We recommend - # you configure your source control system to ignore this file. - config.example_status_persistence_file_path = "spec/examples.txt" - - # Limits the available syntax to the non-monkey patched syntax that is - # recommended. For more details, see: - # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode - config.disable_monkey_patching! - - # Many RSpec users commonly either run the entire suite or an individual - # file, and it's useful to allow more verbose output when running an - # individual spec file. - if config.files_to_run.one? - # Use the documentation formatter for detailed output, - # unless a formatter has already been configured - # (e.g. via a command-line flag). - config.default_formatter = "doc" - end - - # Print the 10 slowest examples and example groups at the - # end of the spec run, to help surface which specs are running - # particularly slow. - config.profile_examples = 10 - - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - config.order = :random - - # Seed global randomization in this process using the `--seed` CLI option. - # Setting this allows you to use `--seed` to deterministically reproduce - # test failures related to randomization by passing the same `--seed` value - # as the one that triggered the failure. - Kernel.srand config.seed -=end end diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 0000000..bdc295d --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,17 @@ +RSpec.configure do |config| + config.before(:suite) do + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end +end \ No newline at end of file diff --git a/spec/support/shoulda_matchers.rb b/spec/support/shoulda_matchers.rb new file mode 100644 index 0000000..88b03b0 --- /dev/null +++ b/spec/support/shoulda_matchers.rb @@ -0,0 +1,8 @@ +RSpec.configure do |config| + Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :rails + end + end +end \ No newline at end of file diff --git a/spec/views/blocks/index.html.slim_spec.rb b/spec/views/blocks/index.html.slim_spec.rb index 97e3869..dd896f9 100755 --- a/spec/views/blocks/index.html.slim_spec.rb +++ b/spec/views/blocks/index.html.slim_spec.rb @@ -26,6 +26,7 @@ it "renders a list of blocks" do render cell_selector = Rails::VERSION::STRING >= "7" ? "div>p" : "tr>td" - assert_select cell_selector, text: Regexp.new("Previous Hash".to_s), count: 2 + assert_select cell_selector, text: Regexp.new("Block 49") + assert_select cell_selector, text: Regexp.new("Block 50") end end diff --git a/spec/views/chains/edit.html.slim_spec.rb b/spec/views/chains/edit.html.slim_spec.rb deleted file mode 100644 index 915d9d4..0000000 --- a/spec/views/chains/edit.html.slim_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "chains/edit", type: :view do - let(:chain) { - Chain.create!( - name: "MyString", - blocks_count: 1, - maintainer: "MyString", - chain_version: "MyString", - description: "MyText" - ) - } - - before(:each) do - assign(:chain, chain) - end - - it "renders the edit chain form" do - render - - assert_select "form[action=?][method=?]", chain_path(chain), "post" do - assert_select "input[name=?]", "chain[name]" - - assert_select "input[name=?]", "chain[blocks_count]" - - assert_select "input[name=?]", "chain[maintainer]" - - assert_select "input[name=?]", "chain[chain_version]" - - assert_select "textarea[name=?]", "chain[description]" - end - end -end diff --git a/spec/views/chains/index.html.slim_spec.rb b/spec/views/chains/index.html.slim_spec.rb deleted file mode 100755 index d5dc341..0000000 --- a/spec/views/chains/index.html.slim_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "chains/index", type: :view do - let(:chain_hash) do - { - name: Faker::Name.name[0 .. 19], maintainer: Faker::Internet.email, - chain_version: Faker::App.version, description: Faker::Lorem.paragraph - } - end - - let(:chain_hash_2) do - { - name: Faker::Name.name[0 .. 19], maintainer: Faker::Internet.email, - chain_version: Faker::App.version, description: Faker::Lorem.paragraph - } - end - - before(:each) do - assign(:chains, [ - create(:chain, chain_hash), - create(:chain, chain_hash_2) - ]) - end - - it "renders a list of chains" do - render - cell_selector = Rails::VERSION::STRING >= "7" ? "div>p" : "tr>td" - assert_select cell_selector, text: Regexp.new(chain_hash[:name]), count: 1 - assert_select cell_selector, text: Regexp.new(chain_hash_2[:name]), count: 1 - assert_select cell_selector, text: Regexp.new(chain_hash[:maintainer]), count: 1 - assert_select cell_selector, text: Regexp.new(chain_hash_2[:maintainer]), count: 1 - assert_select cell_selector, text: Regexp.new(chain_hash[:chain_version]), count: 1 - assert_select cell_selector, text: Regexp.new(chain_hash_2[:chain_version]), count: 1 - assert_select cell_selector, text: Regexp.new(chain_hash[:description]), count: 1 - assert_select cell_selector, text: Regexp.new(chain_hash_2[:description]), count: 1 - end -end diff --git a/spec/views/chains/new.html.slim_spec.rb b/spec/views/chains/new.html.slim_spec.rb deleted file mode 100644 index 9e76bdb..0000000 --- a/spec/views/chains/new.html.slim_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "chains/new", type: :view do - before(:each) do - assign(:chain, Chain.new( - name: "MyString", - blocks_count: 1, - maintainer: "MyString", - chain_version: "MyString", - description: "MyText" - )) - end - - it "renders new chain form" do - render - - assert_select "form[action=?][method=?]", chains_path, "post" do - assert_select "input[name=?]", "chain[name]" - - assert_select "input[name=?]", "chain[blocks_count]" - - assert_select "input[name=?]", "chain[maintainer]" - - assert_select "input[name=?]", "chain[chain_version]" - - assert_select "textarea[name=?]", "chain[description]" - end - end -end diff --git a/spec/views/chains/show.html.slim_spec.rb b/spec/views/chains/show.html.slim_spec.rb deleted file mode 100644 index 0fef03b..0000000 --- a/spec/views/chains/show.html.slim_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "chains/show", type: :view do - before(:each) do - assign(:chain, Chain.create!( - name: "Name", - blocks_count: 2, - maintainer: "Maintainer", - chain_version: "Chain Version", - description: "MyText" - )) - end - - it "renders attributes in

" do - render - expect(rendered).to match(/Name/) - expect(rendered).to match(/2/) - expect(rendered).to match(/Maintainer/) - expect(rendered).to match(/Chain Version/) - expect(rendered).to match(/MyText/) - end -end diff --git a/spec/views/signatures/edit.html.slim_spec.rb b/spec/views/signatures/edit.html.slim_spec.rb deleted file mode 100644 index d3398c6..0000000 --- a/spec/views/signatures/edit.html.slim_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "signatures/edit", type: :view do - let(:chain) { create(:chain) } - let(:block) { create(:block, chain: chain) } - let(:pool) { create(:pool, block: block) } - let(:user) { create(:user) } - let(:user_2) { create(:user, email: "second@email.com", username: "second") } - let(:wallet) { create(:wallet, user: user) } - let(:wallet_2) { create(:wallet, user: user_2, pr_key: "second_pr_key", pv_key: "second_pv_key") } - let(:ticket) { create(:ticket, pool: pool, user: user) } - let(:transaction) { create(:transaction, block: block, sender_key: wallet.pr_key, receiver_key: wallet_2.pv_key) } - let(:contract) { create(:contract, blk_transaction: transaction) } - let(:acceptable_word) { create(:acceptable_word) } - let(:acceptable_symbol_sequence) { create(:acceptable_symbol_sequence) } - let(:acceptable_number_sequence) { create(:acceptable_number_sequence) } - let(:signature) { - Signature.create!( - signature: "a" * 64, - contract: contract, - ) - } - - before(:each) do - assign(:signature, signature) - end -end diff --git a/spec/views/signatures/index.html.slim_spec.rb b/spec/views/signatures/index.html.slim_spec.rb deleted file mode 100644 index 5d09926..0000000 --- a/spec/views/signatures/index.html.slim_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "signatures/index", type: :view do - let(:chain) { create(:chain) } - let(:block) { create(:block, chain: chain) } - let(:pool) { create(:pool, block: block) } - let(:user) { create(:user) } - let(:user_2) { create(:user, email: "second@email.com", username: "second") } - let(:wallet) { create(:wallet, user: user) } - let(:wallet_2) { create(:wallet, user: user_2, pr_key: "second_pr_key", pv_key: "second_pv_key") } - let(:ticket) { create(:ticket, pool: pool, user: user) } - let(:transaction) { create(:transaction, block: block, sender_key: wallet.pr_key, receiver_key: wallet_2.pv_key) } - let(:contract) { create(:contract, blk_transaction: transaction) } - let(:acceptable_word) { create(:acceptable_word) } - let(:acceptable_symbol_sequence) { create(:acceptable_symbol_sequence) } - let(:acceptable_number_sequence) { create(:acceptable_number_sequence) } - before(:each) do - assign(:signatures, [ - Signature.create!( - signature: "a" * 64, - contract: contract, - user: user, - ticket: ticket, - ), - Signature.create!( - signature: "b" * 64, - contract: contract, - user: user, - ticket: ticket, - ) - ]) - end - - it "renders a list of signatures" do - render - cell_selector = Rails::VERSION::STRING >= "7" ? "div>p" : "tr>td" - assert_select cell_selector, text: Regexp.new("a" * 64), count: 1 - assert_select cell_selector, text: Regexp.new(contract.id.to_s), count: 2 - end -end diff --git a/spec/views/signatures/new.html.slim_spec.rb b/spec/views/signatures/new.html.slim_spec.rb deleted file mode 100644 index 82c5172..0000000 --- a/spec/views/signatures/new.html.slim_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "signatures/new", type: :view do - let(:chain) { create(:chain) } - let(:block) { create(:block, chain: chain) } - let(:pool) { create(:pool, block: block) } - let(:user) { create(:user) } - let(:user_2) { create(:user, email: "second@email.com", username: "second") } - let(:wallet) { create(:wallet, user: user) } - let(:wallet_2) { create(:wallet, user: user_2, pr_key: "second_pr_key", pv_key: "second_pv_key") } - let(:ticket) { create(:ticket, pool: pool, user: user) } - let(:transaction) { create(:transaction, block: block, sender_key: wallet.pr_key, receiver_key: wallet_2.pv_key) } - let(:contract) { create(:contract, blk_transaction: transaction) } - let(:acceptable_word) { create(:acceptable_word) } - let(:acceptable_symbol_sequence) { create(:acceptable_symbol_sequence) } - let(:acceptable_number_sequence) { create(:acceptable_number_sequence) } - before(:each) do - assign(:signature, Signature.new( - signature: "a" * 64, - contract: contract, - )) - end -end diff --git a/spec/views/signatures/show.html.slim_spec.rb b/spec/views/signatures/show.html.slim_spec.rb deleted file mode 100644 index f2c38c5..0000000 --- a/spec/views/signatures/show.html.slim_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "signatures/show", type: :view do - let(:chain) { create(:chain) } - let(:block) { create(:block, chain: chain) } - let(:pool) { create(:pool, block: block) } - let(:user) { create(:user) } - let(:user_2) { create(:user, email: "second@email.com", username: "second") } - let(:wallet) { create(:wallet, user: user) } - let(:wallet_2) { create(:wallet, user: user_2, pr_key: "second_pr_key", pv_key: "second_pv_key") } - let(:ticket) { create(:ticket, pool: pool, user: user) } - let(:transaction) { create(:transaction, block: block, sender_key: wallet.pr_key, receiver_key: wallet_2.pv_key) } - let(:contract) { create(:contract, blk_transaction: transaction) } - let(:acceptable_word) { create(:acceptable_word) } - let(:acceptable_symbol_sequence) { create(:acceptable_symbol_sequence) } - let(:acceptable_number_sequence) { create(:acceptable_number_sequence) } - - - before(:each) do - assign(:signature, Signature.create!( - signature: "S" * 64, - contract: contract, - user: user, - ticket: ticket, - )) - end - - it "renders attributes in

" do - render - expect(rendered).to match(/Signature/) - expect(rendered).to match(//) - end -end diff --git a/spec/workers/application_worker_spec.rb b/spec/workers/application_worker_spec.rb index 8196e21..1e30aae 100644 --- a/spec/workers/application_worker_spec.rb +++ b/spec/workers/application_worker_spec.rb @@ -62,4 +62,10 @@ expect(worker.send(:block_exists?)).to be_falsey end end + + describe "pool check" do + it "#pool_exists? should return false if not pool not passed" do + expect(worker.send(:pool_exists?)).to be_falsey + end + end end diff --git a/spec/workers/assign_contract_worker_spec.rb b/spec/workers/assign_contract_worker_spec.rb new file mode 100644 index 0000000..2b33dff --- /dev/null +++ b/spec/workers/assign_contract_worker_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AssignContractWorker, type: :worker do + let(:chain) { create(:chain) } + let(:block) { create(:block, chain: chain) } + let(:pool) { create(:pool, block: block) } + let(:user) { create(:user) } + let(:user_two) { create(:user, email: 'lorem@ipsum.com', username: 'lorem', password: 'loremaludias', password_confirmation: 'loremaludias') } + let(:wallet) { create(:wallet, user: user) } + let(:wallet_two) { create(:wallet, user: user_two) } + let(:transaction) { create(:transaction, sender_key: wallet.pv_key, receiver_key: wallet_two.pr_key, amount: 100 , fee: 10, block: block) } + let(:transaction_two) { create(:transaction, sender_key: wallet.pv_key, receiver_key: wallet_two.pr_key, amount: 100 , fee: 10, block: block) } + let(:contract) { create(:contract, blk_transaction: transaction) } + let(:contract_two) { create(:contract, blk_transaction: transaction_two) } + let(:word) { create(:acceptable_word) } + let(:number_sequence) { create(:acceptable_number_sequence) } + let(:symbol_sequence) { create(:acceptable_symbol_sequence) } + let(:ticket) { create(:ticket, pool: pool, user: user, transaction_id_list: [transaction.id, transaction_two.id]) } + + before(:each) do + chain + block + pool + word + number_sequence + symbol_sequence + wallet.update!(balance: 1000) + transaction + transaction_two + contract + contract_two + ticket + end + + it 'should confirm all contracts' do + expect(contract.confirmed).to eq(false) + expect(contract_two.confirmed).to eq(false) + + 5.times do + AssignContractWorker.new.perform(ticket.id) + end + expect(contract.reload.signatures_count).to eq(5) + expect(contract.reload.confirmed).to eq(true) + expect(contract_two.reload.confirmed).to eq(true) + end +end diff --git a/spec/workers/create_ticket_worker_spec.rb b/spec/workers/create_ticket_worker_spec.rb new file mode 100644 index 0000000..8d9d50f --- /dev/null +++ b/spec/workers/create_ticket_worker_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe CreateTicketWorker, type: :worker do + let(:chain) { create(:chain) } + let(:block) { create(:block, chain: chain) } + let(:pool) { create(:pool, block: block) } + let(:user) { create(:user) } + let(:ticket) { create(:ticket, user: user, pool: pool) } + let(:worker) { CreateTicketWorker.new } + let(:word) { create(:acceptable_word) } + let(:number_sequence) { create(:acceptable_number_sequence) } + let(:symbol_sequence) { create(:acceptable_symbol_sequence) } + + before(:each) do + word + number_sequence + symbol_sequence + end + + it "should call create ticket service" do + time = Time.now + expect(CreateTicketService).to receive(:new).with(user.id, pool.id, time).and_call_original + worker.perform(user.id, pool.id, time) + expect(worker.success?).to be_truthy + end + + it "should raise error if user not found" do + expect { worker.perform(9000, pool.id, Time.now) }.to raise_error(StandardError) + end +end