preferences_token_access_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Profile > Token Access', type: :system do
  4. let(:name) { 'Some App Token' }
  5. let(:checkbox_input) { 'input[value="ticket.agent"]' }
  6. let(:expiry_date) { '05/15/2024' }
  7. let(:token_list) { find('.js-tokenList') }
  8. shared_examples 'having an error notification message' do
  9. it 'has error notification message' do
  10. within '#notify' do
  11. noty_message = find('.noty_message', visible: :all)
  12. expect(noty_message).to have_text(error_message)
  13. end
  14. end
  15. end
  16. context 'with valid fields' do
  17. before do
  18. visit 'profile/token_access'
  19. within :active_content do
  20. find('.js-create').click
  21. end
  22. # modal closes but it is swiftly replaced by another modal
  23. in_modal disappears: false do
  24. fill_in 'Name', with: name
  25. checkbox = find(checkbox_input, visible: :all)
  26. checkbox.check allow_label_click: true
  27. find('.js-datepicker').fill_in with: expiry_date
  28. send_keys(:tab)
  29. click_on 'Create'
  30. end
  31. end
  32. context 'with expire date' do
  33. it 'generates a new personal token' do
  34. in_modal do
  35. expect(page).to have_css('.form-control.input.js-select')
  36. .and have_text('Your New Personal Access Token')
  37. end
  38. end
  39. it 'shows active report profile' do
  40. in_modal do
  41. click_on "OK, I've copied my token"
  42. end
  43. within :active_content do
  44. expect(token_list).to have_text(name)
  45. .and have_text(expiry_date)
  46. end
  47. end
  48. end
  49. context 'without expire date' do
  50. let(:expiry_date) { nil }
  51. it 'generates a new personal token' do
  52. in_modal do
  53. expect(page).to have_css('.form-control.input.js-select')
  54. .and have_text('Your New Personal Access Token')
  55. end
  56. end
  57. it 'shows active report profile' do
  58. in_modal do
  59. click_on "OK, I've copied my token"
  60. end
  61. within :active_content do
  62. expect(token_list).to have_text(name)
  63. end
  64. end
  65. end
  66. end
  67. context 'with invalid fields' do
  68. before do
  69. visit 'profile/token_access'
  70. within :active_content do
  71. find('.content.active .js-create').click
  72. end
  73. in_modal disappears: false do
  74. fill_in 'name', with: name
  75. send_keys(:tab)
  76. end
  77. end
  78. context 'without name' do
  79. let(:name) { nil }
  80. let(:error_message) { "The required parameter 'name' is missing." }
  81. before do
  82. in_modal disappears: false do
  83. checkbox = find(checkbox_input, visible: :all)
  84. checkbox.check allow_label_click: true
  85. click_on 'Create'
  86. end
  87. end
  88. it_behaves_like 'having an error notification message'
  89. end
  90. context 'without permission' do
  91. let(:name) { nil }
  92. let(:error_message) { "The required parameter 'permission' is missing." }
  93. before { click_on 'Create' }
  94. it_behaves_like 'having an error notification message'
  95. end
  96. end
  97. context 'with already created token', authenticated_as: :admin_user do
  98. let(:admin_user) { create(:admin) }
  99. let(:create_token) do
  100. create(:api_token,
  101. user: admin_user,
  102. name: name,
  103. preferences: { permission: %w[admin ticket.agent] })
  104. end
  105. before do
  106. create_token
  107. visit 'profile/token_access'
  108. end
  109. it 'shows the created token' do
  110. expect(token_list).to have_text(name)
  111. end
  112. it 'deletes created token' do
  113. token_delete_button = find('.js-tokenList tr .js-delete')
  114. token_delete_button.click
  115. in_modal do
  116. click_on 'Yes'
  117. end
  118. expect(token_list).to have_no_text(name)
  119. end
  120. end
  121. end