lock_spec.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::System::Setup::Lock, :aggregate_failures, type: :graphql do
  4. context 'when locking system setup' do
  5. let(:mutation) do
  6. <<~MUTATION
  7. mutation systemSetupLock($ttl: Int) {
  8. systemSetupLock(ttl: $ttl) {
  9. resource
  10. value
  11. errors {
  12. message
  13. field
  14. }
  15. }
  16. }
  17. MUTATION
  18. end
  19. let(:resource) { 'Zammad::System::Setup' }
  20. let(:value) { SecureRandom.uuid }
  21. let(:ttl) { 1 }
  22. let(:lock_info) { { resource: resource, value: value } }
  23. let(:variables) { { ttl: ttl } }
  24. it 'returns lock info' do
  25. allow_any_instance_of(Redlock::Client).to receive(:lock).with(resource, ttl).and_return(lock_info)
  26. gql.execute(mutation, variables: variables)
  27. expect(gql.result.data).to include({ 'resource' => resource, 'value' => value })
  28. end
  29. context 'when system setup is already done' do
  30. before do
  31. Setting.set('system_init_done', true)
  32. end
  33. it 'raises error' do
  34. gql.execute(mutation, variables: variables)
  35. expect { gql.result.data }.to raise_error(RuntimeError)
  36. end
  37. end
  38. context 'when system setup is already locked' do
  39. it 'raises error' do
  40. allow_any_instance_of(Redlock::Client).to receive(:locked?).with(resource).and_return(true)
  41. gql.execute(mutation, variables: variables)
  42. expect { gql.result.data }.to raise_error(RuntimeError)
  43. end
  44. end
  45. end
  46. end