Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[#3506] Bye-bye specs

  • Loading branch information...
commit 43bf8c6ede34dd36c203b66883bc44c2d0f6ba95 1 parent fddf19c
@edavis10 authored
View
4 Rakefile
@@ -5,8 +5,8 @@ Dir[File.expand_path(File.dirname(__FILE__)) + "/lib/tasks/**/*.rake"].sort.each
RedminePluginSupport::Base.setup do |plugin|
plugin.project_name = 'timesheet_plugin'
- plugin.default_task = [:spec]
- plugin.tasks = [:doc, :release, :clean, :spec, :stats, :test, :db]
+ plugin.default_task = [:test]
+ plugin.tasks = [:doc, :release, :clean, :stats, :test, :db]
# TODO: gem not getting this automaticly
plugin.redmine_root = File.expand_path(File.dirname(__FILE__) + '/../../../')
end
View
0  spec/controllers/empty
No changes.
View
272 spec/controllers/timesheet_controller_spec.rb
@@ -1,272 +0,0 @@
-require File.dirname(__FILE__) + '/../spec_helper'
-
-module TimesheetControllerHelper
- # Sets up the default mocks
- def default_mocks(options = {})
- # User
- @current_user = mock_model(User)
- @current_user.stub!(:admin?).and_return(false)
- @user_project_mock = mock_model(Project)
- @user_project_mock.stub!(:find).and_return([ ])
- @current_user.stub!(:projects).and_return(@user_project_mock)
- stub_current_user
-
- # Redmine application controller
- controller.stub!(:check_if_login_required).and_return(true)
- controller.stub!(:set_localization)
-
- # Timesheet
- @timesheet = mock_model(Timesheet)
- @timesheet.stub!(:projects).and_return([ ])
- @timesheet.stub!(:projects=)
- @timesheet.stub!(:allowed_projects).and_return(['not empty'])
- @timesheet.stub!(:allowed_projects=)
- @timesheet.stub!(:date_from)
- @timesheet.stub!(:date_from=)
- @timesheet.stub!(:date_to)
- @timesheet.stub!(:date_to=)
- @timesheet.stub!(:activities)
- @timesheet.stub!(:activities=)
- @timesheet.stub!(:users)
- @timesheet.stub!(:users=)
- @timesheet.stub!(:fetch_time_entries)
- @timesheet.stub!(:time_entries).and_return([ ])
- @timesheet.stub!(:sort)
- @timesheet.stub!(:sort=)
- @timesheet.stub!(:period_type=)
- stub_timesheet unless options[:skip_timesheet_stub]
- end
-
- # Converts current user to admin
- def mock_admin
- @current_user.stub!(:admin?).and_return(true)
- stub_current_user
- end
-
- # Restubs the current user
- def stub_current_user
- User.stub!(:current).and_return(@current_user)
- end
-
- # Restubs the new timesheet
- def stub_timesheet
- Timesheet.stub!(:new).and_return(@timesheet)
- end
-end
-
-describe 'TimesheetControllerShared', :shared => true do
- it 'should set @timesheet.allowed_projects to the list of current projects the user is a member of' do
- project1 = mock_model(Project)
- project2 = mock_model(Project)
- projects = [project1, project2]
-
- # Adjust mocks
- @user_project_mock.should_receive(:find).and_return(projects)
- stub_current_user
- @timesheet.should_receive(:allowed_projects=).with(projects)
- @timesheet.stub!(:allowed_projects).and_return(projects)
- stub_timesheet
-
- send_request
- assigns[:timesheet].allowed_projects.should eql(projects)
- end
-
- it 'should set @timesheet.allowed_projects to all the projects if the user is an admin' do
- mock_admin
- project1 = mock_model(Project)
- project2 = mock_model(Project)
- projects = [project1, project2]
-
- # Adjust mocks
- Project.stub!(:find).with(:all, { :order => "name ASC" }).and_return(projects)
- @timesheet.should_receive(:allowed_projects=).with(projects)
- @timesheet.stub!(:allowed_projects).and_return(projects)
- stub_timesheet
-
- send_request
- assigns[:timesheet].allowed_projects.should eql(projects)
- end
-
- it 'should get the list size from the settings' do
- settings = { 'list_size' => 10, 'precision' => '2' }
- Setting.should_receive(:plugin_timesheet_plugin).twice.and_return(settings)
-
- send_request
- assigns[:list_size].should eql(10)
- end
-
- it 'should get the precision from the settings' do
- settings = { 'list_size' => 10, 'precision' => '2' }
- Setting.should_receive(:plugin_timesheet_plugin).twice.and_return(settings)
-
- send_request
- assigns[:precision].should eql(2)
- end
-
- it 'should create a new @timesheet' do
- Timesheet.should_receive(:new).and_return(@timesheet)
-
- send_request
- assigns[:timesheet].should eql(@timesheet)
- end
-end
-
-
-describe TimesheetController do
- it "should use TimesheetController" do
- controller.should be_an_instance_of(TimesheetController)
- end
-
-end
-describe TimesheetController,"#index with GET request" do
- include TimesheetControllerHelper
-
- def send_request
- get 'index'
- end
-
- before(:each) do
- default_mocks
- end
-
- it_should_behave_like "TimesheetControllerShared"
-
- it 'should have no timelog entries' do
- get 'index'
- assigns[:timesheet].time_entries.should be_empty
- end
-
- it 'should render the index template' do
- get 'index'
- response.should render_template('index')
- end
-end
-
-describe TimesheetController,"#index with GET request and a session" do
- include TimesheetControllerHelper
- it 'should read the session data' do
- default_mocks(:skip_timesheet_stub => true)
-
- projects = []
- 4.times do |i|
- projects << mock_model(Project, :id => i + 1)
- end
-
- controller.stub!(:allowed_projects).and_return(projects)
- session[TimesheetController::SessionKey] = HashWithIndifferentAccess.new(
- :projects => projects.collect(&:id).collect(&:to_s),
- :date_to => '2009-01-01',
- :date_from => '2009-01-01'
- )
-
- get :index
- assigns[:timesheet].date_from.should eql('2009-01-01')
- assigns[:timesheet].date_to.should eql('2009-01-01')
- assigns[:timesheet].projects.should eql(projects)
- end
-end
-
-describe TimesheetController,"#index with GET request from an Anonymous user" do
- include TimesheetControllerHelper
-
- it 'should render the no_projects template' do
- get 'index'
- response.should render_template('no_projects')
- end
-
-end
-
-describe TimesheetController,"#report with GET request from an Anonymous user" do
- include TimesheetControllerHelper
-
- it 'should redirect to the index' do
- get 'report'
- response.should be_redirect
- response.should redirect_to(:action => 'index')
- end
-
-end
-
-describe TimesheetController,"#report with POST request from an Anonymous user" do
- include TimesheetControllerHelper
-
- it 'should redirect to the index' do
- get 'report'
- response.should be_redirect
- response.should redirect_to(:action => 'index')
- end
-
-end
-
-describe TimesheetController,"#report with POST request" do
- include TimesheetControllerHelper
-
- before(:each) do
- default_mocks
- end
-
- def send_request
- post_report
- end
-
- def post_report(data={ :timesheet => { } })
- post 'report', data
- end
-
- it_should_behave_like "TimesheetControllerShared"
-
- it 'should only allow the allowed projects into @timesheet.projects' do
- project1 = mock_model(Project, :to_param => '1', :id => 1)
- project2 = mock_model(Project, :to_param => '2', :id => 2)
- projects = [project1, project2]
-
- # Adjust mocks
- @user_project_mock.should_receive(:find).and_return(projects)
- stub_current_user
-
- @timesheet.should_receive(:allowed_projects=).with(projects)
- @timesheet.should_receive(:allowed_projects).twice.and_return(projects)
- @timesheet.should_receive(:projects=).with([project1])
- stub_timesheet
-
- post_report({ :timesheet => { :projects => ['1'] } })
- end
-
- it 'should save the session data' do
- post_report({ :timesheet => { :projects => ['1'] } })
- session[TimesheetController::SessionKey].should_not be_nil
- session[TimesheetController::SessionKey].keys.should include('projects')
- session[TimesheetController::SessionKey]['projects'].should eql(['1'])
- end
-
- context ":csv format" do
- it 'should return the timesheet data as csv' do
- @timesheet.should_receive(:to_csv).and_return('csv content')
- post_report({:timesheet => {:projects => ['1']}, :format => 'csv'})
- response.body.should == 'csv content'
- response.content_type.should == 'text/csv'
- end
- end
-end
-
-describe TimesheetController,"#report with request with no data" do
- include TimesheetControllerHelper
-
- before(:each) do
- default_mocks
- end
-
- describe 'should redirect to the index' do
- it 'from a GET request' do
- post 'report', { }
- response.should be_redirect
- response.should redirect_to(:action => 'index')
- end
-
- it 'from a POST request' do
- post 'report', { }
- response.should be_redirect
- response.should redirect_to(:action => 'index')
- end
- end
-end
View
0  spec/models/empty
No changes.
View
698 spec/models/timesheet_spec.rb
@@ -1,698 +0,0 @@
-require File.dirname(__FILE__) + '/../spec_helper'
-
-module TimesheetSpecHelper
- def timesheet_factory(options={ })
- timesheet = Timesheet.new(options)
- timesheet.date_from ||= Date.today.to_s
- timesheet.date_to ||= Date.today.to_s
-
- return timesheet
- end
-
- def project_factory(id, options = { })
- object_options = {
- :parent => nil,
- :id => id,
- :to_param => id.to_s
- }.merge(options)
-
- project = mock_model(Project, object_options)
- project_te1 = mock_model(TimeEntry, :id => '100' + id.to_s, :project_id => project.id, :issue_id => '1', :hours => '5', :activity_id => '1', :spent_on => Date.today, :user => 1)
- project_te2 = mock_model(TimeEntry, :id => '101' + id.to_s, :project_id => project.id, :issue_id => '1', :hours => '10', :activity_id => '1', :spent_on => Date.today, :user => 1)
- project_time_entries_mock = mock('project_time_entries_mock')
- project_time_entries_mock.stub!(:find).and_return([project_te1, project_te2])
- project.stub!(:time_entries).and_return(project_time_entries_mock)
- project.stub!(:name).and_return('Project ' + id.to_s)
-
- return project
- end
-
- def time_entry_factory(id, options = { })
- object_options = {
- :id => id,
- :to_param => id.to_s,
- :spent_on => Date.today,
- }.merge(options)
-
- time_entry = mock_model(TimeEntry, object_options)
- time_entry.stub!(:issue).and_return(options[:issue]) unless options[:issue].nil?
- time_entry.stub!(:project).and_return(options[:project]) unless options[:project].nil?
- return time_entry
- end
-
- def stub_non_member_user(projects)
- @current_user = mock_model(User)
- @current_user.stub!(:admin?).and_return(false)
- projects.each do |project|
- @current_user.stub!(:allowed_to?).with(:view_time_entries, project).and_return(false)
- @current_user.stub!(:allowed_to?).with(:see_project_timesheets, project).and_return(false)
- end
- User.stub!(:current).and_return(@current_user)
- end
-
- def stub_normal_user(projects)
- @current_user = mock_model(User)
- @current_user.stub!(:admin?).and_return(false)
- projects.each do |project|
- @current_user.stub!(:allowed_to?).with(:view_time_entries, project).and_return(true)
- @current_user.stub!(:allowed_to?).with(:see_project_timesheets, project).and_return(false)
- end
- User.stub!(:current).and_return(@current_user)
- end
-
- def stub_manager_user(projects)
- @current_user = mock_model(User)
- @current_user.stub!(:admin?).and_return(false)
- projects.each do |project|
- @current_user.stub!(:allowed_to?).with(:view_time_entries, project).and_return(true)
- @current_user.stub!(:allowed_to?).with(:see_project_timesheets, project).and_return(true)
- end
- User.stub!(:current).and_return(@current_user)
- end
-
- def stub_admin_user
- @current_user = mock_model(User)
- @current_user.stub!(:admin?).and_return(true)
- @current_user.stub!(:allowed_to?).and_return(true)
- @current_user.stub!(:name).and_return("Administrator Bob")
- User.stub!(:current).and_return(@current_user)
- end
-
- def stub_common_csv_records
- {
- :user => User.current,
- :activity => stub('Activity', :name => 'activity'),
- :spent_on => '2009-04-05',
- :project => mock_model(Project, :name => 'Project Name'),
- :comments => 'comments',
- :hours => 10.0,
- :issue => mock_model(Issue, :id => 1, :tracker => mock_model(Tracker, :name => 'Tracker'))
- }
- end
-end
-
-describe Timesheet do
- it 'should not be an ActiveRecord class' do
- Timesheet.should_not be_a_kind_of(ActiveRecord::Base)
- end
-end
-
-describe Timesheet, 'initializing' do
- it 'should initialize time_entries to an empty Hash' do
- timesheet = Timesheet.new
- timesheet.time_entries.should be_a_kind_of(Hash)
- timesheet.time_entries.should be_empty
- end
-
- it 'should initialize projects to an empty Array' do
- timesheet = Timesheet.new
- timesheet.projects.should be_a_kind_of(Array)
- timesheet.projects.should be_empty
- end
-
- it 'should initialize allowed_projects to an empty Array' do
- timesheet = Timesheet.new
- timesheet.allowed_projects.should be_a_kind_of(Array)
- timesheet.allowed_projects.should be_empty
- end
-
- it 'should initialize activities to an Array' do
- timesheet = Timesheet.new
- timesheet.activities.should be_a_kind_of(Array)
- end
-
- it 'should initialize users to an Array' do
- timesheet = Timesheet.new
- timesheet.users.should be_a_kind_of(Array)
- end
-
- it 'should initialize sort to :project' do
- timesheet = Timesheet.new
- timesheet.sort.should eql(:project)
- end
-
- it 'should initialize time_entries to the passed in options' do
- data = { :test => true }
- timesheet = Timesheet.new({ :time_entries => data })
- timesheet.time_entries.should_not be_empty
- timesheet.time_entries.should eql(data)
- end
-
- it 'should initialize allowed_projects to the passed in options' do
- data = ['project1', 'project2']
- timesheet = Timesheet.new({ :allowed_projects => data })
- timesheet.allowed_projects.should_not be_empty
- timesheet.allowed_projects.should eql(data)
- end
-
- it 'should initialize activities to the integers of the passed in options' do
- act1 = mock('act1')
- act1.stub!(:to_i).and_return(200)
- act2 = mock('act2')
- act2.stub!(:to_i).and_return(300)
- data = [act1, act2]
- timesheet = Timesheet.new({ :activities => data })
- timesheet.activities.should_not be_empty
- timesheet.activities.should eql([200, 300])
- end
-
- it 'should initialize users to the ids of the passed in options' do
- user1 = mock('user1')
- user1.stub!(:to_i).and_return(100)
- user2 = mock('user2')
- user2.stub!(:to_i).and_return(101)
- data = [user1, user2]
- timesheet = Timesheet.new({ :users => data })
- timesheet.users.should_not be_empty
- timesheet.users.should eql([100, 101])
- end
-
- it 'should initialize sort to the :user option when passed :user' do
- timesheet = Timesheet.new({ :sort => :user })
- timesheet.sort.should eql(:user)
- end
-
- it 'should initialize sort to the :project option when passed :project' do
- timesheet = Timesheet.new({ :sort => :project })
- timesheet.sort.should eql(:project)
- end
-
- it 'should initialize sort to the :issue option when passed :issue' do
- timesheet = Timesheet.new({ :sort => :issue })
- timesheet.sort.should eql(:issue)
- end
-
- it 'should initialize sort to the :project option when passed an invalid sort' do
- timesheet = Timesheet.new({ :sort => :invalid })
- timesheet.sort.should eql(:project)
- end
-end
-
-describe Timesheet,'.fetch_time_entries' do
- include TimesheetSpecHelper
-
- it 'should clear .time_entries' do
- timesheet = Timesheet.new
- timesheet.time_entries = { :filled => 'data' }
- proc {
- timesheet.fetch_time_entries
- }.should change(timesheet, :time_entries)
-
- end
-
- it 'should add a time_entry Hash for each project' do
- timesheet = timesheet_factory
-
- project1 = project_factory(1)
- project2 = project_factory(2)
-
- stub_admin_user
- timesheet.projects = [project1, project2]
-
- timesheet.fetch_time_entries
- timesheet.time_entries.should_not be_empty
- timesheet.time_entries.should have(2).things
- end
-
- it 'should use the project name for each time_entry key' do
-
- timesheet = timesheet_factory
-
- project1 = project_factory(1)
- project1.should_receive(:name).and_return('Project 1')
- project2 = project_factory(2)
- project2.should_receive(:name).and_return('Project 2')
-
- stub_admin_user
- timesheet.projects = [project1, project2]
-
- timesheet.fetch_time_entries
- timesheet.time_entries.keys.should include("Project 1")
- timesheet.time_entries.keys.should include("Project 2")
- end
-
- it 'should add the parent project name for each time_entry array for sub-projects' do
- timesheet = timesheet_factory
-
- project1 = project_factory(1)
- project1.should_receive(:name).twice.and_return('Project 1')
- project2 = project_factory(2, :parent => project1 )
- project2.should_receive(:name).and_return('Project 2')
-
- stub_admin_user
- timesheet.projects = [project1, project2]
-
- timesheet.fetch_time_entries
- timesheet.time_entries.keys.should include("Project 1")
- timesheet.time_entries.keys.should include("Project 1 / Project 2")
- end
-
- it 'should fetch all the time entries on a project in the date range'
- it 'should fetch all the time entries on a project matching the activities'
- it 'should fetch all the time entries on a project matching the users'
-end
-
-describe Timesheet,'.fetch_time_entries with user sorting' do
- include TimesheetSpecHelper
-
- it 'should clear .time_entries' do
- stub_admin_user
- timesheet = Timesheet.new({ :sort => :user })
- timesheet.time_entries = { :filled => 'data' }
- proc {
- timesheet.fetch_time_entries
- }.should change(timesheet, :time_entries)
-
- end
-
- it 'should add a time_entry array for each user' do
- stub_admin_user
- timesheet = timesheet_factory(:sort => :user, :users => [User.current.id])
-
- time_entries = [
- time_entry_factory(1, { :user => User.current }),
- time_entry_factory(2, { :user => User.current }),
- time_entry_factory(3, { :user => User.current }),
- time_entry_factory(4, { :user => User.current }),
- time_entry_factory(5, { :user => User.current })
- ]
-
- TimeEntry.stub!(:find).and_return(time_entries)
- User.stub!(:find_by_id).and_return(User.current)
-
- timesheet.fetch_time_entries
- timesheet.time_entries.should_not be_empty
- timesheet.time_entries.should have(1).thing # One user
- end
-
- it 'should use the user name for each time_entry array' do
- stub_admin_user
- timesheet = timesheet_factory(:sort => :user, :users => [User.current.id])
-
- time_entries = [
- time_entry_factory(1, { :user => User.current }),
- time_entry_factory(2, { :user => User.current }),
- time_entry_factory(3, { :user => User.current }),
- time_entry_factory(4, { :user => User.current }),
- time_entry_factory(5, { :user => User.current })
- ]
-
- TimeEntry.stub!(:find).and_return(time_entries)
- User.stub!(:find_by_id).and_return(User.current)
-
- timesheet.fetch_time_entries
- timesheet.time_entries.keys.should include("Administrator Bob")
- end
-end
-
-describe Timesheet,'.fetch_time_entries with issue sorting' do
- include TimesheetSpecHelper
-
- it 'should clear .time_entries' do
- timesheet = Timesheet.new({ :sort => :issue })
- timesheet.time_entries = { :filled => 'data' }
- proc {
- timesheet.fetch_time_entries
- }.should change(timesheet, :time_entries)
-
- end
-
- it 'should add a time_entry array for each project' do
- stub_admin_user
- project1 = project_factory(1)
- timesheet = timesheet_factory(:sort => :issue, :users => [User.current.id])
- timesheet.projects = [project1]
-
- @issue1 = mock_model(Issue, :id => 1, :to_param => '1', :project => project1)
- @issue2 = mock_model(Issue, :id => 1, :to_param => '1', :project => project1)
- @issue3 = mock_model(Issue, :id => 1, :to_param => '1', :project => project1)
-
- time_entry1 = time_entry_factory(1, { :user => User.current, :issue => @issue1 })
- time_entry2 = time_entry_factory(2, { :user => User.current, :issue => @issue1 })
- time_entry3 = time_entry_factory(3, { :user => User.current, :issue => @issue2 })
- time_entry4 = time_entry_factory(4, { :user => User.current, :issue => @issue2 })
- time_entry5 = time_entry_factory(5, { :user => User.current, :issue => @issue3 })
-
- project1.should_receive(:issues).and_return([@issue1, @issue2, @issue3])
-
- time_entries = [
- time_entry1,
- time_entry2,
- time_entry3,
- time_entry4,
- time_entry5
- ]
-
- timesheet.should_receive(:issue_time_entries_for_all_users).with(@issue1).and_return([time_entry1, time_entry2])
- timesheet.should_receive(:issue_time_entries_for_all_users).with(@issue2).and_return([time_entry3, time_entry4])
- timesheet.should_receive(:issue_time_entries_for_all_users).with(@issue3).and_return([time_entry5])
-
- timesheet.fetch_time_entries
- timesheet.time_entries.should_not be_empty
- timesheet.time_entries.should have(1).thing
- end
-
- it 'should use the project for each time_entry array' do
- stub_admin_user
- project1 = project_factory(1)
- timesheet = timesheet_factory(:sort => :issue, :users => [User.current.id])
- timesheet.projects = [project1]
-
- @issue1 = mock_model(Issue, :id => 1, :to_param => '1', :project => project1)
- @issue2 = mock_model(Issue, :id => 1, :to_param => '1', :project => project1)
- @issue3 = mock_model(Issue, :id => 1, :to_param => '1', :project => project1)
-
- time_entry1 = time_entry_factory(1, { :user => User.current, :issue => @issue1 })
- time_entry2 = time_entry_factory(2, { :user => User.current, :issue => @issue1 })
- time_entry3 = time_entry_factory(3, { :user => User.current, :issue => @issue2 })
- time_entry4 = time_entry_factory(4, { :user => User.current, :issue => @issue2 })
- time_entry5 = time_entry_factory(5, { :user => User.current, :issue => @issue3 })
-
- project1.should_receive(:issues).and_return([@issue1, @issue2, @issue3])
-
- time_entries = [
- time_entry1,
- time_entry2,
- time_entry3,
- time_entry4,
- time_entry5
- ]
-
- timesheet.should_receive(:issue_time_entries_for_all_users).with(@issue1).and_return([time_entry1, time_entry2])
- timesheet.should_receive(:issue_time_entries_for_all_users).with(@issue2).and_return([time_entry3, time_entry4])
- timesheet.should_receive(:issue_time_entries_for_all_users).with(@issue3).and_return([time_entry5])
-
- timesheet.fetch_time_entries
- timesheet.time_entries.keys.should include(project1)
- end
-end
-
-describe Timesheet,'.fetch_time_entries as an administrator' do
- include TimesheetSpecHelper
-
- it 'should collect time entries for all users on each project' do
- timesheet = timesheet_factory
-
- project1 = project_factory(1)
- project1.stub!(:name).and_return('Project 1')
- project2 = project_factory(2)
- project2.stub!(:name).and_return('Project 2')
-
- stub_admin_user
- timesheet.projects = [project1, project2]
-
- timesheet.should_receive(:time_entries_for_all_users).with(project1).and_return([ ])
- timesheet.should_receive(:time_entries_for_all_users).with(project2).and_return([ ])
- timesheet.fetch_time_entries
- end
-end
-
-describe Timesheet,'.fetch_time_entries as a user with see_project_timesheet permission on a project' do
- include TimesheetSpecHelper
-
- it 'should collect time entries for all users' do
- timesheet = timesheet_factory
-
- project1 = project_factory(1)
- project1.stub!(:name).and_return('Project 1')
- project2 = project_factory(2)
- project2.stub!(:name).and_return('Project 2')
- project3 = project_factory(3)
- project3.stub!(:name).and_return('Project 3')
-
- stub_manager_user([project1, project2])
- # Make user a 'non-manager' on project3
- @current_user.stub!(:allowed_to?).with(:view_time_entries, project3).and_return(true)
- @current_user.stub!(:allowed_to?).with(:see_project_timesheets, project3).and_return(false)
- User.stub!(:current).and_return(@current_user)
-
- timesheet.projects = [project1, project2, project3]
-
- timesheet.should_receive(:time_entries_for_all_users).with(project1).and_return([ ])
- timesheet.should_receive(:time_entries_for_all_users).with(project2).and_return([ ])
- timesheet.should_receive(:time_entries_for_current_user).with(project3).and_return([ ])
- timesheet.fetch_time_entries
- end
-end
-
-describe Timesheet,'.fetch_time_entries as a user with view_time_entries permission on a project' do
- include TimesheetSpecHelper
-
- it 'should collect time entries for only themself' do
- timesheet = timesheet_factory
-
- project1 = project_factory(1)
- project1.stub!(:name).and_return('Project 1')
- project2 = project_factory(2)
- project2.stub!(:name).and_return('Project 2')
-
- stub_normal_user([project1, project2])
- timesheet.projects = [project1, project2]
-
- timesheet.should_receive(:time_entries_for_current_user).with(project1).and_return([ ])
- timesheet.should_receive(:time_entries_for_current_user).with(project2).and_return([ ])
- timesheet.fetch_time_entries
- end
-end
-
-describe Timesheet,'.fetch_time_entries as a non-member of a project' do
- include TimesheetSpecHelper
-
- it 'should get no time entries' do
- timesheet = timesheet_factory
-
- project1 = project_factory(1)
- project1.stub!(:name).and_return('Project 1')
- project2 = project_factory(2)
- project2.stub!(:name).and_return('Project 2')
-
- stub_non_member_user([project1, project2])
- timesheet.projects = [project1, project2]
-
- timesheet.should_not_receive(:time_entries_for_current_user).with(project1).and_return([ ])
- timesheet.should_not_receive(:time_entries_for_current_user).with(project2).and_return([ ])
- timesheet.fetch_time_entries
- end
-end
-
-describe Timesheet, '#period=' do
- include TimesheetSpecHelper
- describe 'should set the date_to and date_from for' do
- before(:each) do
- @date = Date.new(2009,2,4)
- Date.stub!(:today).and_return(@date)
- @timesheet = Timesheet.new(:period_type => Timesheet::ValidPeriodType[:default])
- end
-
- it 'today' do
- @timesheet.should_receive(:date_from=).with(@date)
- @timesheet.should_receive(:date_to=).with(@date)
- @timesheet.period = 'today'
- end
-
- it 'yesterday' do
- @timesheet.should_receive(:date_from=).with(@date.yesterday)
- @timesheet.should_receive(:date_to=).with(@date.yesterday)
- @timesheet.period = 'yesterday'
- end
-
- it 'current_week' do
- @timesheet.should_receive(:date_from=).with(Date.new(2009, 2, 2))
- @timesheet.should_receive(:date_from).and_return(Date.new(2009, 2, 2))
- @timesheet.should_receive(:date_to=).with(Date.new(2009, 2, 8))
- @timesheet.period = 'current_week'
- end
-
- it 'last_week' do
- @timesheet.should_receive(:date_from=).with(Date.new(2009, 1, 26))
- @timesheet.should_receive(:date_from).and_return(Date.new(2009, 1, 26))
- @timesheet.should_receive(:date_to=).with(Date.new(2009, 2, 1))
- @timesheet.period = 'last_week'
- end
-
- it '7_days' do
- @timesheet.should_receive(:date_from=).with(@date - 7)
- @timesheet.should_receive(:date_to=).with(@date)
- @timesheet.period = '7_days'
- end
-
- it 'current_month' do
- @timesheet.should_receive(:date_from=).with(Date.new(2009, 2, 1))
- @timesheet.should_receive(:date_from).and_return(Date.new(2009, 2, 1))
- @timesheet.should_receive(:date_to=).with(Date.new(2009, 2, 28))
- @timesheet.period = 'current_month'
- end
-
- it 'last_month' do
- @timesheet.should_receive(:date_from=).with(Date.new(2009, 1, 1))
- @timesheet.should_receive(:date_from).and_return(Date.new(2009, 1, 1))
- @timesheet.should_receive(:date_to=).with(Date.new(2009, 1, 31))
- @timesheet.period = 'last_month'
- end
-
- it '30_days' do
- @timesheet.should_receive(:date_from=).with(@date - 30)
- @timesheet.should_receive(:date_to=).with(@date)
- @timesheet.period = '30_days'
- end
-
- it 'current_year' do
- @timesheet.should_receive(:date_from=).with(Date.new(2009,1,1))
- @timesheet.should_receive(:date_to=).with(Date.new(2009,12,31))
- @timesheet.period = 'current_year'
- end
-
- it 'all' do
- @timesheet.should_receive(:date_from=).with(nil)
- @timesheet.should_receive(:date_to=).with(nil)
- @timesheet.period = 'all'
- end
- end
-end
-
-describe Timesheet, '#to_csv' do
- include TimesheetSpecHelper
-
- before(:each) do
- stub_admin_user
- @another_user = mock_model(User, :admin? => true, :allowed_to? => true, :name => "Another user")
- @another_user.stub!(:<=>).with(User.current).and_return(-1)
- end
-
- describe "sorted by :user" do
- it "should return a csv grouped by user" do
- timesheet = timesheet_factory(:sort => :user, :users => [User.current.id, @another_user.id])
-
- time_entries = [
- time_entry_factory(1, stub_common_csv_records.merge({})),
- time_entry_factory(3, stub_common_csv_records.merge({})),
- time_entry_factory(4, stub_common_csv_records.merge({})),
- time_entry_factory(5, stub_common_csv_records.merge({:issue => nil}))
- ]
-
- time_entries_another_user = [
- time_entry_factory(2, stub_common_csv_records.merge({:user => @another_user }))
- ]
-
-
- timesheet.stub!(:time_entries_for_user).with(User.current.id).and_return(time_entries)
- timesheet.stub!(:time_entries_for_user).with(@another_user.id).and_return(time_entries_another_user)
- User.stub!(:find_by_id).with(User.current.id).and_return(User.current)
- User.stub!(:find_by_id).with(@another_user.id).and_return(@another_user)
-
- timesheet.fetch_time_entries
- timesheet.to_csv.should == [
- "#,Date,Member,Activity,Project,Issue,Comment,Hours",
- "1,2009-04-05,Administrator Bob,activity,Project Name,Tracker #1,comments,10.0",
- "3,2009-04-05,Administrator Bob,activity,Project Name,Tracker #1,comments,10.0",
- "4,2009-04-05,Administrator Bob,activity,Project Name,Tracker #1,comments,10.0",
- "5,2009-04-05,Administrator Bob,activity,Project Name,,comments,10.0",
- "2,2009-04-05,Another user,activity,Project Name,Tracker #1,comments,10.0",
- ].join("\n") + "\n" # trailing newline
- end
- end
-
- describe "sorted by :project" do
- it "should return a csv grouped by project" do
- project_a = mock_model(Project, :name => 'Project Name', :parent => nil)
- another_project = mock_model(Project, :name => 'Another Project', :parent => nil)
- timesheet = timesheet_factory(:sort => :project, :users => [User.current.id, @another_user.id])
- timesheet.projects = [project_a, another_project]
-
- project_a_time_entries = [
- time_entry_factory(1, stub_common_csv_records.merge({:project => project_a})),
- time_entry_factory(3, stub_common_csv_records.merge({:project => project_a})),
- time_entry_factory(5, stub_common_csv_records.merge({:issue => nil}))
- ]
-
- another_project_time_entries = [
- time_entry_factory(2, stub_common_csv_records.merge({:user => @another_user, :project => another_project })),
- time_entry_factory(4, stub_common_csv_records.merge({:project => another_project})),
-
- ]
-
- project_a.stub!(:time_entries).and_return do
- te = mock('TimeEntryProxy')
- te.stub!(:find).and_return(project_a_time_entries)
- te
- end
-
- another_project.stub!(:time_entries).and_return do
- te = mock('TimeEntryProxy')
- te.stub!(:find).and_return(another_project_time_entries)
- te
- end
-
- User.stub!(:find_by_id).with(User.current.id).and_return(User.current)
- User.stub!(:find_by_id).with(@another_user.id).and_return(@another_user)
-
- timesheet.fetch_time_entries
- timesheet.to_csv.should == [
- "#,Date,Member,Activity,Project,Issue,Comment,Hours",
- "2,2009-04-05,Another user,activity,Another Project,Tracker #1,comments,10.0",
- "4,2009-04-05,Administrator Bob,activity,Another Project,Tracker #1,comments,10.0",
- "1,2009-04-05,Administrator Bob,activity,Project Name,Tracker #1,comments,10.0",
- "3,2009-04-05,Administrator Bob,activity,Project Name,Tracker #1,comments,10.0",
- "5,2009-04-05,Administrator Bob,activity,Project Name,,comments,10.0",
- ].join("\n") + "\n" # trailing newline
- end
- end
-
- describe "sorted by :issue" do
- it "should return a csv grouped by issue" do
- # Ignore the nasty mocks here, they will be gone once this is
- # moved to shoulda and object_daddy
- project_a = mock_model(Project, :name => 'Project Name', :parent => nil)
- another_project = mock_model(Project, :name => 'Another Project', :parent => nil)
- project_a.stub!(:<=>).with(another_project).and_return(1)
- another_project.stub!(:<=>).with(project_a).and_return(-1)
-
- @issue_1 = mock_model(Issue, :id => 1, :tracker => mock_model(Tracker, :name => 'Tracker'))
- @issue_1.stub!(:time_entries).and_return do
- te = mock('TimeEntryProxy')
- te.stub!(:find).and_return(time_entry_factory(1, stub_common_csv_records.merge({:project => project_a, :issue => @issue_1})))
- te
- end
-
- @issue_2 = mock_model(Issue, :id => 2, :tracker => mock_model(Tracker, :name => 'Tracker'))
- @issue_2.stub!(:time_entries).and_return do
- te = mock('TimeEntryProxy')
- te.stub!(:find).and_return(time_entry_factory(3, stub_common_csv_records.merge({:project => project_a, :issue => @issue_2})))
- te
- end
-
- @issue_3 = mock_model(Issue, :id => 3, :tracker => mock_model(Tracker, :name => 'Tracker'))
- @issue_3.stub!(:time_entries).and_return do
- te = mock('TimeEntryProxy')
- te.stub!(:find).and_return(time_entry_factory(2, stub_common_csv_records.merge({:project => another_project, :issue => @issue_3, :user => @another_user})))
- te
- end
-
- @issue_4 = mock_model(Issue, :id => 4, :tracker => mock_model(Tracker, :name => 'Tracker'))
- @issue_4.stub!(:time_entries).and_return do
- te = mock('TimeEntryProxy')
- te.stub!(:find).and_return(time_entry_factory(4, stub_common_csv_records.merge({:project => another_project, :issue => @issue_4})))
- te
- end
-
- project_a.stub!(:issues).and_return([@issue_1, @issue_2])
- another_project.stub!(:issues).and_return([@issue_3, @issue_4])
-
- timesheet = timesheet_factory(:sort => :issue, :users => [User.current.id, @another_user.id])
- timesheet.projects = [project_a, another_project]
-
- User.stub!(:find_by_id).with(User.current.id).and_return(User.current)
- User.stub!(:find_by_id).with(@another_user.id).and_return(@another_user)
-
- timesheet.fetch_time_entries
- timesheet.to_csv.should == [
- "#,Date,Member,Activity,Project,Issue,Comment,Hours",
- "2,2009-04-05,Another user,activity,Another Project,Tracker #3,comments,10.0",
- "4,2009-04-05,Administrator Bob,activity,Another Project,Tracker #4,comments,10.0",
- "1,2009-04-05,Administrator Bob,activity,Project Name,Tracker #1,comments,10.0",
- "3,2009-04-05,Administrator Bob,activity,Project Name,Tracker #2,comments,10.0",
- ].join("\n") + "\n" # trailing newline
-
- end
- end
-end
View
0  spec/rcov.opts
No changes.
View
7 spec/sanity_spec.rb
@@ -1,7 +0,0 @@
-require File.dirname(__FILE__) + '/spec_helper'
-
-describe Class do
- it "should be a class of Class" do
- Class.class.should eql(Class)
- end
-end
View
7 spec/spec.opts
@@ -1,7 +0,0 @@
---colour
---format
-progress
---loadby
-mtime
---reverse
---backtrace
View
39 spec/spec_helper.rb
@@ -1,39 +0,0 @@
-# This file is copied to ~/spec when you run 'ruby script/generate rspec'
-# from the project root directory.
-ENV["RAILS_ENV"] = "test"
-require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
-require 'spec'
-require 'spec/rails'
-
-Spec::Runner.configure do |config|
- # If you're not using ActiveRecord you should remove these
- # lines, delete config/database.yml and disable :active_record
- # in your config/boot.rb
- config.use_transactional_fixtures = true
- config.use_instantiated_fixtures = false
- config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
-
- # == Fixtures
- #
- # You can declare fixtures for each example_group like this:
- # describe "...." do
- # fixtures :table_a, :table_b
- #
- # Alternatively, if you prefer to declare them only once, you can
- # do so right here. Just uncomment the next line and replace the fixture
- # names with your fixtures.
- #
- # config.global_fixtures = :table_a, :table_b
- #
- # If you declare global fixtures, be aware that they will be declared
- # for all of your examples, even those that don't use them.
- #
- # == Mock Framework
- #
- # RSpec uses it's own mocking framework by default. If you prefer to
- # use mocha, flexmock or RR, uncomment the appropriate line:
- #
- # config.mock_with :mocha
- # config.mock_with :flexmock
- # config.mock_with :rr
-end
View
0  spec/views/empty
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.