/spec/controllers/application_controller_spec.rb

https://gitlab.com/SaiAshirwadInformatia/gitlab-ce · Ruby · 413 lines · 333 code · 80 blank · 0 comment · 0 complexity · 2354bcd70e8438067227da7566761e34 MD5 · raw file

  1. require 'spec_helper'
  2. describe ApplicationController do
  3. let(:user) { create(:user) }
  4. describe '#check_password_expiration' do
  5. let(:controller) { described_class.new }
  6. it 'redirects if the user is over their password expiry' do
  7. user.password_expires_at = Time.new(2002)
  8. expect(user.ldap_user?).to be_falsey
  9. allow(controller).to receive(:current_user).and_return(user)
  10. expect(controller).to receive(:redirect_to)
  11. expect(controller).to receive(:new_profile_password_path)
  12. controller.send(:check_password_expiration)
  13. end
  14. it 'does not redirect if the user is under their password expiry' do
  15. user.password_expires_at = Time.now + 20010101
  16. expect(user.ldap_user?).to be_falsey
  17. allow(controller).to receive(:current_user).and_return(user)
  18. expect(controller).not_to receive(:redirect_to)
  19. controller.send(:check_password_expiration)
  20. end
  21. it 'does not redirect if the user is over their password expiry but they are an ldap user' do
  22. user.password_expires_at = Time.new(2002)
  23. allow(user).to receive(:ldap_user?).and_return(true)
  24. allow(controller).to receive(:current_user).and_return(user)
  25. expect(controller).not_to receive(:redirect_to)
  26. controller.send(:check_password_expiration)
  27. end
  28. it 'does not redirect if the user is over their password expiry but sign-in is disabled' do
  29. stub_application_setting(password_authentication_enabled: false)
  30. user.password_expires_at = Time.new(2002)
  31. allow(controller).to receive(:current_user).and_return(user)
  32. expect(controller).not_to receive(:redirect_to)
  33. controller.send(:check_password_expiration)
  34. end
  35. end
  36. describe "#authenticate_user_from_token!" do
  37. describe "authenticating a user from a private token" do
  38. controller(described_class) do
  39. def index
  40. render text: "authenticated"
  41. end
  42. end
  43. context "when the 'private_token' param is populated with the private token" do
  44. it "logs the user in" do
  45. get :index, private_token: user.private_token
  46. expect(response).to have_http_status(200)
  47. expect(response.body).to eq("authenticated")
  48. end
  49. end
  50. context "when the 'PRIVATE-TOKEN' header is populated with the private token" do
  51. it "logs the user in" do
  52. @request.headers['PRIVATE-TOKEN'] = user.private_token
  53. get :index
  54. expect(response).to have_http_status(200)
  55. expect(response.body).to eq("authenticated")
  56. end
  57. end
  58. it "doesn't log the user in otherwise" do
  59. @request.headers['PRIVATE-TOKEN'] = "token"
  60. get :index, private_token: "token", authenticity_token: "token"
  61. expect(response.status).not_to eq(200)
  62. expect(response.body).not_to eq("authenticated")
  63. end
  64. end
  65. describe "authenticating a user from a personal access token" do
  66. controller(described_class) do
  67. def index
  68. render text: 'authenticated'
  69. end
  70. end
  71. let(:personal_access_token) { create(:personal_access_token, user: user) }
  72. context "when the 'personal_access_token' param is populated with the personal access token" do
  73. it "logs the user in" do
  74. get :index, private_token: personal_access_token.token
  75. expect(response).to have_http_status(200)
  76. expect(response.body).to eq('authenticated')
  77. end
  78. end
  79. context "when the 'PERSONAL_ACCESS_TOKEN' header is populated with the personal access token" do
  80. it "logs the user in" do
  81. @request.headers["PRIVATE-TOKEN"] = personal_access_token.token
  82. get :index
  83. expect(response).to have_http_status(200)
  84. expect(response.body).to eq('authenticated')
  85. end
  86. end
  87. it "doesn't log the user in otherwise" do
  88. get :index, private_token: "token"
  89. expect(response.status).not_to eq(200)
  90. expect(response.body).not_to eq('authenticated')
  91. end
  92. end
  93. end
  94. describe 'rescue from Gitlab::Git::Storage::Inaccessible' do
  95. controller(described_class) do
  96. def index
  97. raise Gitlab::Git::Storage::Inaccessible.new('broken', 100)
  98. end
  99. end
  100. it 'renders a 503 when storage is not available' do
  101. sign_in(create(:user))
  102. get :index
  103. expect(response.status).to eq(503)
  104. end
  105. it 'renders includes a Retry-After header' do
  106. sign_in(create(:user))
  107. get :index
  108. expect(response.headers['Retry-After']).to eq(100)
  109. end
  110. end
  111. describe 'response format' do
  112. controller(described_class) do
  113. def index
  114. respond_to do |format|
  115. format.json do
  116. head :ok
  117. end
  118. end
  119. end
  120. end
  121. context 'when format is handled' do
  122. let(:requested_format) { :json }
  123. it 'returns 200 response' do
  124. get :index, private_token: user.private_token, format: requested_format
  125. expect(response).to have_http_status 200
  126. end
  127. end
  128. context 'when format is not handled' do
  129. it 'returns 404 response' do
  130. get :index, private_token: user.private_token
  131. expect(response).to have_http_status 404
  132. end
  133. end
  134. end
  135. describe '#authenticate_user_from_rss_token' do
  136. describe "authenticating a user from an RSS token" do
  137. controller(described_class) do
  138. def index
  139. render text: 'authenticated'
  140. end
  141. end
  142. context "when the 'rss_token' param is populated with the RSS token" do
  143. context 'when the request format is atom' do
  144. it "logs the user in" do
  145. get :index, rss_token: user.rss_token, format: :atom
  146. expect(response).to have_http_status 200
  147. expect(response.body).to eq 'authenticated'
  148. end
  149. end
  150. context 'when the request format is not atom' do
  151. it "doesn't log the user in" do
  152. get :index, rss_token: user.rss_token
  153. expect(response.status).not_to have_http_status 200
  154. expect(response.body).not_to eq 'authenticated'
  155. end
  156. end
  157. end
  158. context "when the 'rss_token' param is populated with an invalid RSS token" do
  159. it "doesn't log the user" do
  160. get :index, rss_token: "token"
  161. expect(response.status).not_to eq 200
  162. expect(response.body).not_to eq 'authenticated'
  163. end
  164. end
  165. end
  166. end
  167. describe '#route_not_found' do
  168. it 'renders 404 if authenticated' do
  169. allow(controller).to receive(:current_user).and_return(user)
  170. expect(controller).to receive(:not_found)
  171. controller.send(:route_not_found)
  172. end
  173. it 'does redirect to login page via authenticate_user! if not authenticated' do
  174. allow(controller).to receive(:current_user).and_return(nil)
  175. expect(controller).to receive(:authenticate_user!)
  176. controller.send(:route_not_found)
  177. end
  178. end
  179. context 'two-factor authentication' do
  180. let(:controller) { described_class.new }
  181. describe '#check_two_factor_requirement' do
  182. subject { controller.send :check_two_factor_requirement }
  183. it 'does not redirect if 2FA is not required' do
  184. allow(controller).to receive(:two_factor_authentication_required?).and_return(false)
  185. expect(controller).not_to receive(:redirect_to)
  186. subject
  187. end
  188. it 'does not redirect if user is not logged in' do
  189. allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
  190. allow(controller).to receive(:current_user).and_return(nil)
  191. expect(controller).not_to receive(:redirect_to)
  192. subject
  193. end
  194. it 'does not redirect if user has 2FA enabled' do
  195. allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
  196. allow(controller).to receive(:current_user).twice.and_return(user)
  197. allow(user).to receive(:two_factor_enabled?).and_return(true)
  198. expect(controller).not_to receive(:redirect_to)
  199. subject
  200. end
  201. it 'does not redirect if 2FA setup can be skipped' do
  202. allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
  203. allow(controller).to receive(:current_user).twice.and_return(user)
  204. allow(user).to receive(:two_factor_enabled?).and_return(false)
  205. allow(controller).to receive(:skip_two_factor?).and_return(true)
  206. expect(controller).not_to receive(:redirect_to)
  207. subject
  208. end
  209. it 'redirects to 2FA setup otherwise' do
  210. allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
  211. allow(controller).to receive(:current_user).twice.and_return(user)
  212. allow(user).to receive(:two_factor_enabled?).and_return(false)
  213. allow(controller).to receive(:skip_two_factor?).and_return(false)
  214. allow(controller).to receive(:profile_two_factor_auth_path)
  215. expect(controller).to receive(:redirect_to)
  216. subject
  217. end
  218. end
  219. describe '#two_factor_authentication_required?' do
  220. subject { controller.send :two_factor_authentication_required? }
  221. it 'returns false if no 2FA requirement is present' do
  222. allow(controller).to receive(:current_user).and_return(nil)
  223. expect(subject).to be_falsey
  224. end
  225. it 'returns true if a 2FA requirement is set in the application settings' do
  226. stub_application_setting require_two_factor_authentication: true
  227. allow(controller).to receive(:current_user).and_return(nil)
  228. expect(subject).to be_truthy
  229. end
  230. it 'returns true if a 2FA requirement is set on the user' do
  231. user.require_two_factor_authentication_from_group = true
  232. allow(controller).to receive(:current_user).and_return(user)
  233. expect(subject).to be_truthy
  234. end
  235. end
  236. describe '#two_factor_grace_period' do
  237. subject { controller.send :two_factor_grace_period }
  238. it 'returns the grace period from the application settings' do
  239. stub_application_setting two_factor_grace_period: 23
  240. allow(controller).to receive(:current_user).and_return(nil)
  241. expect(subject).to eq 23
  242. end
  243. context 'with a 2FA requirement set on the user' do
  244. let(:user) { create :user, require_two_factor_authentication_from_group: true, two_factor_grace_period: 23 }
  245. it 'returns the user grace period if lower than the application grace period' do
  246. stub_application_setting two_factor_grace_period: 24
  247. allow(controller).to receive(:current_user).and_return(user)
  248. expect(subject).to eq 23
  249. end
  250. it 'returns the application grace period if lower than the user grace period' do
  251. stub_application_setting two_factor_grace_period: 22
  252. allow(controller).to receive(:current_user).and_return(user)
  253. expect(subject).to eq 22
  254. end
  255. end
  256. end
  257. describe '#two_factor_grace_period_expired?' do
  258. subject { controller.send :two_factor_grace_period_expired? }
  259. before do
  260. allow(controller).to receive(:current_user).and_return(user)
  261. end
  262. it 'returns false if the user has not started their grace period yet' do
  263. expect(subject).to be_falsey
  264. end
  265. context 'with grace period started' do
  266. let(:user) { create :user, otp_grace_period_started_at: 2.hours.ago }
  267. it 'returns true if the grace period has expired' do
  268. allow(controller).to receive(:two_factor_grace_period).and_return(1)
  269. expect(subject).to be_truthy
  270. end
  271. it 'returns false if the grace period is still active' do
  272. allow(controller).to receive(:two_factor_grace_period).and_return(3)
  273. expect(subject).to be_falsey
  274. end
  275. end
  276. end
  277. describe '#two_factor_skippable' do
  278. subject { controller.send :two_factor_skippable? }
  279. before do
  280. allow(controller).to receive(:current_user).and_return(user)
  281. end
  282. it 'returns false if 2FA is not required' do
  283. allow(controller).to receive(:two_factor_authentication_required?).and_return(false)
  284. expect(subject).to be_falsey
  285. end
  286. it 'returns false if the user has already enabled 2FA' do
  287. allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
  288. allow(user).to receive(:two_factor_enabled?).and_return(true)
  289. expect(subject).to be_falsey
  290. end
  291. it 'returns false if the 2FA grace period has expired' do
  292. allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
  293. allow(user).to receive(:two_factor_enabled?).and_return(false)
  294. allow(controller).to receive(:two_factor_grace_period_expired?).and_return(true)
  295. expect(subject).to be_falsey
  296. end
  297. it 'returns true otherwise' do
  298. allow(controller).to receive(:two_factor_authentication_required?).and_return(true)
  299. allow(user).to receive(:two_factor_enabled?).and_return(false)
  300. allow(controller).to receive(:two_factor_grace_period_expired?).and_return(false)
  301. expect(subject).to be_truthy
  302. end
  303. end
  304. describe '#skip_two_factor?' do
  305. subject { controller.send :skip_two_factor? }
  306. it 'returns false if 2FA setup was not skipped' do
  307. allow(controller).to receive(:session).and_return({})
  308. expect(subject).to be_falsey
  309. end
  310. context 'with 2FA setup skipped' do
  311. before do
  312. allow(controller).to receive(:session).and_return({ skip_two_factor: 2.hours.from_now })
  313. end
  314. it 'returns false if the grace period has expired' do
  315. Timecop.freeze(3.hours.from_now) do
  316. expect(subject).to be_falsey
  317. end
  318. end
  319. it 'returns true if the grace period is still active' do
  320. Timecop.freeze(1.hour.from_now) do
  321. expect(subject).to be_truthy
  322. end
  323. end
  324. end
  325. end
  326. end
  327. end