Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/shared_examples/requests/sessionless_auth_request_shared_examples.rb')
-rw-r--r--spec/support/shared_examples/requests/sessionless_auth_request_shared_examples.rb158
1 files changed, 117 insertions, 41 deletions
diff --git a/spec/support/shared_examples/requests/sessionless_auth_request_shared_examples.rb b/spec/support/shared_examples/requests/sessionless_auth_request_shared_examples.rb
index d82da1b01e1..56e90a6ec34 100644
--- a/spec/support/shared_examples/requests/sessionless_auth_request_shared_examples.rb
+++ b/spec/support/shared_examples/requests/sessionless_auth_request_shared_examples.rb
@@ -1,84 +1,160 @@
# frozen_string_literal: true
-RSpec.shared_examples 'authenticates sessionless user for the request spec' do |params|
- params ||= {}
-
+RSpec.shared_examples 'authenticates sessionless user for the request spec' do |name, public_resource:, ignore_metrics: false, params: {}|
before do
stub_authentication_activity_metrics(debug: false)
end
- let(:user) { create(:user) }
+ let_it_be(:user) { create(:user) }
let(:personal_access_token) { create(:personal_access_token, user: user) }
- let(:default_params) { params.except(:public) || {} }
- context "when the 'personal_access_token' param is populated with the personal access token" do
- it 'logs the user in' do
+ shared_examples 'authenticates user and returns response with ok status' do
+ it 'authenticates user and returns response with ok status' do
expect(authentication_metrics)
.to increment(:user_authenticated_counter)
- .and increment(:user_session_override_counter)
- .and increment(:user_sessionless_authentication_counter)
+ .and increment(:user_session_override_counter)
+ .and increment(:user_sessionless_authentication_counter)
- get url, params: default_params.merge(private_token: personal_access_token.token)
+ subject
- expect(response).to have_gitlab_http_status(:ok)
expect(controller.current_user).to eq(user)
+ expect(response).to have_gitlab_http_status(:ok)
end
+ end
- it 'does not log the user in if page is public', if: params[:public] do
- get url, params: default_params
+ shared_examples 'does not authenticate user and returns response with ok status' do
+ it 'does not authenticate user and returns response with ok status' do
+ subject
- expect(response).to have_gitlab_http_status(:ok)
expect(controller.current_user).to be_nil
+ expect(response).to have_gitlab_http_status(:ok)
end
end
- context 'when the personal access token has no api scope', unless: params[:public] do
- it 'does not log the user in' do
+ shared_examples 'does not return response with ok status' do
+ it 'does not return response with ok status' do
# Several instances of where these specs are shared route the request
# through ApplicationController#route_not_found which does not involve
# the usual auth code from Devise, so does not increment the
# :user_unauthenticated_counter
- #
- unless params[:ignore_incrementing]
+ unless ignore_metrics
expect(authentication_metrics)
.to increment(:user_unauthenticated_counter)
end
- personal_access_token.update!(scopes: [:read_user])
-
- get url, params: default_params.merge(private_token: personal_access_token.token)
+ subject
expect(response).not_to have_gitlab_http_status(:ok)
end
end
- context "when the 'PERSONAL_ACCESS_TOKEN' header is populated with the personal access token" do
- it 'logs the user in' do
- expect(authentication_metrics)
- .to increment(:user_authenticated_counter)
- .and increment(:user_session_override_counter)
- .and increment(:user_sessionless_authentication_counter)
+ shared_examples 'using valid token' do
+ context 'when resource is private', unless: public_resource do
+ include_examples 'authenticates user and returns response with ok status'
- headers = { 'PRIVATE-TOKEN': personal_access_token.token }
- get url, params: default_params, headers: headers
+ context 'when user with expired password' do
+ let_it_be(:user) { create(:user, password_expires_at: 2.minutes.ago) }
- expect(response).to have_gitlab_http_status(:ok)
+ include_examples 'does not return response with ok status'
+ end
+
+ context 'when password expiration is not applicable' do
+ context 'when ldap user' do
+ let_it_be(:user) { create(:omniauth_user, provider: 'ldap', password_expires_at: 2.minutes.ago) }
+
+ include_examples 'authenticates user and returns response with ok status'
+ end
+ end
+ end
+
+ context 'when resource is public', if: public_resource do
+ include_examples 'authenticates user and returns response with ok status'
+
+ context 'when user with expired password' do
+ let_it_be(:user) { create(:user, password_expires_at: 2.minutes.ago) }
+
+ include_examples 'does not authenticate user and returns response with ok status'
+ end
end
end
- it "doesn't log the user in otherwise", unless: params[:public] do
- # Several instances of where these specs are shared route the request
- # through ApplicationController#route_not_found which does not involve
- # the usual auth code from Devise, so does not increment the
- # :user_unauthenticated_counter
- #
- unless params[:ignore_incrementing]
- expect(authentication_metrics)
- .to increment(:user_unauthenticated_counter)
+ shared_examples 'using invalid token' do
+ context 'when resource is private', unless: public_resource do
+ include_examples 'does not return response with ok status'
+ end
+
+ context 'when resource is public', if: public_resource do
+ include_examples 'does not authenticate user and returns response with ok status'
+ end
+ end
+
+ shared_examples 'personal access token has no api scope' do
+ context 'when the personal access token has no api scope' do
+ before do
+ personal_access_token.update!(scopes: [:read_user])
+ end
+
+ context 'when resource is private', unless: public_resource do
+ include_examples 'does not return response with ok status'
+ end
+
+ context 'when resource is public', if: public_resource do
+ include_examples 'does not authenticate user and returns response with ok status'
+ end
+ end
+ end
+
+ describe name do
+ context "when the 'private_token' param is populated with the personal access token" do
+ context 'when valid token' do
+ subject { get url, params: params.merge(private_token: personal_access_token.token) }
+
+ include_examples 'using valid token'
+
+ include_examples 'personal access token has no api scope'
+ end
+
+ context 'when invalid token' do
+ subject { get url, params: params.merge(private_token: 'invalid token') }
+
+ include_examples 'using invalid token'
+ end
end
- get url, params: default_params.merge(private_token: 'token')
+ context "when the 'PRIVATE-TOKEN' header is populated with the personal access token" do
+ context 'when valid token' do
+ subject do
+ headers = { 'PRIVATE-TOKEN': personal_access_token.token }
+ get url, params: params, headers: headers
+ end
- expect(response).not_to have_gitlab_http_status(:ok)
+ include_examples 'using valid token'
+
+ include_examples 'personal access token has no api scope'
+ end
+
+ context 'when invalid token' do
+ subject do
+ headers = { 'PRIVATE-TOKEN': 'invalid token' }
+ get url, params: params, headers: headers
+ end
+
+ include_examples 'using invalid token'
+ end
+ end
+
+ context "when the 'feed_token' param is populated with the feed token" do
+ context 'when valid token' do
+ subject { get url, params: params.merge(feed_token: user.feed_token) }
+
+ include_examples 'using valid token'
+ end
+
+ context 'when invalid token' do
+ subject { get url, params: params.merge(feed_token: 'invalid token') }
+
+ include_examples 'using invalid token'
+ end
+ end
end
end