sla_controller_test.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. require 'test_helper'
  2. class SlaControllerTest < ActionDispatch::IntegrationTest
  3. setup do
  4. # set accept header
  5. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  6. # create agent
  7. roles = Role.where(name: %w[Admin Agent])
  8. groups = Group.all
  9. UserInfo.current_user_id = 1
  10. @admin = User.create!(
  11. login: 'sla-admin',
  12. firstname: 'Packages',
  13. lastname: 'Admin',
  14. email: 'sla-admin@example.com',
  15. password: 'adminpw',
  16. active: true,
  17. roles: roles,
  18. groups: groups,
  19. )
  20. end
  21. test '01 sla index with nobody' do
  22. get '/api/v1/slas', params: {}, headers: @headers
  23. assert_response(401)
  24. result = JSON.parse(@response.body)
  25. assert_equal(Hash, result.class)
  26. assert_equal('authentication failed', result['error'])
  27. end
  28. test '02 sla index with admin' do
  29. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('sla-admin@example.com', 'adminpw')
  30. get '/api/v1/slas', params: {}, headers: @headers.merge('Authorization' => credentials)
  31. assert_response(200)
  32. result = JSON.parse(@response.body)
  33. assert_equal(Array, result.class)
  34. assert(result)
  35. assert_equal(0, result.count)
  36. get '/api/v1/slas?expand=true', params: {}, headers: @headers.merge('Authorization' => credentials)
  37. assert_response(200)
  38. result = JSON.parse(@response.body)
  39. assert_equal(Array, result.class)
  40. assert(result)
  41. assert_equal(0, result.count)
  42. get '/api/v1/slas?full=true', params: {}, headers: @headers.merge('Authorization' => credentials)
  43. assert_response(200)
  44. result = JSON.parse(@response.body)
  45. assert_equal(Hash, result.class)
  46. assert(result)
  47. assert(result['record_ids'])
  48. assert(result['record_ids'].blank?)
  49. assert(result['assets'])
  50. assert(result['assets']['Calendar'].present?)
  51. assert(result['assets'].present?)
  52. end
  53. end