BigW Consortium Gitlab

snippets_controller_spec.rb 6.46 KB
Newer Older
1 2 3
require 'spec_helper'

describe Projects::SnippetsController do
4
  let(:project) { create(:project_empty_repo, :public) }
5 6 7 8
  let(:user)    { create(:user) }
  let(:user2)   { create(:user) }

  before do
9 10
    project.add_master(user)
    project.add_master(user2)
11 12 13
  end

  describe 'GET #index' do
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    context 'when page param' do
      let(:last_page) { project.snippets.page().total_pages }
      let!(:project_snippet) { create(:project_snippet, :public, project: project, author: user) }

      it 'redirects to last_page if page number is larger than number of pages' do
        get :index,
          namespace_id: project.namespace.path,
          project_id: project.path, page: (last_page + 1).to_param

        expect(response).to redirect_to(namespace_project_snippets_path(page: last_page))
      end

      it 'redirects to specified page' do
        get :index,
          namespace_id: project.namespace.path,
29
          project_id: project.path, page: last_page.to_param
30 31 32 33 34 35

        expect(assigns(:snippets).current_page).to eq(last_page)
        expect(response).to have_http_status(200)
      end
    end

36 37 38 39 40 41 42 43
    context 'when the project snippet is private' do
      let!(:project_snippet) { create(:project_snippet, :private, project: project, author: user) }

      context 'when anonymous' do
        it 'does not include the private snippet' do
          get :index, namespace_id: project.namespace.path, project_id: project.path

          expect(assigns(:snippets)).not_to include(project_snippet)
44
          expect(response).to have_http_status(200)
45 46 47 48 49 50 51 52 53 54
        end
      end

      context 'when signed in as the author' do
        before { sign_in(user) }

        it 'renders the snippet' do
          get :index, namespace_id: project.namespace.path, project_id: project.path

          expect(assigns(:snippets)).to include(project_snippet)
55
          expect(response).to have_http_status(200)
56 57 58 59 60 61 62 63 64 65
        end
      end

      context 'when signed in as a project member' do
        before { sign_in(user2) }

        it 'renders the snippet' do
          get :index, namespace_id: project.namespace.path, project_id: project.path

          expect(assigns(:snippets)).to include(project_snippet)
66
          expect(response).to have_http_status(200)
67 68 69 70 71
        end
      end
    end
  end

72 73 74 75
  describe 'POST #create' do
    def create_snippet(project, snippet_params = {})
      sign_in(user)

76
      project.add_developer(user)
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

      post :create, {
        namespace_id: project.namespace.to_param,
        project_id: project.to_param,
        project_snippet: { title: 'Title', content: 'Content' }.merge(snippet_params)
      }
    end

    context 'when the snippet is spam' do
      before do
        allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(true)
      end

      context 'when the project is private' do
        let(:private_project) { create(:project_empty_repo, :private) }

        context 'when the snippet is public' do
          it 'creates the snippet' do
            expect { create_snippet(private_project, visibility_level: Snippet::PUBLIC) }.
              to change { Snippet.count }.by(1)
          end
        end
      end

      context 'when the project is public' do
        context 'when the snippet is private' do
          it 'creates the snippet' do
            expect { create_snippet(project, visibility_level: Snippet::PRIVATE) }.
              to change { Snippet.count }.by(1)
          end
        end

        context 'when the snippet is public' do
          it 'rejects the shippet' do
            expect { create_snippet(project, visibility_level: Snippet::PUBLIC) }.
              not_to change { Snippet.count }
            expect(response).to render_template(:new)
          end

          it 'creates a spam log' do
            expect { create_snippet(project, visibility_level: Snippet::PUBLIC) }.
              to change { SpamLog.count }.by(1)
          end
        end
      end
    end
  end

  describe 'POST #mark_as_spam' do
    let(:snippet) { create(:project_snippet, :private, project: project, author: user) }

    before do
      allow_any_instance_of(AkismetService).to receive_messages(submit_spam: true)
      stub_application_setting(akismet_enabled: true)
    end

    def mark_as_spam
      admin = create(:admin)
      create(:user_agent_detail, subject: snippet)
136
      project.add_master(admin)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      sign_in(admin)

      post :mark_as_spam,
           namespace_id: project.namespace.path,
           project_id: project.path,
           id: snippet.id
    end

    it 'updates the snippet' do
      mark_as_spam

      expect(snippet.reload).not_to be_submittable_as_spam
    end
  end

152 153 154 155 156 157 158 159 160
  %w[show raw].each do |action|
    describe "GET ##{action}" do
      context 'when the project snippet is private' do
        let(:project_snippet) { create(:project_snippet, :private, project: project, author: user) }

        context 'when anonymous' do
          it 'responds with status 404' do
            get action, namespace_id: project.namespace.path, project_id: project.path, id: project_snippet.to_param

161
            expect(response).to have_http_status(404)
162 163 164 165 166 167 168 169 170 171
          end
        end

        context 'when signed in as the author' do
          before { sign_in(user) }

          it 'renders the snippet' do
            get action, namespace_id: project.namespace.path, project_id: project.path, id: project_snippet.to_param

            expect(assigns(:snippet)).to eq(project_snippet)
172
            expect(response).to have_http_status(200)
173 174 175 176 177 178 179 180 181 182
          end
        end

        context 'when signed in as a project member' do
          before { sign_in(user2) }

          it 'renders the snippet' do
            get action, namespace_id: project.namespace.path, project_id: project.path, id: project_snippet.to_param

            expect(assigns(:snippet)).to eq(project_snippet)
183
            expect(response).to have_http_status(200)
184 185 186 187 188 189 190 191 192
          end
        end
      end

      context 'when the project snippet does not exist' do
        context 'when anonymous' do
          it 'responds with status 404' do
            get action, namespace_id: project.namespace.path, project_id: project.path, id: 42

193
            expect(response).to have_http_status(404)
194 195 196 197 198 199 200 201 202
          end
        end

        context 'when signed in' do
          before { sign_in(user) }

          it 'responds with status 404' do
            get action, namespace_id: project.namespace.path, project_id: project.path, id: 42

203
            expect(response).to have_http_status(404)
204 205 206 207 208 209
          end
        end
      end
    end
  end
end