BigW Consortium Gitlab

time_tracking_shared_examples.rb 5.06 KB
Newer Older
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
shared_examples 'V3 time tracking endpoints' do |issuable_name|
  issuable_collection_name = issuable_name.pluralize

  describe "POST /projects/:id/#{issuable_collection_name}/:#{issuable_name}_id/time_estimate" do
    context 'with an unauthorized user' do
      subject { post(v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/time_estimate", non_member), duration: '1w') }

      it_behaves_like 'an unauthorized API user'
    end

    it "sets the time estimate for #{issuable_name}" do
      post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/time_estimate", user), duration: '1w'

      expect(response).to have_http_status(200)
      expect(json_response['human_time_estimate']).to eq('1w')
    end

    describe 'updating the current estimate' do
      before do
        post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/time_estimate", user), duration: '1w'
      end

      context 'when duration has a bad format' do
        it 'does not modify the original estimate' do
          post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/time_estimate", user), duration: 'foo'

          expect(response).to have_http_status(400)
          expect(issuable.reload.human_time_estimate).to eq('1w')
        end
      end

      context 'with a valid duration' do
        it 'updates the estimate' do
          post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/time_estimate", user), duration: '3w1h'

          expect(response).to have_http_status(200)
          expect(issuable.reload.human_time_estimate).to eq('3w 1h')
        end
      end
    end
  end

  describe "POST /projects/:id/#{issuable_collection_name}/:#{issuable_name}_id/reset_time_estimate" do
    context 'with an unauthorized user' do
      subject { post(v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/reset_time_estimate", non_member)) }

      it_behaves_like 'an unauthorized API user'
    end

    it "resets the time estimate for #{issuable_name}" do
      post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/reset_time_estimate", user)

      expect(response).to have_http_status(200)
      expect(json_response['time_estimate']).to eq(0)
    end
  end

  describe "POST /projects/:id/#{issuable_collection_name}/:#{issuable_name}_id/add_spent_time" do
    context 'with an unauthorized user' do
      subject do
        post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/add_spent_time", non_member),
             duration: '2h'
      end

      it_behaves_like 'an unauthorized API user'
    end

    it "add spent time for #{issuable_name}" do
      post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/add_spent_time", user),
           duration: '2h'

      expect(response).to have_http_status(201)
      expect(json_response['human_total_time_spent']).to eq('2h')
    end

    context 'when subtracting time' do
      it 'subtracts time of the total spent time' do
        issuable.update_attributes!(spend_time: { duration: 7200, user: user })

        post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/add_spent_time", user),
             duration: '-1h'

        expect(response).to have_http_status(201)
        expect(json_response['total_time_spent']).to eq(3600)
      end
    end

    context 'when time to subtract is greater than the total spent time' do
      it 'does not modify the total time spent' do
        issuable.update_attributes!(spend_time: { duration: 7200, user: user })

        post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/add_spent_time", user),
             duration: '-1w'

        expect(response).to have_http_status(400)
        expect(json_response['message']['time_spent'].first).to match(/exceeds the total time spent/)
      end
    end
  end

  describe "POST /projects/:id/#{issuable_collection_name}/:#{issuable_name}_id/reset_spent_time" do
    context 'with an unauthorized user' do
      subject { post(v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/reset_spent_time", non_member)) }

      it_behaves_like 'an unauthorized API user'
    end

    it "resets spent time for #{issuable_name}" do
      post v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/reset_spent_time", user)

      expect(response).to have_http_status(200)
      expect(json_response['total_time_spent']).to eq(0)
    end
  end

  describe "GET /projects/:id/#{issuable_collection_name}/:#{issuable_name}_id/time_stats" do
    it "returns the time stats for #{issuable_name}" do
      issuable.update_attributes!(spend_time: { duration: 1800, user: user },
                                  time_estimate: 3600)

      get v3_api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.id}/time_stats", user)

      expect(response).to have_http_status(200)
      expect(json_response['total_time_spent']).to eq(1800)
      expect(json_response['time_estimate']).to eq(3600)
    end
  end
end