Skip to content

Commit

Permalink
Merge branch 'master' into datagrid
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Van der Jeugt committed Sep 10, 2015
2 parents e891f24 + 0da73da commit 7bb2502
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 50 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--color
--require spec_helper
--require rails_helper
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def authenticate_user_or_client!
end

def current_client
@current_client ||= Client.find_by key: request.headers["X-API-KEY"]
@current_client ||= Client.find_by key: request.headers["X_API_KEY"]
end

def current_ability
@current_ability ||=
current_client.try { |c| ClientAbility.new(c) } ||
Ability.new(current_user)
UserAbility.new(current_user)
end
end
4 changes: 3 additions & 1 deletion app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class TransactionsController < ApplicationController
load_and_authorize_resource
skip_before_filter :verify_authenticity_token, only: :create

before_action :authenticate_user!, except: :create
before_action :authenticate_user_or_client!, only: :create

# This line MUST be placed after authentication
load_and_authorize_resource

def index
gridparams = params[:transactions_grid] || Hash.new
gridparams = gridparams.merge(
Expand Down
2 changes: 1 addition & 1 deletion app/models/client_ability.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Ability
class ClientAbility
include CanCan::Ability

def initialize(client)
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class User < ActiveRecord::Base
devise :timeoutable, :omniauthable, :omniauth_providers => [:zeuswpi]

has_many :incoming_transactions,
class_name: 'Transaction', foreign_key: 'creditor_id'
has_many :outgoing_transactions,
Expand Down
2 changes: 1 addition & 1 deletion app/models/ability.rb → app/models/user_ability.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Ability
class UserAbility
include CanCan::Ability

def initialize(user)
Expand Down
58 changes: 58 additions & 0 deletions spec/apis/transactions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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
}
end

def post_transaction(extra_attributes = {})
post '/transactions', { transaction: @api_attributes.merge(extra_attributes) },
{ 'HTTP_ACCEPT' => "application/json", "X_API_KEY" => @key }
end

before :each do
@client = Client.create name: "Tap"
@key = @client.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 402 status" do
post_transaction(euros: -4)
expect(last_response.status).to eq(422)
end
end
end
11 changes: 4 additions & 7 deletions spec/controllers/transactions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -22,7 +19,7 @@
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

it "should set debtor" do
Expand Down Expand Up @@ -58,7 +55,7 @@
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

Expand All @@ -71,7 +68,7 @@
euros: 10000000,
message: 'DIT IS OVERVAL'
}
end.not_to change {Transaction.count}
end.not_to change { Transaction.count }
end
end
end
Expand Down
35 changes: 33 additions & 2 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/factories/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
association :debtor, factory: :user
association :creditor, factory: :user
issuer { debtor }
amount { rand(100) }
amount { 1 + rand(100) }
message { Faker::Lorem.sentence }
end
end
12 changes: 0 additions & 12 deletions spec/helpers/transactions_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
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
12 changes: 0 additions & 12 deletions spec/helpers/users_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
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
16 changes: 11 additions & 5 deletions spec/models/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
# updated_at :datetime not null
#

require 'rails_helper'

RSpec.describe Client, type: :model do
describe Client, type: :model do
before :each do
@client = create :client
end
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
20 changes: 17 additions & 3 deletions spec/models/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,4 +34,20 @@
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
13 changes: 10 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
# 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
Expand Down
11 changes: 11 additions & 0 deletions spec/support/api_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7bb2502

Please sign in to comment.