Skip to content
This repository has been archived by the owner on Jul 30, 2022. It is now read-only.

Commit

Permalink
refactoring test code: move ability tests into ability_test.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
daqing committed Jun 4, 2011
1 parent ba43906 commit be7eb42
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 113 deletions.
20 changes: 0 additions & 20 deletions app/models/user.rb
Expand Up @@ -63,24 +63,4 @@ def role_name
def root?
self.id == 1 || self.role == 'root'
end

# def can_assign_issue?(issue)
# self.is_project_manager? || issue.user == self
# end

# def can_change_state?(issue)
# (not issue.assigned_user.nil?) and (self.is_project_manager? || issue.assigned_user == self)
# end

# def can_manage_todo?(issue)
# (not issue.closed?) and (issue.assigned_user == self || issue.user == self)
# end

# def can_manage_issue?(issue)
# (not issue.closed?) and issue.user == self
# end

# def can_manage_comment?(comment)
# (not comment.issue.closed?) and comment.user == self
# end
end
97 changes: 91 additions & 6 deletions test/unit/ability_test.rb
@@ -1,10 +1,95 @@
require 'test_helper'

class AbilityTest < ActiveSupport::TestCase
# setup do
# @daqing = users(:daqing)
# @two = users(:two)
# @daqing_ability = Ability.new(@daqing)
# @two_ability = Ability.new(@two)
# end
setup do
@user = users(:daqing)
@issue = issues(:bug_report)
end

test "only creator of an issue or PM can assign issues" do
ability = Ability.new(@user)
assert ability.can? :assign, @issue

another = users(:two)
another.role = 'ProjectManager'
a2 = Ability.new(another)
assert a2.can? :assign, @issue

another.role = 'Developer'
assert a2.cannot? :assign, @issue
end

test "should not change state before the issue is assigned" do
@issue.assigned_user = nil

ability = Ability.new(@user)
# only root can change issue state even if the issue has not been assigned
assert ability.can? :change_state, @issue

two = users(:two)
a2 = Ability.new(two)
assert a2.cannot? :change_state, @issue

two.role = 'ProjectManager'
assert a2.cannot? :change_state, @issue
end

test "only creator of an issue, PM or assigned user can change issue state after the issue is assigned" do
@issue.assigned_user = users(:daqing)

another = users(:two)
another.role = 'ProjectManager'
ability = Ability.new(another)
assert ability.can? :change_state, @issue

another.role = 'Developer'
assert ability.cannot? :change_state, @issue

@issue.assigned_user = another
assert ability.can? :change_state, @issue
end

test "only assinged user can manage todo items" do
two = users(:two)
@issue.assigned_user = @user
ability = Ability.new(two)
assert ability.cannot? :manage_todo, @issue

@issue.assigned_user = two
assert ability.can? :manage_todo, @issue

@issue.close!
assert ability.cannot? :manage_todo, @issue
end

test "only issue creator can manage issue when issue's not closed" do
@issue.work_on!
issue_two = issues(:two)
ability = Ability.new(users(:two))

issue_two.work_on!
assert ability.can? :manage, issue_two

a2 = Ability.new(users(:nana))
assert a2.cannot? :manage, issue_two

issue_two.mark_finished!
issue_two.close!
assert ability.cannot? :manage, issue_two
end

test "only creator can manage comments when the related issue is not closed" do
comment = comments(:two)
ability = Ability.new(users(:two))
issue = comment.issue
issue.work_on!
assert ability.can? :manage, comment

a2 = Ability.new(users(:nana))
assert a2.cannot? :manage, comment

issue.mark_finished!
issue.close!
assert ability.cannot? :manage, comment
end
end
87 changes: 0 additions & 87 deletions test/unit/user_test.rb
Expand Up @@ -38,91 +38,4 @@ def setup
assert ! user.salt.blank?
assert ! user.encrypted_password.blank?
end

test "only creator of an issue or PM can assign issues" do
ability = Ability.new(@user)
assert ability.can? :assign, @issue

another = users(:two)
another.role = 'ProjectManager'
a2 = Ability.new(another)
assert a2.can? :assign, @issue

another.role = 'Developer'
assert a2.cannot? :assign, @issue
end

test "should not change state before the issue is assigned" do
@issue.assigned_user = nil

ability = Ability.new(@user)
# only root can change issue state even if the issue has not been assigned
assert ability.can? :change_state, @issue

two = users(:two)
a2 = Ability.new(two)
assert a2.cannot? :change_state, @issue

two.role = 'ProjectManager'
assert a2.cannot? :change_state, @issue
end

test "only creator of an issue, PM or assigned user can change issue state after the issue is assigned" do
@issue.assigned_user = users(:daqing)

another = users(:two)
another.role = 'ProjectManager'
ability = Ability.new(another)
assert ability.can? :change_state, @issue

another.role = 'Developer'
assert ability.cannot? :change_state, @issue

@issue.assigned_user = another
assert ability.can? :change_state, @issue
end

test "only assinged user can manage todo items" do
two = users(:two)
@issue.assigned_user = @user
ability = Ability.new(two)
assert ability.cannot? :manage_todo, @issue

@issue.assigned_user = two
assert ability.can? :manage_todo, @issue

@issue.close!
assert ability.cannot? :manage_todo, @issue
end

test "only issue creator can manage issue when issue's not closed" do
@issue.work_on!
issue_two = issues(:two)
ability = Ability.new(users(:two))

issue_two.work_on!
assert ability.can? :manage, issue_two

a2 = Ability.new(users(:nana))
assert a2.cannot? :manage, issue_two

issue_two.mark_finished!
issue_two.close!
assert ability.cannot? :manage, issue_two
end

test "only creator can manage comments when the related issue is not closed" do
comment = comments(:two)
ability = Ability.new(users(:two))
issue = comment.issue
issue.work_on!
assert ability.can? :manage, comment

a2 = Ability.new(users(:nana))
assert a2.cannot? :manage, comment

issue.mark_finished!
issue.close!
assert ability.cannot? :manage, comment
end
end

0 comments on commit be7eb42

Please sign in to comment.