settings_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. require 'api_client'
  2. require 'tempfile'
  3. require 'net/ping'
  4. RSpec.describe 'Settings' do
  5. before(:all) do
  6. @client = ApiClient.new(ENV.fetch('ESPMH_HOSTNAME'), ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  7. @client.upload_json('/settings', 'settings.json')
  8. @username = 'a'
  9. @password = 'a'
  10. end
  11. after(:all) do
  12. @client.set_auth!(@username, @password)
  13. @client.put('/settings', admin_username: '', admin_password: '')
  14. @client.clear_auth!
  15. end
  16. before(:each) do
  17. @client.set_auth!(@username, @password)
  18. @client.put('/settings', admin_username: '', admin_password: '')
  19. @client.clear_auth!
  20. end
  21. context 'keys' do
  22. it 'should persist known settings keys' do
  23. {
  24. 'simple_mqtt_client_status' => [true, false],
  25. 'packet_repeats_per_loop' => [10]
  26. }.each do |key, values|
  27. values.each do |v|
  28. @client.patch_settings({key => v})
  29. expect(@client.get('/settings')[key]).to eq(v)
  30. end
  31. end
  32. end
  33. end
  34. context 'POST settings file' do
  35. it 'should clobber patched settings' do
  36. file = Tempfile.new('espmh-settings.json')
  37. file.write({
  38. mqtt_server: 'test123'
  39. }.to_json)
  40. file.close
  41. @client.upload_json('/settings', file.path)
  42. settings = @client.get('/settings')
  43. expect(settings['mqtt_server']).to eq('test123')
  44. @client.put('/settings', {mqtt_server: 'abc123', mqtt_username: 'foo'})
  45. settings = @client.get('/settings')
  46. expect(settings['mqtt_server']).to eq('abc123')
  47. expect(settings['mqtt_username']).to eq('foo')
  48. @client.upload_json('/settings', file.path)
  49. settings = @client.get('/settings')
  50. expect(settings['mqtt_server']).to eq('test123')
  51. expect(settings['mqtt_username']).to eq('')
  52. File.delete(file.path)
  53. end
  54. it 'should apply POSTed settings' do
  55. file = Tempfile.new('espmh-settings.json')
  56. file.write({
  57. admin_username: @username,
  58. admin_password: @password
  59. }.to_json)
  60. file.close
  61. @client.upload_json('/settings', file.path)
  62. expect { @client.get('/settings') }.to raise_error(Net::HTTPServerException)
  63. end
  64. end
  65. context 'PUT settings file' do
  66. it 'should accept a fairly large request body' do
  67. contents = (1..25).reduce({}) { |a, x| a[x] = "test#{x}"*10; a }
  68. expect { @client.put('/settings', contents) }.to_not raise_error
  69. end
  70. it 'should not cause excessive memory leaks' do
  71. start_mem = @client.get('/about')['free_heap']
  72. 20.times do
  73. @client.put('/settings', mqtt_username: 'a')
  74. end
  75. end_mem = @client.get('/about')['free_heap']
  76. expect(end_mem - start_mem).to_not be < -200
  77. end
  78. end
  79. context 'radio' do
  80. it 'should store a set of channels' do
  81. val = %w(HIGH LOW)
  82. @client.put('/settings', rf24_channels: val)
  83. result = @client.get('/settings')
  84. expect(result['rf24_channels']).to eq(val)
  85. val = %w(MID LOW)
  86. @client.put('/settings', rf24_channels: val)
  87. result = @client.get('/settings')
  88. expect(result['rf24_channels']).to eq(val)
  89. val = %w(MID LOW LOW LOW)
  90. @client.put('/settings', rf24_channels: val)
  91. result = @client.get('/settings')
  92. expect(result['rf24_channels']).to eq(Set.new(val).to_a)
  93. end
  94. it 'should store a listen channel' do
  95. @client.put('/settings', rf24_listen_channel: 'MID')
  96. result = @client.get('/settings')
  97. expect(result['rf24_listen_channel']).to eq('MID')
  98. @client.put('/settings', rf24_listen_channel: 'LOW')
  99. result = @client.get('/settings')
  100. expect(result['rf24_listen_channel']).to eq('LOW')
  101. end
  102. end
  103. context 'static ip' do
  104. it 'should boot with static IP when applied' do
  105. static_ip = ENV.fetch('ESPMH_STATIC_IP')
  106. @client.put(
  107. '/settings',
  108. wifi_static_ip: static_ip,
  109. wifi_static_ip_netmask: ENV.fetch('ESPMH_STATIC_IP_NETMASK'),
  110. wifi_static_ip_gateway: ENV.fetch('ESPMH_STATIC_IP_GATEWAY')
  111. )
  112. # Reboot to apply static ip
  113. @client.reboot
  114. # Wait for it to come back up
  115. ping_test = Net::Ping::External.new(static_ip)
  116. 10.times do
  117. break if ping_test.ping?
  118. sleep 1
  119. end
  120. expect(ping_test.ping?).to be(true)
  121. static_client = ApiClient.new(static_ip, ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  122. static_client.put('/settings', wifi_static_ip: '')
  123. static_client.reboot
  124. ping_test = Net::Ping::External.new(ENV.fetch('ESPMH_HOSTNAME'))
  125. 10.times do
  126. break if ping_test.ping?
  127. sleep 1
  128. end
  129. expect(ping_test.ping?).to be(true)
  130. end
  131. end
  132. context 'defaults' do
  133. before(:all) do
  134. # Clobber all settings
  135. file = Tempfile.new('espmh-settings.json')
  136. file.close
  137. @client.upload_json('/settings', file.path)
  138. end
  139. it 'should have some group state fields defined' do
  140. settings = @client.get('/settings')
  141. expect(settings['group_state_fields']).to_not be_empty
  142. end
  143. it 'should allow for empty group state fields if set' do
  144. @client.patch_settings(group_state_fields: [])
  145. settings = @client.get('/settings')
  146. expect(settings['group_state_fields']).to eq([])
  147. end
  148. end
  149. end