Browse files

added in guard, shoulda, fixed users controller test, fixed users uni…

…t test
  • Loading branch information...
1 parent c5ce730 commit a243c7f130875dd038b8395e5049e559f455e576 @joeellis joeellis committed Jul 24, 2011
View
9 Gemfile
@@ -21,3 +21,12 @@ gem 'haml'
group :development, :test do
gem 'ruby-debug19', :require => 'ruby-debug'
end
+
+group :test do
+ gem 'guard'
+ gem 'guard-test'
+ gem 'guard-bundler'
+ gem 'growl'
+ gem 'rb-fsevent'
+ gem 'shoulda'
+end
View
18 Gemfile.lock
@@ -51,6 +51,15 @@ GEM
actionpack (>= 2.3.7)
activesupport (>= 2.3.7)
i18n (~> 0.4)
+ growl (1.0.3)
+ guard (0.5.1)
+ thor (~> 0.14.6)
+ guard-bundler (0.1.3)
+ bundler (>= 1.0.0)
+ guard (>= 0.2.2)
+ guard-test (0.3.0)
+ guard (>= 0.2.2)
+ test-unit (~> 2.2)
haml (3.1.2)
i18n (0.5.0)
jquery-rails (1.0.12)
@@ -97,6 +106,7 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.4)
rake (0.9.2)
+ rb-fsevent (0.4.1)
rdoc (3.8)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
@@ -109,8 +119,10 @@ GEM
ruby-openid (2.1.8)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
+ shoulda (2.11.3)
sqlite3 (1.3.3)
sqlite3 (1.3.3-x86-mingw32)
+ test-unit (2.3.0)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
@@ -125,13 +137,19 @@ DEPENDENCIES
authlogic!
aws-s3
formtastic
+ growl
+ guard
+ guard-bundler
+ guard-test
haml
jquery-rails
kaminari
paperclip
paperclip-meta
rack-openid
rails (= 3.0.9)
+ rb-fsevent
ruby-debug19
ruby-openid
+ shoulda
sqlite3
View
21 Guardfile
@@ -0,0 +1,21 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'bundler' do
+ watch('Gemfile')
+ # Uncomment next line if Gemfile contain `gemspec' command
+ # watch(/^.+\.gemspec/)
+end
+
+guard 'test' do
+ watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
+ watch(%r{^test/.+_test\.rb$})
+ watch('test/test_helper.rb') { "test" }
+ # watch(%r{^test/fixtures/.+\.yml$}) { "test" }
+
+ # Rails example
+ watch(%r{^app/models/(.+)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
+ watch(%r{^app/controllers/(.+)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
+ watch(%r{^app/views/.+\.rb$}) { "test/integration" }
+ watch('app/controllers/application_controller.rb') { ["test/functional", "test/integration"] }
+end
View
2 app/controllers/application_controller.rb
@@ -25,7 +25,7 @@ def require_user
end
def store_location
- session[:return_to] = request.request_uri
+ session[:return_to] = request.fullpath
end
def redirect_back_or_default(default)
View
22 app/controllers/users_controller.rb
@@ -1,6 +1,6 @@
class UsersController < ApplicationController
- before_filter :require_user, :only => [:show, :edit, :update, :destroy]
+ before_filter :require_user, :only => [:edit, :update, :destroy]
def index
if params[:category]
@@ -16,14 +16,14 @@ def index
end
end
- def show
- @user = current_user
-
- respond_to do |format|
- format.html
- format.xml { render :xml => @user }
- end
- end
+ # def show
+ # @user = current_user
+ #
+ # respond_to do |format|
+ # format.html
+ # format.xml { render :xml => @user }
+ # end
+ # end
def new
@user = User.new
@@ -74,10 +74,12 @@ def create
def update
respond_to do |format|
- if current_user.update_attributes(params[:user])
+ @user = current_user
+ if @user.update_attributes(params[:user])
format.html { redirect_to(root_url, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
+ p @user.errors
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
View
2 app/helpers/authentication_helper.rb
@@ -9,7 +9,7 @@ def current_user
def ensure_signed_in
unless signed_in?
- session[:redirect_to] = request.request_uri
+ session[:redirect_to] = request.fullpath
redirect_to(new_session_path)
end
end
View
68 app/models/user.rb
@@ -1,52 +1,49 @@
class User < ActiveRecord::Base
-
+
acts_as_authentic do |c|
c.login_field :email
c.require_password_confirmation = false
end
-
+
MINIMUM_MISSIONS = 1
AVATAR_S3_BUCKET = 'noladex.org'
- has_many :missions
-
- accepts_nested_attributes_for :missions, :reject_if => proc {|attributes| attributes['statement'].blank? }
-
- def self.avatar_options
- {:styles => { :medium => "300x300#" },
- :storage => Rails.env.production? ? :s3 : :filesystem,
- :bucket => AVATAR_S3_BUCKET,
- :s3_credentials => {
- :access_key_id => ENV['S3_KEY'],
- :secret_access_key => ENV['S3_SECRET']
- }}
- end
-
- has_attached_file :avatar, avatar_options
-
- validates_presence_of :name, :email, :avatar_file_name
- validates :missions, :length => { :minimum => MINIMUM_MISSIONS, :message => "You must have at least one mission to be listed."}
- validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
- validate :avatar_size
+ has_many :missions
+ has_attached_file :avatar, {
+ :styles => { :medium => "300x300#" },
+ :storage => Rails.env.production? ? :s3 : :filesystem,
+ :bucket => AVATAR_S3_BUCKET,
+ :s3_credentials => {
+ :access_key_id => ENV['S3_KEY'],
+ :secret_access_key => ENV['S3_SECRET']
+ }
+ }
+
+ validates_presence_of :name, :email, :avatar_file_name
+ validates :missions, :length => { :minimum => MINIMUM_MISSIONS, :message => "You must have at least one mission to be listed."}
+ validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
+ validate :avatar_size
- before_save :fix_urls
+ before_save :fix_urls
- def self.find_by_category(category_id)
- includes(:missions => :category).where(["categories.id = ?", category_id])
- end
+ accepts_nested_attributes_for :missions, :reject_if => proc {|attributes| attributes['statement'].blank? }
+
+ def self.find_by_category(category_id)
+ includes(:missions => :category).where(["categories.id = ?", category_id])
+ end
private
- def avatar_size
- temp_file = avatar.queued_for_write[:original] #get the file that is being uploaded
- if (temp_file)
- dimensions = Paperclip::Geometry.from_file(temp_file)
- if (dimensions.width < desired_width) || (dimensions.height < desired_height)
- errors.add("photo_size", "must be image size #{desired_width}x#{desired_height}.")
- end
- end
+ def avatar_size
+ temp_file = avatar.queued_for_write[:original] #get the file that is being uploaded
+ if (temp_file)
+ dimensions = Paperclip::Geometry.from_file(temp_file)
+ if (dimensions.width < desired_width) || (dimensions.height < desired_height)
+ errors.add("photo_size", "must be image size #{desired_width}x#{desired_height}.")
+ end
+ end
end
-
+
def desired_height
300
end
@@ -56,6 +53,7 @@ def desired_width
end
def fix_urls
+ Rails.logger.info "hello"
self.url1 = url1.gsub(%r{(^https?://twitter.com/(#!/)?|@)}, '') unless url1.blank?
self.url2 = "http://#{url2}" if !url2.blank? && !url2.match(%r{^https?://})
self.url3 = "http://#{url3}" if !url3.blank? && !url3.match(%r{^https?://})
View
58 config/routes.rb
@@ -12,68 +12,10 @@
resources :users
- resource :session
-
resources :password
match 'logout', :controller => 'user_sessions', :action => 'destroy', :as => 'logout'
match 'login', :controller => 'user_sessions', :action => 'new', :as => 'login'
- # The priority is based upon order of creation:
- # first created -> highest priority.
-
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', :on => :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- # root :to => "welcome#index"
root :to => "users#index"
-
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id(.:format)))'
end
View
8 test/fixtures/users.yml
@@ -6,10 +6,18 @@ gob:
avatar_file_name: magic.png
admin: false
url1: magician69
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
+ crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("ive_made_a_huge_mistake" + salt) %>
+ persistence_token: <%= Authlogic::Random.hex_token %>
+ single_access_token: <%= Authlogic::Random.friendly_token %>
michael:
name: Michael Bluth
email: michael@example.com
avatar_file_name: boring.png
admin: false
url1: michaelbluth
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
+ crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("ive_made_a_huge_mistake" + salt) %>
+ persistence_token: <%= Authlogic::Random.hex_token %>
+ single_access_token: <%= Authlogic::Random.friendly_token %>
View
4 test/functional/neighborhoods_controller_test.rb
@@ -1,8 +1,4 @@
require 'test_helper'
class NeighborhoodsControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
end
View
10 test/functional/password_controller_test.rb
@@ -1,14 +1,4 @@
require 'test_helper'
class PasswordControllerTest < ActionController::TestCase
- test "should get new" do
- get :new
- assert_response :success
- end
-
- test "should get create" do
- get :create
- assert_response :success
- end
-
end
View
4 test/functional/password_mailer_test.rb
@@ -1,8 +1,4 @@
require 'test_helper'
class PasswordMailerTest < ActionMailer::TestCase
- # replace this with your real tests
- test "the truth" do
- assert true
- end
end
View
104 test/functional/users_controller_test.rb
@@ -1,54 +1,96 @@
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
+ setup :activate_authlogic
+
setup do
@user = users(:gob)
+ @other_user = users(:michael)
end
- test "should get index" do
- get :index
- assert_response :success
- assert_not_nil assigns(:users)
- end
+ context "A user not logged in" do
+ should "be able to access the index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:users)
+ end
- test "should get new" do
- get :new
- assert_response :success
- end
+ should "be able to signup" do
+ get :new
+ assert_response :success
+ end
- test "should create user" do
- assert_difference('User.count') do
- post :create, :user => new_user
+ should "be able to create a new user" do
+ assert_difference('User.count') do
+ post :create, :user => new_user
+ end
+
+ assert_redirected_to root_path
end
- assert_redirected_to user_path(assigns(:user))
+ should "not be able to edit a user" do
+ get :edit, :id => @user.to_param
+ assert_response :redirect
+ end
end
- test "should show user" do
- get :show, :id => @user.to_param
- assert_response :success
- end
+ context "A logged in user" do
+ setup do
+ UserSession.create(@user)
+ end
+
+ should "be able to access the index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:users)
+ end
- test "should get edit" do
- get :edit, :id => @user.to_param
- assert_response :success
- end
+ should "be able to signup" do
+ get :new
+ assert_response :success
+ end
- test "should update user" do
- put :update, :id => @user.to_param, :user => @user.attributes
- assert_redirected_to user_path(assigns(:user))
- end
+ should "be able to create a new user" do
+ assert_difference('User.count') do
+ post :create, :user => new_user
+ end
- test "should destroy user" do
- assert_difference('User.count', -1) do
- delete :destroy, :id => @user.to_param
+ assert_redirected_to root_path
end
- assert_redirected_to users_path
+ should "be able to access his own information" do
+ # TODO test that it cant edit another user
+ get :edit, :id => @user.to_param
+ assert_response :success
+ end
+
+ should "be able to update his own information" do
+ put :update, :id => @user.to_param, :user => @user.attributes
+ assert_redirected_to root_path
+ end
+
+ should "be not able to update another's information" do
+ # UPDATE should only run for the current_user
+ put :update, :id => @other_user.to_param, :user => {:email => "test@test.com"}
+ assert @user.email = "test@test.com"
+ assert @other_user.email == "michael@example.com"
+ assert_redirected_to root_path
+ end
end
def new_user
- {:name => "New guy", :email => "guy@example.com", :avatar_file_name => 'guy.png', :url1 => 'guy',
- :missions_attributes => {"0" =>{:category_id => 1, :statement => "stuff"}}}
+ {
+ :name => "Tobias Funke",
+ :email => "tobias@funke.com",
+ :password => "nevernude",
+ :avatar_file_name => 'guy.png',
+ :url1 => 'guy',
+ :missions_attributes => {"0" =>
+ {
+ :category_id => 1,
+ :statement => "stuff"
+ }
+ }
+ }
end
end
View
9 test/test_helper.rb
@@ -3,11 +3,8 @@
require 'rails/test_help'
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
+ require "authlogic/test_case"
+
fixtures :all
-
- # Add more helper methods to be used by all tests here...
+
end
View
5 test/unit/mission_test.rb
@@ -1,8 +1,5 @@
require 'test_helper'
class MissionTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
+ should validate_presence_of :statement
end
View
4 test/unit/neighborhood_test.rb
@@ -1,8 +1,4 @@
require 'test_helper'
class NeighborhoodTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
end
View
4 test/unit/tag_test.rb
@@ -1,8 +1,4 @@
require 'test_helper'
class TagTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
end
View
2 test/unit/user_test.rb
@@ -13,7 +13,7 @@ class UserTest < ActiveSupport::TestCase
test "url1 removes the @" do
@user.url1 = '@gob'
- @user.save
+ @user.save!
assert @user.url1 == 'gob'
end

0 comments on commit a243c7f

Please sign in to comment.