change_password_spec.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::User::Current::ChangePassword, type: :graphql do
  4. context 'when changing the password', authenticated_as: :agent do
  5. let(:agent) { create(:agent, password: 'password') }
  6. let(:mutation) do
  7. <<~MUTATION
  8. mutation userCurrentChangePassword($currentPassword: String!, $newPassword: String!) {
  9. userCurrentChangePassword(currentPassword: $currentPassword, newPassword: $newPassword) {
  10. success
  11. errors {
  12. message
  13. field
  14. }
  15. }
  16. }
  17. MUTATION
  18. end
  19. let(:variables) { {} }
  20. before do
  21. gql.execute(mutation, variables: variables)
  22. end
  23. context 'with invalid current password' do
  24. let(:variables) do
  25. {
  26. currentPassword: 'foobar',
  27. newPassword: 'new_password'
  28. }
  29. end
  30. it 'fails with error message', :aggregate_failures do
  31. errors = gql.result.data['errors'].first
  32. expect(errors['message']).to eq('The current password you provided is incorrect.')
  33. expect(errors['field']).to eq('current_password')
  34. end
  35. end
  36. context 'with password policy violation' do
  37. let(:variables) do
  38. {
  39. currentPassword: 'password',
  40. newPassword: 'FooBarbazbaz'
  41. }
  42. end
  43. it 'fails with error message', :aggregate_failures do
  44. errors = gql.result.data['errors'].first
  45. expect(errors['message']).to eq('Invalid password, it must contain at least 1 digit!')
  46. expect(errors['field']).to eq('new_password')
  47. end
  48. end
  49. context 'with valid passwords' do
  50. let(:variables) do
  51. {
  52. currentPassword: 'password',
  53. newPassword: 'IamAValidPassword111einseinself'
  54. }
  55. end
  56. it 'succeeds' do
  57. expect(gql.result.data['success']).to be_truthy
  58. end
  59. end
  60. end
  61. end