summaryrefslogtreecommitdiffstats
path: root/test/functional/aliases_controller_test.rb
blob: 201ecf3818752918ed0fce6140478035ca62fa4a (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
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
#   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 AliasesControllerTest < ActionController::TestCase
  
  should_render_in_global_context
  
  def setup
    @user = users(:johan)
    @email = emails(:johans1)
    login_as :johan
  end
  
  context "Listing all emails" do
    should "require login" do
      login_as nil
      get :index, :user_id => @user.to_param
      assert_redirected_to new_sessions_path
    end
    
    should "require current_user" do
      login_as :moe
      get :index, :user_id => @user.to_param
      assert_redirected_to user_path(users(:moe))
    end
    
    should "List the current users emails" do
      get :index, :user_id => @user.to_param
      assert_response :success
      assert_equal @user.email_aliases, assigns(:emails)
    end
  end
  
  context "GETting the new email page" do
    should "require login" do
      login_as nil
      get :new, :user_id => @user.to_param
      assert_redirected_to new_sessions_path
    end
    
    should "require current_user" do
      login_as :moe
      get :new, :user_id => @user.to_param
      assert_redirected_to user_path(users(:moe))
    end
    
    should "should be ok" do
      get :new, :user_id => @user.to_param
      assert_response :success
      assert_template "new"
      assert_equal @user, assigns(:user)
    end
  end
  
  context "POSTing to create a new email page" do
    should "require login" do
      login_as nil
      post :create, :user_id => @user.to_param, :email => {:address => "bob@example.com"}
      assert_redirected_to new_sessions_path
    end
    
    should "require current_user" do
      login_as :moe
      post :create, :user_id => @user.to_param, :email => {:address => "bob@example.com"}
      assert_redirected_to user_path(users(:moe))
    end
    
    should "create the email" do
      assert_difference("@user.email_aliases.count") do
        post :create, :user_id => @user.to_param, :email => {:address => "bob@example.com"}
      end
      assert_response :redirect
      assert_redirected_to user_aliases_path(@user)
      assert_match(/receive an email asking you to confirm.+bob@example\.com/, flash[:success])
    end
    
    should "re-render the form on invalid data" do
      assert_no_difference("@user.email_aliases.count") do
        post :create, :user_id => @user.to_param, :email => {:address => "bob"}
      end
      assert_response :success
      assert_template "new"
    end
  end
  
  context "DELETE an email" do
    should "require login" do
      login_as nil
      delete :destroy, :user_id => @user.to_param, :id => @email.to_param
      assert_redirected_to new_sessions_path
    end
    
    should "require current_user" do
      login_as :moe
      delete :destroy, :user_id => @user.to_param, :id => @email.to_param
      assert_redirected_to user_path(users(:moe))
    end
    
    should "destroys the email" do
      assert_difference("@user.reload.email_aliases.count", -1) do
        delete :destroy, :user_id => @user.to_param, :id => @email.to_param
        assert_response :redirect
      end
      assert_equal "Email alias deleted", flash[:success]
      assert_redirected_to user_aliases_path(@user)
    end
  end
  
  context "Email confirmation" do
    setup do
      @email = Email.new(:address => "foo@bar.com")
      @email.user = users(:johan)
      @email.save!
      assert @email.pending?
    end
    
    should "should not confirm the email if the confirmation_code doesn't match" do
      login_as :johan
      get :confirm, :user_id => users(:johan).to_param, :id => "wrongcode"
      assert_response :redirect
      assert_redirected_to user_path(users(:johan))
      assert_match(/is incorrect/, flash[:error])
      assert @email.reload.pending?
    end
    
    should "should not confirm the email if the user isn't right" do
      login_as :moe
      get :confirm, :user_id => users(:johan).to_param, :id => @email.confirmation_code
      assert_response :redirect
      assert_redirected_to user_path(users(:moe))
      assert_nil flash[:success]
      assert @email.reload.pending?
    end
    
    should "should confirm the email if everything is ok" do
      login_as :johan
      get :confirm, :user_id => users(:johan).to_param, :id => @email.confirmation_code
      assert_response :redirect
      assert_redirected_to user_aliases_path(users(:johan))
      assert_nil flash[:error]
      assert_match(/is now confirmed as belonging to you/, flash[:success])
      assert @email.reload.confirmed?
    end
  end
end