rest_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. require 'api_client'
  2. RSpec.describe 'REST Server' do
  3. before(:all) do
  4. @client = ApiClient.new(ENV.fetch('ESPMH_HOSTNAME'), ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  5. @client.upload_json('/settings', 'settings.json')
  6. @username = 'a'
  7. @password = 'a'
  8. end
  9. context 'authentication' do
  10. after(:all) do
  11. @client.set_auth!(@username, @password)
  12. @client.put('/settings', admin_username: '', admin_password: '')
  13. end
  14. it 'should not require auth unless both username and password are set' do
  15. @client.put('/settings', admin_username: 'abc', admin_password: '')
  16. expect { @client.get('/settings') }.not_to raise_error
  17. @client.put('/settings', admin_username: '', admin_password: 'abc')
  18. expect { @client.get('/settings') }.not_to raise_error
  19. @client.put('/settings', admin_username: '', admin_password: '')
  20. expect { @client.get('/settings') }.not_to raise_error
  21. end
  22. it 'should require auth for all routes when password is set' do
  23. @client.put('/settings', admin_username: @username, admin_password: @password)
  24. # Try no auth
  25. expect { @client.get('/settings') }.to raise_error(Net::HTTPServerException)
  26. # Try wrong username
  27. @client.set_auth!("#{@username}wronguser", @password)
  28. expect { @client.get('/settings') }.to raise_error(Net::HTTPServerException)
  29. # Try wrong password
  30. @client.set_auth!(@username, "wrong#{@password}")
  31. expect { @client.get('/settings') }.to raise_error(Net::HTTPServerException)
  32. # Try right username
  33. @client.set_auth!(@username, @password)
  34. expect { @client.get('/settings') }.not_to raise_error
  35. # Make sure all routes are protected
  36. @client.clear_auth!
  37. [
  38. '/about',
  39. '/gateways/0/rgb_cct/1',
  40. '/remote_configs',
  41. '/'
  42. ].each do |page|
  43. expect { @client.get(page) }.to raise_error(Net::HTTPServerException), "No auth required for page: #{page}"
  44. end
  45. expect { @client.post('/system', {}) }.to raise_error(Net::HTTPServerException)
  46. expect { @client.post('/firmware', {}) }.to raise_error(Net::HTTPServerException)
  47. # Clear auth
  48. @client.set_auth!(@username, @password)
  49. @client.put('/settings', admin_username: '', admin_password: '')
  50. @client.clear_auth!
  51. expect { @client.get('/settings') }.not_to raise_error
  52. end
  53. end
  54. context 'misc routes' do
  55. it 'should respond to /about' do
  56. result = @client.get('/about')
  57. expect(result['firmware']).to eq('milight-hub')
  58. end
  59. it 'should respond to /system' do
  60. expect { @client.post('/system', {}) }.to raise_error('400 "Bad Request"')
  61. end
  62. it 'should respond to /remote_configs' do
  63. result = @client.get('/remote_configs')
  64. expect(result).to be_a(Array)
  65. expect(result).to include('rgb_cct')
  66. end
  67. end
  68. end