settings_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. require 'api_client'
  2. require 'tempfile'
  3. RSpec.describe 'Settings' do
  4. before(:all) do
  5. @client = ApiClient.new(ENV.fetch('ESPMH_HOSTNAME'), ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  6. @client.upload_json('/settings', 'settings.json')
  7. @username = 'a'
  8. @password = 'a'
  9. end
  10. after(:all) do
  11. @client.set_auth!(@username, @password)
  12. @client.put('/settings', admin_username: '', admin_password: '')
  13. @client.clear_auth!
  14. end
  15. before(:each) do
  16. @client.set_auth!(@username, @password)
  17. @client.put('/settings', admin_username: '', admin_password: '')
  18. @client.clear_auth!
  19. end
  20. context 'POST settings file' do
  21. it 'should clobber patched settings' do
  22. file = Tempfile.new('espmh-settings.json')
  23. file.write({
  24. mqtt_server: 'test123'
  25. }.to_json)
  26. file.close
  27. @client.upload_json('/settings', file.path)
  28. settings = @client.get('/settings')
  29. expect(settings['mqtt_server']).to eq('test123')
  30. @client.put('/settings', {mqtt_server: 'abc123', mqtt_username: 'foo'})
  31. settings = @client.get('/settings')
  32. expect(settings['mqtt_server']).to eq('abc123')
  33. expect(settings['mqtt_username']).to eq('foo')
  34. @client.upload_json('/settings', file.path)
  35. settings = @client.get('/settings')
  36. expect(settings['mqtt_server']).to eq('test123')
  37. expect(settings['mqtt_username']).to eq('')
  38. File.delete(file.path)
  39. end
  40. it 'should apply POSTed settings' do
  41. file = Tempfile.new('espmh-settings.json')
  42. file.write({
  43. admin_username: @username,
  44. admin_password: @password
  45. }.to_json)
  46. file.close
  47. @client.upload_json('/settings', file.path)
  48. expect { @client.get('/settings') }.to raise_error(Net::HTTPServerException)
  49. end
  50. end
  51. context 'radio' do
  52. it 'should store a set of channels' do
  53. val = %w(HIGH LOW)
  54. @client.put('/settings', rf24_channels: val)
  55. result = @client.get('/settings')
  56. expect(result['rf24_channels']).to eq(val)
  57. val = %w(MID LOW)
  58. @client.put('/settings', rf24_channels: val)
  59. result = @client.get('/settings')
  60. expect(result['rf24_channels']).to eq(val)
  61. val = %w(MID LOW LOW LOW)
  62. @client.put('/settings', rf24_channels: val)
  63. result = @client.get('/settings')
  64. expect(result['rf24_channels']).to eq(Set.new(val).to_a)
  65. end
  66. it 'should store a listen channel' do
  67. @client.put('/settings', rf24_listen_channel: 'MID')
  68. result = @client.get('/settings')
  69. expect(result['rf24_listen_channel']).to eq('MID')
  70. @client.put('/settings', rf24_listen_channel: 'LOW')
  71. result = @client.get('/settings')
  72. expect(result['rf24_listen_channel']).to eq('LOW')
  73. end
  74. end
  75. end