session_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'rails_helper'
  2. RSpec.describe 'Sessions endpoints', type: :request do
  3. describe 'GET /auth/sso (single sign-on)' do
  4. context 'with invalid user login' do
  5. let(:login) { User.pluck(:login).max.next }
  6. context 'in "REMOTE_USER" request env var' do
  7. let(:env) { { 'REMOTE_USER' => login } }
  8. it 'returns unauthorized response' do
  9. get '/auth/sso', as: :json, env: env
  10. expect(response).to have_http_status(:unauthorized)
  11. end
  12. end
  13. context 'in "HTTP_REMOTE_USER" request env var' do
  14. let(:env) { { 'HTTP_REMOTE_USER' => login } }
  15. it 'returns unauthorized response' do
  16. get '/auth/sso', as: :json, env: env
  17. expect(response).to have_http_status(:unauthorized)
  18. end
  19. end
  20. context 'in "X-Forwarded-User" request header' do
  21. let(:headers) { { 'X-Forwarded-User' => login } }
  22. it 'returns unauthorized response' do
  23. get '/auth/sso', as: :json, headers: headers
  24. expect(response).to have_http_status(:unauthorized)
  25. end
  26. end
  27. end
  28. context 'with valid user login' do
  29. let(:user) { User.last }
  30. let(:login) { user.login }
  31. context 'in Maintenance Mode' do
  32. before { Setting.set('maintenance_mode', true) }
  33. context 'in "REMOTE_USER" request env var' do
  34. let(:env) { { 'REMOTE_USER' => login } }
  35. it 'returns 401 unauthorized' do
  36. get '/auth/sso', as: :json, env: env
  37. expect(response).to have_http_status(:unauthorized)
  38. expect(json_response).to include('error' => 'Maintenance mode enabled!')
  39. end
  40. end
  41. context 'in "HTTP_REMOTE_USER" request env var' do
  42. let(:env) { { 'HTTP_REMOTE_USER' => login } }
  43. it 'returns 401 unauthorized' do
  44. get '/auth/sso', as: :json, env: env
  45. expect(response).to have_http_status(:unauthorized)
  46. expect(json_response).to include('error' => 'Maintenance mode enabled!')
  47. end
  48. end
  49. context 'in "X-Forwarded-User" request header' do
  50. let(:headers) { { 'X-Forwarded-User' => login } }
  51. it 'returns 401 unauthorized' do
  52. get '/auth/sso', as: :json, headers: headers
  53. expect(response).to have_http_status(:unauthorized)
  54. expect(json_response).to include('error' => 'Maintenance mode enabled!')
  55. end
  56. end
  57. end
  58. context 'in "REMOTE_USER" request env var' do
  59. let(:env) { { 'REMOTE_USER' => login } }
  60. it 'returns a new user-session response' do
  61. get '/auth/sso', as: :json, env: env
  62. expect(response).to redirect_to('/#')
  63. end
  64. it 'sets the :user_id session parameter' do
  65. expect { get '/auth/sso', as: :json, env: env }
  66. .to change { request&.session&.fetch(:user_id) }.to(user.id)
  67. end
  68. end
  69. context 'in "HTTP_REMOTE_USER" request env var' do
  70. let(:env) { { 'HTTP_REMOTE_USER' => login } }
  71. it 'returns a new user-session response' do
  72. get '/auth/sso', as: :json, env: env
  73. expect(response).to redirect_to('/#')
  74. end
  75. it 'sets the :user_id session parameter' do
  76. expect { get '/auth/sso', as: :json, env: env }
  77. .to change { request&.session&.fetch(:user_id) }.to(user.id)
  78. end
  79. end
  80. context 'in "X-Forwarded-User" request header' do
  81. let(:headers) { { 'X-Forwarded-User' => login } }
  82. it 'returns a new user-session response' do
  83. get '/auth/sso', as: :json, headers: headers
  84. expect(response).to redirect_to('/#')
  85. end
  86. it 'sets the :user_id session parameter on the client' do
  87. expect { get '/auth/sso', as: :json, headers: headers }
  88. .to change { request&.session&.fetch(:user_id) }.to(user.id)
  89. end
  90. end
  91. end
  92. end
  93. end