summaryrefslogtreecommitdiff
path: root/spec/requests/api/avatar_spec.rb
blob: 45e34b7894b47deceead38cc5f85c2e163a3a7a1 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# frozen_string_literal: true

require 'spec_helper'

describe API::Avatar do
  let(:gravatar_service) { double('GravatarService') }

  describe 'GET /avatar' do
    context 'avatar uploaded to GitLab' do
      context 'user with matching public email address' do
        let(:user) { create(:user, :with_avatar, email: 'public@example.com', public_email: 'public@example.com') }

        before do
          user
        end

        it 'returns the avatar url' do
          get api('/avatar'), params: { email: 'public@example.com' }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['avatar_url']).to eql("#{::Settings.gitlab.base_url}#{user.avatar.local_url}")
        end
      end

      context 'no user with matching public email address' do
        before do
          expect(GravatarService).to receive(:new).and_return(gravatar_service)
          expect(gravatar_service).to(
            receive(:execute)
              .with('private@example.com', nil, 2, { username: nil })
              .and_return('https://gravatar'))
        end

        it 'returns the avatar url from Gravatar' do
          get api('/avatar'), params: { email: 'private@example.com' }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['avatar_url']).to eq('https://gravatar')
        end
      end
    end

    context 'avatar uploaded to Gravatar' do
      context 'user with matching public email address' do
        let(:user) { create(:user, email: 'public@example.com', public_email: 'public@example.com') }

        before do
          user

          expect(GravatarService).to receive(:new).and_return(gravatar_service)
          expect(gravatar_service).to(
            receive(:execute)
              .with('public@example.com', nil, 2, { username: user.username })
              .and_return('https://gravatar'))
        end

        it 'returns the avatar url from Gravatar' do
          get api('/avatar'), params: { email: 'public@example.com' }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['avatar_url']).to eq('https://gravatar')
        end
      end

      context 'no user with matching public email address' do
        before do
          expect(GravatarService).to receive(:new).and_return(gravatar_service)
          expect(gravatar_service).to(
            receive(:execute)
              .with('private@example.com', nil, 2, { username: nil })
              .and_return('https://gravatar'))
        end

        it 'returns the avatar url from Gravatar' do
          get api('/avatar'), params: { email: 'private@example.com' }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['avatar_url']).to eq('https://gravatar')
        end
      end

      context 'public visibility level restricted' do
        let(:user) { create(:user, :with_avatar, email: 'public@example.com', public_email: 'public@example.com') }

        before do
          user

          stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
        end

        context 'when authenticated' do
          it 'returns the avatar url' do
            get api('/avatar', user), params: { email: 'public@example.com' }

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response['avatar_url']).to eql("#{::Settings.gitlab.base_url}#{user.avatar.local_url}")
          end
        end

        context 'when unauthenticated' do
          it_behaves_like '403 response' do
            let(:request) { get api('/avatar'), params: { email: 'public@example.com' } }
          end
        end
      end
    end
  end
end