settings_spec.rb 5.9 KB

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