require_relative '../../test_helper'

class Admin::<%= class_name.pluralize %>ControllerTest < ActionDispatch::IntegrationTest

setup do
  @<%= file_name %> = <%= file_name.pluralize %>(:default)
end

# Vanilla CMS has BasicAuth, so we need to send that with each request.
# Change this to fit your app's authentication strategy.
# Move this to test_helper.rb
def r(verb, path, options = {})
  headers = options[:headers] || {}
  headers['HTTP_AUTHORIZATION'] =
    ActionController::HttpAuthentication::Basic.encode_credentials(
      ComfortableMexicanSofa::AccessControl::AdminAuthentication.username,
      ComfortableMexicanSofa::AccessControl::AdminAuthentication.password
    )
  options.merge!(headers: headers)
  send(verb, path, options)
end

def test_get_index
  r :get, admin_<%= file_name.pluralize %>_path
  assert_response :success
  assert assigns(:<%= file_name.pluralize %>)
  assert_template :index
end

def test_get_show
  r :get, admin_<%= file_name %>_path(@<%= file_name %>)
  assert_response :success
  assert assigns(:<%= file_name %>)
  assert_template :show
end

def test_get_show_failure
  r :get, admin_<%= file_name %>_path('invalid')
  assert_response :redirect
  assert_redirected_to action: :index
  assert_equal '<%= class_name.titleize %> not found', flash[:danger]
end

def test_get_new
  r :get, new_admin_<%= file_name %>_path
  assert_response :success
  assert assigns(:<%= file_name %>)
  assert_template :new
  assert_select "form[action='/admin/<%= file_name.pluralize %>']"
end

def test_get_edit
  r :get, edit_admin_<%= file_name %>_path(@<%= file_name %>)
  assert_response :success
  assert assigns(:<%= file_name %>)
  assert_template :edit
  assert_select "form[action='/admin/<%= file_name.pluralize %>/#{@<%= file_name %>.id}']"
end

def test_creation
  assert_difference '<%= class_name %>.count' do
    r :post, admin_<%= file_name.pluralize %>_path, params: {<%= file_name %>: {
    <%- model_attrs.each do |attr| -%>
      <%= attr.name %>: 'test <%= attr.name %>',
    <%- end -%>
    }}
    <%= file_name %> = <%= class_name %>.last
    assert_response :redirect
    assert_redirected_to action: :show, id: <%= file_name %>
    assert_equal '<%= class_name.titleize %> created', flash[:success]
  end
end

def test_creation_failure
  assert_no_difference '<%= class_name %>.count' do
    r :post, admin_<%= file_name.pluralize %>_path, params: {<%= file_name %>: { }}
    assert_response :success
    assert_template :new
    assert_equal 'Failed to create <%= class_name.titleize %>', flash[:danger]
  end
end

def test_update
  r :put, admin_<%= file_name %>_path(@<%= file_name %>), params: {<%= file_name %>: {
  <%- if attr = model_attrs.first -%>
    <%= attr.name %>: 'Updated'
  <%- end -%>
  }}
  assert_response :redirect
  assert_redirected_to action: :show, id: @<%= file_name %>
  assert_equal '<%= class_name.titleize %> updated', flash[:success]
  @<%= file_name %>.reload
  assert_equal 'Updated', @<%= file_name %>.<%= attr.try(:name) || 'attribute' %>
end

def test_update_failure
  r :put, admin_<%= file_name %>_path(@<%= file_name %>), params: {<%= file_name %>: {
    <%= attr.try(:name) || 'attribute' %>: ''
  }}
  assert_response :success
  assert_template :edit
  assert_equal 'Failed to update <%= class_name.titleize %>', flash[:danger]
  @<%= file_name %>.reload
  refute_equal '', @<%= file_name %>.<%= attr.try(:name) || 'attribute' %>
end

def test_destroy
  assert_difference '<%= class_name %>.count', -1 do
    r :delete, admin_<%= file_name %>_path(@<%= file_name %>)
    assert_response :redirect
    assert_redirected_to action: :index
    assert_equal '<%= class_name.titleize %> deleted', flash[:success]
  end
end

end