BigW Consortium Gitlab

projects_controller_spec.rb 6.09 KB
Newer Older
1 2 3 4
require('spec_helper')

describe ProjectsController do
  let(:project) { create(:project) }
Ciro Santilli committed
5
  let(:public_project) { create(:project, :public) }
6 7
  let(:user)    { create(:user) }
  let(:jpg)     { fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg') }
Marin Jankovski committed
8
  let(:txt)     { fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') }
9

10 11 12 13 14 15 16 17 18
  describe "GET show" do

    context "when requested by `go get`" do
      render_views

      it "renders the go-import meta tag" do
        get :show, "go-get" => "1", namespace_id: "bogus_namespace", id: "bogus_project"

        expect(response.body).to include("name='go-import'")
19

20 21 22 23
        content = "localhost/bogus_namespace/bogus_project git http://localhost/bogus_namespace/bogus_project.git"
        expect(response.body).to include("content='#{content}'")
      end
    end
24 25 26 27

    context "rendering default project view" do
      render_views

28
      it "renders the activity view" do
29 30
        allow(controller).to receive(:current_user).and_return(user)
        allow(user).to receive(:project_view).and_return('activity')
31

32 33 34 35
        get :show, namespace_id: public_project.namespace.path, id: public_project.path
        expect(response).to render_template('_activity')
      end

36
      it "renders the readme view" do
37 38
        allow(controller).to receive(:current_user).and_return(user)
        allow(user).to receive(:project_view).and_return('readme')
39

40 41 42 43
        get :show, namespace_id: public_project.namespace.path, id: public_project.path
        expect(response).to render_template('_readme')
      end

44
      it "renders the files view" do
45 46
        allow(controller).to receive(:current_user).and_return(user)
        allow(user).to receive(:project_view).and_return('files')
47

48 49 50 51
        get :show, namespace_id: public_project.namespace.path, id: public_project.path
        expect(response).to render_template('_files')
      end
    end
52

53
    context "when requested with case sensitive namespace and project path" do
54 55 56
      context "when there is a match with the same casing" do
        it "loads the project" do
          get :show, namespace_id: public_project.namespace.path, id: public_project.path
57

58 59 60
          expect(assigns(:project)).to eq(public_project)
          expect(response.status).to eq(200)
        end
61 62
      end

63 64 65 66 67 68 69 70 71
      context "when there is a match with different casing" do
        it "redirects to the normalized path" do
          get :show, namespace_id: public_project.namespace.path, id: public_project.path.upcase

          expect(assigns(:project)).to eq(public_project)
          expect(response).to redirect_to("/#{public_project.path_with_namespace}")
        end


72
        # MySQL queries are case insensitive by default, so this spec would fail.
73
        if Gitlab::Database.postgresql?
74
          context "when there is also a match with the same casing" do
75

76
            let!(:other_project) { create(:project, :public, namespace: public_project.namespace, path: public_project.path.upcase) }
77

78
            it "loads the exactly matched project" do
79

80 81 82 83 84
              get :show, namespace_id: public_project.namespace.path, id: public_project.path.upcase

              expect(assigns(:project)).to eq(other_project)
              expect(response.status).to eq(200)
            end
85 86
          end
        end
87 88
      end
    end
89
  end
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  describe "#destroy" do
    let(:admin) { create(:admin) }

    it "redirects to the dashboard" do
      controller.instance_variable_set(:@project, project)
      sign_in(admin)

      orig_id = project.id
      delete :destroy, namespace_id: project.namespace.path, id: project.path

      expect { Project.find(orig_id) }.to raise_error(ActiveRecord::RecordNotFound)
      expect(response.status).to eq(302)
      expect(response).to redirect_to(dashboard_projects_path)
    end
  end

Ciro Santilli committed
107
  describe "POST #toggle_star" do
108
    it "toggles star if user is signed in" do
Ciro Santilli committed
109
      sign_in(user)
110
      expect(user.starred?(public_project)).to be_falsey
111 112
      post(:toggle_star,
           namespace_id: public_project.namespace.to_param,
Vinnie Okada committed
113
           id: public_project.to_param)
114
      expect(user.starred?(public_project)).to be_truthy
115 116
      post(:toggle_star,
           namespace_id: public_project.namespace.to_param,
Vinnie Okada committed
117
           id: public_project.to_param)
118
      expect(user.starred?(public_project)).to be_falsey
Ciro Santilli committed
119 120 121
    end

    it "does nothing if user is not signed in" do
122 123
      post(:toggle_star,
           namespace_id: project.namespace.to_param,
Vinnie Okada committed
124
           id: public_project.to_param)
125
      expect(user.starred?(public_project)).to be_falsey
126 127
      post(:toggle_star,
           namespace_id: project.namespace.to_param,
Vinnie Okada committed
128
           id: public_project.to_param)
129
      expect(user.starred?(public_project)).to be_falsey
Ciro Santilli committed
130 131
    end
  end
132

Douwe Maan committed
133
  describe "DELETE remove_fork" do
134 135 136 137 138 139 140 141
    context 'when signed in' do
      before do
        sign_in(user)
      end

      context 'with forked project' do
        let(:project_fork) { create(:project, namespace: user.namespace) }

142
        before do
143
          create(:forked_project_link, forked_to_project: project_fork)
144 145 146 147
        end

        it 'should remove fork from project' do
          delete(:remove_fork,
148 149 150 151
              namespace_id: project_fork.namespace.to_param,
              id: project_fork.to_param, format: :js)

          expect(project_fork.forked?).to be_falsey
Douwe Maan committed
152
          expect(flash[:notice]).to eq('The fork relationship has been removed.')
153 154 155 156
          expect(response).to render_template(:remove_fork)
        end
      end

157 158
      context 'when project not forked' do
        let(:unforked_project) { create(:project, namespace: user.namespace) }
159

160 161 162 163 164 165 166 167
        it 'should do nothing if project was not forked' do
          delete(:remove_fork,
              namespace_id: unforked_project.namespace.to_param,
              id: unforked_project.to_param, format: :js)

          expect(flash[:notice]).to be_nil
          expect(response).to render_template(:remove_fork)
        end
168 169 170 171
      end
    end

    it "does nothing if user is not signed in" do
172
      delete(:remove_fork,
173 174 175 176 177
          namespace_id: project.namespace.to_param,
          id: project.to_param, format: :js)
      expect(response.status).to eq(401)
    end
  end
178
end