settings_spec.rb 4.2 KB

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