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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'getting list of branch rules for a project', feature_category: :source_code_management do
include GraphqlHelpers
let_it_be(:project) { create(:project, :repository, :public) }
let_it_be(:current_user) { create(:user) }
let(:branch_rules_data) { graphql_data_at('project', 'branchRules', 'edges') }
let(:variables) { { path: project.full_path } }
let(:fields) { all_graphql_fields_for('BranchRule') }
let(:query) do
<<~GQL
query($path: ID!, $n: Int, $cursor: String) {
project(fullPath: $path) {
branchRules(first: $n, after: $cursor) {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
cursor
node {
#{fields}
}
}
}
}
}
GQL
end
context 'when the user does not have read_protected_branch abilities' do
before do
project.add_guest(current_user)
post_graphql(query, current_user: current_user, variables: variables)
end
it_behaves_like 'a working graphql query'
it 'hides branch rules data' do
expect(branch_rules_data).to be_empty
end
end
context 'when the user does have read_protected_branch abilities' do
before do
project.add_maintainer(current_user)
end
describe 'queries' do
include_context 'when user tracking is disabled'
let(:query) do
<<~GQL
query($path: ID!) {
project(fullPath: $path) {
branchRules {
nodes {
matchingBranchesCount
}
}
}
}
GQL
end
before do
create(:protected_branch, project: project)
end
it 'avoids N+1 queries', :use_sql_query_cache, :aggregate_failures do
control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
post_graphql(query, current_user: current_user, variables: variables)
end
# Verify the response includes the field
expect_n_matching_branches_count_fields(1)
create(:protected_branch, project: project)
create(:protected_branch, name: '*', project: project)
expect do
post_graphql(query, current_user: current_user, variables: variables)
end.not_to exceed_all_query_limit(control)
expect_n_matching_branches_count_fields(3)
end
def expect_n_matching_branches_count_fields(count)
branch_rule_nodes = graphql_data_at('project', 'branchRules', 'nodes')
expect(branch_rule_nodes.count).to eq(count)
branch_rule_nodes.each do |node|
expect(node['matchingBranchesCount']).to be_present
end
end
end
describe 'response' do
let_it_be(:branch_name_a) { TestEnv::BRANCH_SHA.each_key.first }
let_it_be(:branch_name_b) { 'diff-*' }
let_it_be(:protected_branch_a) do
create(:protected_branch, project: project, name: branch_name_a)
end
let_it_be(:protected_branch_b) do
create(:protected_branch, project: project, name: branch_name_b)
end
let(:branch_rule_a) { Projects::BranchRule.new(project, protected_branch_a) }
let(:branch_rule_b) { Projects::BranchRule.new(project, protected_branch_b) }
let(:branch_rules) { [branch_rule_a, branch_rule_b] }
# branchRules are returned in alphabetical order
let(:branch_rule_a_data) { branch_rules_data.dig(1, 'node') }
let(:branch_rule_b_data) { branch_rules_data.dig(0, 'node') }
before do
post_graphql(query, current_user: current_user, variables: variables)
end
it_behaves_like 'a working graphql query'
it 'includes all fields', :use_sql_query_cache, :aggregate_failures do
expect(branch_rule_a_data).to include(
'id' => branch_rule_a.to_global_id.to_s,
'name' => branch_name_a,
'isDefault' => be_boolean,
'isProtected' => true,
'matchingBranchesCount' => 1,
'branchProtection' => be_kind_of(Hash),
'createdAt' => be_kind_of(String),
'updatedAt' => be_kind_of(String)
)
wildcard_count = TestEnv::BRANCH_SHA.keys.count do |branch_name|
branch_name.starts_with?('diff-')
end
expect(branch_rule_b_data).to include(
'id' => branch_rule_b.to_global_id.to_s,
'name' => branch_name_b,
'isDefault' => be_boolean,
'isProtected' => true,
'matchingBranchesCount' => wildcard_count,
'branchProtection' => be_kind_of(Hash),
'createdAt' => be_kind_of(String),
'updatedAt' => be_kind_of(String)
)
end
context 'when limiting the number of results' do
let(:branch_rule_limit) { 1 }
let(:variables) { { path: project.full_path, n: branch_rule_limit } }
let(:next_variables) do
{ path: project.full_path, n: branch_rule_limit, cursor: last_cursor }
end
it_behaves_like 'a working graphql query'
it 'returns pagination information' do
expect(branch_rules_data.size).to eq(branch_rule_limit)
expect(has_next_page).to be_truthy
expect(has_prev_page).to be_falsey
post_graphql(query, current_user: current_user, variables: next_variables)
expect(branch_rules_data.size).to eq(branch_rule_limit)
expect(has_next_page).to be_falsey
expect(has_prev_page).to be_truthy
end
context 'when no limit is provided' do
let(:branch_rule_limit) { nil }
it 'returns all branch_rules' do
expect(branch_rules_data.size).to eq(branch_rules.size)
end
end
end
end
end
def pagination_info
graphql_data_at('project', 'branchRules', 'pageInfo')
end
def has_next_page
pagination_info['hasNextPage']
end
def has_prev_page
pagination_info['hasPreviousPage']
end
def last_cursor
branch_rules_data.last['cursor']
end
end
|