Fix tests

This commit is contained in:
benji 2017-01-23 17:22:29 +01:00
parent 9e7e793941
commit 0a3c949d09

View file

@ -1,4 +1,9 @@
describe TransactionsController, type: :api do describe TransactionsController, type: :api do
def post_transaction(extra_attributes = {})
post '/transactions', { transaction: @api_attributes.merge(extra_attributes) },
{ 'HTTP_ACCEPT' => "application/json", "HTTP_AUTHORIZATION" => "Token token=#{@key}" }
end
before :each do before :each do
@debtor = create :user @debtor = create :user
@creditor = create :user @creditor = create :user
@ -14,43 +19,55 @@ describe TransactionsController, type: :api do
@key = @client.key @key = @client.key
end end
def post_transaction(extra_attributes = {}) describe 'with key' do
post '/transactions', { transaction: @api_attributes.merge(extra_attributes) }, before :each do
{ 'HTTP_ACCEPT' => "application/json", "HTTP_AUTHORIZATION" => "Token token=#{@key}" } @client.add_role :create_transactions
end
describe "Authentication" do
it "should require a client authentication key" do
post '/transactions'
expect(last_response.status).to eq(302)
end end
it "should work with valid key" do def post_transaction(extra_attributes = {})
post_transaction post '/transactions', { transaction: @api_attributes.merge(extra_attributes) },
expect(last_response.status).to eq(201) { '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(302)
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
end end
describe "successfull creating transaction" do describe 'without key' do
it "should create a transaction" do it "should not create a transaction" do
expect { post_transaction }.to change { Transaction.count }.by(1) expect { post_transaction }.to_not change { Transaction.count }
end 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
end end