Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
correct tests, 98.87% ccoverage
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusGautamah committed Jan 28, 2023
1 parent e37887e commit 3f7c034
Show file tree
Hide file tree
Showing 39 changed files with 330 additions and 580 deletions.
3 changes: 2 additions & 1 deletion .env_sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@
osbc-*.gem
.env
/.env
/.env.*
/.env.*
/mining_concept.ipynb
/mining_concept.ipynb_checkpoints
/hyperminer.py
1 change: 1 addition & 0 deletions app/controllers/api/v1/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions app/controllers/chains_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: true

class ChainsController < ApplicationController
end
class ChainsController < ApplicationController; end
19 changes: 1 addition & 18 deletions app/controllers/signatures_controller.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 0 additions & 30 deletions app/models/concerns/sequences_validator.rb

This file was deleted.

4 changes: 4 additions & 0 deletions app/models/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/services/application_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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

Expand Down
7 changes: 0 additions & 7 deletions app/services/create_ticket_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/workers/application_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def user_exists?

def pool_exists?
Pool.find_by(id: pool_id).present?
rescue NameError
false
end

def block_exists?
Expand Down
2 changes: 0 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
25 changes: 25 additions & 0 deletions spec/controllers/pools_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
112 changes: 112 additions & 0 deletions spec/controllers/tickets_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 3f7c034

Please sign in to comment.