agents_controller_spec.rb 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. require 'spec_helper'
  2. describe AgentsController do
  3. def valid_attributes(options = {})
  4. {
  5. :type => "Agents::WebsiteAgent",
  6. :name => "Something",
  7. :options => agents(:bob_website_agent).options,
  8. :source_ids => [agents(:bob_weather_agent).id, ""]
  9. }.merge(options)
  10. end
  11. describe "GET index" do
  12. it "only returns Agents for the current user" do
  13. sign_in users(:bob)
  14. get :index
  15. assigns(:agents).all? {|i| i.user.should == users(:bob) }.should be_true
  16. end
  17. end
  18. describe "POST handle_details_post" do
  19. it "passes control to handle_details_post on the agent" do
  20. sign_in users(:bob)
  21. post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => "bar" }
  22. JSON.parse(response.body).should == { "success" => true }
  23. agents(:bob_manual_event_agent).events.last.payload.should == { 'foo' => "bar" }
  24. end
  25. it "can only be accessed by the Agent's owner" do
  26. sign_in users(:jane)
  27. lambda {
  28. post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => :bar }
  29. }.should raise_error(ActiveRecord::RecordNotFound)
  30. end
  31. end
  32. describe "GET show" do
  33. it "only shows Agents for the current user" do
  34. sign_in users(:bob)
  35. get :show, :id => agents(:bob_website_agent).to_param
  36. assigns(:agent).should eq(agents(:bob_website_agent))
  37. lambda {
  38. get :show, :id => agents(:jane_website_agent).to_param
  39. }.should raise_error(ActiveRecord::RecordNotFound)
  40. end
  41. end
  42. describe "GET edit" do
  43. it "only shows Agents for the current user" do
  44. sign_in users(:bob)
  45. get :edit, :id => agents(:bob_website_agent).to_param
  46. assigns(:agent).should eq(agents(:bob_website_agent))
  47. lambda {
  48. get :edit, :id => agents(:jane_website_agent).to_param
  49. }.should raise_error(ActiveRecord::RecordNotFound)
  50. end
  51. end
  52. describe "POST create" do
  53. it "errors on bad types" do
  54. sign_in users(:bob)
  55. expect {
  56. post :create, :agent => valid_attributes(:type => "Agents::ThisIsFake")
  57. }.not_to change { users(:bob).agents.count }
  58. assigns(:agent).should be_a(Agent)
  59. assigns(:agent).should have(1).error_on(:type)
  60. sign_in users(:bob)
  61. expect {
  62. post :create, :agent => valid_attributes(:type => "Object")
  63. }.not_to change { users(:bob).agents.count }
  64. assigns(:agent).should be_a(Agent)
  65. assigns(:agent).should have(1).error_on(:type)
  66. sign_in users(:bob)
  67. expect {
  68. post :create, :agent => valid_attributes(:type => "Agent")
  69. }.not_to change { users(:bob).agents.count }
  70. assigns(:agent).should be_a(Agent)
  71. assigns(:agent).should have(1).error_on(:type)
  72. expect {
  73. post :create, :agent => valid_attributes(:type => "User")
  74. }.not_to change { users(:bob).agents.count }
  75. assigns(:agent).should be_a(Agent)
  76. assigns(:agent).should have(1).error_on(:type)
  77. end
  78. it "creates Agents for the current user" do
  79. sign_in users(:bob)
  80. expect {
  81. expect {
  82. post :create, :agent => valid_attributes
  83. }.to change { users(:bob).agents.count }.by(1)
  84. }.to change { Link.count }.by(1)
  85. assigns(:agent).should be_a(Agents::WebsiteAgent)
  86. end
  87. it "shows errors" do
  88. sign_in users(:bob)
  89. expect {
  90. post :create, :agent => valid_attributes(:name => "")
  91. }.not_to change { users(:bob).agents.count }
  92. assigns(:agent).should have(1).errors_on(:name)
  93. response.should render_template("new")
  94. end
  95. it "will not accept Agent sources owned by other users" do
  96. sign_in users(:bob)
  97. expect {
  98. expect {
  99. post :create, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])
  100. }.not_to change { users(:bob).agents.count }
  101. }.not_to change { Link.count }
  102. end
  103. end
  104. describe "PUT update" do
  105. it "does not allow changing types" do
  106. sign_in users(:bob)
  107. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:type => "Agents::WeatherAgent")
  108. assigns(:agent).should have(1).errors_on(:type)
  109. response.should render_template("edit")
  110. end
  111. it "updates attributes on Agents for the current user" do
  112. sign_in users(:bob)
  113. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  114. response.should redirect_to(agents_path)
  115. agents(:bob_website_agent).reload.name.should == "New name"
  116. lambda {
  117. post :update, :id => agents(:jane_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  118. }.should raise_error(ActiveRecord::RecordNotFound)
  119. end
  120. it "will not accept Agent sources owned by other users" do
  121. sign_in users(:bob)
  122. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])
  123. assigns(:agent).should have(1).errors_on(:sources)
  124. end
  125. it "shows errors" do
  126. sign_in users(:bob)
  127. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "")
  128. assigns(:agent).should have(1).errors_on(:name)
  129. response.should render_template("edit")
  130. end
  131. end
  132. describe "DELETE destroy" do
  133. it "destroys only Agents owned by the current user" do
  134. sign_in users(:bob)
  135. expect {
  136. delete :destroy, :id => agents(:bob_website_agent).to_param
  137. }.to change(Agent, :count).by(-1)
  138. lambda {
  139. delete :destroy, :id => agents(:jane_website_agent).to_param
  140. }.should raise_error(ActiveRecord::RecordNotFound)
  141. end
  142. end
  143. end