settings_controller_test.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class SettingsControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # create agent
  8. roles = Role.where( name: %w(Admin Agent) )
  9. groups = Group.all
  10. UserInfo.current_user_id = 1
  11. @admin = User.create_or_update(
  12. login: 'packages-admin',
  13. firstname: 'Packages',
  14. lastname: 'Admin',
  15. email: 'packages-admin@example.com',
  16. password: 'adminpw',
  17. active: true,
  18. roles: roles,
  19. groups: groups,
  20. )
  21. # create agent
  22. roles = Role.where( name: 'Agent' )
  23. @agent = User.create_or_update(
  24. login: 'packages-agent@example.com',
  25. firstname: 'Rest',
  26. lastname: 'Agent',
  27. email: 'packages-agent@example.com',
  28. password: 'agentpw',
  29. active: true,
  30. roles: roles,
  31. groups: groups,
  32. )
  33. # create customer without org
  34. roles = Role.where( name: 'Customer' )
  35. @customer_without_org = User.create_or_update(
  36. login: 'packages-customer1@example.com',
  37. firstname: 'Packages',
  38. lastname: 'Customer1',
  39. email: 'packages-customer1@example.com',
  40. password: 'customer1pw',
  41. active: true,
  42. roles: roles,
  43. )
  44. end
  45. test 'settings index with nobody' do
  46. # index
  47. get '/api/v1/settings'
  48. assert_response(401)
  49. result = JSON.parse(@response.body)
  50. assert_equal(result.class, Hash)
  51. assert_not(result['settings'])
  52. end
  53. test 'settings index with admin' do
  54. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('packages-admin@example.com', 'adminpw')
  55. # index
  56. get '/api/v1/settings', {}, @headers.merge('Authorization' => credentials)
  57. assert_response(200)
  58. result = JSON.parse(@response.body)
  59. assert_equal(Array, result.class)
  60. assert(result)
  61. end
  62. test 'settings index with agent' do
  63. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('packages-agent@example.com', 'adminpw')
  64. # index
  65. get '/api/v1/settings', {}, @headers.merge('Authorization' => credentials)
  66. assert_response(401)
  67. result = JSON.parse(@response.body)
  68. assert_equal(result.class, Hash)
  69. assert_not(result['settings'])
  70. end
  71. test 'settings index with customer' do
  72. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('packages-customer1@example.com', 'customer1pw')
  73. # index
  74. get '/api/v1/settings', {}, @headers.merge('Authorization' => credentials)
  75. assert_response(401)
  76. result = JSON.parse(@response.body)
  77. assert_equal(result.class, Hash)
  78. assert_not(result['settings'])
  79. end
  80. end