api_auth_controller_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. admin_token.preferences[:permission] = ['ticket.agent']
  168. admin_token.save!
  169. get '/api/v1/organizations', {}, @headers.merge('Authorization' => admin_credentials)
  170. assert_response(200)
  171. result = JSON.parse(@response.body)
  172. assert_equal(Array, result.class)
  173. assert(result)
  174. name = "some org name #{rand(999_999_999)}"
  175. post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
  176. assert_response(201)
  177. result = JSON.parse(@response.body)
  178. assert_equal(Hash, result.class)
  179. assert_equal(name, result['name'])
  180. assert(result)
  181. name = "some org name #{rand(999_999_999)} - 2"
  182. put "/api/v1/organizations/#{result['id']}", { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
  183. assert_response(200)
  184. result = JSON.parse(@response.body)
  185. assert_equal(Hash, result.class)
  186. assert_equal(name, result['name'])
  187. assert(result)
  188. admin_token.preferences[:permission] = ['admin.organization']
  189. admin_token.save!
  190. get '/api/v1/organizations', {}, @headers.merge('Authorization' => admin_credentials)
  191. assert_response(200)
  192. result = JSON.parse(@response.body)
  193. assert_equal(Array, result.class)
  194. assert(result)
  195. name = "some org name #{rand(999_999_999)}"
  196. post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
  197. assert_response(201)
  198. result = JSON.parse(@response.body)
  199. assert_equal(Hash, result.class)
  200. assert_equal(name, result['name'])
  201. assert(result)
  202. name = "some org name #{rand(999_999_999)} - 2"
  203. put "/api/v1/organizations/#{result['id']}", { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
  204. assert_response(200)
  205. result = JSON.parse(@response.body)
  206. assert_equal(Hash, result.class)
  207. assert_equal(name, result['name'])
  208. assert(result)
  209. admin_token.preferences[:permission] = ['admin']
  210. admin_token.save!
  211. get '/api/v1/organizations', {}, @headers.merge('Authorization' => admin_credentials)
  212. assert_response(200)
  213. result = JSON.parse(@response.body)
  214. assert_equal(Array, result.class)
  215. assert(result)
  216. name = "some org name #{rand(999_999_999)}"
  217. post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
  218. assert_response(201)
  219. result = JSON.parse(@response.body)
  220. assert_equal(Hash, result.class)
  221. assert_equal(name, result['name'])
  222. assert(result)
  223. name = "some org name #{rand(999_999_999)} - 2"
  224. put "/api/v1/organizations/#{result['id']}", { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
  225. assert_response(200)
  226. result = JSON.parse(@response.body)
  227. assert_equal(Hash, result.class)
  228. assert_equal(name, result['name'])
  229. assert(result)
  230. end
  231. test 'token auth - agent' do
  232. agent_token = Token.create(
  233. action: 'api',
  234. persistent: true,
  235. user_id: @agent.id,
  236. )
  237. agent_credentials = "Token token=#{agent_token.name}"
  238. Setting.set('api_token_access', false)
  239. get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
  240. assert_response(401)
  241. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  242. result = JSON.parse(@response.body)
  243. assert_equal(Hash, result.class)
  244. assert_equal('API token access disabled!', result['error'])
  245. Setting.set('api_token_access', true)
  246. get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
  247. assert_response(200)
  248. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  249. result = JSON.parse(@response.body)
  250. assert_equal(Array, result.class)
  251. assert(result)
  252. get '/api/v1/organizations', {}, @headers.merge('Authorization' => agent_credentials)
  253. assert_response(200)
  254. result = JSON.parse(@response.body)
  255. assert_equal(Array, result.class)
  256. assert(result)
  257. name = "some org name #{rand(999_999_999)}"
  258. post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => agent_credentials)
  259. assert_response(401)
  260. end
  261. test 'token auth - customer' do
  262. customer_token = Token.create(
  263. action: 'api',
  264. persistent: true,
  265. user_id: @customer.id,
  266. )
  267. customer_credentials = "Token token=#{customer_token.name}"
  268. Setting.set('api_token_access', false)
  269. get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
  270. assert_response(401)
  271. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  272. result = JSON.parse(@response.body)
  273. assert_equal(Hash, result.class)
  274. assert_equal('API token access disabled!', result['error'])
  275. Setting.set('api_token_access', true)
  276. get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
  277. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  278. assert_response(200)
  279. result = JSON.parse(@response.body)
  280. assert_equal(Array, result.class)
  281. assert(result)
  282. get '/api/v1/organizations', {}, @headers.merge('Authorization' => customer_credentials)
  283. assert_response(200)
  284. result = JSON.parse(@response.body)
  285. assert_equal(Array, result.class)
  286. assert(result)
  287. name = "some org name #{rand(999_999_999)}"
  288. post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => customer_credentials)
  289. assert_response(401)
  290. end
  291. test 'token auth - invalid user - admin' do
  292. admin_token = Token.create(
  293. action: 'api',
  294. persistent: true,
  295. user_id: @admin.id,
  296. )
  297. admin_credentials = "Token token=#{admin_token.name}"
  298. @admin.active = false
  299. @admin.save!
  300. Setting.set('api_token_access', false)
  301. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  302. assert_response(401)
  303. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  304. result = JSON.parse(@response.body)
  305. assert_equal(Hash, result.class)
  306. assert_equal('API token access disabled!', result['error'])
  307. Setting.set('api_token_access', true)
  308. get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
  309. assert_response(401)
  310. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  311. result = JSON.parse(@response.body)
  312. assert_equal(Hash, result.class)
  313. assert_equal('User is inactive!', result['error'])
  314. end
  315. test 'token auth - expired' do
  316. Setting.set('api_token_access', true)
  317. admin_token = Token.create(
  318. action: 'api',
  319. persistent: true,
  320. user_id: @admin.id,
  321. expires_at: Time.zone.today
  322. )
  323. admin_credentials = "Token token=#{admin_token.name}"
  324. get '/api/v1/tickets', {}, @headers.merge('Authorization' => admin_credentials)
  325. assert_response(401)
  326. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  327. result = JSON.parse(@response.body)
  328. assert_equal(Hash, result.class)
  329. assert_equal('Not authorized (token expired)!', result['error'])
  330. admin_token.reload
  331. assert_in_delta(admin_token.last_used_at, Time.zone.now, 1.second)
  332. end
  333. test 'token auth - not expired' do
  334. Setting.set('api_token_access', true)
  335. admin_token = Token.create(
  336. action: 'api',
  337. persistent: true,
  338. user_id: @admin.id,
  339. expires_at: Time.zone.tomorrow
  340. )
  341. admin_credentials = "Token token=#{admin_token.name}"
  342. get '/api/v1/tickets', {}, @headers.merge('Authorization' => admin_credentials)
  343. assert_response(200)
  344. assert_equal('*', @response.header['Access-Control-Allow-Origin'])
  345. result = JSON.parse(@response.body)
  346. assert_equal(Array, result.class)
  347. assert(result)
  348. admin_token.reload
  349. assert_in_delta(admin_token.last_used_at, Time.zone.now, 1.second)
  350. end
  351. test 'session auth - admin' do
  352. post '/api/v1/signin', { username: 'api-admin@example.com', password: 'adminpw', fingerprint: '123456789' }
  353. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  354. assert_response(201)
  355. get '/api/v1/sessions', {}
  356. assert_response(200)
  357. assert_not(@response.header.key?('Access-Control-Allow-Origin'))
  358. result = JSON.parse(@response.body)
  359. assert_equal(Hash, result.class)
  360. assert(result)
  361. end
  362. end