settings_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 'group id labels' do
  104. it 'should store ID labels' do
  105. id = 1
  106. aliases = Hash[
  107. StateHelpers::ALL_REMOTE_TYPES.map do |remote_type|
  108. ["test_#{id += 1}", [remote_type, id, 1]]
  109. end
  110. ]
  111. @client.patch_settings(group_id_aliases: aliases)
  112. settings = @client.get('/settings')
  113. expect(settings['group_id_aliases']).to eq(aliases)
  114. end
  115. end
  116. context 'static ip' do
  117. it 'should boot with static IP when applied' do
  118. static_ip = ENV.fetch('ESPMH_STATIC_IP')
  119. @client.put(
  120. '/settings',
  121. wifi_static_ip: static_ip,
  122. wifi_static_ip_netmask: ENV.fetch('ESPMH_STATIC_IP_NETMASK'),
  123. wifi_static_ip_gateway: ENV.fetch('ESPMH_STATIC_IP_GATEWAY')
  124. )
  125. # Reboot to apply static ip
  126. @client.reboot
  127. # Wait for it to come back up
  128. ping_test = Net::Ping::External.new(static_ip)
  129. 10.times do
  130. break if ping_test.ping?
  131. sleep 1
  132. end
  133. expect(ping_test.ping?).to be(true)
  134. static_client = ApiClient.new(static_ip, ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  135. static_client.put('/settings', wifi_static_ip: '')
  136. static_client.reboot
  137. ping_test = Net::Ping::External.new(ENV.fetch('ESPMH_HOSTNAME'))
  138. 10.times do
  139. break if ping_test.ping?
  140. sleep 1
  141. end
  142. expect(ping_test.ping?).to be(true)
  143. end
  144. end
  145. context 'defaults' do
  146. before(:all) do
  147. # Clobber all settings
  148. file = Tempfile.new('espmh-settings.json')
  149. file.close
  150. @client.upload_json('/settings', file.path)
  151. end
  152. it 'should have some group state fields defined' do
  153. settings = @client.get('/settings')
  154. expect(settings['group_state_fields']).to_not be_empty
  155. end
  156. it 'should allow for empty group state fields if set' do
  157. @client.patch_settings(group_state_fields: [])
  158. settings = @client.get('/settings')
  159. expect(settings['group_state_fields']).to eq([])
  160. end
  161. end
  162. end