agents_controller_spec.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 new with :id" do
  43. it "opens a clone of a given Agent" do
  44. sign_in users(:bob)
  45. get :new, :id => agents(:bob_website_agent).to_param
  46. assigns(:agent).attributes.should eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
  47. end
  48. it "only allows the current user to clone his own Agent" do
  49. sign_in users(:bob)
  50. lambda {
  51. get :new, :id => agents(:jane_website_agent).to_param
  52. }.should raise_error(ActiveRecord::RecordNotFound)
  53. end
  54. end
  55. describe "GET edit" do
  56. it "only shows Agents for the current user" do
  57. sign_in users(:bob)
  58. get :edit, :id => agents(:bob_website_agent).to_param
  59. assigns(:agent).should eq(agents(:bob_website_agent))
  60. lambda {
  61. get :edit, :id => agents(:jane_website_agent).to_param
  62. }.should raise_error(ActiveRecord::RecordNotFound)
  63. end
  64. end
  65. describe "POST create" do
  66. it "errors on bad types" do
  67. sign_in users(:bob)
  68. expect {
  69. post :create, :agent => valid_attributes(:type => "Agents::ThisIsFake")
  70. }.not_to change { users(:bob).agents.count }
  71. assigns(:agent).should be_a(Agent)
  72. assigns(:agent).should have(1).error_on(:type)
  73. sign_in users(:bob)
  74. expect {
  75. post :create, :agent => valid_attributes(:type => "Object")
  76. }.not_to change { users(:bob).agents.count }
  77. assigns(:agent).should be_a(Agent)
  78. assigns(:agent).should have(1).error_on(:type)
  79. sign_in users(:bob)
  80. expect {
  81. post :create, :agent => valid_attributes(:type => "Agent")
  82. }.not_to change { users(:bob).agents.count }
  83. assigns(:agent).should be_a(Agent)
  84. assigns(:agent).should have(1).error_on(:type)
  85. expect {
  86. post :create, :agent => valid_attributes(:type => "User")
  87. }.not_to change { users(:bob).agents.count }
  88. assigns(:agent).should be_a(Agent)
  89. assigns(:agent).should have(1).error_on(:type)
  90. end
  91. it "creates Agents for the current user" do
  92. sign_in users(:bob)
  93. expect {
  94. expect {
  95. post :create, :agent => valid_attributes
  96. }.to change { users(:bob).agents.count }.by(1)
  97. }.to change { Link.count }.by(1)
  98. assigns(:agent).should be_a(Agents::WebsiteAgent)
  99. end
  100. it "shows errors" do
  101. sign_in users(:bob)
  102. expect {
  103. post :create, :agent => valid_attributes(:name => "")
  104. }.not_to change { users(:bob).agents.count }
  105. assigns(:agent).should have(1).errors_on(:name)
  106. response.should render_template("new")
  107. end
  108. it "will not accept Agent sources owned by other users" do
  109. sign_in users(:bob)
  110. expect {
  111. expect {
  112. post :create, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])
  113. }.not_to change { users(:bob).agents.count }
  114. }.not_to change { Link.count }
  115. end
  116. end
  117. describe "PUT update" do
  118. it "does not allow changing types" do
  119. sign_in users(:bob)
  120. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:type => "Agents::WeatherAgent")
  121. assigns(:agent).should have(1).errors_on(:type)
  122. response.should render_template("edit")
  123. end
  124. it "updates attributes on Agents for the current user" do
  125. sign_in users(:bob)
  126. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  127. response.should redirect_to(agents_path)
  128. agents(:bob_website_agent).reload.name.should == "New name"
  129. lambda {
  130. post :update, :id => agents(:jane_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  131. }.should raise_error(ActiveRecord::RecordNotFound)
  132. end
  133. it "will not accept Agent sources owned by other users" do
  134. sign_in users(:bob)
  135. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])
  136. assigns(:agent).should have(1).errors_on(:sources)
  137. end
  138. it "shows errors" do
  139. sign_in users(:bob)
  140. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "")
  141. assigns(:agent).should have(1).errors_on(:name)
  142. response.should render_template("edit")
  143. end
  144. end
  145. describe "DELETE destroy" do
  146. it "destroys only Agents owned by the current user" do
  147. sign_in users(:bob)
  148. expect {
  149. delete :destroy, :id => agents(:bob_website_agent).to_param
  150. }.to change(Agent, :count).by(-1)
  151. lambda {
  152. delete :destroy, :id => agents(:jane_website_agent).to_param
  153. }.should raise_error(ActiveRecord::RecordNotFound)
  154. end
  155. end
  156. end