summaryrefslogtreecommitdiff
path: root/test/controllers/events_controller_test.rb
blob: 46f3f4f1648cabb92253da28b2a1ff237b0cf024 (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
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
require 'test_helper'

class EventsControllerTest < ActionController::TestCase

  test "should get index" do
    login_as :quentin
    node = create_node_with_published_page
    Event.create!(node_id: node.id, start_time: Time.now, end_time: Time.now + 1.hour)

    get :index
    assert_response :success
    assert_not_nil assigns(:events)
  end

  test "should get new" do
    login_as :quentin
    get :new
    assert_response :success
  end

  test "new pre-fills tag_list and explains it via flash when auto_tag_source is given" do
    login_as :quentin
    node = create_node_with_published_page

    get :new, params: { node_id: node.id, tag_list: "open-day", auto_tag_source: "erfa-detail" }

    assert_response :success
    assert_equal "open-day", assigns(:event).tag_list.to_s
    assert_match "open-day", flash[:notice]
    assert_match "erfa-detail", flash[:notice]
  end

  test "new does not flash without an auto_tag_source" do
    login_as :quentin

    get :new, params: { tag_list: "open-day" }

    assert_response :success
    assert_nil flash[:notice]
  end

  test "new with no params renders a blank, unflashed form" do
    login_as :quentin

    get :new

    assert_response :success
    assert_nil assigns(:event).tag_list.presence
    assert_nil flash[:notice]
  end

  test "should show event" do
    login_as :quentin
    node  = create_node_with_published_page
    event = Event.create!(node_id: node.id, start_time: Time.now, end_time: Time.now + 1.hour)

    get :show, params: { id: event.id }
    assert_response :success
  end

  test "should get edit" do
    login_as :quentin
    node  = create_node_with_published_page
    event = Event.create!(node_id: node.id, start_time: Time.now, end_time: Time.now + 1.hour)

    get :edit, params: { id: event.id }
    assert_response :success
  end

  test "should create event attached to a node" do
    login_as :quentin
    node = create_node_with_published_page

    assert_difference('Event.count') do
      post :create, params: {
        event: {
          node_id:    node.id,
          start_time: Time.now,
          end_time:   Time.now + 1.hour,
          tag_list:   "open-day"
        }
      }
    end

    assert_redirected_to edit_node_path(node)
    assert_equal 'Event was successfully created.', flash[:notice]
  end

  test "should not create an event without a title or a node_id" do
    login_as :quentin

    assert_no_difference('Event.count') do
      post :create, params: { event: { start_time: Time.now, end_time: Time.now + 1.hour } }
    end

    assert_response :success # re-renders :new, not a redirect
  end

  test "should honour return_to on create" do
    login_as :quentin
    node = create_node_with_published_page

    post :create, params: {
      event:     { node_id: node.id, start_time: Time.now, end_time: Time.now + 1.hour },
      return_to: node_path(node)
    }

    assert_redirected_to node_path(node)
  end

  test "should update event" do
    login_as :quentin
    node  = create_node_with_published_page
    event = Event.create!(node_id: node.id, start_time: Time.now, end_time: Time.now + 1.hour)

    put :update, params: { id: event.id, event: { title: "Updated title" } }

    assert_redirected_to events_path
    assert_equal "Updated title", event.reload.title
  end

  test "should destroy event" do
    login_as :quentin
    node  = create_node_with_published_page
    event = Event.create!(node_id: node.id, start_time: Time.now, end_time: Time.now + 1.hour)

    assert_difference('Event.count', -1) do
      delete :destroy, params: { id: event.id }
    end

    assert_redirected_to events_url
  end
end