user_credentials_controller_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. require 'spec_helper'
  2. describe UserCredentialsController do
  3. def valid_attributes(options = {})
  4. {
  5. :credential_name => "some_name",
  6. :credential_value => "some_value"
  7. }.merge(options)
  8. end
  9. before do
  10. sign_in users(:bob)
  11. end
  12. describe "GET index" do
  13. it "only returns UserCredentials for the current user" do
  14. get :index
  15. expect(assigns(:user_credentials).all? {|i| expect(i.user).to eq(users(:bob)) }).to be_truthy
  16. end
  17. end
  18. describe "GET edit" do
  19. it "only shows UserCredentials for the current user" do
  20. get :edit, :id => user_credentials(:bob_aws_secret).to_param
  21. expect(assigns(:user_credential)).to eq(user_credentials(:bob_aws_secret))
  22. expect {
  23. get :edit, :id => user_credentials(:jane_aws_secret).to_param
  24. }.to raise_error(ActiveRecord::RecordNotFound)
  25. end
  26. end
  27. describe "POST create" do
  28. it "creates UserCredentials for the current user" do
  29. expect {
  30. post :create, :user_credential => valid_attributes
  31. }.to change { users(:bob).user_credentials.count }.by(1)
  32. end
  33. it "shows errors" do
  34. expect {
  35. post :create, :user_credential => valid_attributes(:credential_name => "")
  36. }.not_to change { users(:bob).user_credentials.count }
  37. expect(assigns(:user_credential)).to have(1).errors_on(:credential_name)
  38. expect(response).to render_template("new")
  39. end
  40. it "will not create UserCredentials for other users" do
  41. expect {
  42. post :create, :user_credential => valid_attributes(:user_id => users(:jane).id)
  43. }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
  44. end
  45. end
  46. describe "PUT update" do
  47. it "updates attributes on UserCredentials for the current user" do
  48. post :update, :id => user_credentials(:bob_aws_key).to_param, :user_credential => { :credential_name => "new_name" }
  49. expect(response).to redirect_to(user_credentials_path)
  50. expect(user_credentials(:bob_aws_key).reload.credential_name).to eq("new_name")
  51. expect {
  52. post :update, :id => user_credentials(:jane_aws_key).to_param, :user_credential => { :credential_name => "new_name" }
  53. }.to raise_error(ActiveRecord::RecordNotFound)
  54. expect(user_credentials(:jane_aws_key).reload.credential_name).not_to eq("new_name")
  55. end
  56. it "shows errors" do
  57. post :update, :id => user_credentials(:bob_aws_key).to_param, :user_credential => { :credential_name => "" }
  58. expect(assigns(:user_credential)).to have(1).errors_on(:credential_name)
  59. expect(response).to render_template("edit")
  60. end
  61. end
  62. describe "DELETE destroy" do
  63. it "destroys only UserCredentials owned by the current user" do
  64. expect {
  65. delete :destroy, :id => user_credentials(:bob_aws_key).to_param
  66. }.to change(UserCredential, :count).by(-1)
  67. expect {
  68. delete :destroy, :id => user_credentials(:jane_aws_key).to_param
  69. }.to raise_error(ActiveRecord::RecordNotFound)
  70. end
  71. end
  72. end