Advertisement
Guest User

Untitled

a guest
Nov 16th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. require "spec_helper"
  2.  
  3. describe HivesController do
  4. before do
  5. @user = FactoryGirl.create(:user)
  6. sign_in(@user)
  7. end
  8.  
  9. describe "list_shared" do
  10. let(:user) { FactoryGirl.create(:user, email: "alice@example.com") }
  11. let(:shared_hive1) { FactoryGirl.create(:hive, user_id: user.id) }
  12. let(:shared_hive2) { FactoryGirl.create(:hive, user_id: user.id) }
  13. let(:not_shared_hive) { FactoryGirl.create(:hive, user_id: user.id) }
  14.  
  15. it "gets hives which shared with the current user" do
  16. shared_hive1.share_with(@user.email)
  17. shared_hive2.share_with(@user.email)
  18. get :list_shared
  19.  
  20. expect(assigns(:hives)).to include(shared_hive1)
  21. expect(assigns(:hives)).to include(shared_hive2)
  22. end
  23. end
  24.  
  25. describe "index" do
  26. it "gets hives of the current user" do
  27. hive1 = FactoryGirl.create(:hive, user_id: @user.id)
  28. hive2 = FactoryGirl.create(:hive, user_id: @user.id)
  29. get :index
  30. expect(assigns(:hives)).to include(hive1)
  31. expect(assigns(:hives)).to include(hive2)
  32. end
  33.  
  34. context "#role_permission" do
  35. let(:non_beekeepers) do
  36. [
  37. FactoryGirl.create(:user, email: "alice@example.com", roles_mask: SCIENTIST),
  38. FactoryGirl.create(:user, email: "john@example.com", roles_mask: ADMIN)
  39. ]
  40. end
  41.  
  42. it "raises Forbidden error when not beekeeper" do
  43. non_beekeepers.each do |user|
  44. sign_in_as(user)
  45. expect do
  46. get :index
  47. end.to raise_error(ApplicationController::Forbidden)
  48. end
  49. end
  50.  
  51. def sign_in_as(user)
  52. sign_out(:user)
  53. sign_in(user)
  54. end
  55. end
  56.  
  57. context "#pagination" do
  58. before do
  59. (Hive.per_page * 2 + 1).times do
  60. FactoryGirl.create(:hive, user_id: @user.id)
  61. end
  62. end
  63.  
  64. it "gets first page" do
  65. get :index
  66. expect(assigns(:hives).to_a.count).to eq(Hive.per_page)
  67. #.to_a for eager loading the actual records from ActiveRelation
  68. end
  69.  
  70. it "gets third page" do
  71. get :index, page: 3
  72. expect(assigns(:hives).to_a.count).to eq(1)
  73. end
  74. end
  75. end
  76.  
  77. describe "show" do
  78. let(:hive) { double(:hive, id: 1, user_id: @user.id) }
  79.  
  80. before do
  81. allow(Hive).to receive_messages(find: hive)
  82. @m1 = FactoryGirl.create(:message, hive_id: hive.id)
  83. @m2 = FactoryGirl.create(:message, hive_id: hive.id)
  84. allow(hive).to receive_messages(get_messages: [@m1, @m2])
  85. end
  86.  
  87. it "prepares hive messages for data table and presentation" do
  88. expect(GraphPresenter).to receive(:prepare_single_hive_messages_for_datatable_highcharts).
  89. with([@m1, @m2], an_instance_of(DateTime), an_instance_of(DateTime),
  90. @user.us_metric_enabled?)
  91. get :show, id: hive.id
  92. end
  93. end
  94.  
  95. describe ".set_hive" do
  96. context "#show_action" do
  97. before do
  98. other_user = FactoryGirl.create(:user, email: "alice@example.com")
  99. @hive = FactoryGirl.create(:hive, user_id: other_user.id)
  100. end
  101.  
  102. it "checks the visibility of hive" do
  103. expect(HiveVisibilityPolicy).to receive(
  104. :visible_for_user?).with(@hive, @user.id, @user.id)
  105. get :show, id: @hive.id
  106. end
  107.  
  108. it "sets hive when it is visible to user" do
  109. allow(HiveVisibilityPolicy).to receive(:visible_for_user?) { true }
  110. get :show, id: @hive.id
  111. expect(assigns(:hive)).not_to be_nil
  112. end
  113. end
  114.  
  115. context "#other_actions" do
  116. before do
  117. @hive = FactoryGirl.create(:hive, user_id: @user.id)
  118. end
  119.  
  120. it "checks user owns the hive or not" do
  121. expect(HiveVisibilityPolicy).to receive(
  122. :user_own_hive?).with(@hive, @user.id)
  123. get :edit, id: @hive.id
  124. end
  125.  
  126. it "sets hive when it is owned by user" do
  127. allow(HiveVisibilityPolicy).to receive(:user_own_hive?) { true }
  128. get :edit, id: @hive.id
  129. expect(assigns(:hive)).not_to be_nil
  130. end
  131. end
  132. end
  133.  
  134. describe "new" do
  135. it "sets a new hive" do
  136. get :new
  137. expect(assigns(:hive)).not_to be_nil
  138. end
  139. end
  140.  
  141. describe "create" do
  142. let(:scale) { FactoryGirl.create(:scale) }
  143.  
  144. before do
  145. @hive_params = {
  146. name: "foo",
  147. colony_count: 1,
  148. latlng: "default"
  149. }
  150. end
  151.  
  152. it "creates a hive with provided existing scale" do
  153. @hive_params.merge!(scale: scale.id)
  154. expect do
  155. post :create, hive: @hive_params
  156. expect(@user.hives).to include(assigns(:hive))
  157. end.to change(Hive, :count).by 1
  158. end
  159.  
  160. it "sets hive's scale when vendor & serial_number provided" do
  161. vendor_serial_number = "#{scale.vendor}: #{scale.serial_number}"
  162. post :create, { hive: @hive_params,
  163. scale_vendor_sn: vendor_serial_number }
  164. expect(Hive.first.scale).to eq(scale)
  165. end
  166.  
  167. context "#already_in_use_scale" do
  168. before do
  169. hive = FactoryGirl.create(:hive)
  170. scale.update_attributes(hive_id: hive.id)
  171. @hive_params.merge!(scale: scale.id)
  172. end
  173.  
  174. it "does NOT create a hive" do
  175. expect do
  176. post :create, hive: @hive_params
  177. end.not_to change(Hive, :count)
  178. end
  179.  
  180. it "redirects to hives_path" do
  181. post :create, hive: @hive_params
  182. expect(response).to redirect_to(hives_path)
  183. end
  184. end
  185.  
  186. context "#not_providing_vendor_and_serial_number" do
  187. it "does NOT create a hive" do
  188. expect do
  189. post :create, { hive: @hive_params, scale_vendor_sn: "" }
  190. end.not_to change(Hive, :count)
  191. end
  192.  
  193. it "redirects to hives_path" do
  194. post :create, { hive: @hive_params, scale_vendor_sn: "" }
  195. expect(response).to redirect_to(hives_path)
  196. end
  197. end
  198.  
  199. context "#lat_long_provided" do
  200. let(:latitude) { 42 }
  201. let(:longitude) { -85 }
  202.  
  203. before do
  204. allow(CoordinateBuilder).to receive_messages(parse: [longitude, latitude])
  205. @hive_params.merge!(latlng: "(#{latitude}, #{longitude})",
  206. scale: scale.id)
  207. end
  208.  
  209. it "sets the hive location based on lat/lng" do
  210. post :create, hive: @hive_params
  211. expect(assigns(:hive).current_location.latitude).to eq(latitude.to_f)
  212. expect(assigns(:hive).current_location.longitude).to eq(longitude.to_f)
  213. end
  214. end
  215. end
  216.  
  217. describe "update" do
  218. let(:scale) { FactoryGirl.create(:scale) }
  219. let(:hive) { FactoryGirl.create(:hive, user_id: @user.id) }
  220.  
  221. before do
  222. @hive_params = {
  223. name: "foo",
  224. colony_count: 1,
  225. latlng: "default"
  226. }
  227. end
  228.  
  229. it "updates hive basic info" do
  230. @hive_params.merge!(name: "bar", colony_count: 2)
  231. put :update, { id: hive.id, hive: @hive_params }
  232.  
  233. updated_hive = Hive.find(hive.id)
  234. expect(updated_hive.name).to eq("bar")
  235. expect(updated_hive.colony_count).to eq(2)
  236. end
  237.  
  238. it "sets scale to nil when no scale provided" do
  239. put :update, { id: hive.id, hive: @hive_params }
  240. updated = Hive.find(hive.id)
  241. expect(updated.scale).to be_nil
  242. end
  243.  
  244. it "sets scale based on provided scale id" do
  245. @hive_params.merge!(scale: scale.id)
  246. put :update, { id: hive.id, hive: @hive_params }
  247. expect(Hive.find(hive.id).scale).to eq(scale)
  248. end
  249.  
  250. it "sets scale based on provided vendor and serial number" do
  251. hive.use_scale(scale)
  252. vendor, serial_number = "SolutionBee", "007"
  253. new_scale = FactoryGirl.create(:scale, vendor: vendor,
  254. serial_number: serial_number)
  255. put :update, {
  256. id: hive.id,
  257. hive: @hive_params,
  258. scale_vendor_sn: "#{vendor}: #{serial_number}"
  259. }
  260.  
  261. expect(Hive.find(hive.id).scale).to eq(new_scale)
  262. end
  263.  
  264. context "#lat_long_provided" do
  265. it "sets the hive location based on lat/lng" do
  266. longitude, latitude = -85, 42
  267. allow(CoordinateBuilder).to receive_messages(parse: [longitude, latitude])
  268. @hive_params.merge!(latlng: "(#{latitude}, #{longitude})",
  269. scale: scale.id)
  270. put :update, id: hive.id, hive: @hive_params
  271.  
  272. updated_hive = Hive.find(hive.id)
  273. expect(updated_hive.current_location.latitude).to eq(latitude.to_f)
  274. expect(updated_hive.current_location.longitude).to eq(longitude.to_f)
  275. end
  276. end
  277. end
  278.  
  279. describe "destroy" do
  280. it "deletes the hive" do
  281. hive = FactoryGirl.create(:hive, user_id: @user.id)
  282. expect do
  283. delete :destroy, id: hive.id
  284. end.to change(Hive, :count).by -1
  285. end
  286.  
  287. it "unlinks hive from scale before deleting it" do
  288. hive = FactoryGirl.create(:hive, user_id: @user.id)
  289. scale = FactoryGirl.create(:scale, hive_id: hive.id)
  290. HiveScale.create!(hive_id: hive.id, scale_id: scale.id,
  291. from_date: Time.now)
  292.  
  293. delete :destroy, id: hive.id
  294. expect(Scale.last.hive_id).to be_nil
  295. end
  296. end
  297. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement