apps_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
require 'spec_helper'

describe AppsController do
  render_views

  it_requires_authentication
  it_requires_admin_privileges :for => {:new => :get, :edit => :get, :create => :post, :update => :put, :destroy => :delete}

  describe "GET /apps" do
    context 'when logged in as an admin' do
      it 'finds all apps' do
        sign_in Factory(:admin)
        3.times { Factory(:app) }
        apps = App.all
        get :index
        assigns(:apps).should == apps
      end
    end

    context 'when logged in as a regular user' do
      it 'finds apps the user is watching' do
        sign_in(user = Factory(:user))
        unwatched_app = Factory(:app)
        watched_app1 = Factory(:app)
        watched_app2 = Factory(:app)
        Factory(:user_watcher, :user => user, :app => watched_app1)
        Factory(:user_watcher, :user => user, :app => watched_app2)
        get :index
        assigns(:apps).should include(watched_app1, watched_app2)
        assigns(:apps).should_not include(unwatched_app)
      end
    end
  end

  describe "GET /apps/:id" do
    context 'logged in as an admin' do
      before(:each) do
        @user = Factory(:admin)
        sign_in @user
        @app = Factory(:app)
        @err = Factory :err, :app => @app
        @notice = Factory :notice, :err => @err
      end

      it 'finds the app' do
        get :show, :id => @app.id
        assigns(:app).should == @app
      end

      it "should not raise errors for app with err without notices" do
        Factory :err, :app => @app
        lambda { get :show, :id => @app.id }.should_not raise_error
      end

      it "should list atom feed successfully" do
        get :show, :id => @app.id, :format => "atom"
        response.should be_success
        response.body.should match(@err.message)
      end

      context "pagination" do
        before(:each) do
          35.times { Factory :err, :app => @app }
        end

        it "should have default per_page value for user" do
          get :show, :id => @app.id
          assigns(:errs).size.should == User::PER_PAGE
        end

        it "should be able to override default per_page value" do
          @user.update_attribute :per_page, 10
          get :show, :id => @app.id
          assigns(:errs).size.should == 10
        end
      end

      context 'with resolved errors' do
        before(:each) do
          resolved_err = Factory.create(:err, :app => @app, :resolved => true)
          Factory.create(:notice, :err => resolved_err)
        end

        context 'and no params' do
          it 'shows only unresolved errs' do
            get :show, :id => @app.id
            assigns(:errs).size.should == 1
          end
        end

        context 'and all_errs=true params' do
          it 'shows all errors' do
            get :show, :id => @app.id, :all_errs => true
            assigns(:errs).size.should == 2
          end
        end
      end

      context 'with environment filters' do
        before(:each) do
          environments = ['production', 'test', 'development', 'staging']
          20.times do |i|
            Factory.create(:err, :app => @app, :environment => environments[i % environments.length])
          end
        end

        context 'no params' do
          it 'shows errs for all environments' do
            get :show, :id => @app.id
            assigns(:errs).size.should == 21
          end
        end

        context 'environment production' do
          it 'shows errs for just production' do
            get :show, :id => @app.id, :environment => :production
            assigns(:errs).size.should == 6
          end
        end

        context 'environment staging' do
          it 'shows errs for just staging' do
            get :show, :id => @app.id, :environment => :staging
            assigns(:errs).size.should == 5
          end
        end

        context 'environment development' do
          it 'shows errs for just development' do
            get :show, :id => @app.id, :environment => :development
            assigns(:errs).size.should == 5
          end
        end

        context 'environment test' do
          it 'shows errs for just test' do
            get :show, :id => @app.id, :environment => :test
            assigns(:errs).size.should == 5
          end
        end
      end
    end

    context 'logged in as a user' do
      it 'finds the app if the user is watching it' do
        user = Factory(:user)
        app = Factory(:app)
        watcher = Factory(:user_watcher, :app => app, :user => user)
        sign_in user
        get :show, :id => app.id
        assigns(:app).should == app
      end

      it 'does not find the app if the user is not watching it' do
        sign_in Factory(:user)
        app = Factory(:app)
        lambda {
          get :show, :id => app.id
        }.should raise_error(Mongoid::Errors::DocumentNotFound)
      end
    end
  end

  context 'logged in as an admin' do
    before do
      sign_in Factory(:admin)
    end

    describe "GET /apps/new" do
      it 'instantiates a new app with a prebuilt watcher' do
        get :new
        assigns(:app).should be_a(App)
        assigns(:app).should be_new_record
        assigns(:app).watchers.should_not be_empty
      end
    end

    describe "GET /apps/:id/edit" do
      it 'finds the correct app' do
        app = Factory(:app)
        get :edit, :id => app.id
        assigns(:app).should == app
      end
    end

    describe "POST /apps" do
      before do
        @app = Factory(:app)
        App.stub(:new).and_return(@app)
      end

      context "when the create is successful" do
        before do
          @app.should_receive(:save).and_return(true)
        end

        it "should redirect to the app page" do
          post :create, :app => {}
          response.should redirect_to(app_path(@app))
        end

        it "should display a message" do
          post :create, :app => {}
          request.flash[:success].should match(/success/)
        end
      end
    end

    describe "PUT /apps/:id" do
      before do
        @app = Factory(:app)
      end

      context "when the update is successful" do
        it "should redirect to the app page" do
          put :update, :id => @app.id, :app => {}
          response.should redirect_to(app_path(@app))
        end

        it "should display a message" do
          put :update, :id => @app.id, :app => {}
          request.flash[:success].should match(/success/)
        end
      end

      context "changing name" do
        it "should redirect to app page" do
          id = @app.id
          put :update, :id => id, :app => {:name => "new name"}
          response.should redirect_to(app_path(id))
        end
      end

      context "when the update is unsuccessful" do
        it "should render the edit page" do
          put :update, :id => @app.id, :app => { :name => '' }
          response.should render_template(:edit)
        end
      end

      context "changing email_at_notices" do
        it "should parse legal csv values" do
          put :update, :id => @app.id, :app => { :email_at_notices => '1,   4,      7,8,  10' }
          @app.reload
          @app.email_at_notices.should == [1, 4, 7, 8, 10]
        end
        context "failed parsing of CSV" do
          it "should set the default value" do
            @app = Factory(:app, :email_at_notices => [1, 2, 3, 4])
            put :update, :id => @app.id, :app => { :email_at_notices => 'asdf, -1,0,foobar,gd00,0,abc' }
            @app.reload
            @app.email_at_notices.should == Errbit::Config.email_at_notices
          end

          it "should display a message" do
            put :update, :id => @app.id, :app => { :email_at_notices => 'qwertyuiop' }
            request.flash[:error].should match(/Couldn't parse/)
          end
        end
      end

      context "setting up issue tracker", :cur => true do
        context "unknown tracker type" do
          before(:each) do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
              :type => 'unknown', :project_id => '1234', :api_token => '123123', :account => 'myapp'
            } }
            @app.reload
          end

          it "should not create issue tracker" do
            @app.issue_tracker_configured?.should == false
          end
        end

        context "lighthouseapp" do
          it "should save tracker params" do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
              :type => 'LighthouseTracker', :project_id => '1234', :api_token => '123123', :account => 'myapp'
            } }
            @app.reload

            tracker = @app.issue_tracker
            tracker.should be_a(LighthouseTracker)
            tracker.project_id.should == '1234'
            tracker.api_token.should == '123123'
            tracker.account.should == 'myapp'
          end

          it "should show validation notice when sufficient params are not present" do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
              :type => 'LighthouseTracker', :project_id => '1234', :api_token => '123123'
            } }
            @app.reload

            @app.issue_tracker_configured?.should == false
            response.body.should match(/You must specify your Lighthouseapp account, API token and Project ID/)
          end
        end

        context "redmine" do
          it "should save tracker params" do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
              :type => 'RedmineTracker', :project_id => '1234', :api_token => '123123', :account => 'http://myapp.com'
            } }
            @app.reload

            tracker = @app.issue_tracker
            tracker.should be_a(RedmineTracker)
            tracker.project_id.should == '1234'
            tracker.api_token.should == '123123'
            tracker.account.should == 'http://myapp.com'
          end

          it "should show validation notice when sufficient params are not present" do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
              :type => 'RedmineTracker', :project_id => '1234', :api_token => '123123'
            } }
            @app.reload

            @app.issue_tracker_configured?.should == false
            response.body.should match(/You must specify your Redmine URL, API token and Project ID/)
          end
        end

        context "pivotal" do
          it "should save tracker params" do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
              :type => 'PivotalLabsTracker', :project_id => '1234', :api_token => '123123' } }
            @app.reload

            tracker = @app.issue_tracker
            tracker.should be_a(PivotalLabsTracker)
            tracker.project_id.should == '1234'
            tracker.api_token.should == '123123'
          end

          it "should show validation notice when sufficient params are not present" do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
              :type => 'PivotalLabsTracker', :project_id => '1234' } }
            @app.reload

            @app.issue_tracker_configured?.should == false
            response.body.should match(/You must specify your Pivotal Tracker API token and Project ID/)
          end
        end

        context "fogbugz" do
          context 'with correct params' do
            before do
              put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
                :type => 'FogbugzTracker', :account => 'abc', :project_id => 'Service - Peon', :username => '1234', :password => '123123' } }
              @app.reload
            end

            subject {@app.issue_tracker}
            its(:type) {should == "FogbugzTracker"}
            its(:account) {should == 'abc'}
            its(:project_id) {should == 'Service - Peon'}
            its(:username) {should == '1234'}
            its(:password) {should == '123123'}
          end

          context 'insufficient params' do
            it 'shows validation notice' do
              put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
                :type => 'FogbugzTracker', :project_id => '1234' } }
              @app.reload

              @app.issue_tracker_configured?.should == false
              response.body.should match(/You must specify your FogBugz Area Name, FogBugz URL, Username, and Password/)
            end
          end
        end

        context "mingle" do
          context 'with correct params' do
            before do
              put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
                :type => 'MingleTracker', :project_id => 'test', :account => 'http://mingle.example.com',
                :username => '1234', :password => '123123', :ticket_properties => "card_type = Defect"
              } }
              @app.reload
            end

            subject {@app.issue_tracker}
            its(:type) {should == "MingleTracker"}
            its(:project_id) {should == 'test'}
            its(:username) {should == '1234'}
            its(:password) {should == '123123'}
          end

          it "should show validation notice when sufficient params are not present" do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
              :type => 'MingleTracker', :project_id => 'test', :account => 'http://mingle.example.com',
              :username => '1234', :password => '1234', :ticket_properties => "cards_type = Defect"
            } }
            @app.reload

            @app.issue_tracker_configured?.should == false
            response.body.should match(/You must specify your Mingle URL, Project ID, Card Type \(in default card properties\), Sign-in name, and Password/)
          end
        end

        context "github issues" do
          context 'with correct params' do
            before do
              put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
                :type => 'GithubTracker', :project_id => 'test', :username => 'user',
                :api_token => '123123'
              } }
              @app.reload
            end

            subject {@app.issue_tracker}
            its(:type) {should == "GithubTracker"}
            its(:project_id) {should == 'test'}
            its(:username) {should == 'user'}
            its(:api_token) {should == '123123'}
          end

          it "should show validation notice when sufficient params are not present" do
            put :update, :id => @app.id, :app => { :issue_tracker_attributes => {
                :type => 'GithubTracker', :project_id => 'test', :username => 'user'
            } }
            @app.reload

            @app.issue_tracker_configured?.should == false
            response.body.should match(/You must specify your Github repository, username and API token/)
          end
        end

      end
    end

    describe "DELETE /apps/:id" do
      before do
        @app = Factory(:app)
        App.stub(:find).with(@app.id).and_return(@app)
      end

      it "should find the app" do
        delete :destroy, :id => @app.id
        assigns(:app).should == @app
      end

      it "should destroy the app" do
        @app.should_receive(:destroy)
        delete :destroy, :id => @app.id
      end

      it "should display a message" do
        delete :destroy, :id => @app.id
        request.flash[:success].should match(/success/)
      end

      it "should redirect to the apps page" do
        delete :destroy, :id => @app.id
        response.should redirect_to(apps_path)
      end
    end
  end

end