summaryrefslogtreecommitdiffstats
path: root/test/functional/favorites_controller_test.rb
blob: 4fec95f7a848dc5c01d0342ef3a1ee7ab969bb88 (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
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
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#   Copyright (C) 2007, 2008 Johan Sørensen <johan@johansorensen.com>
#   Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
#   Copyright (C) 2008 Patrick Aljord <patcito@gmail.com>
#   Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
#   Copyright (C) 2009 Fabio Akita <fabio.akita@gmail.com>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

require File.dirname(__FILE__) +  '/../test_helper'

class FavoritesControllerTest < ActionController::TestCase
  def do_create_post(type, id, extra_options={})
    post :create, extra_options.merge(:watchable_type => type,
      :watchable_id => id)
  end

  context "Creating a new favorite" do
    setup {
      login_as :johan
      @repository = repositories(:johans2)
    }

    should "require login" do
      session[:user_id] = nil
      post :create
      assert_redirected_to new_sessions_path
    end

    should "assign to watchable" do
      do_create_post(@repository.class.name, @repository.id)
      assert_response :redirect
      assert_equal @repository, assigns(:watchable)
    end

    should "render not found when missing watchable" do
      do_create_post(@repository.class.name, 999)
      assert_response :not_found
    end

    should "render not found when invalid watchable type is provided" do
      do_create_post("RRepository", @repository.id)
      assert_response :not_found
    end

    should "create a favorite" do
      do_create_post(@repository.class.name, @repository.id)
      assert_not_nil(favorite = assigns(:favorite))
    end

    should "redirect to the watchable itself" do
      do_create_post(@repository.class.name, @repository.id)
      assert_redirected_to([@repository.owner, @repository.project, @repository])
    end

    context "JS requests" do
      should "render :created" do
        do_create_post(@repository.class.name, @repository.id, {:format => "js"})
        assert_response :created
      end

      should "render :not_found" do
        do_create_post("Rrepository", @repository.id)
        assert_response :not_found
      end

      should "supply deletion URL in Location:" do
        do_create_post(@repository.class.name, @repository.id, {:format => "js"})
        assert_not_nil(favorite = assigns(:favorite))
        assert_equal("/favorites/#{favorite.id}", @response.headers["Location"])
      end
    end
  end

  context "Watching a merge request" do
    setup {
      login_as :johan
      @merge_request = merge_requests(:moes_to_johans)
    }

    should "create it" do
      do_create_post(@merge_request.class.name, @merge_request.id,
        {:format => "js"})
      assert_response :created
    end

    should "destroy it" do
      favorite = users(:johan).favorites.create(:watchable => @merge_request)
      delete :destroy, :id => favorite, :format => "js"
      assert_response :ok
    end
  end

  context "Deleting a favorite" do
    setup {
      login_as :johan
      @repository = repositories(:johans2)
      @favorite = users(:johan).favorites.create(:watchable => @repository)
    }

    should "assign to favorite" do
      delete :destroy, :id => @favorite
      assert_equal @favorite, assigns(:favorite)
    end

    should "redirect for HTML" do
      delete :destroy, :id => @favorite
      assert_redirected_to([@repository.owner, @repository.project, @repository])
    end

    should "render :deleted for JS" do
      delete :destroy, :id => @favorite, :format => "js"
      assert_response :ok
    end

    should "supply re-creation URL in Location:" do
      delete :destroy, :id => @favorite, :format => "js"
      assert_equal(
        favorites_path(:watchable_id => @repository.id, :watchable_type => "Repository"),
        @response.headers["Location"])
    end

    should "delete the favorite" do
      delete :destroy, :id => @favorite
      assert_raises ActiveRecord::RecordNotFound do
        Favorite.find(@favorite.id)
      end
    end
  end

  context "listing a users own favorites" do
    setup do
      @user = users(:mike)
      repositories(:johans).watched_by!(@user)
      login_as :mike
    end

    should "require login" do
      login_as nil
      get :index
      assert_redirected_to new_sessions_path
    end

    should "only list the users favorites" do
      assert @user.favorites.count > 0, "user has no favs"
      other_fav = Favorite.create!({:user => users(:johan),
          :watchable => Repository.last})
      get :index
      assert !assigns(:favorites).include?(other_fav)
      assert_equal @user.favorites, assigns(:favorites)
      assert_response :success
    end

    should "have a button to toggle the mail flag" do
      get :index
      assert_response :success
      assert_select "td.notification .favorite a.toggle"
    end

    should "have a button to delete the favorite" do
      get :index
      assert_response :success
      assert_select "td.unwatch .favorite a.watch-link"
    end
  end

  context "editing a favorite" do
    setup do
      @user = users(:mike)
      login_as @user
      @favorite = Repository.last.watched_by!(@user)
    end

    should "scope the find to the user" do
      fav = Favorite.create!({:user => users(:johan),
          :watchable => Repository.last})
      put :update, :id => fav.id
      assert_response :not_found
    end

    should "be able to add the mail flag" do
      assert !@favorite.notify_by_email?
      get :update, :id => @favorite.id, :favorite => {:notify_by_email => true}
      assert_response :redirect
      assert_redirected_to favorites_path
      assert @favorite.reload.notify_by_email?
    end

    should "only be able to change the mail flag" do
      assert !@favorite.notify_by_email?
      get :update, :id => @favorite.id, :favorite => {:user_id => users(:johan).id}
      assert_response :redirect
      assert_equal @user, @favorite.reload.user
    end
  end
end