diff --git a/.rspec b/.rspec index 83e16f8..a472683 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1,3 @@ --color --require spec_helper +--require rails_helper diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0b697fd..27bab4f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -12,7 +12,7 @@ class ApplicationController < ActionController::Base 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 diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index 7b5626d..c43728d 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -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 @transactions = Transaction.all end diff --git a/spec/apis/transactions_controller_spec.rb b/spec/apis/transactions_controller_spec.rb new file mode 100644 index 0000000..e987efd --- /dev/null +++ b/spec/apis/transactions_controller_spec.rb @@ -0,0 +1,18 @@ +describe TransactionsController, type: :api do + 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 '/transactions', { transaction: attributes_for(:transaction) }, { 'HTTP_ACCEPT' => "application/json", "X_API_KEY" => @key } + expect(last_response.status).to eq(201) + end + end +end diff --git a/spec/controllers/transactions_controller_spec.rb b/spec/controllers/transactions_controller_spec.rb index 4bd2e9d..3c6ced4 100644 --- a/spec/controllers/transactions_controller_spec.rb +++ b/spec/controllers/transactions_controller_spec.rb @@ -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) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index f681ffe..86d0660 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -1,6 +1,4 @@ -require 'rails_helper' - -RSpec.describe UsersController, type: :controller do +describe UsersController, type: :controller do before :each do @user = create :penning sign_in @user diff --git a/spec/helpers/transactions_helper_spec.rb b/spec/helpers/transactions_helper_spec.rb index 48c6c73..3b3e225 100644 --- a/spec/helpers/transactions_helper_spec.rb +++ b/spec/helpers/transactions_helper_spec.rb @@ -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 diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb index 890768c..bb943ca 100644 --- a/spec/helpers/users_helper_spec.rb +++ b/spec/helpers/users_helper_spec.rb @@ -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 diff --git a/spec/models/client_spec.rb b/spec/models/client_spec.rb index 3b23675..d96c171 100644 --- a/spec/models/client_spec.rb +++ b/spec/models/client_spec.rb @@ -9,9 +9,7 @@ # 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 diff --git a/spec/models/transaction_spec.rb b/spec/models/transaction_spec.rb index 1a9ab8c..4cd2a8f 100644 --- a/spec/models/transaction_spec.rb +++ b/spec/models/transaction_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 812c023..e188fc1 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -10,9 +10,7 @@ # updated_at :datetime not null # -require 'rails_helper' - -RSpec.describe User, type: :model do +describe User, type: :model do before :each do @user = create :user end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 831f776..f3829a4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,6 +22,11 @@ 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 diff --git a/spec/support/api_helper.rb b/spec/support/api_helper.rb new file mode 100644 index 0000000..b67d167 --- /dev/null +++ b/spec/support/api_helper.rb @@ -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