settings_spec.rb 5.1 KB

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