user_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::AutocompleteSearch::User, authenticated_as: :agent, type: :graphql do
  4. context 'when searching for users' do
  5. let(:agent) { create(:agent) }
  6. let(:users) { create_list(:customer, 3, lastname: 'AutocompleteSearch') }
  7. let(:query) do
  8. <<~QUERY
  9. query autocompleteSearchUser($input: AutocompleteSearchUserInput!) {
  10. autocompleteSearchUser(input: $input) {
  11. value
  12. label
  13. labelPlaceholder
  14. heading
  15. headingPlaceholder
  16. disabled
  17. icon
  18. }
  19. }
  20. QUERY
  21. end
  22. let(:variables) { { input: { query: query_string, limit: limit, exceptInternalId: except } } }
  23. let(:query_string) { users.last.lastname }
  24. let(:limit) { nil }
  25. let(:except) { nil }
  26. before do
  27. gql.execute(query, variables: variables)
  28. end
  29. context 'without limit' do
  30. it 'finds all users' do
  31. expect(gql.result.data.length).to eq(users.length)
  32. end
  33. end
  34. context 'with limit' do
  35. let(:limit) { 1 }
  36. it 'respects the limit' do
  37. expect(gql.result.data.length).to eq(limit)
  38. end
  39. end
  40. context 'with exact search' do
  41. let(:first_user_payload) do
  42. {
  43. 'value' => users.first.id,
  44. 'label' => users.first.fullname,
  45. 'labelPlaceholder' => nil,
  46. 'heading' => nil,
  47. 'headingPlaceholder' => nil,
  48. 'icon' => nil,
  49. 'disabled' => nil,
  50. }
  51. end
  52. let(:query_string) { users.first.login }
  53. it 'has data' do
  54. expect(gql.result.data).to include(first_user_payload)
  55. end
  56. end
  57. context 'when sending an empty search string' do
  58. let(:query_string) { ' ' }
  59. it 'returns nothing' do
  60. expect(gql.result.data.length).to eq(0)
  61. end
  62. end
  63. context 'when a specific user is excepted' do
  64. let(:except) { users.first.id }
  65. let(:second_user_payload) do
  66. {
  67. 'value' => users.second.id,
  68. 'label' => users.second.fullname,
  69. 'labelPlaceholder' => nil,
  70. 'heading' => nil,
  71. 'headingPlaceholder' => nil,
  72. 'icon' => nil,
  73. 'disabled' => nil,
  74. }
  75. end
  76. let(:third_user_payload) do
  77. {
  78. 'value' => users.third.id,
  79. 'label' => users.third.fullname,
  80. 'labelPlaceholder' => nil,
  81. 'heading' => nil,
  82. 'headingPlaceholder' => nil,
  83. 'icon' => nil,
  84. 'disabled' => nil,
  85. }
  86. end
  87. it 'filters out provided user' do
  88. expect(gql.result.data).to eq([third_user_payload, second_user_payload])
  89. end
  90. end
  91. it_behaves_like 'graphql responds with error if unauthenticated'
  92. end
  93. end