repositories_controller_spec.rb 15.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
require 'rails_helper'

describe RepositoriesController, :type => :controller do
  describe 'index' do
    let!(:repository) { FactoryGirl.build(:repository) }

    before :each do
      Repository.expects(:all).returns([repository])
      get :index
    end

    it { is_expected.to render_template(:index) }
  end

  describe 'new' do
    context 'with a User logged in' do
      let!(:user) { FactoryGirl.create(:user) }
      let!(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] }
      before :each do
        sign_in user
      end

      context 'when a project_id is provided' do
        let(:project) { FactoryGirl.build(:project_with_id) }

        context 'and the current user owns the project' do
          before :each do
            KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
            Repository.expects(:repository_types).returns([])
            subject.expects(:project_owner?).returns true

            get :new, project_id: project.id.to_json
          end

          it { is_expected.to respond_with(:success) }
          it { is_expected.to render_template(:new) }
        end

        context "and the current user doesn't own the project" do
          before :each do
            get :new, project_id: project.id.to_s
          end

          it { is_expected.to redirect_to(projects_url) }
          it { is_expected.to respond_with(:redirect) }
        end
      end

      context 'when no project_id is provided' do
        before :each do
          KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
          Repository.expects(:repository_types).returns([])

          get :new
        end

        it { is_expected.to respond_with(:success) }
        it { is_expected.to render_template(:new) }
      end
    end

    context 'with no user logged in' do
      let(:project) { FactoryGirl.build(:project_with_id) }

      before :each do
        get :new, project_id: project.id.to_s
      end

      it { is_expected.to redirect_to new_user_session_path }
    end
  end

  describe 'create' do
    let!(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] }
    let!(:user) { FactoryGirl.create(:user) }
    let(:repository_params) { FactoryGirl.build(:repository).to_hash }

    before do
      sign_in user
    end

    context 'when a project_id is provided' do
      let(:project) { FactoryGirl.build(:project_with_id) }
      let(:repository) { FactoryGirl.build(:repository, project_id: project.id) }

      context 'and the current user owns the project' do
        before :each do
          subject.expects(:project_owner?).returns true
        end

        context 'with valid fields' do
          before :each do
            Repository.any_instance.expects(:save).returns(true)

            post :create, project_id: project.id, repository: repository_params
          end

          it { is_expected.to redirect_to(repository_process_path(id: repository.id)) }
          it { is_expected.to respond_with(:redirect) }
          it "is expected to set the project_id" do
            expect(assigns(:repository).project_id).to be > 0
          end
        end

        context 'with an invalid field' do
          before :each do
            KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
            Repository.any_instance.expects(:save).returns(false)
            Repository.expects(:repository_types).returns([])

            post :create, project_id: project.id.to_s, repository: repository_params
          end

          it { is_expected.to render_template(:new) }
        end
      end

      context "and the current user doesn't own the project " do
        before :each do
          post :create, project_id: project.id, repository: repository_params
        end

        it { is_expected.to redirect_to(projects_url) }
        it { is_expected.to respond_with(:redirect) }
      end
    end

    context 'when no project_id is provided' do
      let(:repository) { FactoryGirl.build(:repository) }

      context 'with valid fields' do
        before :each do
          Repository.any_instance.expects(:save).returns(true)

          post :create, repository: repository_params
        end

        it { is_expected.to redirect_to(repository_process_path(id: repository.id)) }
        it { is_expected.to respond_with(:redirect) }
        it "is expected to not set the project_id" do
          expect(assigns(:repository).project_id).to be_nil
        end
      end

      context 'with an invalid field' do
        before :each do
          KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
          Repository.any_instance.expects(:save).returns(false)
          Repository.expects(:repository_types).returns([])

          post :create.to_s, repository: repository_params
        end

        it { is_expected.to render_template(:new) }
      end
    end
  end

  describe 'show' do
    let(:repository) { FactoryGirl.build(:repository) }

    context 'without a specific module_result' do
      before :each do
        KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id))
        Repository.expects(:find).with(repository.id).returns(repository)

        get :show, id: repository.id.to_s
      end

      it { is_expected.to render_template(:show) }
    end

    context 'for an specific module_result' do

      before :each do
        KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id))
        Repository.expects(:find).with(repository.id).returns(repository)

        get :show, id: repository.id.to_s
      end

      it { is_expected.to render_template(:show) }
    end
  end

  describe 'destroy' do
    let(:repository) { FactoryGirl.build(:repository) }

    context 'with an User logged in' do
      before do
        sign_in FactoryGirl.create(:user)
      end

      context 'when the user owns the project' do
        before :each do
          subject.expects(:repository_owner?).returns true
          repository.expects(:destroy)
          Repository.expects(:find).at_least_once.with(repository.id).returns(repository)

          delete :destroy, id: repository.id
        end

        it { is_expected.to redirect_to(projects_path) }
        it { is_expected.to respond_with(:redirect) }
      end

      context "when the user doesn't own the project" do
        before :each do
          delete :destroy, id: repository.id
        end

         it { is_expected.to redirect_to(projects_url) }
         it { is_expected.to respond_with(:redirect) }
      end
    end

    context 'with no User logged in' do
      before :each do
        delete :destroy, id: repository.id
      end

      it { is_expected.to redirect_to new_user_session_path }
    end
  end

  describe 'edit' do
    let(:repository) { FactoryGirl.build(:repository) }

    context 'with an User logged in' do
      let!(:user) { FactoryGirl.create(:user) }
      let!(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] }
      before do
        sign_in user
      end

      context 'when the user owns the repository' do
        before :each do
          KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
          subject.expects(:repository_owner?).returns true
          Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
          Repository.expects(:repository_types).returns(["SUBVERSION"])
          get :edit, id: repository.id
        end

        it { is_expected.to render_template(:edit) }
      end

      context 'when the user does not own the repository' do
        before do
          get :edit, id: repository.id
        end

        it { is_expected.to redirect_to(projects_url) }
        it { is_expected.to respond_with(:redirect) }
        it { is_expected.to set_flash[:notice].to("You're not allowed to do this operation") }
      end
    end

    context 'with no user logged in' do
      before :each do
        get :edit, id: repository.id
      end

      it { is_expected.to redirect_to new_user_session_path }
    end
  end

  describe 'update' do
    let(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] }
    let(:repository) { FactoryGirl.build(:repository) }
    let(:repository_params) { Hash[FactoryGirl.attributes_for(:repository).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers

    context 'when the user is logged in' do
      let!(:user) { FactoryGirl.create(:user) }

      before do
        sign_in user
      end

      context 'when user owns the repository' do
        before :each do
          subject.expects(:repository_owner?).returns true
        end

        context 'with valid fields' do
          before :each do
            Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
            Repository.any_instance.expects(:update).with(repository_params).returns(true)

            post :update, id: repository.id, repository: repository_params
          end

          it { is_expected.to redirect_to(repository_path(repository.id)) }
          it { is_expected.to respond_with(:redirect) }
        end

        context 'with an invalid field' do
          before :each do
            KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
            Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
            Repository.any_instance.expects(:update).with(repository_params).returns(false)
            Repository.expects(:repository_types).returns([])

            post :update, id: repository.id, repository: repository_params
          end

          it { is_expected.to render_template(:edit) }
        end
      end

      context 'when the user does not own the repository' do
        before :each do
          post :update, id: repository.id, repository: repository_params
        end

        it { is_expected.to redirect_to projects_path }
      end
    end

    context 'with no user logged in' do
      before :each do
        post :update, id: repository.id, repository: repository_params
      end

      it { is_expected.to redirect_to new_user_session_path }
    end
  end

  describe 'state' do
    let(:repository) { FactoryGirl.build(:repository) }

    context 'with a READY state' do
      let(:ready_processing) { FactoryGirl.build(:processing) }

      before :each do
        repository.expects(:last_processing).returns(ready_processing)
        Repository.expects(:find).at_least_once.with(repository.id).returns(repository)

        xhr :get, :state, {id: repository.id, last_state: 'ANALYZING'}
      end

      it { is_expected.to respond_with(:success) }
      it { is_expected.to render_template(:load_ready_processing) }
    end

    context 'with another state then READY' do
      let(:processing) { FactoryGirl.build(:processing, state: 'ANALYZING') }

       before :each do
        repository.expects(:last_processing).returns(processing)
        Repository.expects(:find).at_least_once.with(repository.id).returns(repository)

        xhr :get, :state, {id: repository.id, last_state: 'LOADING'}
      end

      it { is_expected.to respond_with(:success) }
      it { is_expected.to render_template(:reload_processing) }
    end

    context 'when it was already READY' do
      before :each do
        Repository.expects(:find).at_least_once.with(repository.id).returns(repository)

        xhr :get, :state, {id: repository.id, last_state: 'READY'}
      end

      it { is_expected.to respond_with(:ok) }
      it { is_expected.not_to render_with_layout }
    end


    context 'with a ERROR state' do
      let(:errored_processing) { FactoryGirl.build(:errored_processing) }

      before :each do
        repository.expects(:last_processing).returns(errored_processing)
        Repository.expects(:find).at_least_once.with(repository.id).returns(repository)

        xhr :get, :state, {id: repository.id, last_state: 'ANALYZING'}
      end

      it { is_expected.to respond_with(:success) }
      it { is_expected.to render_template(:load_error) }
    end
  end

  describe 'state_with_date' do
    let(:processing) { FactoryGirl.build(:processing) }
    let(:repository) { FactoryGirl.build(:repository) }

    before :each do
      repository.expects(:processing_with_date).with("2013-11-11").returns(processing)
      Repository.expects(:find).at_least_once.with(repository.id).returns(repository)

      xhr :get, :state_with_date, {id: repository.id, day: '11', month: '11', year: '2013'}
    end

    it { is_expected.to respond_with(:ok) }
    it { is_expected.not_to render_with_layout }
  end

  describe 'process_repository' do
      let(:repository) { FactoryGirl.build(:repository) }
      before :each do
        sign_in FactoryGirl.create(:user)
        subject.expects(:repository_owner?).returns true
        repository.expects(:process)
        Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
        KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id))
        get :process_repository, id: repository.id
      end
      it { is_expected.to redirect_to(repository_path(repository.id)) }
  end

  describe 'branches' do
    let(:url) { "dummy-url" }
    let(:scm_type) { "GIT" }

    context 'valid parameters' do
      let!(:branches) { ['branch1', 'branch2'] }

      before :each do
        sign_in FactoryGirl.create(:user)
        Repository.expects(:branches).with(url, scm_type).returns(branches: branches)
        get :branches, url: url, scm_type: scm_type, format: :json
      end

      it 'is expected to return a list of branches' do
        expect(JSON.parse(response.body)).to eq(JSON.parse({branches: branches}.to_json))
      end

      it { is_expected.to respond_with(:success) }
    end

    context 'invalid parameters' do
      before :each do
        sign_in FactoryGirl.create(:user)
        Repository.expects(:branches).with(url, scm_type).returns(errors: ['Error'])
        get :branches, url: url, scm_type: scm_type, format: :json
      end

      it 'is expected to return an empty list' do
        expect(JSON.parse(response.body)).to eq(JSON.parse({errors: ['Error']}.to_json))
      end

      it { is_expected.to respond_with(:success) }
    end
  end

  describe 'branches_params' do
    let!(:url) { 'dummy-url' }
    let!(:scm_type) { 'GIT' }
    let(:parameters) { ActionController::Parameters.new(scm_type: scm_type, url: url) }

    context 'valid parameters' do
      it 'should return a hash with the permitted parameters' do
        subject.params = parameters
        expect(subject.params.permitted?).to be_falsey
        result = subject.send(:branches_params)
        expect(result).to eq(parameters)
        expect(result.permitted?).to be_truthy
      end
    end

    context 'invalid parameters' do
      let(:invalid_parameters) { ActionController::Parameters.new(scm_type: scm_type, url: url, something_evil: 'fizzbuzz') }

      it 'should return a valid parameters hash' do
        subject.params = invalid_parameters
        expect(subject.params.permitted?).to be_falsey
        result = subject.send(:branches_params)
        expect(result).to eq(parameters)
        expect(result.permitted?).to be_truthy
      end
    end
  end
end