agents_controller_spec.rb 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.to_json,
  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 "GET show" do
  19. it "only shows Agents for the current user" do
  20. sign_in users(:bob)
  21. get :show, :id => agents(:bob_website_agent).to_param
  22. assigns(:agent).should eq(agents(:bob_website_agent))
  23. lambda {
  24. get :show, :id => agents(:jane_website_agent).to_param
  25. }.should raise_error(ActiveRecord::RecordNotFound)
  26. end
  27. end
  28. describe "GET edit" do
  29. it "only shows Agents for the current user" do
  30. sign_in users(:bob)
  31. get :edit, :id => agents(:bob_website_agent).to_param
  32. assigns(:agent).should eq(agents(:bob_website_agent))
  33. lambda {
  34. get :edit, :id => agents(:jane_website_agent).to_param
  35. }.should raise_error(ActiveRecord::RecordNotFound)
  36. end
  37. end
  38. describe "POST create" do
  39. it "errors on bad types" do
  40. sign_in users(:bob)
  41. expect {
  42. post :create, :agent => valid_attributes(:type => "Agents::ThisIsFake")
  43. }.not_to change { users(:bob).agents.count }
  44. assigns(:agent).should be_a(Agent)
  45. assigns(:agent).should have(1).error_on(:type)
  46. sign_in users(:bob)
  47. expect {
  48. post :create, :agent => valid_attributes(:type => "Object")
  49. }.not_to change { users(:bob).agents.count }
  50. assigns(:agent).should be_a(Agent)
  51. assigns(:agent).should have(1).error_on(:type)
  52. sign_in users(:bob)
  53. expect {
  54. post :create, :agent => valid_attributes(:type => "Agent")
  55. }.not_to change { users(:bob).agents.count }
  56. assigns(:agent).should be_a(Agent)
  57. assigns(:agent).should have(1).error_on(:type)
  58. expect {
  59. post :create, :agent => valid_attributes(:type => "User")
  60. }.not_to change { users(:bob).agents.count }
  61. assigns(:agent).should be_a(Agent)
  62. assigns(:agent).should have(1).error_on(:type)
  63. end
  64. it "creates Agents for the current user" do
  65. sign_in users(:bob)
  66. expect {
  67. expect {
  68. post :create, :agent => valid_attributes
  69. }.to change { users(:bob).agents.count }.by(1)
  70. }.to change { Link.count }.by(1)
  71. assigns(:agent).should be_a(Agents::WebsiteAgent)
  72. end
  73. it "shows errors" do
  74. sign_in users(:bob)
  75. expect {
  76. post :create, :agent => valid_attributes(:name => "")
  77. }.not_to change { users(:bob).agents.count }
  78. assigns(:agent).should have(1).errors_on(:name)
  79. response.should render_template("new")
  80. end
  81. it "will not accept Agent sources owned by other users" do
  82. sign_in users(:bob)
  83. expect {
  84. expect {
  85. post :create, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])
  86. }.not_to change { users(:bob).agents.count }
  87. }.not_to change { Link.count }
  88. end
  89. end
  90. describe "PUT update" do
  91. it "does not allow changing types" do
  92. sign_in users(:bob)
  93. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:type => "Agents::WeatherAgent")
  94. assigns(:agent).should have(1).errors_on(:type)
  95. response.should render_template("edit")
  96. end
  97. it "updates attributes on Agents for the current user" do
  98. sign_in users(:bob)
  99. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  100. response.should redirect_to(agents_path)
  101. agents(:bob_website_agent).reload.name.should == "New name"
  102. lambda {
  103. post :update, :id => agents(:jane_website_agent).to_param, :agent => valid_attributes(:name => "New name")
  104. }.should raise_error(ActiveRecord::RecordNotFound)
  105. end
  106. it "will not accept Agent sources owned by other users" do
  107. sign_in users(:bob)
  108. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:source_ids => [agents(:jane_weather_agent).id])
  109. assigns(:agent).should have(1).errors_on(:sources)
  110. end
  111. it "shows errors" do
  112. sign_in users(:bob)
  113. post :update, :id => agents(:bob_website_agent).to_param, :agent => valid_attributes(:name => "")
  114. assigns(:agent).should have(1).errors_on(:name)
  115. response.should render_template("edit")
  116. end
  117. end
  118. describe "DELETE destroy" do
  119. it "destroys only Agents owned by the current user" do
  120. sign_in users(:bob)
  121. expect {
  122. delete :destroy, :id => agents(:bob_website_agent).to_param
  123. }.to change(Agent, :count).by(-1)
  124. lambda {
  125. delete :destroy, :id => agents(:jane_website_agent).to_param
  126. }.should raise_error(ActiveRecord::RecordNotFound)
  127. end
  128. end
  129. end