permissions_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Knowledge Base Locale Knowledge Base Permissions', type: :system do
  4. include_context 'basic Knowledge Base'
  5. let(:role_editor) { Role.find_by name: 'Admin' }
  6. let(:role_another_editor) { create(:role, permission_names: %w[knowledge_base.editor]) }
  7. let(:role_reader) { Role.find_by name: 'Agent' }
  8. it 'shows roles with has KB permissions only' do
  9. open_page
  10. in_modal do
  11. expect(page)
  12. .to have_text(%r{Admin}i)
  13. .and(have_text(%r{Agent}i))
  14. .and(have_no_text(%r{Customer}i))
  15. end
  16. end
  17. describe 'permissions shown' do
  18. it 'shows existing permissions when KB has no permissions' do
  19. open_page
  20. in_modal do
  21. expect(page)
  22. .to have_css("input[name='#{role_editor.id}'][value='editor'][checked]:not([disabled])", visible: :all)
  23. .and(have_css("input[name='#{role_editor.id}'][value='reader']:not([disabled])", visible: :all))
  24. .and(have_css("input[name='#{role_editor.id}'][value='none']:not([disabled])", visible: :all))
  25. end
  26. end
  27. it 'shows existing permissions' do
  28. KnowledgeBase::PermissionsUpdate.new(knowledge_base).update! role_another_editor => 'reader'
  29. open_page
  30. in_modal do
  31. expect(page)
  32. .to have_css("input[name='#{role_another_editor.id}'][value='reader'][checked]:not([disabled])", visible: :all)
  33. .and(have_css("input[name='#{role_another_editor.id}'][value='editor']:not([disabled])", visible: :all))
  34. .and(have_css("input[name='#{role_another_editor.id}'][value='none']:not([disabled])", visible: :all))
  35. end
  36. end
  37. it 'shows reader permissions limited by role itself' do
  38. open_page
  39. in_modal do
  40. expect(page)
  41. .to have_css("input[name='#{role_reader.id}'][value='none']:not([disabled])", visible: :all)
  42. .and(have_css("input[name='#{role_reader.id}'][value='reader'][checked]:not([disabled])", visible: :all))
  43. .and(have_css("input[name='#{role_reader.id}'][value='editor'][disabled]", visible: :all))
  44. end
  45. end
  46. end
  47. describe 'saving changes' do
  48. it 'saves permissions' do
  49. role_another_editor
  50. open_page
  51. in_modal do
  52. find("input[name='#{role_another_editor.id}'][value='reader']", visible: :all)
  53. .ancestor('label')
  54. .click
  55. click_on 'Submit'
  56. end
  57. expect(knowledge_base.reload.permissions)
  58. .to contain_exactly(
  59. have_attributes(role: role_reader, access: 'reader', permissionable: knowledge_base),
  60. have_attributes(role: role_another_editor, access: 'reader', permissionable: knowledge_base),
  61. have_attributes(role: role_editor, access: 'editor', permissionable: knowledge_base)
  62. )
  63. end
  64. it 'allows to modify existing permissions' do
  65. KnowledgeBase::PermissionsUpdate.new(knowledge_base).update! role_another_editor => 'reader'
  66. open_page
  67. in_modal do
  68. find("input[name='#{role_another_editor.id}'][value='editor']", visible: :all)
  69. .ancestor('label')
  70. .click
  71. click_on 'Submit'
  72. end
  73. expect(knowledge_base.reload.permissions)
  74. .to contain_exactly(
  75. have_attributes(role: role_reader, access: 'reader', permissionable: knowledge_base),
  76. have_attributes(role: role_another_editor, access: 'editor', permissionable: knowledge_base),
  77. have_attributes(role: role_editor, access: 'editor', permissionable: knowledge_base)
  78. )
  79. end
  80. it 'does not allow to lock user himself' do
  81. open_page
  82. in_modal do
  83. find("input[name='#{role_editor.id}'][value='reader']", visible: :all)
  84. .ancestor('label')
  85. .click
  86. click_on 'Submit'
  87. expect(page).to have_css('.alert')
  88. end
  89. end
  90. end
  91. def open_page
  92. visit "knowledge_base/#{knowledge_base.id}/locale/#{Locale.first.locale}/edit"
  93. find('[data-action=permissions]').click
  94. end
  95. end