current_user_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::CurrentUser, type: :graphql do
  4. context 'when fetching user information' do
  5. let(:organization) { create(:organization) }
  6. let(:secondary_orgs) { create_list(:organization, 2) }
  7. let(:agent) { create(:agent, department: 'TestDepartment', organization: organization, organizations: secondary_orgs) }
  8. let(:query) do
  9. <<~QUERY
  10. query currentUser {
  11. currentUser {
  12. id
  13. firstname
  14. lastname
  15. fullname
  16. objectAttributeValues {
  17. attribute {
  18. name
  19. }
  20. value
  21. renderedLink
  22. }
  23. organization {
  24. name
  25. }
  26. secondaryOrganizations {
  27. edges {
  28. node {
  29. name
  30. }
  31. }
  32. }
  33. permissions {
  34. names
  35. }
  36. createdBy {
  37. firstname
  38. }
  39. updatedBy {
  40. firstname
  41. }
  42. policy {
  43. update
  44. destroy
  45. }
  46. }
  47. }
  48. QUERY
  49. end
  50. before do
  51. gql.execute(query)
  52. end
  53. context 'with authenticated session', authenticated_as: :agent do
  54. it 'has data' do
  55. expect(gql.result.data).to include('fullname' => agent.fullname, 'policy' => { 'update' => false, 'destroy' => false })
  56. end
  57. context 'when admin' do
  58. let(:agent) { create(:admin) }
  59. it 'has policy field data' do
  60. expect(gql.result.data).to include('policy' => { 'update' => true, 'destroy' => true })
  61. end
  62. end
  63. it 'has objectAttributeValue data for User' do
  64. oas = gql.result.data['objectAttributeValues']
  65. expect(oas.find { |oa| oa['attribute']['name'].eql?('department') }).to include('value' => 'TestDepartment', 'renderedLink' => nil)
  66. end
  67. context 'with custom object attribute with linktemplate', db_strategy: :reset do
  68. let(:object_attribute) do
  69. screens = { create: { 'admin.organization': { shown: true, required: false } } }
  70. create(:object_manager_attribute_text, name: 'UserLink', object_name: 'User', screens: screens).tap do |oa|
  71. oa.data_option['linktemplate'] = 'http://test?user=#{user.fullname};missing=#{missing.nonexisting}' # rubocop:disable Lint/InterpolationCheck
  72. oa.save!
  73. ObjectManager::Attribute.migration_execute
  74. end
  75. end
  76. let(:organization) do
  77. object_attribute
  78. create(:organization)
  79. end
  80. # Space in fullname must be encoded as %20, missing values must be '-'.
  81. let(:encoded_fullname) { ERB::Util.url_encode(agent.fullname) }
  82. let(:rendered_link) { "http://test?user=#{encoded_fullname};missing=-" }
  83. it 'has rendered and URL encoded objectAttributeValue data for User' do
  84. oas = gql.result.data['objectAttributeValues']
  85. expect(oas.find { |oa| oa['attribute']['name'].eql?('UserLink') }).to include('value' => '', 'renderedLink' => rendered_link)
  86. end
  87. end
  88. it 'has data for primary and secondary organizations', :aggregate_failures do
  89. expect(gql.result.data['organization']).to include('name' => organization.name)
  90. expect(gql.result.nodes('secondaryOrganizations')).to eq(secondary_orgs.map { |o| { 'name' => o.name } })
  91. end
  92. it 'has permission data' do
  93. expect(gql.result.data['permissions']['names']).to eq(agent.permissions_with_child_names)
  94. end
  95. it 'has updatedBy data' do
  96. expect(gql.result.data['updatedBy']['firstname']).to eq(User.first.firstname)
  97. end
  98. end
  99. it_behaves_like 'graphql responds with error if unauthenticated'
  100. end
  101. end