summaryrefslogtreecommitdiff
path: root/test/functional/users_controller_test.rb
blob: a8333fe3ea47c8db6e19f6def1c9bdf170663716 (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
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  
  test "get index as regular user renders stripped partial" do
    login_as :quentin
    get :index
    assert_response :success
    assert_select "a", { :count => 0, :text => "Destroy" }
  end
  
  test "get index as admin user renders admin partial" do
    login_as :aaron
    get :index
    assert_response :success
    assert_select "a", "Destroy"
    assert_select "a", "Show", "Edit Link is missing"
  end
  
  test "get new when logged in as admin" do
    login_as :aaron
    get :new
    assert_response :success
  end
  
  test "get new without being logged in as admin redirects back to index" do
    login_as :quentin
    get :new
    assert_response :redirect
    assert_redirected_to users_path
    assert_equal(
      "Sorry, you need to be an admin for this action", 
      @response.flash[:notice]
    )
  end
  
  test "creating new users being logged in as admin" do
    login_as :aaron
    assert_difference "User.count", +1 do
      post :create, :user => {
        :login                  => "peter",
        :email                  => "foo@bar.com",
        :password               => "xxxzzz",
        :password_confirmation  => "xxxzzz"
      }
    end
    
    assert_redirected_to user_path(User.last)
    assert !User.last.admin
  end
  
  test "creating new admin users being logged in as admin" do
    login_as :aaron
    assert_difference "User.count", +1 do
      post :create, :user => {
        :login                  => "peter",
        :email                  => "foo@bar.com",
        :password               => "xxxzzz",
        :password_confirmation  => "xxxzzz",
        :admin                  => true
      }
    end
    
    assert_redirected_to user_path(User.last)
    assert User.last.admin
  end
  
  test "creating new users not being logged as regular user wont work" do
    login_as :quentin
    assert_no_difference "User.count" do
      post :create, :user => {
        :login                  => "peter",
        :email                  => "foo@bar.com",
        :password               => "xxxzzz",
        :password_confirmation  => "xxxzzz"
      }
    end
    
    assert_redirected_to users_path
    assert_equal(
      "Sorry, you need to be an admin for this action", 
      @response.flash[:notice]
    )
  end
  
  test "get edit of another user being logged in as regular user wont work" do
    login_as :quentin
    get :edit, :id => User.find_by_login("aaron").id
    assert_redirected_to users_path
    assert_equal(
      "Sorry, you need to be an admin for this action", 
      @response.flash[:notice]
    )
  end
  
  test "get edit of another user being logged in as admin user" do
    login_as :aaron
    get :edit, :id => User.find_by_login("quentin").id
    assert_response :success
  end
  
  test "updating an user when being logged in as regular user wont work" do
    user = User.find_by_login("aaron")
    login_as :quentin
    put :update, :id => user.id, :user => {:login => "random"}
    assert_redirected_to users_path
    assert_equal(
      "Sorry, you need to be an admin for this action", 
      @response.flash[:notice]
    )
  end
  
  test "updating an user when being login in as admin user" do
    user = User.find_by_login("quentin")
    login_as :aaron
    put :update, :id => user.id, :user => {:login => "random"}
    assert_redirected_to user_path(user)
    assert_equal "random", user.reload.login
  end
  
  test "showing a user" do
    login_as :quentin
    get :show, :id => User.find_by_login("aaron").id
    assert_response :success
  end
  
  test "destroying an user being logged in as regular user wont work" do
    login_as :quentin
    assert_no_difference "User.count" do
      delete :destroy, :id => User.find_by_login("aaron").id
    end
    assert_redirected_to users_path
    assert_equal(
      "Sorry, you need to be an admin for this action", 
      @response.flash[:notice]
    )
  end
  
  test "destroying an user being logged in as admin user" do
    login_as :aaron
    assert_difference "User.count", -1 do
      delete :destroy, :id => User.find_by_login("quentin").id
    end
    assert_redirected_to users_path
  end
  
  
end