BigW Consortium Gitlab

todos_spec.rb 2.11 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::V3::Todos do
4 5
  let(:project_1) { create(:project) }
  let(:project_2) { create(:project) }
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
  let(:author_1) { create(:user) }
  let(:author_2) { create(:user) }
  let(:john_doe) { create(:user, username: 'john_doe') }
  let!(:pending_1) { create(:todo, :mentioned, project: project_1, author: author_1, user: john_doe) }
  let!(:pending_2) { create(:todo, project: project_2, author: author_2, user: john_doe) }
  let!(:pending_3) { create(:todo, project: project_1, author: author_2, user: john_doe) }
  let!(:done) { create(:todo, :done, project: project_1, author: author_1, user: john_doe) }

  before do
    project_1.team << [john_doe, :developer]
    project_2.team << [john_doe, :developer]
  end

  describe 'DELETE /todos/:id' do
    context 'when unauthenticated' do
      it 'returns authentication error' do
        delete v3_api("/todos/#{pending_1.id}")

        expect(response.status).to eq(401)
      end
    end

    context 'when authenticated' do
      it 'marks a todo as done' do
        delete v3_api("/todos/#{pending_1.id}", john_doe)

        expect(response.status).to eq(200)
        expect(pending_1.reload).to be_done
      end

      it 'updates todos cache' do
        expect_any_instance_of(User).to receive(:update_todos_count_cache).and_call_original

        delete v3_api("/todos/#{pending_1.id}", john_doe)
      end
    end
  end

  describe 'DELETE /todos' do
    context 'when unauthenticated' do
      it 'returns authentication error' do
        delete v3_api('/todos')

        expect(response.status).to eq(401)
      end
    end

    context 'when authenticated' do
      it 'marks all todos as done' do
        delete v3_api('/todos', john_doe)

        expect(response.status).to eq(200)
        expect(response.body).to eq('3')
        expect(pending_1.reload).to be_done
        expect(pending_2.reload).to be_done
        expect(pending_3.reload).to be_done
      end

      it 'updates todos cache' do
        expect_any_instance_of(User).to receive(:update_todos_count_cache).and_call_original

        delete v3_api("/todos", john_doe)
      end
    end
  end
end