summaryrefslogtreecommitdiff
path: root/spec/controllers/dashboard/todos_controller_spec.rb
blob: 6075259ea99182e7e4da4de46ffcc62f8f79a01b (plain)
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
require 'spec_helper'

describe Dashboard::TodosController do
  include ApiHelpers

  let(:user) { create(:user) }
  let(:author)  { create(:user) }
  let(:project) { create(:empty_project) }
  let(:todo_service) { TodoService.new }

  before do
    sign_in(user)
    project.team << [user, :developer]
  end

  describe 'GET #index' do
    context 'when using pagination' do
      let(:last_page) { user.todos.page.total_pages }
      let!(:issues) { create_list(:issue, 2, project: project, assignee: user) }

      before do
        issues.each { |issue| todo_service.new_issue(issue, user) }
        allow(Kaminari.config).to receive(:default_per_page).and_return(1)
      end

      it 'redirects to last_page if page number is larger than number of pages' do
        get :index, page: (last_page + 1).to_param

        expect(response).to redirect_to(dashboard_todos_path(page: last_page))
      end

      it 'redirects to correspondent page' do
        get :index, page: last_page

        expect(assigns(:todos).current_page).to eq(last_page)
        expect(response).to have_http_status(200)
      end

      it 'does not redirect to external sites when provided a host field' do
        external_host = "www.example.com"
        get :index, page: (last_page + 1).to_param, host: external_host

        expect(response).to redirect_to(dashboard_todos_path(page: last_page))
      end
    end
  end

  describe 'PATCH #restore' do
    let(:todo) { create(:todo, :done, user: user, project: project, author: author) }

    it 'restores the todo to pending state' do
      patch :restore, id: todo.id

      expect(todo.reload).to be_pending
      expect(response).to have_http_status(200)
      expect(json_response).to eq({ "count" => "1", "done_count" => "0" })
    end
  end

  describe 'PATCH #bulk_restore' do
    let(:todos) { create_list(:todo, 2, :done, user: user, project: project, author: author) }

    it 'restores the todos to pending state' do
      patch :bulk_restore, ids: todos.map(&:id)

      todos.each do |todo|
        expect(todo.reload).to be_pending
      end
      expect(response).to have_http_status(200)
      expect(json_response).to eq({ 'count' => '2', 'done_count' => '0' })
    end
  end
end