add_spec.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::User::Add, :aggregate_failures, type: :graphql do
  4. context 'when creating a new user', authenticated_as: :agent do
  5. let(:agent) { create(:agent) }
  6. let(:dummy_email) { 'dummy@zammad.com' }
  7. let(:query) do
  8. <<~QUERY
  9. mutation userAdd($input: UserInput!, $sendInvite: Boolean) {
  10. userAdd(input: $input, sendInvite: $sendInvite) {
  11. user {
  12. id
  13. firstname
  14. lastname
  15. fullname
  16. }
  17. errors {
  18. message
  19. field
  20. }
  21. }
  22. }
  23. QUERY
  24. end
  25. let(:variables) do
  26. {
  27. input: {
  28. email: dummy_email,
  29. firstname: 'Bender',
  30. lastname: 'Rodríguez',
  31. }
  32. }
  33. end
  34. let(:expected_response) do
  35. {
  36. 'id' => gql.id(User.find_by(email: dummy_email)),
  37. 'firstname' => 'Bender',
  38. 'lastname' => 'Rodríguez',
  39. 'fullname' => 'Bender Rodríguez',
  40. }
  41. end
  42. it 'creates User record' do
  43. gql.execute(query, variables: variables)
  44. expect(gql.result.data['user']).to eq(expected_response)
  45. end
  46. context 'with not unique email', :aggregate_failures do
  47. it 'returns a user error' do
  48. create(:user, email: dummy_email)
  49. gql.execute(query, variables: variables)
  50. expect(gql.result.data['errors'].first)
  51. .to include({ 'message' => "Email address '#{dummy_email}' is already used for another user." })
  52. end
  53. end
  54. context 'without permission', authenticated_as: :customer do
  55. let(:customer) { create(:customer) }
  56. it 'raises an error' do
  57. gql.execute(query, variables: variables)
  58. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  59. end
  60. end
  61. end
  62. end