packages_controller_test.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class PackagesControllerTest < 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 '01 packages index with nobody' do
  46. # index
  47. get '/api/v1/packages', {}, @headers
  48. assert_response(401)
  49. result = JSON.parse(@response.body)
  50. assert_equal(Hash, result.class)
  51. assert_not(result['packages'])
  52. assert_equal('authentication failed', result['error'])
  53. end
  54. test '02 packages index with admin' do
  55. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('packages-admin@example.com', 'adminpw')
  56. # index
  57. get '/api/v1/packages', {}, @headers.merge('Authorization' => credentials)
  58. assert_response(200)
  59. result = JSON.parse(@response.body)
  60. assert_equal(Hash, result.class)
  61. assert(result['packages'])
  62. end
  63. test '03 packages index with admin and wrong pw' do
  64. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('packages-admin@example.com', 'wrongadminpw')
  65. # index
  66. get '/api/v1/packages', {}, @headers.merge('Authorization' => credentials)
  67. assert_response(401)
  68. result = JSON.parse(@response.body)
  69. assert_equal(Hash, result.class)
  70. assert_equal('authentication failed', result['error'])
  71. end
  72. test '04 packages index with inactive admin' do
  73. @admin.active = false
  74. @admin.save!
  75. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('packages-admin@example.com', 'adminpw')
  76. # index
  77. get '/api/v1/packages', {}, @headers.merge('Authorization' => credentials)
  78. assert_response(401)
  79. result = JSON.parse(@response.body)
  80. assert_equal(Hash, result.class)
  81. assert_equal('authentication failed', result['error'])
  82. end
  83. test '05 packages index with agent' do
  84. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('packages-agent@example.com', 'agentpw')
  85. # index
  86. get '/api/v1/packages', {}, @headers.merge('Authorization' => credentials)
  87. assert_response(401)
  88. result = JSON.parse(@response.body)
  89. assert_equal(Hash, result.class)
  90. assert_not(result['packages'])
  91. assert_equal('Not authorized (user)!', result['error'])
  92. end
  93. test '06 packages index with customer' do
  94. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('packages-customer1@example.com', 'customer1pw')
  95. # index
  96. get '/api/v1/packages', {}, @headers.merge('Authorization' => credentials)
  97. assert_response(401)
  98. result = JSON.parse(@response.body)
  99. assert_equal(Hash, result.class)
  100. assert_not(result['packages'])
  101. assert_equal('Not authorized (user)!', result['error'])
  102. end
  103. end