api_auth_controller_test.rb 11 KB

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