settings_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 'radio' do
  53. it 'should store a set of channels' do
  54. val = %w(HIGH LOW)
  55. @client.put('/settings', rf24_channels: val)
  56. result = @client.get('/settings')
  57. expect(result['rf24_channels']).to eq(val)
  58. val = %w(MID LOW)
  59. @client.put('/settings', rf24_channels: val)
  60. result = @client.get('/settings')
  61. expect(result['rf24_channels']).to eq(val)
  62. val = %w(MID LOW LOW LOW)
  63. @client.put('/settings', rf24_channels: val)
  64. result = @client.get('/settings')
  65. expect(result['rf24_channels']).to eq(Set.new(val).to_a)
  66. end
  67. it 'should store a listen channel' do
  68. @client.put('/settings', rf24_listen_channel: 'MID')
  69. result = @client.get('/settings')
  70. expect(result['rf24_listen_channel']).to eq('MID')
  71. @client.put('/settings', rf24_listen_channel: 'LOW')
  72. result = @client.get('/settings')
  73. expect(result['rf24_listen_channel']).to eq('LOW')
  74. end
  75. end
  76. context 'static ip' do
  77. it 'should boot with static IP when applied' do
  78. static_ip = ENV.fetch('ESPMH_STATIC_IP')
  79. @client.put(
  80. '/settings',
  81. wifi_static_ip: static_ip,
  82. wifi_static_ip_netmask: ENV.fetch('ESPMH_STATIC_IP_NETMASK'),
  83. wifi_static_ip_gateway: ENV.fetch('ESPMH_STATIC_IP_GATEWAY')
  84. )
  85. # Reboot to apply static ip
  86. @client.reboot
  87. # Wait for it to come back up
  88. ping_test = Net::Ping::External.new(static_ip)
  89. 10.times do
  90. break if ping_test.ping?
  91. sleep 1
  92. end
  93. expect(ping_test.ping?).to be(true)
  94. static_client = ApiClient.new(static_ip, ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  95. static_client.put('/settings', wifi_static_ip: '')
  96. static_client.reboot
  97. ping_test = Net::Ping::External.new(ENV.fetch('ESPMH_HOSTNAME'))
  98. 10.times do
  99. break if ping_test.ping?
  100. sleep 1
  101. end
  102. expect(ping_test.ping?).to be(true)
  103. end
  104. end
  105. end