api_auth_controller_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. require 'test_helper'
  2. class ApiAuthControllerTest < 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: 'api-admin',
  12. firstname: 'API',
  13. lastname: 'Admin',
  14. email: 'api-admin@example.com',
  15. password: 'adminpw',
  16. active: true,
  17. roles: roles,
  18. groups: groups,
  19. )
  20. # create agent
  21. roles = Role.where(name: 'Agent')
  22. @agent = User.create!(
  23. login: 'api-agent@example.com',
  24. firstname: 'API',
  25. lastname: 'Agent',
  26. email: 'api-agent@example.com',
  27. password: 'agentpw',
  28. active: true,
  29. roles: roles,
  30. groups: groups,
  31. )
  32. # create customer without org
  33. roles = Role.where(name: 'Customer')
  34. @customer = User.create!(
  35. login: 'api-customer1@example.com',
  36. firstname: 'API',
  37. lastname: 'Customer1',
  38. email: 'api-customer1@example.com',
  39. password: 'customer1pw',
  40. active: true,
  41. roles: roles,
  42. )
  43. end
  44. test 'basic auth - admin' do
  45. admin_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-admin@example.com', 'adminpw')
  46. Setting.set('api_password_access', false)
  47. get '/api/v1/sessions', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  48. assert_response(401)
  49. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  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', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  55. assert_response(200)
  56. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  57. result = JSON.parse(@response.body)
  58. assert_equal(Hash, result.class)
  59. assert(result)
  60. end
  61. test 'basic auth - agent' do
  62. agent_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-agent@example.com', 'agentpw')
  63. Setting.set('api_password_access', false)
  64. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => agent_credentials)
  65. assert_response(401)
  66. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  67. result = JSON.parse(@response.body)
  68. assert_equal(Hash, result.class)
  69. assert_equal('API password access disabled!', result['error'])
  70. Setting.set('api_password_access', true)
  71. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => agent_credentials)
  72. assert_response(200)
  73. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  74. result = JSON.parse(@response.body)
  75. assert_equal(Array, result.class)
  76. assert(result)
  77. end
  78. test 'basic auth - customer' do
  79. customer_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-customer1@example.com', 'customer1pw')
  80. Setting.set('api_password_access', false)
  81. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => customer_credentials)
  82. assert_response(401)
  83. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  84. result = JSON.parse(@response.body)
  85. assert_equal(Hash, result.class)
  86. assert_equal('API password access disabled!', result['error'])
  87. Setting.set('api_password_access', true)
  88. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => customer_credentials)
  89. assert_response(200)
  90. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  91. result = JSON.parse(@response.body)
  92. assert_equal(Array, result.class)
  93. assert(result)
  94. end
  95. test 'token auth - admin' do
  96. admin_token = Token.create(
  97. action: 'api',
  98. persistent: true,
  99. user_id: @admin.id,
  100. preferences: {
  101. permission: ['admin.session'],
  102. },
  103. )
  104. admin_credentials = "Token token=#{admin_token.name}"
  105. Setting.set('api_token_access', false)
  106. get '/api/v1/sessions', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  107. assert_response(401)
  108. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  109. result = JSON.parse(@response.body)
  110. assert_equal(Hash, result.class)
  111. assert_equal('API token access disabled!', result['error'])
  112. Setting.set('api_token_access', true)
  113. get '/api/v1/sessions', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  114. assert_response(200)
  115. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  116. result = JSON.parse(@response.body)
  117. assert_equal(Hash, result.class)
  118. assert(result)
  119. admin_token.preferences[:permission] = ['admin.session_not_existing']
  120. admin_token.save!
  121. get '/api/v1/sessions', params: {}, headers: @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_token.preferences[:permission] = []
  127. admin_token.save!
  128. get '/api/v1/sessions', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  129. assert_response(401)
  130. result = JSON.parse(@response.body)
  131. assert_equal(Hash, result.class)
  132. assert_equal('Not authorized (token)!', result['error'])
  133. @admin.active = false
  134. @admin.save!
  135. get '/api/v1/sessions', params: {}, headers: @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_token.preferences[:permission] = ['admin.session']
  141. admin_token.save!
  142. get '/api/v1/sessions', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  143. assert_response(401)
  144. result = JSON.parse(@response.body)
  145. assert_equal(Hash, result.class)
  146. assert_equal('User is inactive!', result['error'])
  147. @admin.active = true
  148. @admin.save!
  149. get '/api/v1/sessions', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  150. assert_response(200)
  151. result = JSON.parse(@response.body)
  152. assert_equal(Hash, result.class)
  153. assert(result)
  154. get '/api/v1/roles', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  155. assert_response(401)
  156. result = JSON.parse(@response.body)
  157. assert_equal(Hash, result.class)
  158. assert_equal('Not authorized (token)!', result['error'])
  159. admin_token.preferences[:permission] = ['admin.session_not_existing', 'admin.role']
  160. admin_token.save!
  161. get '/api/v1/roles', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  162. assert_response(200)
  163. result = JSON.parse(@response.body)
  164. assert_equal(Array, result.class)
  165. assert(result)
  166. admin_token.preferences[:permission] = ['ticket.agent']
  167. admin_token.save!
  168. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  169. assert_response(200)
  170. result = JSON.parse(@response.body)
  171. assert_equal(Array, result.class)
  172. assert(result)
  173. name = "some org name #{rand(999_999_999)}"
  174. post '/api/v1/organizations', params: { name: name }.to_json, headers: @headers.merge('Authorization' => admin_credentials)
  175. assert_response(201)
  176. result = JSON.parse(@response.body)
  177. assert_equal(Hash, result.class)
  178. assert_equal(name, result['name'])
  179. assert(result)
  180. name = "some org name #{rand(999_999_999)} - 2"
  181. put "/api/v1/organizations/#{result['id']}", params: { name: name }.to_json, headers: @headers.merge('Authorization' => admin_credentials)
  182. assert_response(200)
  183. result = JSON.parse(@response.body)
  184. assert_equal(Hash, result.class)
  185. assert_equal(name, result['name'])
  186. assert(result)
  187. admin_token.preferences[:permission] = ['admin.organization']
  188. admin_token.save!
  189. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  190. assert_response(200)
  191. result = JSON.parse(@response.body)
  192. assert_equal(Array, result.class)
  193. assert(result)
  194. name = "some org name #{rand(999_999_999)}"
  195. post '/api/v1/organizations', params: { name: name }.to_json, headers: @headers.merge('Authorization' => admin_credentials)
  196. assert_response(201)
  197. result = JSON.parse(@response.body)
  198. assert_equal(Hash, result.class)
  199. assert_equal(name, result['name'])
  200. assert(result)
  201. name = "some org name #{rand(999_999_999)} - 2"
  202. put "/api/v1/organizations/#{result['id']}", params: { name: name }.to_json, headers: @headers.merge('Authorization' => admin_credentials)
  203. assert_response(200)
  204. result = JSON.parse(@response.body)
  205. assert_equal(Hash, result.class)
  206. assert_equal(name, result['name'])
  207. assert(result)
  208. admin_token.preferences[:permission] = ['admin']
  209. admin_token.save!
  210. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  211. assert_response(200)
  212. result = JSON.parse(@response.body)
  213. assert_equal(Array, result.class)
  214. assert(result)
  215. name = "some org name #{rand(999_999_999)}"
  216. post '/api/v1/organizations', params: { name: name }.to_json, headers: @headers.merge('Authorization' => admin_credentials)
  217. assert_response(201)
  218. result = JSON.parse(@response.body)
  219. assert_equal(Hash, result.class)
  220. assert_equal(name, result['name'])
  221. assert(result)
  222. name = "some org name #{rand(999_999_999)} - 2"
  223. put "/api/v1/organizations/#{result['id']}", params: { name: name }.to_json, headers: @headers.merge('Authorization' => admin_credentials)
  224. assert_response(200)
  225. result = JSON.parse(@response.body)
  226. assert_equal(Hash, result.class)
  227. assert_equal(name, result['name'])
  228. assert(result)
  229. end
  230. test 'token auth - agent' do
  231. agent_token = Token.create(
  232. action: 'api',
  233. persistent: true,
  234. user_id: @agent.id,
  235. )
  236. agent_credentials = "Token token=#{agent_token.name}"
  237. Setting.set('api_token_access', false)
  238. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => agent_credentials)
  239. assert_response(401)
  240. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  241. result = JSON.parse(@response.body)
  242. assert_equal(Hash, result.class)
  243. assert_equal('API token access disabled!', result['error'])
  244. Setting.set('api_token_access', true)
  245. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => agent_credentials)
  246. assert_response(200)
  247. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  248. result = JSON.parse(@response.body)
  249. assert_equal(Array, result.class)
  250. assert(result)
  251. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => agent_credentials)
  252. assert_response(200)
  253. result = JSON.parse(@response.body)
  254. assert_equal(Array, result.class)
  255. assert(result)
  256. name = "some org name #{rand(999_999_999)}"
  257. post '/api/v1/organizations', params: { name: name }.to_json, headers: @headers.merge('Authorization' => agent_credentials)
  258. assert_response(401)
  259. end
  260. test 'token auth - customer' do
  261. customer_token = Token.create(
  262. action: 'api',
  263. persistent: true,
  264. user_id: @customer.id,
  265. )
  266. customer_credentials = "Token token=#{customer_token.name}"
  267. Setting.set('api_token_access', false)
  268. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => customer_credentials)
  269. assert_response(401)
  270. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  271. result = JSON.parse(@response.body)
  272. assert_equal(Hash, result.class)
  273. assert_equal('API token access disabled!', result['error'])
  274. Setting.set('api_token_access', true)
  275. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => customer_credentials)
  276. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  277. assert_response(200)
  278. result = JSON.parse(@response.body)
  279. assert_equal(Array, result.class)
  280. assert(result)
  281. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => customer_credentials)
  282. assert_response(200)
  283. result = JSON.parse(@response.body)
  284. assert_equal(Array, result.class)
  285. assert(result)
  286. name = "some org name #{rand(999_999_999)}"
  287. post '/api/v1/organizations', params: { name: name }.to_json, headers: @headers.merge('Authorization' => customer_credentials)
  288. assert_response(401)
  289. end
  290. test 'token auth - invalid user - admin' do
  291. admin_token = Token.create(
  292. action: 'api',
  293. persistent: true,
  294. user_id: @admin.id,
  295. )
  296. admin_credentials = "Token token=#{admin_token.name}"
  297. @admin.active = false
  298. @admin.save!
  299. Setting.set('api_token_access', false)
  300. get '/api/v1/sessions', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  301. assert_response(401)
  302. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  303. result = JSON.parse(@response.body)
  304. assert_equal(Hash, result.class)
  305. assert_equal('API token access disabled!', result['error'])
  306. Setting.set('api_token_access', true)
  307. get '/api/v1/sessions', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  308. assert_response(401)
  309. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  310. result = JSON.parse(@response.body)
  311. assert_equal(Hash, result.class)
  312. assert_equal('User is inactive!', result['error'])
  313. end
  314. test 'token auth - expired' do
  315. Setting.set('api_token_access', true)
  316. admin_token = Token.create(
  317. action: 'api',
  318. persistent: true,
  319. user_id: @admin.id,
  320. expires_at: Time.zone.today
  321. )
  322. admin_credentials = "Token token=#{admin_token.name}"
  323. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  324. assert_response(401)
  325. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  326. result = JSON.parse(@response.body)
  327. assert_equal(Hash, result.class)
  328. assert_equal('Not authorized (token expired)!', result['error'])
  329. admin_token.reload
  330. assert_in_delta(admin_token.last_used_at, Time.zone.now, 1.second)
  331. end
  332. test 'token auth - not expired' do
  333. Setting.set('api_token_access', true)
  334. admin_token = Token.create(
  335. action: 'api',
  336. persistent: true,
  337. user_id: @admin.id,
  338. expires_at: Time.zone.tomorrow
  339. )
  340. admin_credentials = "Token token=#{admin_token.name}"
  341. get '/api/v1/tickets', params: {}, headers: @headers.merge('Authorization' => admin_credentials)
  342. assert_response(200)
  343. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  344. result = JSON.parse(@response.body)
  345. assert_equal(Array, result.class)
  346. assert(result)
  347. admin_token.reload
  348. assert_in_delta(admin_token.last_used_at, Time.zone.now, 1.second)
  349. end
  350. test 'session auth - admin' do
  351. post '/api/v1/signin', params: { username: 'api-admin@example.com', password: 'adminpw', fingerprint: '123456789' }
  352. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  353. assert_response(201)
  354. get '/api/v1/sessions', params: {}
  355. assert_response(200)
  356. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  357. result = JSON.parse(@response.body)
  358. assert_equal(Hash, result.class)
  359. assert(result)
  360. end
  361. end