todo_controller_spec.rb 3.99 KB
Newer Older
Phil Hughes's avatar
Phil Hughes committed
1 2 3
require('spec_helper')

describe Projects::TodosController do
4 5
  include ApiHelpers

6
  let(:user)          { create(:user) }
7
  let(:project)       { create(:empty_project) }
8 9 10 11
  let(:issue)         { create(:issue, project: project) }
  let(:merge_request) { create(:merge_request, source_project: project) }

  context 'Issues' do
Phil Hughes's avatar
Phil Hughes committed
12
    describe 'POST create' do
13 14 15 16 17 18 19 20 21
      def go
        post :create,
          namespace_id: project.namespace.path,
          project_id: project.path,
          issuable_id: issue.id,
          issuable_type: 'issue',
          format: 'html'
      end

Phil Hughes's avatar
Phil Hughes committed
22 23 24 25 26 27
      context 'when authorized' do
        before do
          sign_in(user)
          project.team << [user, :developer]
        end

28
        it 'creates todo for issue' do
Phil Hughes's avatar
Phil Hughes committed
29
          expect do
30
            go
Phil Hughes's avatar
Phil Hughes committed
31 32
          end.to change { user.todos.count }.by(1)

33
          expect(response).to have_http_status(200)
Phil Hughes's avatar
Phil Hughes committed
34
        end
35 36 37 38 39 40 41 42

        it 'returns todo path and pending count' do
          go

          expect(response).to have_http_status(200)
          expect(json_response['count']).to eq 1
          expect(json_response['delete_path']).to match(/\/dashboard\/todos\/\d{1}/)
        end
43 44
      end

45
      context 'when not authorized for project' do
46
        it 'does not create todo for issue that user has no access to' do
Phil Hughes's avatar
Phil Hughes committed
47 48
          sign_in(user)
          expect do
49
            go
Phil Hughes's avatar
Phil Hughes committed
50 51
          end.to change { user.todos.count }.by(0)

52
          expect(response).to have_http_status(404)
Phil Hughes's avatar
Phil Hughes committed
53 54
        end

55
        it 'does not create todo for issue when user not logged in' do
Phil Hughes's avatar
Phil Hughes committed
56
          expect do
57
            go
Phil Hughes's avatar
Phil Hughes committed
58 59
          end.to change { user.todos.count }.by(0)

60
          expect(response).to have_http_status(302)
Phil Hughes's avatar
Phil Hughes committed
61
        end
62
      end
63 64 65 66 67 68 69 70 71 72 73 74 75

      context 'when not authorized for issue' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
          project.project_feature.update!(issues_access_level: ProjectFeature::PRIVATE)
          sign_in(user)
        end

        it "doesn't create todo" do
          expect{ go }.not_to change { user.todos.count }
          expect(response).to have_http_status(404)
        end
      end
Phil Hughes's avatar
Phil Hughes committed
76 77 78
    end
  end

79
  context 'Merge Requests' do
Phil Hughes's avatar
Phil Hughes committed
80
    describe 'POST create' do
81 82 83 84 85 86 87 88 89
      def go
        post :create,
          namespace_id: project.namespace.path,
          project_id: project.path,
          issuable_id: merge_request.id,
          issuable_type: 'merge_request',
          format: 'html'
      end

Phil Hughes's avatar
Phil Hughes committed
90 91 92 93 94 95
      context 'when authorized' do
        before do
          sign_in(user)
          project.team << [user, :developer]
        end

96
        it 'creates todo for merge request' do
Phil Hughes's avatar
Phil Hughes committed
97
          expect do
98
            go
Phil Hughes's avatar
Phil Hughes committed
99 100
          end.to change { user.todos.count }.by(1)

101
          expect(response).to have_http_status(200)
Phil Hughes's avatar
Phil Hughes committed
102
        end
103 104 105 106 107 108 109 110

        it 'returns todo path and pending count' do
          go

          expect(response).to have_http_status(200)
          expect(json_response['count']).to eq 1
          expect(json_response['delete_path']).to match(/\/dashboard\/todos\/\d{1}/)
        end
111 112
      end

113
      context 'when not authorized for project' do
114
        it 'does not create todo for merge request user has no access to' do
Phil Hughes's avatar
Phil Hughes committed
115 116
          sign_in(user)
          expect do
117
            go
Phil Hughes's avatar
Phil Hughes committed
118 119
          end.to change { user.todos.count }.by(0)

120
          expect(response).to have_http_status(404)
Phil Hughes's avatar
Phil Hughes committed
121 122
        end

123
        it 'does not create todo for merge request user has no access to' do
Phil Hughes's avatar
Phil Hughes committed
124
          expect do
125
            go
Phil Hughes's avatar
Phil Hughes committed
126 127
          end.to change { user.todos.count }.by(0)

128
          expect(response).to have_http_status(302)
Phil Hughes's avatar
Phil Hughes committed
129
        end
130
      end
131 132 133 134 135 136 137 138 139 140 141 142 143

      context 'when not authorized for merge_request' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
          project.project_feature.update!(merge_requests_access_level: ProjectFeature::PRIVATE)
          sign_in(user)
        end

        it "doesn't create todo" do
          expect{ go }.not_to change { user.todos.count }
          expect(response).to have_http_status(404)
        end
      end
144 145
    end
  end
Phil Hughes's avatar
Phil Hughes committed
146
end