You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ApplicationController, type: :controller do
  4. controller do
  5. include Localized
  6. def success
  7. head 200
  8. end
  9. end
  10. before do
  11. routes.draw { get 'success' => 'anonymous#success' }
  12. end
  13. shared_examples 'default locale' do
  14. context 'when DEFAULT_LOCALE environment variable is set' do
  15. around do |example|
  16. ClimateControl.modify 'DEFAULT_LOCALE' => 'ca', &example.method(:run)
  17. I18n.locale = I18n.default_locale
  18. end
  19. it 'sets language specified by ENV if preferred' do
  20. request.headers['Accept-Language'] = 'ca, fa'
  21. get 'success'
  22. expect(I18n.locale).to eq :ca
  23. end
  24. it 'sets available and preferred language if language specified by ENV is not preferred' do
  25. request.headers['Accept-Language'] = 'ca-ES, fa'
  26. get 'success'
  27. expect(I18n.locale).to eq :fa
  28. end
  29. it 'sets language specified by ENV if it is compatible and none of available languages are preferred' do
  30. request.headers['Accept-Language'] = 'ca-ES, fa-IR'
  31. get 'success'
  32. expect(I18n.locale).to eq :ca
  33. end
  34. it 'sets available and compatible langauge if language specified by ENV is not compatible none of available languages are preferred' do
  35. request.headers['Accept-Language'] = 'fa-IR'
  36. get 'success'
  37. expect(I18n.locale).to eq :fa
  38. end
  39. it 'sets language specified by ENV if none of available languages are compatible' do
  40. request.headers['Accept-Language'] = ''
  41. get 'success'
  42. expect(I18n.locale).to eq :ca
  43. end
  44. end
  45. context 'when DEFAULT_LOCALE environment variable is not set' do
  46. it 'sets default locale if none of available languages are compatible' do
  47. request.headers['Accept-Language'] = ''
  48. get 'success'
  49. expect(I18n.locale).to eq :en
  50. end
  51. end
  52. end
  53. context 'user with valid locale has signed in' do
  54. it "sets user's locale" do
  55. user = Fabricate(:user, locale: :ca)
  56. sign_in(user)
  57. get 'success'
  58. expect(I18n.locale).to eq :ca
  59. end
  60. end
  61. context 'user with invalid locale has signed in' do
  62. before do
  63. user = Fabricate.build(:user, locale: :invalid)
  64. user.save!(validate: false)
  65. sign_in(user)
  66. end
  67. include_examples 'default locale'
  68. end
  69. context 'user has not signed in' do
  70. include_examples 'default locale'
  71. end
  72. end