api_auth_spec.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. require 'rails_helper'
  2. RSpec.describe 'Api Auth', type: :request do
  3. let(:admin_user) do
  4. create(:admin_user)
  5. end
  6. let(:agent_user) do
  7. create(:agent_user)
  8. end
  9. let(:customer_user) do
  10. create(:customer_user)
  11. end
  12. describe 'request handling' do
  13. it 'does basic auth - admin' do
  14. Setting.set('api_password_access', false)
  15. authenticated_as(admin_user)
  16. get '/api/v1/sessions', params: {}, as: :json
  17. expect(response).to have_http_status(401)
  18. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  19. expect(json_response).to be_a_kind_of(Hash)
  20. expect(json_response['error']).to eq('API password access disabled!')
  21. Setting.set('api_password_access', true)
  22. get '/api/v1/sessions', params: {}, as: :json
  23. expect(response).to have_http_status(200)
  24. expect(response.header['Access-Control-Allow-Origin']).to eq('*')
  25. expect(json_response).to be_a_kind_of(Hash)
  26. expect(json_response).to be_truthy
  27. end
  28. it 'does basic auth - agent' do
  29. Setting.set('api_password_access', false)
  30. authenticated_as(agent_user)
  31. get '/api/v1/tickets', params: {}, as: :json
  32. expect(response).to have_http_status(401)
  33. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  34. expect(json_response).to be_a_kind_of(Hash)
  35. expect(json_response['error']).to eq('API password access disabled!')
  36. Setting.set('api_password_access', true)
  37. get '/api/v1/tickets', params: {}, as: :json
  38. expect(response).to have_http_status(200)
  39. expect(response.header['Access-Control-Allow-Origin']).to eq('*')
  40. expect(json_response).to be_a_kind_of(Array)
  41. expect(json_response).to be_truthy
  42. end
  43. it 'does basic auth - customer' do
  44. Setting.set('api_password_access', false)
  45. authenticated_as(customer_user)
  46. get '/api/v1/tickets', params: {}, as: :json
  47. expect(response).to have_http_status(401)
  48. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  49. expect(json_response).to be_a_kind_of(Hash)
  50. expect(json_response['error']).to eq('API password access disabled!')
  51. Setting.set('api_password_access', true)
  52. get '/api/v1/tickets', params: {}, as: :json
  53. expect(response).to have_http_status(200)
  54. expect(response.header['Access-Control-Allow-Origin']).to eq('*')
  55. expect(json_response).to be_a_kind_of(Array)
  56. expect(json_response).to be_truthy
  57. end
  58. it 'does token auth - admin', last_admin_check: false do
  59. admin_token = create(
  60. :token,
  61. action: 'api',
  62. persistent: true,
  63. user_id: admin_user.id,
  64. preferences: {
  65. permission: ['admin.session'],
  66. },
  67. )
  68. authenticated_as(admin_user, token: admin_token)
  69. Setting.set('api_token_access', false)
  70. get '/api/v1/sessions', params: {}, as: :json
  71. expect(response).to have_http_status(401)
  72. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  73. expect(json_response).to be_a_kind_of(Hash)
  74. expect(json_response['error']).to eq('API token access disabled!')
  75. Setting.set('api_token_access', true)
  76. get '/api/v1/sessions', params: {}, as: :json
  77. expect(response).to have_http_status(200)
  78. expect(response.header['Access-Control-Allow-Origin']).to eq('*')
  79. expect(json_response).to be_a_kind_of(Hash)
  80. expect(json_response).to be_truthy
  81. admin_token.preferences[:permission] = ['admin.session_not_existing']
  82. admin_token.save!
  83. get '/api/v1/sessions', params: {}, as: :json
  84. expect(response).to have_http_status(401)
  85. expect(json_response).to be_a_kind_of(Hash)
  86. expect(json_response['error']).to eq('Not authorized (token)!')
  87. admin_token.preferences[:permission] = []
  88. admin_token.save!
  89. get '/api/v1/sessions', params: {}, as: :json
  90. expect(response).to have_http_status(401)
  91. expect(json_response).to be_a_kind_of(Hash)
  92. expect(json_response['error']).to eq('Not authorized (token)!')
  93. admin_user.active = false
  94. admin_user.save!
  95. get '/api/v1/sessions', params: {}, as: :json
  96. expect(response).to have_http_status(401)
  97. expect(json_response).to be_a_kind_of(Hash)
  98. expect(json_response['error']).to eq('User is inactive!')
  99. admin_token.preferences[:permission] = ['admin.session']
  100. admin_token.save!
  101. get '/api/v1/sessions', params: {}, as: :json
  102. expect(response).to have_http_status(401)
  103. expect(json_response).to be_a_kind_of(Hash)
  104. expect(json_response['error']).to eq('User is inactive!')
  105. admin_user.active = true
  106. admin_user.save!
  107. get '/api/v1/sessions', params: {}, as: :json
  108. expect(response).to have_http_status(200)
  109. expect(json_response).to be_a_kind_of(Hash)
  110. expect(json_response).to be_truthy
  111. get '/api/v1/roles', params: {}, as: :json
  112. expect(response).to have_http_status(401)
  113. expect(json_response).to be_a_kind_of(Hash)
  114. expect(json_response['error']).to eq('Not authorized (token)!')
  115. admin_token.preferences[:permission] = ['admin.session_not_existing', 'admin.role']
  116. admin_token.save!
  117. get '/api/v1/roles', params: {}, as: :json
  118. expect(response).to have_http_status(200)
  119. expect(json_response).to be_a_kind_of(Array)
  120. expect(json_response).to be_truthy
  121. admin_token.preferences[:permission] = ['ticket.agent']
  122. admin_token.save!
  123. get '/api/v1/organizations', params: {}, as: :json
  124. expect(response).to have_http_status(200)
  125. expect(json_response).to be_a_kind_of(Array)
  126. expect(json_response).to be_truthy
  127. name = "some org name #{rand(999_999_999)}"
  128. post '/api/v1/organizations', params: { name: name }, as: :json
  129. expect(response).to have_http_status(201)
  130. expect(json_response).to be_a_kind_of(Hash)
  131. expect(json_response['name']).to eq(name)
  132. expect(json_response).to be_truthy
  133. name = "some org name #{rand(999_999_999)} - 2"
  134. put "/api/v1/organizations/#{json_response['id']}", params: { name: name }, as: :json
  135. expect(response).to have_http_status(200)
  136. expect(json_response).to be_a_kind_of(Hash)
  137. expect(json_response['name']).to eq(name)
  138. expect(json_response).to be_truthy
  139. admin_token.preferences[:permission] = ['admin.organization']
  140. admin_token.save!
  141. get '/api/v1/organizations', params: {}, as: :json
  142. expect(response).to have_http_status(200)
  143. expect(json_response).to be_a_kind_of(Array)
  144. expect(json_response).to be_truthy
  145. name = "some org name #{rand(999_999_999)}"
  146. post '/api/v1/organizations', params: { name: name }, as: :json
  147. expect(response).to have_http_status(201)
  148. expect(json_response).to be_a_kind_of(Hash)
  149. expect(json_response['name']).to eq(name)
  150. expect(json_response).to be_truthy
  151. name = "some org name #{rand(999_999_999)} - 2"
  152. put "/api/v1/organizations/#{json_response['id']}", params: { name: name }, as: :json
  153. expect(response).to have_http_status(200)
  154. expect(json_response).to be_a_kind_of(Hash)
  155. expect(json_response['name']).to eq(name)
  156. expect(json_response).to be_truthy
  157. admin_token.preferences[:permission] = ['admin']
  158. admin_token.save!
  159. get '/api/v1/organizations', params: {}, as: :json
  160. expect(response).to have_http_status(200)
  161. expect(json_response).to be_a_kind_of(Array)
  162. expect(json_response).to be_truthy
  163. name = "some org name #{rand(999_999_999)}"
  164. post '/api/v1/organizations', params: { name: name }, as: :json
  165. expect(response).to have_http_status(201)
  166. expect(json_response).to be_a_kind_of(Hash)
  167. expect(json_response['name']).to eq(name)
  168. expect(json_response).to be_truthy
  169. name = "some org name #{rand(999_999_999)} - 2"
  170. put "/api/v1/organizations/#{json_response['id']}", params: { name: name }, as: :json
  171. expect(response).to have_http_status(200)
  172. expect(json_response).to be_a_kind_of(Hash)
  173. expect(json_response['name']).to eq(name)
  174. expect(json_response).to be_truthy
  175. end
  176. it 'does token auth - agent' do
  177. agent_token = create(
  178. :token,
  179. action: 'api',
  180. persistent: true,
  181. user_id: agent_user.id,
  182. )
  183. authenticated_as(agent_user, token: agent_token)
  184. Setting.set('api_token_access', false)
  185. get '/api/v1/tickets', params: {}, as: :json
  186. expect(response).to have_http_status(401)
  187. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  188. expect(json_response).to be_a_kind_of(Hash)
  189. expect(json_response['error']).to eq('API token access disabled!')
  190. Setting.set('api_token_access', true)
  191. get '/api/v1/tickets', params: {}, as: :json
  192. expect(response).to have_http_status(200)
  193. expect(response.header['Access-Control-Allow-Origin']).to eq('*')
  194. expect(json_response).to be_a_kind_of(Array)
  195. expect(json_response).to be_truthy
  196. get '/api/v1/organizations', params: {}, as: :json
  197. expect(response).to have_http_status(200)
  198. expect(json_response).to be_a_kind_of(Array)
  199. expect(json_response).to be_truthy
  200. name = "some org name #{rand(999_999_999)}"
  201. post '/api/v1/organizations', params: { name: name }, as: :json
  202. expect(response).to have_http_status(401)
  203. end
  204. it 'does token auth - customer' do
  205. customer_token = create(
  206. :token,
  207. action: 'api',
  208. persistent: true,
  209. user_id: customer_user.id,
  210. )
  211. authenticated_as(customer_user, token: customer_token)
  212. Setting.set('api_token_access', false)
  213. get '/api/v1/tickets', params: {}, as: :json
  214. expect(response).to have_http_status(401)
  215. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  216. expect(json_response).to be_a_kind_of(Hash)
  217. expect(json_response['error']).to eq('API token access disabled!')
  218. Setting.set('api_token_access', true)
  219. get '/api/v1/tickets', params: {}, as: :json
  220. expect(response.header['Access-Control-Allow-Origin']).to eq('*')
  221. expect(response).to have_http_status(200)
  222. expect(json_response).to be_a_kind_of(Array)
  223. expect(json_response).to be_truthy
  224. get '/api/v1/organizations', params: {}, as: :json
  225. expect(response).to have_http_status(200)
  226. expect(json_response).to be_a_kind_of(Array)
  227. expect(json_response).to be_truthy
  228. name = "some org name #{rand(999_999_999)}"
  229. post '/api/v1/organizations', params: { name: name }, as: :json
  230. expect(response).to have_http_status(401)
  231. end
  232. it 'does token auth - invalid user - admin', last_admin_check: false do
  233. admin_token = create(
  234. :token,
  235. action: 'api',
  236. persistent: true,
  237. user_id: admin_user.id,
  238. )
  239. authenticated_as(admin_user, token: admin_token)
  240. admin_user.active = false
  241. admin_user.save!
  242. Setting.set('api_token_access', false)
  243. get '/api/v1/sessions', params: {}, as: :json
  244. expect(response).to have_http_status(401)
  245. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  246. expect(json_response).to be_a_kind_of(Hash)
  247. expect(json_response['error']).to eq('API token access disabled!')
  248. Setting.set('api_token_access', true)
  249. get '/api/v1/sessions', params: {}, as: :json
  250. expect(response).to have_http_status(401)
  251. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  252. expect(json_response).to be_a_kind_of(Hash)
  253. expect(json_response['error']).to eq('User is inactive!')
  254. end
  255. it 'does token auth - expired' do
  256. Setting.set('api_token_access', true)
  257. admin_token = create(
  258. :token,
  259. action: 'api',
  260. persistent: true,
  261. user_id: admin_user.id,
  262. expires_at: Time.zone.today
  263. )
  264. authenticated_as(admin_user, token: admin_token)
  265. get '/api/v1/tickets', params: {}, as: :json
  266. expect(response).to have_http_status(401)
  267. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  268. expect(json_response).to be_a_kind_of(Hash)
  269. expect(json_response['error']).to eq('Not authorized (token expired)!')
  270. admin_token.reload
  271. expect(admin_token.last_used_at).to be_within(1.second).of(Time.zone.now)
  272. end
  273. it 'does token auth - not expired' do
  274. Setting.set('api_token_access', true)
  275. admin_token = create(
  276. :token,
  277. action: 'api',
  278. persistent: true,
  279. user_id: admin_user.id,
  280. expires_at: Time.zone.tomorrow
  281. )
  282. authenticated_as(admin_user, token: admin_token)
  283. get '/api/v1/tickets', params: {}, as: :json
  284. expect(response).to have_http_status(200)
  285. expect(response.header['Access-Control-Allow-Origin']).to eq('*')
  286. expect(json_response).to be_a_kind_of(Array)
  287. expect(json_response).to be_truthy
  288. admin_token.reload
  289. expect(admin_token.last_used_at).to be_within(1.second).of(Time.zone.now)
  290. end
  291. it 'does session auth - admin' do
  292. create(:admin_user, login: 'api-admin@example.com', password: 'adminpw')
  293. post '/api/v1/signin', params: { username: 'api-admin@example.com', password: 'adminpw', fingerprint: '123456789' }
  294. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  295. expect(response).to have_http_status(201)
  296. get '/api/v1/sessions', params: {}
  297. expect(response).to have_http_status(200)
  298. expect(response.header.key?('Access-Control-Allow-Origin')).to be_falsey
  299. expect(json_response).to be_a_kind_of(Hash)
  300. expect(json_response).to be_truthy
  301. end
  302. end
  303. end