tab/spec/apis/transactions_controller_spec.rb

74 lines
2.1 KiB
Ruby
Raw Normal View History

2015-09-12 13:03:04 +02:00
describe TransactionsController, type: :api do
2017-01-23 17:22:29 +01:00
def post_transaction(extra_attributes = {})
post '/transactions', { transaction: @api_attributes.merge(extra_attributes) },
{ 'HTTP_ACCEPT' => "application/json", "HTTP_AUTHORIZATION" => "Token token=#{@key}" }
end
2015-09-12 13:03:04 +02:00
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,
id_at_client: 1
2015-09-12 13:03:04 +02:00
}
@client = Client.create name: "Tap"
@key = @client.key
end
2017-01-23 17:22:29 +01:00
describe 'with key' do
before :each do
@client.add_role :create_transactions
end
2015-09-12 13:03:04 +02:00
2017-01-23 17:22:29 +01:00
def post_transaction(extra_attributes = {})
post '/transactions', { transaction: @api_attributes.merge(extra_attributes) },
{ 'HTTP_ACCEPT' => "application/json", "HTTP_AUTHORIZATION" => "Token token=#{@key}" }
2015-09-12 13:03:04 +02:00
end
2017-01-23 17:22:29 +01:00
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
2015-09-12 13:03:04 +02:00
end
2017-01-23 17:22:29 +01:00
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
2015-09-12 13:03:04 +02:00
end
2017-01-23 17:22:29 +01:00
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
2015-09-12 13:03:04 +02:00
end
end
2017-01-23 17:22:29 +01:00
describe 'without key' do
it "should not create a transaction" do
expect { post_transaction }.to_not change { Transaction.count }
end
2015-09-12 13:03:04 +02:00
end
end