calendar_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Calendars', type: :request do
  4. let(:admin) do
  5. create(:admin)
  6. end
  7. describe 'request handling' do
  8. it 'does calendar index with nobody' do
  9. get '/api/v1/calendars', as: :json
  10. expect(response).to have_http_status(:forbidden)
  11. expect(json_response).to be_a(Hash)
  12. expect(json_response['error']).to eq('Authentication required')
  13. get '/api/v1/calendars_init', as: :json
  14. expect(response).to have_http_status(:forbidden)
  15. expect(json_response).to be_a(Hash)
  16. expect(json_response['error']).to eq('Authentication required')
  17. get '/api/v1/calendars/timezones', as: :json
  18. expect(response).to have_http_status(:forbidden)
  19. expect(json_response).to be_a(Hash)
  20. expect(json_response['error']).to eq('Authentication required')
  21. end
  22. it 'does calendar index with admin' do
  23. authenticated_as(admin)
  24. get '/api/v1/calendars', as: :json
  25. expect(response).to have_http_status(:ok)
  26. expect(json_response).to be_a(Array)
  27. expect(json_response).to be_truthy
  28. expect(json_response.count).to eq(1)
  29. get '/api/v1/calendars?expand=true', as: :json
  30. expect(response).to have_http_status(:ok)
  31. expect(json_response).to be_a(Array)
  32. expect(json_response).to be_truthy
  33. expect(json_response.count).to eq(1)
  34. get '/api/v1/calendars?full=true', as: :json
  35. expect(response).to have_http_status(:ok)
  36. expect(json_response).to be_a(Hash)
  37. expect(json_response).to be_truthy
  38. expect(json_response['record_ids']).to be_truthy
  39. expect(json_response['record_ids'].count).to eq(1)
  40. expect(json_response['assets']).to be_truthy
  41. expect(json_response['assets']).to be_present
  42. # index
  43. get '/api/v1/calendars_init', as: :json
  44. expect(response).to have_http_status(:ok)
  45. expect(json_response).to be_a(Hash)
  46. expect(json_response['record_ids']).to be_truthy
  47. expect(json_response['ical_feeds']).to be_truthy
  48. expect(json_response['ical_feeds']['https://www.google.com/calendar/ical/da.danish%23holiday%40group.v.calendar.google.com/public/basic.ics']).to eq('Denmark')
  49. expect(json_response['ical_feeds']['https://www.google.com/calendar/ical/de.austrian%23holiday%40group.v.calendar.google.com/public/basic.ics']).to eq('Austria')
  50. expect(json_response['timezones']).to be_truthy
  51. expect(json_response['timezones']['Africa/Johannesburg']).to eq(2)
  52. expect(json_response['timezones']['America/Sitka']).to be_between(-9, -8)
  53. expect(json_response['timezones']['Europe/Berlin']).to be_between(1, 2)
  54. expect(json_response['assets']).to be_truthy
  55. # timezones
  56. get '/api/v1/calendars/timezones', as: :json
  57. expect(response).to have_http_status(:ok)
  58. expect(json_response).to be_a(Hash)
  59. expect(json_response['timezones']).to be_a(Hash)
  60. expect(json_response['timezones']['America/New_York']).to be_truthy
  61. end
  62. end
  63. describe 'Removing calendars via UI and API does not check for references #3845', authenticated_as: -> { user } do
  64. let(:calendar) { create(:calendar) }
  65. let(:sla) { create(:sla, calendar: calendar) }
  66. let(:user) { create(:admin) }
  67. before do
  68. sla
  69. end
  70. it 'does return reference error on delete if related objects exist' do
  71. delete "/api/v1/calendars/#{calendar.id}", params: {}, as: :json
  72. expect(json_response['error']).to eq("Can't delete, object has references.")
  73. end
  74. end
  75. context 'when fetching timezones' do
  76. shared_examples 'returns a list of timezones' do
  77. it 'returns a list of timezones' do
  78. authenticated_as(user)
  79. get '/api/v1/calendars/timezones', as: :json
  80. expect(response).to have_http_status(:ok)
  81. expect(json_response).to be_a(Hash)
  82. expect(json_response['timezones']).to be_a(Hash)
  83. expect(json_response['timezones']['America/New_York']).to be_truthy
  84. end
  85. end
  86. shared_examples 'returns an error' do
  87. it 'returns an error' do
  88. authenticated_as(user)
  89. get '/api/v1/calendars/timezones', as: :json
  90. expect(response).to have_http_status(:forbidden)
  91. end
  92. end
  93. context 'when user is an agent' do
  94. let(:user) { create(:agent) }
  95. it_behaves_like 'returns an error'
  96. end
  97. context 'when user is an admin' do
  98. let(:user) { create(:admin_only, roles: []) }
  99. # https://github.com/zammad/zammad/issues/5196
  100. context 'with specific permissions' do
  101. before do
  102. user.roles << create(:role, permissions: [permission])
  103. user.save!
  104. end
  105. context 'with admin permission' do
  106. let(:permission) { Permission.find_by(name: 'admin') }
  107. it_behaves_like 'returns a list of timezones'
  108. end
  109. context 'with admin.trigger permission' do
  110. let(:permission) { Permission.find_by(name: 'admin.trigger') }
  111. it_behaves_like 'returns a list of timezones'
  112. end
  113. context 'with admin.calendar permission' do
  114. let(:permission) { Permission.find_by(name: 'admin.calendar') }
  115. it_behaves_like 'returns a list of timezones'
  116. end
  117. context 'with admin.scheduler permission' do
  118. let(:permission) { Permission.find_by(name: 'admin.scheduler') }
  119. it_behaves_like 'returns a list of timezones'
  120. end
  121. context 'with admin.webhook permission' do
  122. let(:permission) { Permission.find_by(name: 'admin.webhook') }
  123. it_behaves_like 'returns an error'
  124. end
  125. end
  126. end
  127. end
  128. end