issues_controller_spec.rb 5.33 KB
Newer Older
1 2
require 'spec_helper'

3
describe Projects::Boards::IssuesController do
4 5
  let(:project) { create(:project_with_board) }
  let(:user)    { create(:user) }
6
  let(:guest)   { create(:user) }
7

8 9 10
  let(:planning)    { create(:label, project: project, name: 'Planning') }
  let(:development) { create(:label, project: project, name: 'Development') }

11 12
  let!(:list1) { create(:list, board: project.board, label: planning, position: 0) }
  let!(:list2) { create(:list, board: project.board, label: development, position: 1) }
13

14 15
  before do
    project.team << [user, :master]
16
    project.team << [guest, :guest]
17 18
  end

19
  describe 'GET index' do
20 21
    context 'with valid list id' do
      it 'returns issues that have the list label applied' do
22
        johndoe = create(:user, avatar: fixture_file_upload(File.join(Rails.root, 'spec/fixtures/dk.png')))
23
        issue = create(:labeled_issue, project: project, labels: [planning])
24
        create(:labeled_issue, project: project, labels: [planning])
25
        create(:labeled_issue, project: project, labels: [development], due_date: Date.tomorrow)
26
        create(:labeled_issue, project: project, labels: [development], assignee: johndoe)
27
        issue.subscribe(johndoe)
28

29
        list_issues user: user, list_id: list2
30 31 32

        parsed_response = JSON.parse(response.body)

33
        expect(response).to match_response_schema('issues')
34 35 36 37 38 39
        expect(parsed_response.length).to eq 2
      end
    end

    context 'with invalid list id' do
      it 'returns a not found 404 response' do
40
        list_issues user: user, list_id: 999
41 42 43 44

        expect(response).to have_http_status(404)
      end
    end
45

46
    context 'with unauthorized user' do
47
      before do
48 49
        allow(Ability).to receive(:allowed?).with(user, :read_project, project).and_return(true)
        allow(Ability).to receive(:allowed?).with(user, :read_issue, project).and_return(false)
50
      end
51

52
      it 'returns a successful 403 response' do
53 54 55 56 57 58 59 60 61
        list_issues user: user, list_id: list2

        expect(response).to have_http_status(403)
      end
    end

    def list_issues(user:, list_id:)
      sign_in(user)

62 63 64 65
      get :index, namespace_id: project.namespace.to_param,
                  project_id: project.to_param,
                  list_id: list_id.to_param
    end
66
  end
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
  describe 'POST create' do
    context 'with valid params' do
      it 'returns a successful 200 response' do
        create_issue user: user, list: list1, title: 'New issue'

        expect(response).to have_http_status(200)
      end

      it 'returns the created issue' do
        create_issue user: user, list: list1, title: 'New issue'

        expect(response).to match_response_schema('issue')
      end
    end

    context 'with invalid params' do
      context 'when title is nil' do
        it 'returns an unprocessable entity 422 response' do
          create_issue user: user, list: list1, title: nil

          expect(response).to have_http_status(422)
        end
      end

      context 'when list does not belongs to project board' do
        it 'returns a not found 404 response' do
          list = create(:list)

          create_issue user: user, list: list, title: 'New issue'

          expect(response).to have_http_status(404)
        end
      end
    end

    context 'with unauthorized user' do
      it 'returns a forbidden 403 response' do
        create_issue user: guest, list: list1, title: 'New issue'

        expect(response).to have_http_status(403)
      end
    end

    def create_issue(user:, list:, title:)
      sign_in(user)

      post :create, namespace_id: project.namespace.to_param,
                    project_id: project.to_param,
                    list_id: list.to_param,
                    issue: { title: title },
                    format: :json
    end
  end

122
  describe 'PATCH update' do
123 124 125 126
    let(:issue) { create(:labeled_issue, project: project, labels: [planning]) }

    context 'with valid params' do
      it 'returns a successful 200 response' do
127
        move user: user, issue: issue, from_list_id: list1.id, to_list_id: list2.id
128 129 130 131 132

        expect(response).to have_http_status(200)
      end

      it 'moves issue to the desired list' do
133
        move user: user, issue: issue, from_list_id: list1.id, to_list_id: list2.id
134 135 136 137 138 139 140

        expect(issue.reload.labels).to contain_exactly(development)
      end
    end

    context 'with invalid params' do
      it 'returns a unprocessable entity 422 response for invalid lists' do
141
        move user: user, issue: issue, from_list_id: nil, to_list_id: nil
142 143 144 145 146

        expect(response).to have_http_status(422)
      end

      it 'returns a not found 404 response for invalid issue id' do
147
        move user: user, issue: 999, from_list_id: list1.id, to_list_id: list2.id
148 149 150 151 152

        expect(response).to have_http_status(404)
      end
    end

153
    context 'with unauthorized user' do
154
      it 'returns a forbidden 403 response' do
155
        move user: guest, issue: issue, from_list_id: list1.id, to_list_id: list2.id
156 157 158 159 160

        expect(response).to have_http_status(403)
      end
    end

161
    def move(user:, issue:, from_list_id:, to_list_id:)
162 163
      sign_in(user)

164 165 166
      patch :update, namespace_id: project.namespace.to_param,
                     project_id: project.to_param,
                     id: issue.to_param,
167 168
                     from_list_id: from_list_id,
                     to_list_id: to_list_id,
169 170 171
                     format: :json
    end
  end
172
end