suggestions_spec.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Mention::Suggestions, searchindex: true, type: :graphql do
  4. context 'when searching' do
  5. let(:agent) { create(:agent, groups: [group]) }
  6. let(:customer) { create(:customer) }
  7. let(:group) { create(:group) }
  8. let(:query) do
  9. <<~QUERY
  10. query mentionSuggestions($query: String!, $groupId: ID!) {
  11. mentionSuggestions(query: $query, groupId: $groupId) {
  12. id
  13. fullname
  14. email
  15. }
  16. }
  17. QUERY
  18. end
  19. let!(:variables) { { query: search_query, groupId: Gql::ZammadSchema.id_from_object(group) } }
  20. before do
  21. searchindex_model_reload([User])
  22. gql.execute(query, variables: variables)
  23. end
  24. shared_examples 'returns the correct users' do
  25. it 'has data' do
  26. expect(gql.result.data).to include(expected_result)
  27. end
  28. end
  29. context 'with authenticated session', authenticated_as: :agent do
  30. let(:search_query) { agent.firstname[0..2] }
  31. let(:expected_result) do
  32. {
  33. 'id' => Gql::ZammadSchema.id_from_object(agent),
  34. 'fullname' => agent.fullname,
  35. 'email' => agent.email,
  36. }
  37. end
  38. include_examples 'returns the correct users'
  39. context 'with no results' do
  40. let(:search_query) { 'foo' }
  41. it 'has no data' do
  42. expect(gql.result.data).to be_empty
  43. end
  44. end
  45. context 'with an agent that has no email' do
  46. let(:agent) { create(:agent, groups: [group], email: nil) }
  47. include_examples 'returns the correct users'
  48. end
  49. context 'with an agent that has no firstname/lastname' do
  50. let(:agent) { create(:agent, groups: [group], firstname: nil, lastname: nil, email: 'foo@zammad.com') }
  51. let(:search_query) { 'foo' }
  52. include_examples 'returns the correct users'
  53. end
  54. end
  55. context 'with a customer', authenticated_as: :customer do
  56. let(:search_query) { agent.firstname[0..2] }
  57. it_behaves_like 'graphql responds with error if unauthenticated'
  58. end
  59. end
  60. end