summaryrefslogtreecommitdiff
path: root/spec/helpers/boards_helper_spec.rb
blob: 00cd44809c7eb4a9bcde7748b00a474003ae6b57 (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BoardsHelper do
  let_it_be(:user) { create(:user) }
  let_it_be(:base_group) { create(:group, path: 'base') }
  let_it_be(:project) { create(:project, group: base_group) }
  let_it_be(:project_board) { create(:board, project: project) }
  let_it_be(:group_board) { create(:board, group: base_group) }

  describe '#build_issue_link_base' do
    context 'project board' do
      it 'returns correct path for project board' do
        assign(:project, project)
        assign(:board, project_board)

        expect(helper.build_issue_link_base).to eq("/#{project.namespace.path}/#{project.path}/-/issues")
      end
    end

    context 'group board' do
      it 'returns correct path for base group' do
        assign(:board, group_board)

        expect(helper.build_issue_link_base).to eq('/base/:project_path/issues')
      end

      it 'returns correct path for subgroup' do
        subgroup = create(:group, parent: base_group, path: 'sub')
        assign(:board, create(:board, group: subgroup))

        expect(helper.build_issue_link_base).to eq('/base/sub/:project_path/issues')
      end
    end
  end

  describe '#board_base_url' do
    context 'when project board' do
      it 'generates the correct url' do
        assign(:board, group_board)
        assign(:group, base_group)

        expect(helper.board_base_url).to eq "http://test.host/groups/#{base_group.full_path}/-/boards"
      end
    end

    context 'when project board' do
      it 'generates the correct url' do
        assign(:board, project_board)
        assign(:project, project)

        expect(helper.board_base_url).to eq "/#{project.full_path}/-/boards"
      end
    end
  end

  describe '#board_data' do
    context 'project_board' do
      before do
        assign(:project, project)
        assign(:board, project_board)

        allow(helper).to receive(:current_user) { user }
        allow(helper).to receive(:can?).with(user, :create_non_backlog_issues, project_board).and_return(true)
        allow(helper).to receive(:can?).with(user, :admin_issue, project_board).and_return(true)
        allow(helper).to receive(:can?).with(user, :admin_issue_board_list, project).and_return(false)
      end

      it 'returns a board_lists_path as lists_endpoint' do
        expect(helper.board_data[:lists_endpoint]).to eq(board_lists_path(project_board))
      end

      it 'returns board type as parent' do
        expect(helper.board_data[:parent]).to eq('project')
      end

      it 'returns can_update for user permissions on the board' do
        expect(helper.board_data[:can_update]).to eq('true')
      end

      it 'returns required label endpoints' do
        expect(helper.board_data[:labels_fetch_path]).to eq("/#{project.full_path}/-/labels.json?include_ancestor_groups=true")
        expect(helper.board_data[:labels_manage_path]).to eq("/#{project.full_path}/-/labels")
      end

      it 'returns the group id of a project' do
        expect(helper.board_data[:group_id]).to eq(project.group.id)
      end

      context 'can_admin_list' do
        it 'returns can_admin_list as false by default' do
          expect(helper.board_data[:can_admin_list]).to eq('false')
        end
        it 'returns can_admin_list as true when user can admin the board' do
          allow(helper).to receive(:can?).with(user, :admin_issue_board_list, project).and_return(true)

          expect(helper.board_data[:can_admin_list]).to eq('true')
        end
      end
    end

    context 'group board' do
      before do
        assign(:group, base_group)
        assign(:board, group_board)

        allow(helper).to receive(:current_user) { user }
        allow(helper).to receive(:can?).with(user, :create_non_backlog_issues, group_board).and_return(true)
        allow(helper).to receive(:can?).with(user, :admin_issue, group_board).and_return(true)
        allow(helper).to receive(:can?).with(user, :admin_issue_board_list, base_group).and_return(false)
      end

      it 'returns correct path for base group' do
        expect(helper.build_issue_link_base).to eq("/#{base_group.full_path}/:project_path/issues")
      end

      it 'returns required label endpoints' do
        expect(helper.board_data[:labels_fetch_path]).to eq("/groups/#{base_group.full_path}/-/labels.json?include_ancestor_groups=true&only_group_labels=true")
        expect(helper.board_data[:labels_manage_path]).to eq("/groups/#{base_group.full_path}/-/labels")
      end

      it 'returns the group id' do
        expect(helper.board_data[:group_id]).to eq(base_group.id)
      end

      context 'can_admin_list' do
        it 'returns can_admin_list as false by default' do
          expect(helper.board_data[:can_admin_list]).to eq('false')
        end
        it 'returns can_admin_list as true when user can admin the board' do
          allow(helper).to receive(:can?).with(user, :admin_issue_board_list, base_group).and_return(true)

          expect(helper.board_data[:can_admin_list]).to eq('true')
        end
      end
    end
  end

  describe '#current_board_json' do
    let(:board_json) { helper.current_board_json }

    it 'can serialise with a basic set of attributes' do
      assign(:board, project_board)

      expect(board_json).to match_schema('current-board')
    end
  end
end