require 'test_helper' <% module_namespacing do -%> class <%= controller_class_name %>ControllerTest < ActionController::TestCase setup do @<%= singular_table_name %> = <%= table_name %>(:one) end test "should get index" do get :index assert_response :success assert_not_nil assigns(:<%= table_name %>) end test "should get new" do get :new assert_response :success end test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do post :create, <%= "#{singular_table_name}: #{new_resource_attributes.inspect19}" %> end assert_redirected_to <%= login_path %> end test "should show <%= singular_table_name %>" do get :show, id: <%= "@#{singular_table_name}" %> assert_response :success end test "should not get edit unless logged in" do get :edit, id: <%= "@#{singular_table_name}" %> assert_redirected_to root_path assert_match /Login required/, flash[:alert] end test "should get edit" do force_login(@user) get :edit, id: <%= "@#{singular_table_name}" %> assert_response :success end test "should not get edit of another user" do force_login(@<%= singular_table_name %>) get :edit, id: <%= table_name %>(:two) assert_redirected_to root_url end <% if goma_config.modules.include? :confirmable -%> test "should not update <%= goma_config.email_attribute_name %> unless logged in" do original_email = @<%= singular_table_name %>.<%= goma_config.email_attribute_name %> patch :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{goma_config.email_attribute_name}: 'new@example.com' }" %> assert_redirected_to root_path assert_match /Login required/, flash[:alert] @<%= singular_table_name %>.reload assert_equal original_email, @<%= singular_table_name %>.<%= goma_config.email_attribute_name %> assert_equal nil, @<%= singular_table_name %>.<%= goma_config.unconfirmed_email_attribute_name %> end test "should confirm <%= goma_config.email_attribute_name %> before update" do force_login(@<%= singular_table_name %>) original_email = @<%= singular_table_name %>.<%= goma_config.email_attribute_name %> assert_difference 'ActionMailer::Base.deliveries.size', 1 do patch :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{goma_config.email_attribute_name}: 'new@example.com' }" %> end assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) assert_match /we need to verify your new email address/, flash[:notice] @<%= singular_table_name %>.reload assert_equal original_email, @<%= singular_table_name %>.<%= goma_config.email_attribute_name %> assert_equal 'new@example.com', @<%= singular_table_name %>.<%= goma_config.unconfirmed_email_attribute_name %> end <% auth_keys = goma_config.authentication_keys - [goma_config.email_attribute_name] -%> <% else -%> <% auth_keys = goma_config.authentication_keys -%> <% end -%> <% auth_keys.each do |key| -%> <% new_value = key == goma_config.email_attribute_name ? 'new@example.com' : "new_#{key}" %> test "should not update <%= key %> unless logged in" do original = @<%= singular_table_name %>.<%= key %> patch :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{key}: '#{new_value}' }" %> assert_redirected_to root_path @<%= singular_table_name %>.reload assert_equal original, @<%= singular_table_name %>.<%= key %> assert_match /Login required/, flash[:alert] end test "should update <%= key %>" do force_login(@<%= singular_table_name %>) patch :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{key}: '#{new_value}' }" %> assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) @<%= singular_table_name %>.reload assert_equal '<%= new_value %>', @<%= singular_table_name %>.<%= key %> end <% end -%> test "should not destroy <%= singular_table_name %> unless logged in" do assert_no_difference('<%= class_name %>.count') do delete :destroy, id: <%= "@#{singular_table_name}" %> end assert_redirected_to root_path assert_match /Login required/, flash[:alert] end test "should destroy <%= singular_table_name %>" do force_login(@<%= singular_table_name %>) assert_difference('<%= class_name %>.count', -1) do delete :destroy, id: <%= "@#{singular_table_name}" %> end assert_redirected_to <%= index_helper %>_path end end <% end -%>