Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Untitled #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class VersionsController < ApplicationController
before_filter :find_model_object, :except => [:index, :new, :create, :close_completed]
before_filter :find_project_from_association, :except => [:index, :new, :create, :close_completed]
before_filter :find_project, :only => [:index, :new, :create, :close_completed]
before_filter :build_version, :only => [:new, :create]
before_filter :authorize

helper :custom_fields
Expand Down Expand Up @@ -57,23 +58,9 @@ def show
end

def new
@version = @project.versions.build
if params[:version]
attributes = params[:version].dup
attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
@version.attributes = attributes
end
end

def create
# TODO: refactor with code above in #new
@version = @project.versions.build
if params[:version]
attributes = params[:version].dup
attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
@version.attributes = attributes
end

if request.post?
if @version.save
respond_to do |format|
Expand Down Expand Up @@ -152,4 +139,12 @@ def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
end
end

def build_version
@version = @project.versions.build
if params[:version]
attributes = params[:version].dup
attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
@version.attributes = attributes
end
end
end
45 changes: 45 additions & 0 deletions test/functional/versions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,51 @@ def test_create_from_issue_form
assert_equal 1, version.project_id
end

def test_create_without_version_param
@request.session[:user_id] = 2 # manager
assert_no_difference 'Version.count' do
post :create, :project_id => '1'
end
assert_response :success
assert_template 'new'
end

def test_create_with_allowed_sharing
@request.session[:user_id] = 2 # manager
assert_difference 'Version.count' do
post :create, :project_id => '1', :version => {:name => 'test_add_version', :sharing => 'descendants'}
end
assert_redirected_to '/projects/ecookbook/settings/versions'
version = Version.find_by_name('test_add_version')
assert_not_nil version
assert_equal 1, version.project_id
assert_equal 'descendants', version.sharing
end

def test_create_with_not_allowed_sharing
@request.session[:user_id] = 2 # manager
assert_difference 'Version.count' do
post :create, :project_id => '1', :version => {:name => 'test_add_version', :sharing => 'system'}
end
assert_redirected_to '/projects/ecookbook/settings/versions'
version = Version.find_by_name('test_add_version')
assert_not_nil version
assert_equal 1, version.project_id
assert_equal 'none', version.sharing
end

def test_create_with_unknown_sharing
@request.session[:user_id] = 2 # manager
assert_difference 'Version.count' do
post :create, :project_id => '1', :version => {:name => 'test_add_version', :sharing => 'unknown'}
end
assert_redirected_to '/projects/ecookbook/settings/versions'
version = Version.find_by_name('test_add_version')
assert_not_nil version
assert_equal 1, version.project_id
assert_equal 'none', version.sharing
end

def test_get_edit
@request.session[:user_id] = 2
get :edit, :id => 2
Expand Down