Merge branch 'master' into cute-little-statistics
This commit is contained in:
commit
54b8e8808b
16 changed files with 160 additions and 61 deletions
1
.rspec
1
.rspec
|
@ -1,2 +1,3 @@
|
|||
--color
|
||||
--require spec_helper
|
||||
--require rails_helper
|
||||
|
|
|
@ -15,8 +15,9 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
def current_client
|
||||
Client.find_by key: request.headers.inspect.to_s
|
||||
@current_client ||= Client.find_by key: (request.headers["X_API_KEY"] || request.headers["HTTP_X_API_KEY"])
|
||||
@current_client ||= authenticate_with_http_token do |token, options|
|
||||
Client.find_by key: token
|
||||
end
|
||||
end
|
||||
|
||||
def current_ability
|
||||
|
|
|
@ -25,8 +25,8 @@ class TransactionsController < ApplicationController
|
|||
.permit(:debtor, :creditor, :message, :euros, :cents)
|
||||
|
||||
{
|
||||
debtor: User.find_by(name: t[:debtor]) || User.zeus,
|
||||
creditor: User.find_by(name: t[:creditor]) || User.zeus,
|
||||
debtor: t[:debtor] ? User.find_or_create_by(name: t[:debtor]) : User.zeus,
|
||||
creditor: t[:creditor] ? User.find_or_create_by(name: t[:creditor]) : User.zeus,
|
||||
issuer: current_client || current_user,
|
||||
amount: (t[:euros].to_f*100 + t[:cents].to_f).to_i,
|
||||
message: t[:message]
|
||||
|
|
55
spec/apis/transactions_controller_spec.rb
Normal file
55
spec/apis/transactions_controller_spec.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
describe TransactionsController, type: :api do
|
||||
before :each do
|
||||
@debtor = create :user
|
||||
@creditor = create :user
|
||||
@api_attributes = {
|
||||
debtor: @debtor.name,
|
||||
creditor: @creditor.name,
|
||||
message: Faker::Lorem.sentence,
|
||||
euros: 1,
|
||||
cents: 25
|
||||
}
|
||||
@client = Client.create name: "Tap"
|
||||
@key = @client.key
|
||||
end
|
||||
|
||||
def post_transaction(extra_attributes = {})
|
||||
post '/transactions', { transaction: @api_attributes.merge(extra_attributes) },
|
||||
{ 'HTTP_ACCEPT' => "application/json", "HTTP_AUTHORIZATION" => "Token token=#{@key}" }
|
||||
end
|
||||
|
||||
describe "Authentication" do
|
||||
it "should require a client authentication key" do
|
||||
post '/transactions'
|
||||
expect(last_response.status).to eq(401)
|
||||
end
|
||||
|
||||
it "should work with valid key" do
|
||||
post_transaction
|
||||
expect(last_response.status).to eq(201)
|
||||
end
|
||||
end
|
||||
|
||||
describe "successfull creating transaction" do
|
||||
it "should create a transaction" do
|
||||
expect { post_transaction }.to change { Transaction.count }.by(1)
|
||||
end
|
||||
|
||||
it "should set issuer" do
|
||||
post_transaction
|
||||
@transaction = Transaction.last
|
||||
expect(@transaction.issuer).to eq(@client)
|
||||
end
|
||||
end
|
||||
|
||||
describe "failed creating transaction" do
|
||||
# it "should create a transaction" do
|
||||
# expect { post_transaction(euros: -5) }.to change { Transaction.count }.by(0)
|
||||
# end
|
||||
|
||||
# it "should give 422 status" do
|
||||
# post_transaction(euros: -4)
|
||||
# expect(last_response.status).to eq(422)
|
||||
# end
|
||||
end
|
||||
end
|
|
@ -1,7 +1,4 @@
|
|||
require 'rails_helper'
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe TransactionsController, type: :controller do
|
||||
describe TransactionsController, type: :controller do
|
||||
describe "creating transaction" do
|
||||
before :each do
|
||||
@debtor = create(:user)
|
||||
|
@ -15,12 +12,12 @@ RSpec.describe TransactionsController, type: :controller do
|
|||
debtor: @debtor.name,
|
||||
creditor: @creditor.name,
|
||||
cents: 70,
|
||||
message: 'hoi'
|
||||
}}
|
||||
message: "hoi"
|
||||
} }
|
||||
end
|
||||
|
||||
it "should create a new transaction" do
|
||||
expect {post :create, @attributes}.to change {Transaction.count}.by(1)
|
||||
expect { post :create, @attributes }.to change { Transaction.count }.by(1)
|
||||
end
|
||||
|
||||
describe "fields" do
|
||||
|
@ -63,7 +60,7 @@ RSpec.describe TransactionsController, type: :controller do
|
|||
it "should be refused" do
|
||||
expect do
|
||||
post :create, transaction: attributes_for(:transaction, cents: -20)
|
||||
end.not_to change {Transaction.count}
|
||||
end.not_to change { Transaction.count }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -74,9 +71,9 @@ RSpec.describe TransactionsController, type: :controller do
|
|||
debtor: @creditor.name,
|
||||
creditor: @debtor.name,
|
||||
euros: 10000000,
|
||||
message: 'DIT IS OVERVAL'
|
||||
message: "DIT IS OVERVAL"
|
||||
}
|
||||
end.not_to change {Transaction.count}
|
||||
end.not_to change { Transaction.count }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,36 @@
|
|||
require 'rails_helper'
|
||||
describe UsersController, type: :controller do
|
||||
before :each do
|
||||
@user = create :penning
|
||||
sign_in @user
|
||||
end
|
||||
|
||||
RSpec.describe UsersController, type: :controller do
|
||||
describe "GET show" do
|
||||
before :each do
|
||||
get :show, id: @user
|
||||
end
|
||||
|
||||
it "should be successful" do
|
||||
expect(response).to render_template(:show)
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
|
||||
it "should load the correct user" do
|
||||
expect(assigns(:user)).to eq(@user)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET index" do
|
||||
before :each do
|
||||
get :index
|
||||
end
|
||||
|
||||
it "should load an array of all users" do
|
||||
expect(assigns(:users)).to eq([@user])
|
||||
end
|
||||
|
||||
it "should render the correct template" do
|
||||
expect(response).to have_http_status(200)
|
||||
expect(response).to render_template(:index)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,7 +18,7 @@ FactoryGirl.define do
|
|||
association :debtor, factory: :user
|
||||
association :creditor, factory: :user
|
||||
issuer { debtor }
|
||||
amount { rand(100) }
|
||||
amount { 1 + rand(100) }
|
||||
message { Faker::Lorem.sentence }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,5 +18,4 @@ FactoryGirl.define do
|
|||
penning true
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
require 'rails_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the TransactionsHelper. For example:
|
||||
#
|
||||
# describe TransactionsHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
RSpec.describe TransactionsHelper, type: :helper do
|
||||
end
|
|
@ -1,14 +0,0 @@
|
|||
require 'rails_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the UsersHelper. For example:
|
||||
#
|
||||
# describe UsersHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
RSpec.describe UsersHelper, type: :helper do
|
||||
end
|
|
@ -9,14 +9,21 @@
|
|||
# updated_at :datetime not null
|
||||
#
|
||||
|
||||
require 'rails_helper'
|
||||
describe Client, type: :model do
|
||||
before :each do
|
||||
@client = create :client
|
||||
end
|
||||
|
||||
RSpec.describe Client, type: :model do
|
||||
it "should have a valid factory" do
|
||||
expect(create(:client)).to be_valid
|
||||
expect(@client).to be_valid
|
||||
end
|
||||
|
||||
it "should generate a key" do
|
||||
expect(create(:client).key).to be_present
|
||||
expect(@client.key).to be_present
|
||||
end
|
||||
|
||||
it "should have a unique name" do
|
||||
client = build :client, name: @client.name
|
||||
expect(client).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,9 +13,7 @@
|
|||
# updated_at :datetime not null
|
||||
#
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Transaction, type: :model do
|
||||
describe Transaction, type: :model do
|
||||
it "has a valid factory" do
|
||||
expect(create(:transaction)).to be_valid
|
||||
end
|
||||
|
@ -27,12 +25,29 @@ RSpec.describe Transaction, type: :model do
|
|||
|
||||
it "should update creditor cache" do
|
||||
trans = build(:transaction, creditor: @user, amount: 10)
|
||||
expect {trans.save!}.to change {@user.balance}.by(10)
|
||||
expect { trans.save! }.to change { @user.balance }.by(10)
|
||||
end
|
||||
|
||||
it "should update debtor cache" do
|
||||
trans = build(:transaction, debtor: @user, amount: 10)
|
||||
expect {trans.save!}.to change {@user.balance}.by(-10)
|
||||
expect { trans.save! }.to change { @user.balance }.by(-10)
|
||||
end
|
||||
end
|
||||
|
||||
describe "amount" do
|
||||
it "should be positive" do
|
||||
expect(build :transaction, amount: -5).to_not be_valid
|
||||
end
|
||||
|
||||
it "should not be 0" do
|
||||
expect(build :transaction, amount: 0).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "debtor/creditor" do
|
||||
it "should be different" do
|
||||
@user = create :user
|
||||
expect(build :transaction, debtor: @user, creditor: @user).to_not be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,10 +10,17 @@
|
|||
# updated_at :datetime not null
|
||||
#
|
||||
|
||||
require 'rails_helper'
|
||||
describe User, type: :model do
|
||||
before :each do
|
||||
@user = create :user
|
||||
end
|
||||
|
||||
RSpec.describe User, type: :model do
|
||||
it "has a valid factory" do
|
||||
expect(create(:user)).to be_valid
|
||||
expect(@user).to be_valid
|
||||
end
|
||||
|
||||
it "has a unique name" do
|
||||
user = build :user, name: @user.name
|
||||
expect(user).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
|
|
@ -27,9 +27,6 @@ require 'rspec/rails'
|
|||
ActiveRecord::Migration.maintain_test_schema!
|
||||
|
||||
RSpec.configure do |config|
|
||||
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
||||
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.
|
||||
|
|
|
@ -22,9 +22,15 @@ Coveralls.wear!('rails')
|
|||
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
||||
require 'factory_girl'
|
||||
require 'devise'
|
||||
|
||||
Dir[File.join(File.dirname(__FILE__), 'support', '**', '*.rb')].each do |f|
|
||||
require f
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.include FactoryGirl::Syntax::Methods
|
||||
config.include Devise::TestHelpers, type: :controller
|
||||
|
||||
# rspec-expectations config goes here. You can use an alternate
|
||||
# assertion/expectation library such as wrong or the stdlib/minitest
|
||||
# assertions if you prefer.
|
||||
|
|
11
spec/support/api_helper.rb
Normal file
11
spec/support/api_helper.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
module ApiHelper
|
||||
include Rack::Test::Methods
|
||||
|
||||
def app
|
||||
Rails.application
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.include ApiHelper, type: :api #apply to all spec for apis folder
|
||||
end
|
Loading…
Reference in a new issue