BigW Consortium Gitlab

snippets_controller_spec.rb 3.33 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 9 10 11 12 13 14 15 16 17 18 19 20 21
  let(:user)    { create(:user) }
  let(:user2)   { create(:user) }

  before do
    project.team << [user, :master]
    project.team << [user2, :master]
  end

  describe 'GET #index' 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 '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)
22
          expect(response).to have_http_status(200)
23 24 25 26 27 28 29 30 31 32
        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)
33
          expect(response).to have_http_status(200)
34 35 36 37 38 39 40 41 42 43
        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)
44
          expect(response).to have_http_status(200)
45 46 47 48 49 50 51 52 53 54 55 56 57 58
        end
      end
    end
  end

  %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

59
            expect(response).to have_http_status(404)
60 61 62 63 64 65 66 67 68 69
          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)
70
            expect(response).to have_http_status(200)
71 72 73 74 75 76 77 78 79 80
          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)
81
            expect(response).to have_http_status(200)
82 83 84 85 86 87 88 89 90
          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

91
            expect(response).to have_http_status(404)
92 93 94 95 96 97 98 99 100
          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

101
            expect(response).to have_http_status(404)
102 103 104 105 106 107
          end
        end
      end
    end
  end
end