api_auth_controller_test.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class ApiAuthControllerTest < 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: 'api-admin',
  13. firstname: 'API',
  14. lastname: 'Admin',
  15. email: 'api-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: 'api-agent@example.com',
  25. firstname: 'API',
  26. lastname: 'Agent',
  27. email: 'api-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 = User.create_or_update(
  36. login: 'api-customer1@example.com',
  37. firstname: 'API',
  38. lastname: 'Customer1',
  39. email: 'api-customer1@example.com',
  40. password: 'customer1pw',
  41. active: true,
  42. roles: roles,
  43. )
  44. end
  45. test 'basic auth - admin' do
  46. admin_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-admin@example.com', 'adminpw')
  47. Setting.set('api_password_access', false)
  48. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  49. assert_response(401)
  50. result = JSON.parse(@response.body)
  51. assert_equal(Hash, result.class)
  52. assert_equal('API password access disabled!', result['error'])
  53. Setting.set('api_password_access', true)
  54. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  55. assert_response(200)
  56. result = JSON.parse(@response.body)
  57. assert_equal(Hash, result.class)
  58. assert(result)
  59. end
  60. test 'basic auth - agent' do
  61. agent_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-agent@example.com', 'agentpw')
  62. Setting.set('api_password_access', false)
  63. get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
  64. assert_response(401)
  65. result = JSON.parse(@response.body)
  66. assert_equal(Hash, result.class)
  67. assert_equal('API password access disabled!', result['error'])
  68. Setting.set('api_password_access', true)
  69. get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
  70. assert_response(200)
  71. result = JSON.parse(@response.body)
  72. assert_equal(Array, result.class)
  73. assert(result)
  74. end
  75. test 'basic auth - customer' do
  76. customer_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-customer1@example.com', 'customer1pw')
  77. Setting.set('api_password_access', false)
  78. get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
  79. assert_response(401)
  80. result = JSON.parse(@response.body)
  81. assert_equal(Hash, result.class)
  82. assert_equal('API password access disabled!', result['error'])
  83. Setting.set('api_password_access', true)
  84. get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
  85. assert_response(200)
  86. result = JSON.parse(@response.body)
  87. assert_equal(Array, result.class)
  88. assert(result)
  89. end
  90. test 'token auth - admin' do
  91. admin_token = Token.create(
  92. action: 'api',
  93. persistent: true,
  94. user_id: @admin.id,
  95. preferences: {
  96. permission: ['admin.session'],
  97. },
  98. )
  99. admin_credentials = "Token token=#{admin_token.name}"
  100. Setting.set('api_token_access', false)
  101. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  102. assert_response(401)
  103. result = JSON.parse(@response.body)
  104. assert_equal(Hash, result.class)
  105. assert_equal('API token access disabled!', result['error'])
  106. Setting.set('api_token_access', true)
  107. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  108. assert_response(200)
  109. result = JSON.parse(@response.body)
  110. assert_equal(Hash, result.class)
  111. assert(result)
  112. admin_token.preferences[:permission] = ['admin.session_not_existing']
  113. admin_token.save!
  114. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  115. assert_response(401)
  116. result = JSON.parse(@response.body)
  117. assert_equal(Hash, result.class)
  118. assert_equal('Not authorized (token)!', result['error'])
  119. admin_token.preferences[:permission] = []
  120. admin_token.save!
  121. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  122. assert_response(401)
  123. result = JSON.parse(@response.body)
  124. assert_equal(Hash, result.class)
  125. assert_equal('Not authorized (token)!', result['error'])
  126. @admin.active = false
  127. @admin.save!
  128. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  129. assert_response(401)
  130. result = JSON.parse(@response.body)
  131. assert_equal(Hash, result.class)
  132. assert_equal('User is inactive!', result['error'])
  133. admin_token.preferences[:permission] = ['admin.session']
  134. admin_token.save!
  135. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  136. assert_response(401)
  137. result = JSON.parse(@response.body)
  138. assert_equal(Hash, result.class)
  139. assert_equal('User is inactive!', result['error'])
  140. @admin.active = true
  141. @admin.save!
  142. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  143. assert_response(200)
  144. result = JSON.parse(@response.body)
  145. assert_equal(Hash, result.class)
  146. assert(result)
  147. get '/api/v1/roles', {}, @headers.merge('Authorization' => admin_credentials)
  148. assert_response(401)
  149. result = JSON.parse(@response.body)
  150. assert_equal(Hash, result.class)
  151. assert_equal('Not authorized (token)!', result['error'])
  152. admin_token.preferences[:permission] = ['admin.session_not_existing', 'admin.role']
  153. admin_token.save!
  154. get '/api/v1/roles', {}, @headers.merge('Authorization' => admin_credentials)
  155. assert_response(200)
  156. result = JSON.parse(@response.body)
  157. assert_equal(Array, result.class)
  158. assert(result)
  159. end
  160. test 'token auth - agent' do
  161. agent_token = Token.create(
  162. action: 'api',
  163. persistent: true,
  164. user_id: @agent.id,
  165. )
  166. agent_credentials = "Token token=#{agent_token.name}"
  167. Setting.set('api_token_access', false)
  168. get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
  169. assert_response(401)
  170. result = JSON.parse(@response.body)
  171. assert_equal(Hash, result.class)
  172. assert_equal('API token access disabled!', result['error'])
  173. Setting.set('api_token_access', true)
  174. get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
  175. assert_response(200)
  176. result = JSON.parse(@response.body)
  177. assert_equal(Array, result.class)
  178. assert(result)
  179. end
  180. test 'token auth - customer' do
  181. customer_token = Token.create(
  182. action: 'api',
  183. persistent: true,
  184. user_id: @customer.id,
  185. )
  186. customer_credentials = "Token token=#{customer_token.name}"
  187. Setting.set('api_token_access', false)
  188. get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
  189. assert_response(401)
  190. result = JSON.parse(@response.body)
  191. assert_equal(Hash, result.class)
  192. assert_equal('API token access disabled!', result['error'])
  193. Setting.set('api_token_access', true)
  194. get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
  195. assert_response(200)
  196. result = JSON.parse(@response.body)
  197. assert_equal(Array, result.class)
  198. assert(result)
  199. end
  200. test 'token auth - invalid user - admin' do
  201. admin_token = Token.create(
  202. action: 'api',
  203. persistent: true,
  204. user_id: @admin.id,
  205. )
  206. admin_credentials = "Token token=#{admin_token.name}"
  207. @admin.active = false
  208. @admin.save!
  209. Setting.set('api_token_access', false)
  210. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  211. assert_response(401)
  212. result = JSON.parse(@response.body)
  213. assert_equal(Hash, result.class)
  214. assert_equal('API token access disabled!', result['error'])
  215. Setting.set('api_token_access', true)
  216. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  217. assert_response(401)
  218. result = JSON.parse(@response.body)
  219. assert_equal(Hash, result.class)
  220. assert_equal('User is inactive!', result['error'])
  221. end
  222. test 'token auth - expired' do
  223. Setting.set('api_token_access', true)
  224. admin_token = Token.create(
  225. action: 'api',
  226. persistent: true,
  227. user_id: @admin.id,
  228. expires_at: Time.zone.today
  229. )
  230. admin_credentials = "Token token=#{admin_token.name}"
  231. get '/api/v1/tickets', {}, @headers.merge('Authorization' => admin_credentials)
  232. assert_response(401)
  233. result = JSON.parse(@response.body)
  234. assert_equal(Hash, result.class)
  235. assert_equal('Not authorized (token expired)!', result['error'])
  236. admin_token.reload
  237. assert_in_delta(admin_token.last_used_at, Time.zone.now, 1.second)
  238. end
  239. test 'token auth - not expired' do
  240. Setting.set('api_token_access', true)
  241. admin_token = Token.create(
  242. action: 'api',
  243. persistent: true,
  244. user_id: @admin.id,
  245. expires_at: Time.zone.tomorrow
  246. )
  247. admin_credentials = "Token token=#{admin_token.name}"
  248. get '/api/v1/tickets', {}, @headers.merge('Authorization' => admin_credentials)
  249. assert_response(200)
  250. result = JSON.parse(@response.body)
  251. assert_equal(Array, result.class)
  252. assert(result)
  253. admin_token.reload
  254. assert_in_delta(admin_token.last_used_at, Time.zone.now, 1.second)
  255. end
  256. end