Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "spec_helper"
- describe HivesController do
- before do
- @user = FactoryGirl.create(:user)
- sign_in(@user)
- end
- describe "list_shared" do
- let(:user) { FactoryGirl.create(:user, email: "alice@example.com") }
- let(:shared_hive1) { FactoryGirl.create(:hive, user_id: user.id) }
- let(:shared_hive2) { FactoryGirl.create(:hive, user_id: user.id) }
- let(:not_shared_hive) { FactoryGirl.create(:hive, user_id: user.id) }
- it "gets hives which shared with the current user" do
- shared_hive1.share_with(@user.email)
- shared_hive2.share_with(@user.email)
- get :list_shared
- expect(assigns(:hives)).to include(shared_hive1)
- expect(assigns(:hives)).to include(shared_hive2)
- end
- end
- describe "index" do
- it "gets hives of the current user" do
- hive1 = FactoryGirl.create(:hive, user_id: @user.id)
- hive2 = FactoryGirl.create(:hive, user_id: @user.id)
- get :index
- expect(assigns(:hives)).to include(hive1)
- expect(assigns(:hives)).to include(hive2)
- end
- context "#role_permission" do
- let(:non_beekeepers) do
- [
- FactoryGirl.create(:user, email: "alice@example.com", roles_mask: SCIENTIST),
- FactoryGirl.create(:user, email: "john@example.com", roles_mask: ADMIN)
- ]
- end
- it "raises Forbidden error when not beekeeper" do
- non_beekeepers.each do |user|
- sign_in_as(user)
- expect do
- get :index
- end.to raise_error(ApplicationController::Forbidden)
- end
- end
- def sign_in_as(user)
- sign_out(:user)
- sign_in(user)
- end
- end
- context "#pagination" do
- before do
- (Hive.per_page * 2 + 1).times do
- FactoryGirl.create(:hive, user_id: @user.id)
- end
- end
- it "gets first page" do
- get :index
- expect(assigns(:hives).to_a.count).to eq(Hive.per_page)
- #.to_a for eager loading the actual records from ActiveRelation
- end
- it "gets third page" do
- get :index, page: 3
- expect(assigns(:hives).to_a.count).to eq(1)
- end
- end
- end
- describe "show" do
- let(:hive) { double(:hive, id: 1, user_id: @user.id) }
- before do
- allow(Hive).to receive_messages(find: hive)
- @m1 = FactoryGirl.create(:message, hive_id: hive.id)
- @m2 = FactoryGirl.create(:message, hive_id: hive.id)
- allow(hive).to receive_messages(get_messages: [@m1, @m2])
- end
- it "prepares hive messages for data table and presentation" do
- expect(GraphPresenter).to receive(:prepare_single_hive_messages_for_datatable_highcharts).
- with([@m1, @m2], an_instance_of(DateTime), an_instance_of(DateTime),
- @user.us_metric_enabled?)
- get :show, id: hive.id
- end
- end
- describe ".set_hive" do
- context "#show_action" do
- before do
- other_user = FactoryGirl.create(:user, email: "alice@example.com")
- @hive = FactoryGirl.create(:hive, user_id: other_user.id)
- end
- it "checks the visibility of hive" do
- expect(HiveVisibilityPolicy).to receive(
- :visible_for_user?).with(@hive, @user.id, @user.id)
- get :show, id: @hive.id
- end
- it "sets hive when it is visible to user" do
- allow(HiveVisibilityPolicy).to receive(:visible_for_user?) { true }
- get :show, id: @hive.id
- expect(assigns(:hive)).not_to be_nil
- end
- end
- context "#other_actions" do
- before do
- @hive = FactoryGirl.create(:hive, user_id: @user.id)
- end
- it "checks user owns the hive or not" do
- expect(HiveVisibilityPolicy).to receive(
- :user_own_hive?).with(@hive, @user.id)
- get :edit, id: @hive.id
- end
- it "sets hive when it is owned by user" do
- allow(HiveVisibilityPolicy).to receive(:user_own_hive?) { true }
- get :edit, id: @hive.id
- expect(assigns(:hive)).not_to be_nil
- end
- end
- end
- describe "new" do
- it "sets a new hive" do
- get :new
- expect(assigns(:hive)).not_to be_nil
- end
- end
- describe "create" do
- let(:scale) { FactoryGirl.create(:scale) }
- before do
- @hive_params = {
- name: "foo",
- colony_count: 1,
- latlng: "default"
- }
- end
- it "creates a hive with provided existing scale" do
- @hive_params.merge!(scale: scale.id)
- expect do
- post :create, hive: @hive_params
- expect(@user.hives).to include(assigns(:hive))
- end.to change(Hive, :count).by 1
- end
- it "sets hive's scale when vendor & serial_number provided" do
- vendor_serial_number = "#{scale.vendor}: #{scale.serial_number}"
- post :create, { hive: @hive_params,
- scale_vendor_sn: vendor_serial_number }
- expect(Hive.first.scale).to eq(scale)
- end
- context "#already_in_use_scale" do
- before do
- hive = FactoryGirl.create(:hive)
- scale.update_attributes(hive_id: hive.id)
- @hive_params.merge!(scale: scale.id)
- end
- it "does NOT create a hive" do
- expect do
- post :create, hive: @hive_params
- end.not_to change(Hive, :count)
- end
- it "redirects to hives_path" do
- post :create, hive: @hive_params
- expect(response).to redirect_to(hives_path)
- end
- end
- context "#not_providing_vendor_and_serial_number" do
- it "does NOT create a hive" do
- expect do
- post :create, { hive: @hive_params, scale_vendor_sn: "" }
- end.not_to change(Hive, :count)
- end
- it "redirects to hives_path" do
- post :create, { hive: @hive_params, scale_vendor_sn: "" }
- expect(response).to redirect_to(hives_path)
- end
- end
- context "#lat_long_provided" do
- let(:latitude) { 42 }
- let(:longitude) { -85 }
- before do
- allow(CoordinateBuilder).to receive_messages(parse: [longitude, latitude])
- @hive_params.merge!(latlng: "(#{latitude}, #{longitude})",
- scale: scale.id)
- end
- it "sets the hive location based on lat/lng" do
- post :create, hive: @hive_params
- expect(assigns(:hive).current_location.latitude).to eq(latitude.to_f)
- expect(assigns(:hive).current_location.longitude).to eq(longitude.to_f)
- end
- end
- end
- describe "update" do
- let(:scale) { FactoryGirl.create(:scale) }
- let(:hive) { FactoryGirl.create(:hive, user_id: @user.id) }
- before do
- @hive_params = {
- name: "foo",
- colony_count: 1,
- latlng: "default"
- }
- end
- it "updates hive basic info" do
- @hive_params.merge!(name: "bar", colony_count: 2)
- put :update, { id: hive.id, hive: @hive_params }
- updated_hive = Hive.find(hive.id)
- expect(updated_hive.name).to eq("bar")
- expect(updated_hive.colony_count).to eq(2)
- end
- it "sets scale to nil when no scale provided" do
- put :update, { id: hive.id, hive: @hive_params }
- updated = Hive.find(hive.id)
- expect(updated.scale).to be_nil
- end
- it "sets scale based on provided scale id" do
- @hive_params.merge!(scale: scale.id)
- put :update, { id: hive.id, hive: @hive_params }
- expect(Hive.find(hive.id).scale).to eq(scale)
- end
- it "sets scale based on provided vendor and serial number" do
- hive.use_scale(scale)
- vendor, serial_number = "SolutionBee", "007"
- new_scale = FactoryGirl.create(:scale, vendor: vendor,
- serial_number: serial_number)
- put :update, {
- id: hive.id,
- hive: @hive_params,
- scale_vendor_sn: "#{vendor}: #{serial_number}"
- }
- expect(Hive.find(hive.id).scale).to eq(new_scale)
- end
- context "#lat_long_provided" do
- it "sets the hive location based on lat/lng" do
- longitude, latitude = -85, 42
- allow(CoordinateBuilder).to receive_messages(parse: [longitude, latitude])
- @hive_params.merge!(latlng: "(#{latitude}, #{longitude})",
- scale: scale.id)
- put :update, id: hive.id, hive: @hive_params
- updated_hive = Hive.find(hive.id)
- expect(updated_hive.current_location.latitude).to eq(latitude.to_f)
- expect(updated_hive.current_location.longitude).to eq(longitude.to_f)
- end
- end
- end
- describe "destroy" do
- it "deletes the hive" do
- hive = FactoryGirl.create(:hive, user_id: @user.id)
- expect do
- delete :destroy, id: hive.id
- end.to change(Hive, :count).by -1
- end
- it "unlinks hive from scale before deleting it" do
- hive = FactoryGirl.create(:hive, user_id: @user.id)
- scale = FactoryGirl.create(:scale, hive_id: hive.id)
- HiveScale.create!(hive_id: hive.id, scale_id: scale.id,
- from_date: Time.now)
- delete :destroy, id: hive.id
- expect(Scale.last.hive_id).to be_nil
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement