users_controller_spec.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. require 'rails_helper'
  2. describe Admin::UsersController do
  3. describe 'POST #create' do
  4. context 'with valid user params' do
  5. it 'imports the default scenario for the new user' do
  6. mock(DefaultScenarioImporter).import(is_a(User))
  7. sign_in users(:jane)
  8. post :create, params: {:user => {username: 'jdoe', email: 'jdoe@example.com',
  9. password: 's3cr3t55', password_confirmation: 's3cr3t55', admin: false }}
  10. end
  11. end
  12. context 'with invalid user params' do
  13. it 'does not import the default scenario' do
  14. stub(DefaultScenarioImporter).import(is_a(User)) { fail "Should not attempt import" }
  15. sign_in users(:jane)
  16. post :create, params: {:user => {username: 'user'}}
  17. end
  18. end
  19. end
  20. describe 'GET #switch_to_user' do
  21. it "switches to another user" do
  22. sign_in users(:jane)
  23. get :switch_to_user, params: {:id => users(:bob).id}
  24. expect(response).to redirect_to(agents_path)
  25. expect(subject.session[:original_admin_user_id]).to eq(users(:jane).id)
  26. end
  27. it "does not switch if not admin" do
  28. sign_in users(:bob)
  29. get :switch_to_user, params: {:id => users(:jane).id}
  30. expect(response).to redirect_to(root_path)
  31. end
  32. end
  33. describe 'GET #switch_back' do
  34. it "switches to another user and back" do
  35. sign_in users(:jane)
  36. get :switch_to_user, params: {:id => users(:bob).id}
  37. expect(response).to redirect_to(agents_path)
  38. expect(subject.session[:original_admin_user_id]).to eq(users(:jane).id)
  39. get :switch_back
  40. expect(response).to redirect_to(admin_users_path)
  41. expect(subject.session[:original_admin_user_id]).to be_nil
  42. end
  43. it "does not switch_back without having switched" do
  44. sign_in users(:bob)
  45. get :switch_back
  46. expect(response).to redirect_to(root_path)
  47. end
  48. end
  49. end