summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/user/starred_projects_query_spec.rb
blob: 6cb02068f2aa1bb61f766a82ddca34c6fcbc0552 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Getting starredProjects of the user' do
  include GraphqlHelpers

  let(:query) do
    graphql_query_for(:user, user_params, user_fields)
  end

  let(:user_params) { { username: user.username } }

  let_it_be(:project_a) { create(:project, :public) }
  let_it_be(:project_b) { create(:project, :private) }
  let_it_be(:project_c) { create(:project, :private) }
  let_it_be(:user, reload: true) { create(:user) }

  let(:user_fields) { 'starredProjects { nodes { id } }' }
  let(:current_user) { nil }

  let(:starred_projects) do
    post_graphql(query, current_user: current_user)

    graphql_data_at(:user, :starred_projects, :nodes)
  end

  before do
    project_b.add_reporter(user)
    project_c.add_reporter(user)

    user.toggle_star(project_a)
    user.toggle_star(project_b)
    user.toggle_star(project_c)
  end

  it_behaves_like 'a working graphql query' do
    before do
      post_graphql(query)
    end
  end

  it 'found only public project' do
    expect(starred_projects).to contain_exactly(
      a_hash_including('id' => global_id_of(project_a))
    )
  end

  context 'the current user is the user' do
    let(:current_user) { user }

    it 'found all projects' do
      expect(starred_projects).to contain_exactly(
        a_hash_including('id' => global_id_of(project_a)),
        a_hash_including('id' => global_id_of(project_b)),
        a_hash_including('id' => global_id_of(project_c))
      )
    end
  end

  context 'the current user is a member of a private project the user starred' do
    let_it_be(:other_user) { create(:user) }
    let(:current_user) { other_user }

    before do
      project_b.add_reporter(other_user)
    end

    it 'finds public and member projects' do
      expect(starred_projects).to contain_exactly(
        a_hash_including('id' => global_id_of(project_a)),
        a_hash_including('id' => global_id_of(project_b))
      )
    end
  end

  context 'the user has a private profile' do
    before do
      user.update!(private_profile: true)
    end

    context 'the current user does not have access to view the private profile of the user' do
      let(:current_user) { create(:user) }

      it 'finds no projects' do
        expect(starred_projects).to be_empty
      end
    end

    context 'the current user has access to view the private profile of the user' do
      let(:current_user) { create(:admin) }

      it 'finds all projects starred by the user, which the current user has access to' do
        expect(starred_projects).to contain_exactly(
          a_hash_including('id' => global_id_of(project_a)),
          a_hash_including('id' => global_id_of(project_b)),
          a_hash_including('id' => global_id_of(project_c))
        )
      end
    end
  end
end