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

describe Projects::TodosController do
4
  let(:user)          { create(:user) }
5
  let(:project)       { create(:project) }
6 7 8 9
  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
10
    describe 'POST create' do
11 12
      def go
        post :create,
13 14
          namespace_id: project.namespace,
          project_id: project,
15 16 17 18 19
          issuable_id: issue.id,
          issuable_type: 'issue',
          format: 'html'
      end

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

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

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

        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
41 42
      end

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

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

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

58
          expect(response).to have_http_status(302)
Phil Hughes's avatar
Phil Hughes committed
59
        end
60
      end
61 62 63 64 65 66 67 68 69

      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
70
          expect { go }.not_to change { user.todos.count }
71 72 73
          expect(response).to have_http_status(404)
        end
      end
Phil Hughes's avatar
Phil Hughes committed
74 75 76
    end
  end

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

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

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

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

        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
109 110
      end

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

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

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

126
          expect(response).to have_http_status(302)
Phil Hughes's avatar
Phil Hughes committed
127
        end
128
      end
129 130 131 132 133 134 135 136 137

      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
138
          expect { go }.not_to change { user.todos.count }
139 140 141
          expect(response).to have_http_status(404)
        end
      end
142 143
    end
  end
Phil Hughes's avatar
Phil Hughes committed
144
end