Skip to content

Instantly share code, notes, and snippets.

@mkoby
Created September 27, 2011 18:58
Show Gist options
  • Save mkoby/1245912 to your computer and use it in GitHub Desktop.
Save mkoby/1245912 to your computer and use it in GitHub Desktop.
Rails Controller Tests
#Goes in APP_FOLDER/test/functional
require 'test_helper'
class BandsControllerTest < ActionController::TestCase
setup :activate_authlogic
def setup
@host = bands(:one).subdomain + ".localhost"
UserSession.create(users(:one))
ActionMailer::Base.deliveries.clear
end
test "should redirect on get index when not site_admin" do
get :index
assert_response :redirect
end
test "should get new" do
@request.host = "localhost"
get :new
assert_response :success
end
test "should create band" do
assert_difference('Band.count') do
assert_difference('User.count') do
post :create, :band => { :name => "MyBand",
:subdomain => "myband",
:account_type => account_types(:free),
:users_attributes => { :user => {:username => "testuser",
:email_address => "[email protected]",
:password => "1234",
:password_confirmation => "1234" }}}
end
end
end
test "should redirect to band root after create band" do
post :create, :band => { :name => "MyBand",
:account_type => account_types(:free),
:subdomain => "myband" }
assert_redirected_to band_root_url(:subdomain => assigns(:band).subdomain)
end
test "should stay on new page after validation error" do
@request.host = "localhost"
post :create, :band => { :name => "MyBand",
:account_type => account_types(:free),
:subdomain => "" }
assert_template :new
end
test "should show band" do
@request.host = @host
get :show, :id => bands(:one).to_param
assert_response :success
end
end
class BandsController < ApplicationController
before_filter :require_band, :require_user, :load_band, :except => [:create, :new, :check_subdomain]
before_filter :require_band_admin, :only => [:edit, :update, :destroy]
before_filter :require_https, :only => [:new, :edit]
# GET /bands
# GET /bands.xml
def index
@bands = Band.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @band }
end
end
# GET /bands/1
# GET /bands/1.xml
def show
@band = Band.find(current_band.id)
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @band }
end
end
# GET /bands/new
# GET /bands/new.xml
def new
@band = Band.new
@band.users.build
@band.build_credit_card
params[:referring_band_subdomain] = params[:invite_code]
respond_to do |format|
format.html { render :layout => "public"} # new.html.erb
format.xml { render :xml => @band }
end
end
# GET /bands/1/edit
def edit
@band = Band.find(params[:id]) unless current_band
@band.build_credit_card
end
# POST /bands
# POST /bands.xml
def create
@band = Band.create(params[:band])
@band.account_status = AccountStatus.find_by_status_code("ACTIVE")
referring_subdomain = params[:referring_band_subdomain] if params[:referring_band_subdomain]
if !referring_subdomain.nil? && !referring_subdomain.empty?
referring_band = Band.find_by_subdomain(referring_subdomain)
@band.referring_band_id = referring_band.id if referring_band
end
respond_to do |format|
if @band.save
flash[:success] = 'Band was successfully created.'
format.html { redirect_to band_root_url(:subdomain => @band.subdomain) }
format.xml { render :xml => @band, :status => :created, :location => @band }
else
@band.build_credit_card if [email protected]_card
format.html { render :action => "new", :layout => "public" }
format.xml { render :xml => @band.errors, :status => :unprocessable_entity }
end
end
end
# PUT /bands/1
# PUT /bands/1.xml
def update
@band = Band.find(params[:id]) unless @band #current_band
respond_to do |format|
if @band.update_attributes(params[:band])
flash[:success] = 'Band was successfully updated.'
format.html { redirect_to @band }
format.xml { head :ok }
else
@band.build_credit_card if [email protected]_card
format.html { render :action => "edit" }
format.xml { render :xml => @band.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /bands/1
# DELETE /bands/1.xml
def destroy
@band = Band.find(params[:id]) unless @band
if @band.account_type_name == "FREE"
@band.destroy
redirect_to(public_root_path)
else
@band.account_status = AccountStatus.find_by_status_code("DELETE")
if @band.save
respond_to do |format|
format.html { redirect_to(@band) }
format.xml { head :ok }
end
end
end
end
def invite
@band = Band.find(params[:id]) unless @band #current_band
if !request.get?
result = nil
result = Postoffice.send_email("invite_band", params[:invitee_first_name], params[:invitee_last_name], params[:invitee_email_address], @band)
if result
flash[:success] = "Band successfully invited"
redirect_to(band_root_path)
end
end
end
#check subdomain
def check_subdomain
returnObj = {:valid => false}
bands = Band.find_by_subdomain(params[:subdomain]) if params[:subdomain]
returnObj = {:valid => true} if bands.nil?
response.content_type = Mime::JSON
render :text => returnObj.to_json
end
private
def load_band
@band = current_band
end
end
#Goes in APP_FOLDER/test/
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'authlogic/test_case'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment