two_factor_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'User', current_user_id: 1, performs_jobs: true, type: :request do
  4. let(:agent) { create(:agent) }
  5. let(:admin) { create(:admin) }
  6. let(:two_factor_pref) { create(:user_two_factor_preference, :authenticator_app, user: agent) }
  7. let(:two_factor_enabled) { true }
  8. before do |example|
  9. Setting.set('two_factor_authentication_method_authenticator_app', two_factor_enabled)
  10. two_factor_pref
  11. if example.metadata[:as] == :admin
  12. action_user = admin
  13. permissions = %w[admin.user]
  14. else
  15. action_user = agent
  16. permissions = %w[ticket.agent]
  17. end
  18. authenticated_as(action_user, token: create(:token, user: action_user, permissions: permissions))
  19. end
  20. describe 'DELETE /users/:id/two_factor_remove_authentication_method' do
  21. context 'when agent' do
  22. it 'gets the result', :aggregate_failures do
  23. delete "/api/v1/users/#{agent.id}/two_factor_remove_authentication_method", params: { method: 'authenticator_app' }, as: :json
  24. expect(response).to have_http_status(:ok)
  25. expect { two_factor_pref.reload }.to raise_error(ActiveRecord::RecordNotFound)
  26. end
  27. end
  28. context 'when admin', as: :admin do
  29. it 'gets the result', :aggregate_failures do
  30. delete "/api/v1/users/#{agent.id}/two_factor_remove_authentication_method", params: { method: 'authenticator_app' }, as: :json
  31. expect(response).to have_http_status(:ok)
  32. expect { two_factor_pref.reload }.to raise_error(ActiveRecord::RecordNotFound)
  33. end
  34. end
  35. end
  36. describe 'DELETE /users/:id/two_factor_remove_all_authentication_methods' do
  37. context 'when agent' do
  38. it 'gets the result', :aggregate_failures do
  39. delete "/api/v1/users/#{agent.id}/two_factor_remove_all_authentication_methods", as: :json
  40. expect(response).to have_http_status(:ok)
  41. expect { two_factor_pref.reload }.to raise_error(ActiveRecord::RecordNotFound)
  42. end
  43. end
  44. context 'when admin', as: :admin do
  45. it 'gets the result', :aggregate_failures do
  46. delete "/api/v1/users/#{agent.id}/two_factor_remove_all_authentication_methods", as: :json
  47. expect(response).to have_http_status(:ok)
  48. expect { two_factor_pref.reload }.to raise_error(ActiveRecord::RecordNotFound)
  49. end
  50. end
  51. end
  52. describe 'GET /users/two_factor_enabled_authentication_methods' do
  53. context 'with disabled authenticator app method' do
  54. let(:two_factor_enabled) { false }
  55. let(:two_factor_pref) { nil }
  56. it 'returns nothing', :aggregate_failures do
  57. get "/api/v1/users/#{agent.id}/two_factor_enabled_authentication_methods", as: :json
  58. expect(response).to have_http_status(:ok)
  59. expect(json_response).to be_blank
  60. end
  61. end
  62. context 'with not having authenticator app configured' do
  63. let(:two_factor_pref) { nil }
  64. it 'returns the correct result', :aggregate_failures do
  65. get "/api/v1/users/#{agent.id}/two_factor_enabled_authentication_methods", as: :json
  66. expect(response).to have_http_status(:ok)
  67. expect(json_response.first).to eq({
  68. 'method' => 'authenticator_app',
  69. 'configured' => false,
  70. 'default' => false,
  71. })
  72. end
  73. end
  74. context 'with having authenticator app configured' do
  75. it 'returns the correct result', :aggregate_failures do
  76. get "/api/v1/users/#{agent.id}/two_factor_enabled_authentication_methods", as: :json
  77. expect(response).to have_http_status(:ok)
  78. expect(json_response.first).to eq({
  79. 'method' => 'authenticator_app',
  80. 'configured' => true,
  81. 'default' => true,
  82. })
  83. end
  84. end
  85. end
  86. describe 'POST /users/two_factor_verify_configuration' do
  87. let(:recover_codes_enabled) { true }
  88. let(:two_factor_pref) { nil }
  89. let(:params) { {} }
  90. let(:method) { 'authenticator_app' }
  91. let(:verification_code) { ROTP::TOTP.new(configuration[:secret]).now }
  92. let(:configuration) { agent.auth_two_factor.authentication_method_object(method).initiate_configuration }
  93. before do
  94. Setting.set('two_factor_authentication_recovery_codes', recover_codes_enabled)
  95. post '/api/v1/users/two_factor_verify_configuration', params: params, as: :json
  96. end
  97. it 'fails without needed params' do
  98. expect(response).to have_http_status(:unprocessable_entity)
  99. end
  100. context 'with needed params' do
  101. let(:params) do
  102. {
  103. method: method,
  104. payload: verification_code,
  105. configuration: configuration,
  106. }
  107. end
  108. context 'with wrong verification code' do
  109. let(:verification_code) { 'wrong' }
  110. it 'verified is false' do
  111. expect(json_response['verified']).to be(false)
  112. end
  113. end
  114. context 'with correct verification code', :aggregate_failures do
  115. it 'verified is true' do
  116. expect(json_response['verified']).to be(true)
  117. expect(json_response['recovery_codes'].length).to eq(10)
  118. end
  119. context 'with disabled recovery codes' do
  120. let(:recover_codes_enabled) { false }
  121. it 'verified is true (but without recovery codes)' do
  122. expect(json_response['verified']).to be(true)
  123. expect(json_response['recovery_codes']).to be_nil
  124. end
  125. end
  126. end
  127. end
  128. end
  129. describe 'POST /users/two_factor_recovery_codes_generate' do
  130. let(:recover_codes_enabled) { true }
  131. let(:current_codes) { [] }
  132. before do
  133. Setting.set('two_factor_authentication_recovery_codes', recover_codes_enabled)
  134. current_codes
  135. post '/api/v1/users/two_factor_recovery_codes_generate', params: {}, as: :json
  136. end
  137. context 'with disabled recovery codes' do
  138. let(:recover_codes_enabled) { false }
  139. it 'does not generate codes' do
  140. expect(json_response).to be_nil
  141. end
  142. end
  143. context 'without existing recovery codes' do
  144. it 'does generate codes' do
  145. expect(json_response.length).to eq(10)
  146. end
  147. end
  148. context 'with existing recovery codes' do
  149. let(:current_codes) { Auth::TwoFactor::RecoveryCodes.new(agent).generate }
  150. it 'does not generate codes' do
  151. expect(json_response).not_to eq(current_codes)
  152. end
  153. end
  154. end
  155. describe 'GET /users/two_factor_authentication_method_initiate_configuration/:method' do
  156. let(:two_factor_pref) { nil }
  157. let(:method) { 'authenticator_app' }
  158. before do
  159. get "/api/v1/users/two_factor_authentication_method_initiate_configuration/#{method}", as: :json
  160. end
  161. context 'with invalid params' do
  162. context 'with an unknown method' do
  163. let(:method) { 'unknown' }
  164. it 'fails' do
  165. expect(response).to have_http_status(:unprocessable_entity)
  166. end
  167. end
  168. end
  169. context 'with valid params' do
  170. it 'returns configuration', :aggregate_failures do
  171. expect(response).to have_http_status(:ok)
  172. expect(json_response['configuration']).to include('secret').and include('provisioning_uri')
  173. end
  174. end
  175. end
  176. describe 'GET /users/two_factor_authentication_method_configuration/:method' do
  177. let(:method) { 'authenticator_app' }
  178. before do
  179. get "/api/v1/users/two_factor_authentication_method_configuration/#{method}", as: :json
  180. end
  181. context 'with invalid params' do
  182. context 'with an unknown method' do
  183. let(:method) { 'unknown' }
  184. it 'fails' do
  185. expect(response).to have_http_status(:unprocessable_entity)
  186. end
  187. end
  188. end
  189. context 'with valid params' do
  190. context 'with no stored two-factor preference' do
  191. let(:two_factor_pref) { nil }
  192. it 'returns nothing', :aggregate_failures do
  193. expect(response).to have_http_status(:ok)
  194. expect(json_response['configuration']).to be_empty
  195. end
  196. end
  197. it 'returns configuration', :aggregate_failures do
  198. expect(response).to have_http_status(:ok)
  199. expect(json_response['configuration']).to include('secret').and include('code').and include('provisioning_uri')
  200. end
  201. end
  202. end
  203. describe 'PUT /users/two_factor_authentication_method_configuration/:method' do
  204. let(:method) { 'unknown' }
  205. let(:params) { {} }
  206. before do
  207. put "/api/v1/users/two_factor_authentication_method_configuration/#{method}", params: params, as: :json
  208. end
  209. it 'fails without needed params' do
  210. expect(response).to have_http_status(:unprocessable_entity)
  211. end
  212. context 'with needed params' do
  213. let(:method) { 'authenticator_app' }
  214. context 'when updating configuration' do
  215. let(:secret) { ROTP::Base32.random_base32 }
  216. let(:code) { ROTP::TOTP.new(secret).now }
  217. let(:params) { { configuration: two_factor_pref.configuration.merge(secret: secret, code: code) } }
  218. it 'returns ok and updates configuration', :aggregate_failures do
  219. expect(response).to have_http_status(:ok)
  220. expect(two_factor_pref.reload.configuration).to include('secret' => secret, 'code' => code)
  221. end
  222. end
  223. context 'when removing configuration' do
  224. let(:params) { { configuration: nil } }
  225. it 'returns ok and removes configuration', :aggregate_failures do
  226. expect(response).to have_http_status(:ok)
  227. expect { two_factor_pref.reload }.to raise_error(ActiveRecord::RecordNotFound)
  228. end
  229. end
  230. end
  231. end
  232. describe 'POST /users/two_factor_default_authentication_method' do
  233. let(:method) { 'unknown' }
  234. let(:params) { {} }
  235. before do
  236. create(:user_two_factor_preference, :security_keys, user: agent)
  237. post '/api/v1/users/two_factor_default_authentication_method', params: params, as: :json
  238. end
  239. it 'fails without needed params' do
  240. expect(response).to have_http_status(:unprocessable_entity)
  241. end
  242. context 'with needed params' do
  243. let(:params) { { method: method } }
  244. let(:method) { 'security_keys' }
  245. it 'returns ok and updates default method', :aggregate_failures do
  246. expect(response).to have_http_status(:ok)
  247. expect(agent.reload.preferences.dig(:two_factor_authentication, :default)).to eq(method)
  248. end
  249. end
  250. end
  251. end