user_credentials_controller_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. require 'rails_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. @file = fixture_file_upload('user_credentials.json')
  12. end
  13. describe "GET index" do
  14. it "only returns UserCredentials for the current user" do
  15. get :index
  16. expect(assigns(:user_credentials).all? {|i| expect(i.user).to eq(users(:bob)) }).to be_truthy
  17. end
  18. end
  19. describe "GET edit" do
  20. it "only shows UserCredentials for the current user" do
  21. get :edit, params: {:id => user_credentials(:bob_aws_secret).to_param}
  22. expect(assigns(:user_credential)).to eq(user_credentials(:bob_aws_secret))
  23. expect {
  24. get :edit, params: {:id => user_credentials(:jane_aws_secret).to_param}
  25. }.to raise_error(ActiveRecord::RecordNotFound)
  26. end
  27. end
  28. describe "Post import" do
  29. it "asserts user credentials were created for current user only" do
  30. post :import, params: {:file => @file}
  31. expect(controller.current_user.id).to eq(users(:bob).id)
  32. expect(controller.current_user.user_credentials).to eq(users(:bob).user_credentials)
  33. end
  34. it "asserts that primary id in json file is ignored" do
  35. post :import, params: {:file => @file}
  36. expect(controller.current_user.user_credentials.last.id).not_to eq(24)
  37. end
  38. it "duplicate credential name shows an error that it is not saved" do
  39. file1 = fixture_file_upload('multiple_user_credentials.json')
  40. post :import, params: {:file => file1}
  41. expect(flash[:notice]).to eq("One or more of the uploaded credentials was not imported due to an error. Perhaps an existing credential had the same name?")
  42. expect(response).to redirect_to(user_credentials_path)
  43. end
  44. end
  45. describe "POST create" do
  46. it "creates UserCredentials for the current user" do
  47. expect {
  48. post :create, params: {:user_credential => valid_attributes}
  49. }.to change { users(:bob).user_credentials.count }.by(1)
  50. end
  51. it "shows errors" do
  52. expect {
  53. post :create, params: {:user_credential => valid_attributes(:credential_name => "")}
  54. }.not_to change { users(:bob).user_credentials.count }
  55. expect(assigns(:user_credential)).to have(1).errors_on(:credential_name)
  56. expect(response).to render_template("new")
  57. end
  58. it "will not create UserCredentials for other users" do
  59. expect {
  60. post :create, params: {:user_credential => valid_attributes(:user_id => users(:jane).id)}
  61. }.to raise_error(ActionController::UnpermittedParameters)
  62. end
  63. end
  64. describe "PUT update" do
  65. it "updates attributes on UserCredentials for the current user" do
  66. post :update, params: {:id => user_credentials(:bob_aws_key).to_param, :user_credential => { :credential_name => "new_name" }}
  67. expect(response).to redirect_to(user_credentials_path)
  68. expect(user_credentials(:bob_aws_key).reload.credential_name).to eq("new_name")
  69. expect {
  70. post :update, params: {:id => user_credentials(:jane_aws_key).to_param, :user_credential => { :credential_name => "new_name" }}
  71. }.to raise_error(ActiveRecord::RecordNotFound)
  72. expect(user_credentials(:jane_aws_key).reload.credential_name).not_to eq("new_name")
  73. end
  74. it "shows errors" do
  75. post :update, params: {:id => user_credentials(:bob_aws_key).to_param, :user_credential => { :credential_name => "" }}
  76. expect(assigns(:user_credential)).to have(1).errors_on(:credential_name)
  77. expect(response).to render_template("edit")
  78. end
  79. end
  80. describe "DELETE destroy" do
  81. it "destroys only UserCredentials owned by the current user" do
  82. expect {
  83. delete :destroy, params: {:id => user_credentials(:bob_aws_key).to_param}
  84. }.to change(UserCredential, :count).by(-1)
  85. expect {
  86. delete :destroy, params: {:id => user_credentials(:jane_aws_key).to_param}
  87. }.to raise_error(ActiveRecord::RecordNotFound)
  88. end
  89. end
  90. end