Permalink
Browse files

Added tests for Activity when models created/updated.

  • Loading branch information...
kentaro committed Jun 19, 2012
1 parent e4c7d97 commit 7ac8a2d0981853450b41bc8c9dd3f70474861d34
View
@@ -29,7 +29,7 @@ end
group :test do
gem 'sqlite3-ruby', :require => 'sqlite3'
- gem 'test-unit', '2.0.9', :require => 'test/unit'
+ gem 'test-unit'
gem 'mocha'
gem 'factory_girl_rails'
gem 'database_cleaner'
@@ -40,6 +40,7 @@ group :test do
gem 'guard'
gem 'spork'
gem 'rb-fsevent'
+ gem 'guard-test'
gem 'guard-rspec'
gem 'guard-spork'
end
View
@@ -6,7 +6,7 @@ GIT
GIT
remote: git://github.com/seyhunak/twitter-bootstrap-rails.git
- revision: 047cfa271660b1ce9c2a08a64b41012174892228
+ revision: 803dfbe60760345905712bac79eb4783ec182673
specs:
twitter-bootstrap-rails (2.1.0)
actionpack (>= 3.1)
@@ -80,7 +80,7 @@ GEM
compass-rails (1.0.2)
compass (>= 0.12.0, < 0.14)
database_cleaner (0.8.0)
- devise (2.1.1)
+ devise (2.1.2)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
railties (~> 3.1)
@@ -97,7 +97,7 @@ GEM
actionmailer (>= 3.0.4)
execjs (1.4.0)
multi_json (~> 1.0)
- factory_girl (3.4.0)
+ factory_girl (3.4.1)
activesupport (>= 3.0.0)
factory_girl_rails (3.4.0)
factory_girl (~> 3.4.0)
@@ -114,11 +114,14 @@ GEM
em-websocket (>= 0.2.0)
guard (>= 1.1.0)
multi_json (~> 1.0)
- guard-rspec (1.0.1)
+ guard-rspec (1.1.0)
guard (>= 1.1)
- guard-spork (1.0.0)
+ guard-spork (1.0.1)
guard (>= 1.0.0)
spork (>= 0.8.4)
+ guard-test (0.5.0)
+ guard (>= 1.1.0)
+ test-unit (~> 2.2)
haml (3.1.6)
highline (1.6.13)
hike (1.2.1)
@@ -200,7 +203,7 @@ GEM
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
- raindrops (0.9.0)
+ raindrops (0.10.0)
rake (0.9.2.2)
rb-fchange (0.0.5)
ffi
@@ -246,10 +249,10 @@ GEM
syntax (1.0.0)
tapp (1.3.1)
thor
- test-unit (2.0.9)
+ test-unit (2.5.0)
therubyracer (0.10.1)
libv8 (~> 3.3.10)
- thor (0.15.2)
+ thor (0.15.3)
tilt (1.3.3)
treetop (1.4.10)
polyglot
@@ -283,6 +286,7 @@ DEPENDENCIES
guard-livereload
guard-rspec
guard-spork
+ guard-test
haml
jquery-rails
mocha
@@ -302,7 +306,7 @@ DEPENDENCIES
sqlite3-ruby
syntax (= 1.0.0)
tapp
- test-unit (= 2.0.9)
+ test-unit
twitter-bootstrap-rails!
unicorn
version_fu!
View
@@ -1,4 +1,20 @@
-group :specs do
+group :test do
+ guard 'test' do
+ watch(%r{^lib/(.*)\.rb}) { |m| "test/lib/#{m[1]}_test.rb" }
+ watch(%r{^test/(?!non_transactional/)(.*)_test.rb})
+ watch(%r{^test/test_helper.rb}) { "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/controllers/application_controller.rb}) { "test/functional" }
+ watch(%r{^app/controllers/application_controller.rb}) { "test/integration" }
+ watch(%r{^app/views/(.*)\.rb}) { "test/integration" }
+ watch(%r{^test/factories.rb}) { "test/unit" }
+ end
+end
+
+group :spec do
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
@@ -1,8 +1,7 @@
class HostsController < ApplicationController
respond_to :html, :xml, :json
-
before_filter :ensure_admin, :only => [:new, :edit, :destroy, :create, :update]
-
+
# GET /hosts
# GET /hosts.xml
def index
@@ -14,10 +13,10 @@ def index
# GET /hosts/1.xml
def show
@host = Host.find(params[:id])
-
+
# TODO - Why not in the model?
@stages = @host.stages.uniq.sort_by{|x| x.project.name}
-
+
respond_with(@host)
end
@@ -1,8 +1,7 @@
class ProjectConfigurationsController < ApplicationController
respond_to :html, :xml, :json
-
before_filter :load_project
-
+
# GET /projects/1/project_configurations/1
# GET /projects/1/project_configurations/1.xml
def show
@@ -30,6 +30,7 @@ def create
@role = @stage.roles.build(params[:role])
if @role.save
+ add_activity_for(@role, 'role.created')
flash[:notice] = 'Role was successfully created.'
respond_with(@role, :location => [@project, @stage])
else
@@ -43,6 +44,7 @@ def update
@role = @stage.roles.find(params[:id])
if @role.update_attributes(params[:role])
+ add_activity_for(@role, 'role.updated')
flash[:notice] = 'Role was successfully updated.'
respond_with(@role, :location => [@project, @stage])
else
@@ -23,6 +23,7 @@ def create
@user = User.new(params[:user])
if @user.save
+ add_activity_for(@user, 'user.created')
flash[:notice] = "Account created"
respond_with(@user, :location => @user)
else
@@ -54,6 +55,7 @@ def update
end
if @user.update_attributes(params[:user])
+ add_activity_for(@user, 'user.updated')
flash[:notice] = 'User was successfully updated.'
respond_with(@user, :location => @user)
else
@@ -140,13 +140,10 @@ def any_path(target)
case model_class
when 'stage'
project_stage_path(target.project, target)
+ when 'role'
+ project_stage_role_path(target.stage.project, target.stage, target)
else
send("#{model_class}_path", target)
end
end
end
-
-
-
-
-
View
@@ -2,7 +2,8 @@ class Role < ActiveRecord::Base
belongs_to :stage
belongs_to :host
has_and_belongs_to_many :deployments
-
+ has_many :activities, :as => :target, :dependent => :destroy
+
validates :stage, :host,
:presence => true
validates :name,
View
@@ -8,7 +8,7 @@ class User < ActiveRecord::Base
attr_accessible :login, :email, :password, :password_confirmation, :remember_me, :time_zone, :tz, :admin
has_many :deployments, :dependent => :nullify, :order => 'created_at DESC'
-
+ has_many :activities, :as => :target, :dependent => :destroy
validates_presence_of :login
validates_length_of :login, :within => 3..40
@@ -1,109 +1,133 @@
require 'test_helper'
class HostsControllerTest < ActionController::TestCase
-
def setup
+ Host.delete_all
@host = FactoryGirl.create(:host)
end
test "should_get_index" do
@user = login
-
+
get :index
assert_response :success
assert assigns(:hosts)
end
test "non_admin_should_not_get_new" do
@user = login
-
+
get :new
assert_response :redirect
end
-
+
test "admin_should_get_new" do
@user = admin_login
-
+
get :new
assert_response :success
end
-
+
test "non_admin_should_not_create_host" do
@user = login
-
+
old_count = Host.count
post :create, :host => { :name => '192.168.0.1' }
assert_equal old_count, Host.count
-
+
assert_response :redirect
end
-
+
test "admin_should_create_host" do
@user = admin_login
-
+
old_count = Host.count
post :create, :host => { :name => '192.168.0.1' }
assert_equal old_count+1, Host.count
-
+
assert_redirected_to host_path(assigns(:host))
end
+ test "activity_should_be_created_when_a_host_created" do
+ @user = admin_login
+
+ old_count = Activity.count
+ post :create, :host => { :name => '192.168.0.1' }
+ assert_equal old_count + 1, Activity.count
+
+ activity = Activity.where('target_id = ? and target_type = "Host"', assigns(:host).id).first
+ assert_not_nil activity
+ assert_equal activity.tag, 'host.created'
+ end
+
test "should_show_host" do
@user = login
-
+
get :show, :id => @host.id
assert_response :success
end
test "non_admin_should_not_get_edit" do
@user = login
-
+
get :edit, :id => @host.id
assert_response :redirect
end
-
+
test "admin_should_get_edit" do
@user = admin_login
-
+
get :edit, :id => @host.id
assert_response :success
end
-
+
test "non_admin_should_not_update_host" do
@user = login
-
+
put :update, :id => @host.id, :host => { :name => 'map.example.com' }
assert_response :redirect
@host.reload
- assert_not_equal 'map.example.com', @host.name
+ assert_not_equal 'map.example.com', @host.name
end
-
+
test "admin_should_update_host" do
@user = admin_login
-
+
put :update, :id => @host.id, :host => { :name => 'map.example.com' }
assert_redirected_to host_path(assigns(:host))
@host.reload
- assert_equal 'map.example.com', @host.name
+ assert_equal 'map.example.com', @host.name
+ end
+
+ test "activity_should_be_created_when_a_host_updated" do
+ @user = admin_login
+
+ old_count = Activity.count
+ put :update, :id => @host.id, :host => { :name => 'map.example.com' }
+ assert_equal old_count + 1, Activity.count
+
+ activity = Activity.where('target_id = ? and target_type = "Host"', @host.id).first
+ assert_not_nil activity
+ assert_equal activity.tag, 'host.updated'
end
-
+
test "non_admin_should_not_destroy_host" do
@user = login
-
+
old_count = Host.count
delete :destroy, :id => @host.id
assert_equal old_count, Host.count
-
+
assert_response :redirect
end
-
+
test "should_destroy_host" do
@user = admin_login
-
+
old_count = Host.count
delete :destroy, :id => @host.id
assert_equal old_count-1, Host.count
-
+
assert_redirected_to hosts_path
end
end
Oops, something went wrong.

0 comments on commit 7ac8a2d

Please sign in to comment.