calendar_subscriptions_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'iCal endpoints', type: :request do
  4. context 'with no existing session' do
  5. it 'gives HTTP Basic auth prompt (#3064)' do
  6. get '/ical/tickets'
  7. expect(response.body).to eq("HTTP Basic: Access denied.\n")
  8. end
  9. end
  10. context 'with basic auth as agent' do
  11. let(:password) { Faker::Internet.password }
  12. let(:user) { create(:agent, password: password) }
  13. let(:basic_auth) { ActionController::HttpAuthentication::Basic.encode_credentials(user.email, password) }
  14. context 'when two-factor auth is disabled' do
  15. it 'returns 200 OK' do
  16. get '/ical/tickets', headers: { 'Authorization' => basic_auth }
  17. expect(response).to have_http_status(:ok)
  18. end
  19. end
  20. context 'when two-factor auth is enabled' do
  21. before do
  22. Setting.set('two_factor_authentication_enforce_role_ids', [])
  23. Setting.set('two_factor_authentication_method_authenticator_app', true)
  24. create(:user_two_factor_preference, :authenticator_app, user: user)
  25. end
  26. it 'returns 200 OK' do
  27. get '/ical/tickets', headers: { 'Authorization' => basic_auth }
  28. expect(response).to have_http_status(:ok)
  29. end
  30. end
  31. end
  32. describe 'time zone', authenticated_as: :user do
  33. let(:group) { create(:group) }
  34. let(:user) { create(:agent) }
  35. before do
  36. user.groups << group
  37. create(:ticket, group: group, owner: user, state_name: 'open', pending_time: 1.day.ago)
  38. end
  39. it 'returns zero offset time if no time zone set' do
  40. get '/ical/tickets'
  41. expect(response.body).to match %r{DTSTART:\d{8}T0{6}Z}
  42. end
  43. it 'returns selected time zone' do
  44. Setting.set 'timezone_default', 'Europe/Vilnius'
  45. get '/ical/tickets'
  46. expect(response.body).to match %r{DTSTART;TZID=Europe/Vilnius:\d{8}T0{6}}
  47. end
  48. end
  49. # https://github.com/zammad/zammad/issues/3962
  50. context 'with request method PROPFIND', authenticated_as: :user do
  51. let(:user) { create(:agent) }
  52. it 'contains correct request method' do
  53. get '/ical/tickets', headers: { 'REQUEST_METHOD' => 'PROPFIND' }
  54. expect(response.request.request_method).to eq('PROPFIND')
  55. end
  56. it 'returns the desired calendar file' do
  57. get '/ical/tickets', headers: { 'REQUEST_METHOD' => 'PROPFIND' }
  58. expect(response.body).to match(%r{BEGIN:VCALENDAR})
  59. end
  60. end
  61. describe 'methods', authenticated_as: :user do
  62. let(:group) { create(:group) }
  63. let(:user) { create(:agent) }
  64. let(:ticket_1) { create(:ticket, title: SecureRandom.uuid, group: group, owner: user, state_name: 'open') }
  65. let(:ticket_2) { create(:ticket, title: SecureRandom.uuid, group: group, owner: user, state_name: 'pending reminder', pending_time: 1.day.from_now) }
  66. before do
  67. user.groups << group
  68. ticket_1
  69. ticket_2
  70. end
  71. it 'returns open tickets', :aggregate_failures do
  72. get '/ical/tickets/new_open'
  73. expect(response.body).to include(ticket_1.title)
  74. expect(response.body).not_to include(ticket_2.title)
  75. end
  76. it 'returns pending tickets', :aggregate_failures do
  77. get '/ical/tickets/pending'
  78. expect(response.body).not_to include(ticket_1.title)
  79. expect(response.body).to include(ticket_2.title)
  80. end
  81. it 'raises error on unknown method', :aggregate_failures do
  82. get '/ical/tickets/xxx'
  83. expect(json_response['error']).to eq('An unknown method name was requested.')
  84. expect(response).to have_http_status(:unprocessable_entity)
  85. end
  86. end
  87. end