update_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::User::Update, type: :graphql do
  4. let(:variables) do
  5. {
  6. id: gql.id(user),
  7. input: input
  8. }
  9. end
  10. let(:input) { { email: 'dummy@zammad.com' } }
  11. context 'when updating a user', authenticated_as: :agent do
  12. let(:agent) { create(:agent) }
  13. let(:user) { create(:user, :with_org) }
  14. let(:query) do
  15. <<~QUERY
  16. mutation userUpdate($id: ID!, $input: UserInput!) {
  17. userUpdate(id: $id, input: $input) {
  18. user {
  19. id
  20. firstname
  21. lastname
  22. fullname
  23. }
  24. errors {
  25. message
  26. field
  27. }
  28. }
  29. }
  30. QUERY
  31. end
  32. context 'with basic fields' do
  33. let(:input) do
  34. {
  35. email: 'dummy@zammad.com',
  36. firstname: 'Bender',
  37. lastname: 'Rodríguez',
  38. }
  39. end
  40. let(:expected_response) do
  41. {
  42. 'id' => gql.id(user),
  43. 'firstname' => 'Bender',
  44. 'lastname' => 'Rodríguez',
  45. 'fullname' => 'Bender Rodríguez',
  46. }
  47. end
  48. it 'updates User record' do
  49. gql.execute(query, variables: variables)
  50. expect(gql.result.data[:user]).to eq(expected_response)
  51. end
  52. context 'with not unique email', :aggregate_failures do
  53. it 'returns a user error' do
  54. create(:user, email: 'dummy@zammad.com')
  55. gql.execute(query, variables: variables)
  56. expect(gql.result.data[:errors].first).to include({ 'message' => "Email address 'dummy@zammad.com' is already used for another user." })
  57. end
  58. end
  59. end
  60. context 'with multiple secondary organizations' do
  61. let(:organization_a) { create(:organization) }
  62. let(:organization_b) { create(:organization) }
  63. context 'when user has no secondary organizations' do
  64. let(:input) do
  65. {
  66. email: 'dummy@zammad.com',
  67. organizationIds: [organization_a, organization_b].map { |elem| gql.id(elem) }
  68. }
  69. end
  70. it 'adds given organizations' do
  71. gql.execute(query, variables: variables)
  72. expect(user.reload).to have_attributes(organizations: [organization_a, organization_b])
  73. end
  74. end
  75. context 'when user already has a secondary organization' do
  76. let(:input) do
  77. {
  78. email: 'dummy@zammad.com',
  79. organizationIds: [organization_b].map { |elem| gql.id(elem) }
  80. }
  81. end
  82. it 'replaces secondary organization with a given one' do
  83. user.update! organizations: [organization_a]
  84. gql.execute(query, variables: variables)
  85. expect(user.reload).to have_attributes(organizations: [organization_b])
  86. end
  87. end
  88. end
  89. context 'without permission', authenticated_as: :user do
  90. context 'with not authorized agent' do
  91. let(:user) { create(:admin, roles: [role]) }
  92. let(:role) do
  93. role = create(:role)
  94. role.permission_grant('admin.branding')
  95. role
  96. end
  97. it 'raises an error' do
  98. gql.execute(query, variables: variables)
  99. expect(gql.result.error_type).to eq(Pundit::NotAuthorizedError)
  100. end
  101. end
  102. context 'with customer' do
  103. let(:user) { create(:customer) }
  104. it 'raises an error' do
  105. gql.execute(query, variables: variables)
  106. expect(gql.result.error_type).to eq(Pundit::NotAuthorizedError)
  107. end
  108. end
  109. end
  110. context 'when user is email-less' do
  111. let(:user) { create(:user, :without_email) }
  112. let(:firstname) { 'dummy test name' }
  113. let(:input) { { firstname: firstname } }
  114. it 'updates User record' do
  115. gql.execute(query, variables: variables)
  116. expect(user.reload).to have_attributes(firstname: firstname)
  117. end
  118. end
  119. end
  120. end