todos_controller_spec.rb 4.0 KB
Newer Older
P
Phil Hughes 已提交
1 2 3
require('spec_helper')

describe Projects::TodosController do
4
  let(:user)          { create(:user) }
5
  let(:project)       { create(:empty_project) }
6 7 8 9
  let(:issue)         { create(:issue, project: project) }
  let(:merge_request) { create(:merge_request, source_project: project) }

  context 'Issues' do
P
Phil Hughes 已提交
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

P
Phil Hughes 已提交
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
P
Phil Hughes 已提交
27
          expect do
28
            go
P
Phil Hughes 已提交
29 30
          end.to change { user.todos.count }.by(1)

Z
Z.J. van de Weg 已提交
31
          expect(response).to have_http_status(200)
P
Phil Hughes 已提交
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
P
Phil Hughes 已提交
45 46
          sign_in(user)
          expect do
47
            go
P
Phil Hughes 已提交
48 49
          end.to change { user.todos.count }.by(0)

Z
Z.J. van de Weg 已提交
50
          expect(response).to have_http_status(404)
P
Phil Hughes 已提交
51 52
        end

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

Z
Z.J. van de Weg 已提交
58
          expect(response).to have_http_status(302)
P
Phil Hughes 已提交
59
        end
60
      end
61 62 63 64 65 66 67 68 69 70 71 72 73

      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
P
Phil Hughes 已提交
74 75 76
    end
  end

77
  context 'Merge Requests' do
P
Phil Hughes 已提交
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

P
Phil Hughes 已提交
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
P
Phil Hughes 已提交
95
          expect do
96
            go
P
Phil Hughes 已提交
97 98
          end.to change { user.todos.count }.by(1)

Z
Z.J. van de Weg 已提交
99
          expect(response).to have_http_status(200)
P
Phil Hughes 已提交
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
P
Phil Hughes 已提交
113 114
          sign_in(user)
          expect do
115
            go
P
Phil Hughes 已提交
116 117
          end.to change { user.todos.count }.by(0)

Z
Z.J. van de Weg 已提交
118
          expect(response).to have_http_status(404)
P
Phil Hughes 已提交
119 120
        end

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

Z
Z.J. van de Weg 已提交
126
          expect(response).to have_http_status(302)
P
Phil Hughes 已提交
127
        end
128
      end
129 130 131 132 133 134 135 136 137 138 139 140 141

      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
142 143
    end
  end
P
Phil Hughes 已提交
144
end