diff --git a/Gemfile b/Gemfile index c8a0ca4..59b0cb9 100644 --- a/Gemfile +++ b/Gemfile @@ -51,6 +51,8 @@ gem "rails-erd", group: :development gem 'travis-lint' +gem 'faker' +gem 'fabrication' group :development do gem "binding_of_caller" @@ -64,4 +66,5 @@ end group :test do # Needed for TravisCI gem 'rake' + gem "database_cleaner" end diff --git a/Gemfile.lock b/Gemfile.lock index 37be44b..5f3079c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -73,6 +73,7 @@ GEM coffee-script-source (1.6.3) daemons (1.1.9) dalli (2.6.4) + database_cleaner (1.2.0) debug_inspector (0.0.2) devise (3.1.1) bcrypt-ruby (~> 3.0) @@ -89,6 +90,9 @@ GEM erubis (2.7.0) eventmachine (1.0.0) execjs (2.0.2) + fabrication (2.9.1) + faker (1.2.0) + i18n (~> 0.5) foreman (0.63.0) dotenv (>= 0.7) thor (>= 0.13.6) @@ -242,8 +246,11 @@ DEPENDENCIES binding_of_caller coffee-rails dalli + database_cleaner devise_browserid_authenticatable dotenv-rails + fabrication + faker foreman haml-rails has_scope diff --git a/config/environments/test.rb b/config/environments/test.rb index a8825d9..6cffa8c 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -29,4 +29,6 @@ Timeoverflow::Application.configure do # Print deprecation notices to the stderr config.active_support.deprecation = :stderr + + config.log_level = :error end diff --git a/spec/controllers/inquiries_controller_spec.rb b/spec/controllers/inquiries_controller_spec.rb new file mode 100644 index 0000000..bab5fac --- /dev/null +++ b/spec/controllers/inquiries_controller_spec.rb @@ -0,0 +1,84 @@ +require 'spec_helper' + +describe InquiriesController do + let (:test_organization) { Fabricate(:organization)} + let (:member) { Fabricate(:member, organization: test_organization)} + let (:another_member) { Fabricate(:member, organization: test_organization)} + let! (:inquiry) { Fabricate(:inquiry, user: member.user)} + + describe "GET #index" do + context "with a logged user" do + it "populates and array of inquiries" do + login(another_member.user) + + get 'index' + expect(assigns(:inquiries)).to eq([inquiry]) + end + end + end + + + describe "GET #show" do + context "with valid params" do + context "with a logged user" do + it "assigns the requested inquiry to @inquiry" do + login(another_member.user) + + get 'show', id: inquiry.id + expect(assigns(:inquiry)).to eq(inquiry) + end + end + end + end + + describe "POST #create" do + context "with valid params" do + context "with a logged user" do + it "creates a new inquiry" do + login(another_member.user) + + expect { + post 'create', inquiry: Fabricate.to_params(:inquiry) + }.to change(Inquiry,:count).by(1) + end + end + end + end + + describe "PUT #update" do + context "with valid params" do + context "with a logged user" do + it "located the requested @inquiry" do + login(member.user) + + put 'update', id: inquiry.id, inquiry: Fabricate.to_params(:inquiry) + expect(assigns(:inquiry)).to eq(inquiry) + end + + it "changes @inquiry's attributes" do + login(member.user) + + put 'update', id: inquiry.id, inquiry: Fabricate.to_params(:inquiry, user: member, title: "New title", description: "New description") + + inquiry.reload + expect(inquiry.title).to eq("New title") + expect(inquiry.description).to eq("New description") + end + end + end + + context "with invalid params" do + context "with a logged user" do + it "does not change @inquiry's attributes" do + login(member.user) + + put :update, id: inquiry.id, inquiry: Fabricate.to_params(:inquiry, user: nil, title: "New title", description: "New description") + + expect(inquiry.title).not_to eq("New title") + expect(inquiry.description).not_to eq("New description") + end + end + end + end + +end diff --git a/spec/controllers/transfers_controller_spec.rb b/spec/controllers/transfers_controller_spec.rb new file mode 100644 index 0000000..b1da638 --- /dev/null +++ b/spec/controllers/transfers_controller_spec.rb @@ -0,0 +1,116 @@ +require 'spec_helper' + +describe TransfersController do + let (:test_organization) { Fabricate(:organization)} + let (:member_admin) { Fabricate(:member, organization: test_organization, manager: true)} + let (:member_giver) { Fabricate(:member, organization: test_organization) } + let (:member_taker) { Fabricate(:member, organization: test_organization) } + + describe "POST #create" do + context "with valid params" do + context "with an admin user logged" do + subject { post 'create', transfer: {source: member_giver.account.id, destination: member_taker.account.id, amount: 5} } + + it "creates a new Transfer" do + login(member_admin.user) + + expect { + subject + }.to change(Transfer, :count).by 1 + end + + it "creates two Movements" do + login(member_admin.user) + + expect { + subject + }.to change { Movement.count}.by 2 + end + + it "updates the balance of both accounts" do + login(member_admin.user) + + expect { + subject + member_giver.reload + }.to change { member_giver.account.balance.to_i }.by -5 + + expect { + subject + member_taker.reload + }.to change { member_taker.account.balance.to_i }.by 5 + + end + end + + context "with a regular user logged" do + subject { post 'create', transfer: {destination: member_taker.account.id, amount: 5} } + + it "creates a new Transfer" do + login(member_giver.user) + + expect { + subject + }.to change(Transfer, :count).by 1 + end + + it "creates two Movements" do + login(member_giver.user) + + expect { + subject + }.to change { Movement.count}.by 2 + end + + it "updates the balance of both accounts" do + login(member_giver.user) + + expect { + subject + member_giver.reload + }.to change { member_giver.account.balance.to_i }.by -5 + + expect { + subject + member_taker.reload + }.to change { member_taker.account.balance.to_i }.by 5 + + end + end + end + + # context "with valid params" do + # context "with an admin user logged" do + # it "creates a new Transfer" do + # login(member_admin.user) + + # post 'create', transfer: {source: member_giver.account.id, destination: member_taker.account.id, amount: 5} + + # expect(Transfer.count).to eq(1) + # expect(Movement.count).to eq(2) + + # member_giver.reload + # member_taker.reload + # expect(member_giver.account.balance.to_i).to eq(-5) + # expect(member_taker.account.balance.to_i).to eq(5) + # end + # end + + # context "with a regular user logged" do + # it "creates a new Transfer" do + # login(member_giver.user) + + # post 'create', transfer: {destination: member_taker.account.id, amount: 5} + + # expect(Transfer.count).to eq(1) + # expect(Movement.count).to eq(2) + + # member_giver.reload + # member_taker.reload + # expect(member_giver.account.balance.to_i).to eq(-5) + # expect(member_taker.account.balance.to_i).to eq(5) + # end + # end + # end + end +end diff --git a/spec/fabricators/member_fabricator.rb b/spec/fabricators/member_fabricator.rb new file mode 100644 index 0000000..8607954 --- /dev/null +++ b/spec/fabricators/member_fabricator.rb @@ -0,0 +1,7 @@ +Fabricator(:member) do + + user { Fabricate(:user) } + organization { Fabricate(:organization) } + manager false + +end \ No newline at end of file diff --git a/spec/fabricators/organization_fabricator.rb b/spec/fabricators/organization_fabricator.rb new file mode 100644 index 0000000..b446a08 --- /dev/null +++ b/spec/fabricators/organization_fabricator.rb @@ -0,0 +1,3 @@ +Fabricator(:organization) do + name { Faker::Company.name } +end \ No newline at end of file diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb new file mode 100644 index 0000000..816aed6 --- /dev/null +++ b/spec/fabricators/post_fabricator.rb @@ -0,0 +1,36 @@ +Fabricator(:post) do + + title { Faker::Lorem.sentence } + user { Fabricate(:user) } + description { Faker::Lorem.paragraph } + permanent { false } + joinable { false } + global { false } + +end + +Fabricator(:inquiry) do + + type "Inquiry" + + title { Faker::Lorem.sentence } + user { Fabricate(:user) } + description { Faker::Lorem.paragraph } + permanent { false } + joinable { false } + global { false } + +end + +Fabricator(:offer) do + + type "Offer" + + title { Faker::Lorem.sentence } + user { Fabricate(:user) } + description { Faker::Lorem.paragraph } + permanent { false } + joinable { false } + global { false } + +end \ No newline at end of file diff --git a/spec/fabricators/user_fabricator.rb b/spec/fabricators/user_fabricator.rb new file mode 100644 index 0000000..da2d769 --- /dev/null +++ b/spec/fabricators/user_fabricator.rb @@ -0,0 +1,14 @@ +Fabricator(:user) do + Faker::Config.locale = :es + + username { Faker::Internet.user_name } + email { Faker::Internet.email } + date_of_birth { DateTime.now.utc } + identity_document { sequence(:identity_document, 1) { |n| "X000000#{n}X" } } + phone { Faker::PhoneNumber.phone_number } + alt_phone { Faker::PhoneNumber.cell_phone } + address { Faker::Address.street_address + " " + Faker::Address.zip_code + " " + Faker::Address.city + " (" + Faker::Address.state + ")"} + gender { ["male", "female"].shuffle.first } + description { Faker::Lorem.paragraph } + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 943bc19..c74b8c4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,7 +27,7 @@ RSpec.configure do |config| # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. - config.use_transactional_fixtures = true + config.use_transactional_fixtures = false # If true, the base class of anonymous controllers will be inferred # automatically. This will be the default behavior in future versions of @@ -39,4 +39,25 @@ RSpec.configure do |config| # the seed, which is printed after each run. # --seed 1234 config.order = "random" + + config.include ControllerMacros, :type => :controller + + # Database cleaner configuration + + config.before :suite do + DatabaseCleaner.strategy = :transaction + DatabaseCleaner.clean_with :truncation + + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end + + # Controllers must render the content of the view + config.render_views end diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb new file mode 100644 index 0000000..08b7be6 --- /dev/null +++ b/spec/support/controller_macros.rb @@ -0,0 +1,18 @@ +module ControllerMacros + + def login(user = nil) + user = Fabricate(:user) unless user + + request.session["user_id"] = user.id + request.session["email"] = user.email + end + + def current_user + @current_user ||= User.find(request.session["user_id"]) if request.session["user_id"] + end + + def current_organization + @current_organization ||= current_user.try(:organizations).try(:first) + end + +end \ No newline at end of file