settings_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. 'home_assistant_discovery_prefix' => ['', 'abc', 'a/b/c']
  27. }.each do |key, values|
  28. values.each do |v|
  29. @client.patch_settings({key => v})
  30. expect(@client.get('/settings')[key]).to eq(v)
  31. end
  32. end
  33. end
  34. end
  35. context 'POST settings file' do
  36. it 'should clobber patched settings' do
  37. file = Tempfile.new('espmh-settings.json')
  38. file.write({
  39. mqtt_server: 'test123'
  40. }.to_json)
  41. file.close
  42. @client.upload_json('/settings', file.path)
  43. settings = @client.get('/settings')
  44. expect(settings['mqtt_server']).to eq('test123')
  45. @client.put('/settings', {mqtt_server: 'abc123', mqtt_username: 'foo'})
  46. settings = @client.get('/settings')
  47. expect(settings['mqtt_server']).to eq('abc123')
  48. expect(settings['mqtt_username']).to eq('foo')
  49. @client.upload_json('/settings', file.path)
  50. settings = @client.get('/settings')
  51. expect(settings['mqtt_server']).to eq('test123')
  52. expect(settings['mqtt_username']).to eq('')
  53. File.delete(file.path)
  54. end
  55. it 'should apply POSTed settings' do
  56. file = Tempfile.new('espmh-settings.json')
  57. file.write({
  58. admin_username: @username,
  59. admin_password: @password
  60. }.to_json)
  61. file.close
  62. @client.upload_json('/settings', file.path)
  63. expect { @client.get('/settings') }.to raise_error(Net::HTTPServerException)
  64. end
  65. end
  66. context 'PUT settings file' do
  67. it 'should accept a fairly large request body' do
  68. contents = (1..25).reduce({}) { |a, x| a[x] = "test#{x}"*10; a }
  69. expect { @client.put('/settings', contents) }.to_not raise_error
  70. end
  71. it 'should not cause excessive memory leaks' do
  72. start_mem = @client.get('/about')['free_heap']
  73. 20.times do
  74. @client.put('/settings', mqtt_username: 'a')
  75. end
  76. end_mem = @client.get('/about')['free_heap']
  77. expect(end_mem - start_mem).to_not be < -200
  78. end
  79. end
  80. context 'radio' do
  81. it 'should store a set of channels' do
  82. val = %w(HIGH LOW)
  83. @client.put('/settings', rf24_channels: val)
  84. result = @client.get('/settings')
  85. expect(result['rf24_channels']).to eq(val)
  86. val = %w(MID LOW)
  87. @client.put('/settings', rf24_channels: val)
  88. result = @client.get('/settings')
  89. expect(result['rf24_channels']).to eq(val)
  90. val = %w(MID LOW LOW LOW)
  91. @client.put('/settings', rf24_channels: val)
  92. result = @client.get('/settings')
  93. expect(result['rf24_channels']).to eq(Set.new(val).to_a)
  94. end
  95. it 'should store a listen channel' do
  96. @client.put('/settings', rf24_listen_channel: 'MID')
  97. result = @client.get('/settings')
  98. expect(result['rf24_listen_channel']).to eq('MID')
  99. @client.put('/settings', rf24_listen_channel: 'LOW')
  100. result = @client.get('/settings')
  101. expect(result['rf24_listen_channel']).to eq('LOW')
  102. end
  103. end
  104. context 'group id labels' do
  105. it 'should store ID labels' do
  106. id = 1
  107. aliases = Hash[
  108. StateHelpers::ALL_REMOTE_TYPES.map do |remote_type|
  109. ["test_#{id += 1}", [remote_type, id, 1]]
  110. end
  111. ]
  112. @client.patch_settings(group_id_aliases: aliases)
  113. settings = @client.get('/settings')
  114. expect(settings['group_id_aliases']).to eq(aliases)
  115. end
  116. end
  117. context 'static ip' do
  118. it 'should boot with static IP when applied' do
  119. static_ip = ENV.fetch('ESPMH_STATIC_IP')
  120. @client.put(
  121. '/settings',
  122. wifi_static_ip: static_ip,
  123. wifi_static_ip_netmask: ENV.fetch('ESPMH_STATIC_IP_NETMASK'),
  124. wifi_static_ip_gateway: ENV.fetch('ESPMH_STATIC_IP_GATEWAY')
  125. )
  126. # Reboot to apply static ip
  127. @client.reboot
  128. # Wait for it to come back up
  129. ping_test = Net::Ping::External.new(static_ip)
  130. 10.times do
  131. break if ping_test.ping?
  132. sleep 1
  133. end
  134. expect(ping_test.ping?).to be(true)
  135. static_client = ApiClient.new(static_ip, ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  136. static_client.put('/settings', wifi_static_ip: '')
  137. static_client.reboot
  138. ping_test = Net::Ping::External.new(ENV.fetch('ESPMH_HOSTNAME'))
  139. 10.times do
  140. break if ping_test.ping?
  141. sleep 1
  142. end
  143. expect(ping_test.ping?).to be(true)
  144. end
  145. end
  146. context 'defaults' do
  147. before(:all) do
  148. # Clobber all settings
  149. file = Tempfile.new('espmh-settings.json')
  150. file.close
  151. @client.upload_json('/settings', file.path)
  152. end
  153. it 'should have some group state fields defined' do
  154. settings = @client.get('/settings')
  155. expect(settings['group_state_fields']).to_not be_empty
  156. end
  157. it 'should allow for empty group state fields if set' do
  158. @client.patch_settings(group_state_fields: [])
  159. settings = @client.get('/settings')
  160. expect(settings['group_state_fields']).to eq([])
  161. end
  162. end
  163. end