summaryrefslogtreecommitdiff
path: root/spec/support/controllers/sessionless_auth_controller_shared_examples.rb
blob: 355555d9d195122afa994c360c73cba3ae007f92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
shared_examples 'authenticates sessionless user' do |path, format, params|
  params ||= {}

  before do
    stub_authentication_activity_metrics(debug: false)
  end

  let(:user) { create(:user) }
  let(:personal_access_token) { create(:personal_access_token, user: user) }
  let(:default_params) { { format: format }.merge(params.except(:public) || {}) }

  context "when the 'personal_access_token' param 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)

      get path, params: default_params.merge(private_token: personal_access_token.token)

      expect(response).to have_gitlab_http_status(200)
      expect(controller.current_user).to eq(user)
    end

    it 'does not log the user in if page is public', if: params[:public] do
      get path, params: default_params

      expect(response).to have_gitlab_http_status(200)
      expect(controller.current_user).to be_nil
    end
  end

  context 'when the personal access token has no api scope', unless: params[:public] do
    it 'does not log the user in' do
      expect(authentication_metrics)
        .to increment(:user_unauthenticated_counter)

      personal_access_token.update(scopes: [:read_user])

      get path, params: default_params.merge(private_token: personal_access_token.token)

      expect(response).not_to have_gitlab_http_status(200)
    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)

      @request.headers['PRIVATE-TOKEN'] = personal_access_token.token
      get path, params: default_params

      expect(response).to have_gitlab_http_status(200)
    end
  end

  context "when the 'feed_token' param is populated with the feed token", if: format == :rss 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)

      get path, params: default_params.merge(feed_token: user.feed_token)

      expect(response).to have_gitlab_http_status 200
    end
  end

  context "when the 'feed_token' param is populated with an invalid feed token", if: format == :rss, unless: params[:public] do
    it "logs the user" do
      expect(authentication_metrics)
        .to increment(:user_unauthenticated_counter)

      get path, params: default_params.merge(feed_token: 'token')

      expect(response.status).not_to eq 200
    end
  end

  it "doesn't log the user in otherwise", unless: params[:public] do
    expect(authentication_metrics)
      .to increment(:user_unauthenticated_counter)

    get path, params: default_params.merge(private_token: 'token')

    expect(response.status).not_to eq(200)
  end
end