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

class PhoneModelsControllerTest < ActionController::TestCase
  
  setup do
    # Create a tenant:
    @tenant = Factory.create(:tenant)
    
    # Create a User who is member of the Tenant but has no special rights:
    @user   = Factory.create(:user)
    @tenant.tenant_memberships.create(:user_id => @user.id)
    @user.update_attributes!(:current_tenant_id => @tenant.id)
    
    # Create a User who is member of the Tenant and has super admin rights:
    @super_admin = Factory.create(:user)
    @tenant.tenant_memberships.create(:user_id => @super_admin.id)
    @super_admin.update_attributes!(:current_tenant_id => @tenant.id)
    
    # Create a PhoneModel
    #
    @phone_model = Factory.create(:phone_model)
  end
  
  [ '@user.id', '' ].each do |user_id_code|
    # Note: Do *not* actually create the user outside of tests.
    
    explanation = user_id_code.blank? ?
      "if not logged in" :
      "if logged in as an ordinary user"
    
    test "should not get index #{explanation}" do
      session[:user_id] = eval( user_id_code )
      get :index, manufacturer_id: @phone_model.manufacturer_id
      assert_response :redirect
    end
    
    test "should not get new #{explanation}" do
      session[:user_id] = eval( user_id_code )
      get :new, manufacturer_id: @phone_model.manufacturer_id
      assert_response :redirect
    end
    
    test "should not create phone_model #{explanation}" do
      session[:user_id] = eval( user_id_code )
      
      assert_no_difference('PhoneModel.count') do
        post :create, manufacturer_id: @phone_model.manufacturer_id, phone_model: Factory.build(:phone_model,
          :manufacturer_id => @phone_model.manufacturer_id).attributes
      end
    end
     
    test "should not show phone_model #{explanation}" do
      session[:user_id] = eval( user_id_code )
      get :show, manufacturer_id: @phone_model.manufacturer_id, id: @phone_model.to_param
      assert_response :redirect
    end
     
    test "should not get edit #{explanation}" do
      session[:user_id] = eval( user_id_code )
      get :edit, manufacturer_id: @phone_model.manufacturer_id, id: @phone_model.to_param
      assert_response :redirect
    end

    test "should not update phone_model #{explanation}" do
      session[:user_id] = eval( user_id_code )
      
      # save the old name:
      old_name = PhoneModel.find(@phone_model.id).name
      # try to make an update:
      put :update, manufacturer_id: @phone_model.manufacturer_id, id: @phone_model.to_param, phone_model: @phone_model.attributes.merge({
        'name' => @phone_model.name.reverse
      })
      # check that the update didn't work:
      assert_equal old_name, PhoneModel.find(@phone_model.id).name
      
      assert_response :redirect
    end
     
    test "should not destroy phone_model #{explanation}" do
      session[:user_id] = eval( user_id_code )
      
      assert_no_difference('PhoneModel.count') do
        delete :destroy, manufacturer_id: @phone_model.manufacturer_id, id: @phone_model.to_param
      end
      assert_response :redirect
    end
    
  end
  
  
  test "should get index as super admin" do
    session[:user_id] = @super_admin.id
    get :index, manufacturer_id: @phone_model.manufacturer_id
    assert_response :success
    assert_not_nil assigns(:phone_models)
  end
  
  test "should get new as super admin" do
    session[:user_id] = @super_admin.id
    get :new, manufacturer_id: @phone_model.manufacturer_id
    assert_response :success
  end
  
  
  # # We don't have access to manufacturer_id. We'll need to
  # # add routes first.
  # test "should create phone_model as super admin" do
  #   assert_difference('PhoneModel.count') do
  #     post :create, phone_model: Factory.build(:phone_model,
  #       :manufacturer_id => @phone_model.manufacturer_id).attributes
  #   end
  # 
  #   assert_redirected_to manufacturer_phone_model_path( @phone_model.manufacturer_id, assigns(:phone_model))
  # end
  
  test "should show phone_model as super admin" do
    session[:user_id] = @super_admin.id
    get :show, manufacturer_id: @phone_model.manufacturer_id, id: @phone_model.to_param
    assert_response :success
  end
  
  test "should get edit as super admin" do
    session[:user_id] = @super_admin.id
    get :edit, manufacturer_id: @phone_model.manufacturer_id, id: @phone_model.to_param
    assert_response :success
  end
  
  test "should update phone_model as super admin" do
    session[:user_id] = @super_admin.id
    put :update, manufacturer_id: @phone_model.manufacturer_id, id: @phone_model.to_param, phone_model: @phone_model.attributes
    assert_redirected_to manufacturer_phone_model_path( @phone_model.manufacturer_id, assigns(:phone_model))
  end
  
  test "should destroy phone_model as super admin" do
    session[:user_id] = @super_admin.id
    assert_difference('PhoneModel.count', -1) do
      delete :destroy, manufacturer_id: @phone_model.manufacturer_id, id: @phone_model.to_param
    end
    
    assert_redirected_to manufacturer_phone_models_path( @phone_model.manufacturer_id )
  end
  
end