rest_spec.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. # Clear auth
  47. @client.set_auth!(@username, @password)
  48. @client.put('/settings', admin_username: '', admin_password: '')
  49. @client.clear_auth!
  50. expect { @client.get('/settings') }.not_to raise_error
  51. end
  52. end
  53. end