2015-09-08 15:25:54 +02:00
|
|
|
require 'rails_helper'
|
2015-09-09 15:07:14 +02:00
|
|
|
require 'spec_helper'
|
2015-09-08 15:25:54 +02:00
|
|
|
|
|
|
|
RSpec.describe TransactionsController, type: :controller do
|
2015-09-09 14:37:40 +02:00
|
|
|
describe "creating transaction" do
|
|
|
|
before :each do
|
|
|
|
@debtor = create(:user)
|
|
|
|
@creditor = create(:user)
|
|
|
|
sign_in @debtor
|
|
|
|
end
|
2015-09-08 15:25:54 +02:00
|
|
|
|
2015-09-09 15:07:14 +02:00
|
|
|
context "with valid attributes" do
|
|
|
|
before :each do
|
|
|
|
@attributes = { transaction: {
|
2015-09-09 15:25:02 +02:00
|
|
|
debtor: @debtor.name,
|
2015-09-09 14:37:40 +02:00
|
|
|
creditor: @creditor.name,
|
2015-09-09 16:26:06 +02:00
|
|
|
cents: 70,
|
2015-09-09 15:07:14 +02:00
|
|
|
message: 'hoi'
|
2015-09-09 14:37:40 +02:00
|
|
|
}}
|
2015-09-09 15:07:14 +02:00
|
|
|
post :create, @attributes
|
|
|
|
@transaction = Transaction.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should create a new transaction" do
|
|
|
|
expect {post :create, @attributes}.to change {Transaction.count}.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should set debtor" do
|
|
|
|
expect(@transaction.debtor).to eq(@debtor)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should set amount" do
|
2015-09-09 16:26:06 +02:00
|
|
|
expect(@transaction.amount).to eq(70)
|
2015-09-09 15:07:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should set creditor" do
|
|
|
|
expect(@transaction.creditor).to eq(@creditor)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should set issuer" do
|
|
|
|
expect(@transaction.issuer).to eq(@debtor)
|
|
|
|
end
|
2015-09-09 14:37:40 +02:00
|
|
|
end
|
2015-09-09 15:21:08 +02:00
|
|
|
|
2015-09-09 16:26:06 +02:00
|
|
|
context "with float euros" do
|
|
|
|
it "should set correct amount" do
|
|
|
|
post :create, transaction: {
|
|
|
|
debtor: @debtor.name,
|
|
|
|
creditor: @creditor.name,
|
|
|
|
euros: 10.5,
|
|
|
|
message: "Omdat je een leuke jongen bent!"
|
|
|
|
}
|
|
|
|
expect(Transaction.last.amount).to eq(1050)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-09 15:21:08 +02:00
|
|
|
context "with negative amount" do
|
|
|
|
it "should be refused" do
|
|
|
|
expect do
|
2015-09-09 16:26:06 +02:00
|
|
|
post :create, transaction: attributes_for(:transaction, cents: -20)
|
2015-09-09 15:21:08 +02:00
|
|
|
end.not_to change {Transaction.count}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for other user" do
|
|
|
|
it "should be refused" do
|
|
|
|
expect do
|
|
|
|
post :create, transaction: {
|
2015-09-09 16:26:06 +02:00
|
|
|
debtor: @creditor.name,
|
|
|
|
creditor: @debtor.name,
|
2015-09-09 16:28:11 +02:00
|
|
|
euros: 10000000,
|
2015-09-09 15:21:08 +02:00
|
|
|
message: 'DIT IS OVERVAL'
|
|
|
|
}
|
|
|
|
end.not_to change {Transaction.count}
|
|
|
|
end
|
|
|
|
end
|
2015-09-09 14:37:40 +02:00
|
|
|
end
|
2015-09-08 15:25:54 +02:00
|
|
|
end
|