settings_spec.rb 5.7 KB

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