Project

General

Profile

« Previous | Next » 

Revision 9e225cf5

Added by Djebran Lezzoum almost 6 years ago

Fixes #23802 - Port robottelo tests - sync_plan

View differences:

test/controllers/api/v2/sync_plans_controller_test.rb
def models
@organization = get_organization
@sync_plan = Katello::SyncPlan.find(katello_sync_plans(:sync_plan_hourly).id)
@sync_plan = katello_sync_plans(:sync_plan_hourly)
@products = katello_products(:fedora, :redhat, :empty_product)
end
......
def setup
setup_controller_defaults_api
login_user(User.find(users(:admin).id))
login_user(users(:admin))
@request.env['HTTP_ACCEPT'] = 'application/json'
Repository.any_instance.stubs(:sync_status).returns(PulpSyncStatus.new({}))
Repository.any_instance.stubs(:last_sync).returns(Time.now.to_s)
......
end
def test_create
post :create, params: { :organization_id => @organization.id, :sync_plan => {:name => 'Hourly Sync Plan',
:sync_date => '2014-01-09 17:46:00',
:interval => 'hourly',
:description => 'This is my cool new product.'} }
valid_attr = {
:name => 'Hourly Sync Plan',
:sync_date => '2014-01-09 17:46:00 +0000',
:interval => 'hourly',
:description => 'This is my cool new product.',
:enabled => true
}
post :create, params: { :organization_id => @organization.id, :sync_plan => valid_attr }
assert_response :success
assert_template 'api/v2/common/create'
end
response = JSON.parse(@response.body)
assert response.key?('name')
assert_equal valid_attr[:name], response['name']
assert response.key?('sync_date')
assert_equal Time.parse(valid_attr[:sync_date]), Time.parse(response['sync_date'])
assert response.key?('interval')
assert_equal valid_attr[:interval], response['interval']
assert response.key?('description')
assert_equal valid_attr[:description], response['description']
assert response.key?('enabled')
assert_equal valid_attr[:enabled], response['enabled']
end
test_attributes :pid => 'b4686463-69c8-4538-b040-6fb5246a7b00'
def test_create_fail
post :create, params: { :organization_id => @organization.id, :sync_plan => {:sync_date => '2014-01-09 17:46:00',
:description => 'This is my cool new sync plan.'} }
assert_response :unprocessable_entity
response = JSON.parse(@response.body)
assert response.key?('errors')
assert response['errors'].key?('name')
assert_equal 'can\'t be blank', response['errors']['name'][0]
assert response['errors'].key?('interval')
assert_equal 'is not included in the list', response['errors']['interval'][0]
end
def test_create_protected
......
end
def test_update
put :update, params: { :id => @sync_plan.id, :organization_id => @organization.id, :sync_plan => {:name => 'New Name'} }
datetime_format = '%Y/%m/%d %H:%M:%S %z'
update_attrs = {
:name => 'New Name',
:interval => 'weekly',
:sync_date => Time.now.utc.strftime(datetime_format),
:description => 'New Description',
:enabled => false
}
put :update, params: { :id => @sync_plan.id, :organization_id => @organization.id, :sync_plan => update_attrs }
assert_response :success
assert_template 'api/v2/sync_plans/show'
assert_equal assigns[:sync_plan].name, 'New Name'
assert_equal update_attrs[:name], assigns[:sync_plan].name
assert_equal update_attrs[:interval], assigns[:sync_plan].interval
assert_equal update_attrs[:enabled], assigns[:sync_plan].enabled
assert_equal update_attrs[:sync_date], assigns[:sync_plan].sync_date.strftime(datetime_format)
end
test_attributes :pid => '8c981174-6f55-49c0-8baa-40e5c3fc598c'
def test_update_with_invalid_interval
put :update, params: { :id => @sync_plan.id, :organization_id => @organization.id, :sync_plan => { :interval => 'invalid_interval'} }
assert_response :unprocessable_entity
assert_match 'Validation failed: Interval is not included in the list', @response.body
end
def test_update_protected
test/models/sync_plan_test.rb
@plan_to_audit = SyncPlan.new(:name => 'Test Prod Sync', :organization => @organization, :sync_date => Time.now, :interval => 'daily')
end
def test_invalid_intervals
@plan.interval = 'notgood'
@plan.wont_be :valid?
def valid_attributes
{:name => 'Sync plan', :organization => @organization, :sync_date => Time.now, :interval => 'daily'}
end
def test_modify_interval
@plan.interval = 'weekly'
@plan.must_be :valid?
# Returns a list of valid sync dates.
def valid_sync_dates
to_day = Time.now
[
to_day,
to_day + 5.minutes,
to_day + 5.days,
to_day - 1.day,
to_day - 5.minutes
]
end
# Returns a list of valid sync intervals.
def valid_sync_intervals
%w[hourly daily weekly]
end
test_attributes :pid => 'df5837e7-3d0f-464a-bd67-86b423c16eb4'
def test_create_enabled_disabled
[false, true].each do |enabled|
sync_plan = SyncPlan.new(valid_attributes.merge(:enabled => enabled))
assert sync_plan.valid?, "Validation failed when creating with enabled = #{enabled}"
assert_equal enabled, sync_plan.enabled
end
end
test_attributes :pid => 'c1263134-0d7c-425a-82fd-df5274e1f9ba'
def test_create_with_name
valid_name_list.each do |name|
sync_plan = SyncPlan.new(valid_attributes.merge(:name => name))
assert sync_plan.valid?, "Validation failed for create with valid name: '#{name}' length: #{name.length})"
assert_equal name, sync_plan.name
end
end
test_attributes :pid => '3e5745e8-838d-44a5-ad61-7e56829ad47c'
def test_create_with_description
valid_name_list.each do |description|
sync_plan = SyncPlan.new(valid_attributes.merge(:description => description))
assert sync_plan.valid?, "Validation failed for create with valid description: '#{description}' length: #{description.length})"
assert_equal description, sync_plan.description
end
end
test_attributes :pid => 'd160ed1c-b698-42dc-be0b-67ac693c7840'
def test_create_with_interval
valid_sync_intervals.each do |interval|
sync_plan = SyncPlan.new(valid_attributes.merge(:interval => interval))
assert sync_plan.valid?, "Validation failed for create with valid interval: '#{interval}'"
assert_equal interval, sync_plan.interval
end
end
test_attributes :pid => 'bdb6e0a9-0d3b-4811-83e2-2140b7bb62e3'
def test_create_with_sync_date
valid_sync_dates.each do |sync_date|
sync_plan = SyncPlan.new(valid_attributes.merge(:sync_date => sync_date))
assert sync_plan.valid?, "Validation failed for create with valid sync_date: '#{sync_date}'"
assert_equal sync_date, sync_plan.sync_date
end
end
test_attributes :pid => 'a3a0f844-2f81-4f87-9f68-c25506c29ce2'
def test_create_with_invalid_name
invalid_name_list.each do |name|
sync_plan = SyncPlan.new(valid_attributes.merge(:name => name))
refute sync_plan.valid?, "Validation succeed for create with invalid name: '#{name}' length: #{name.length})"
assert sync_plan.errors.key?(:name)
end
end
test_attributes :pid => 'f5844526-9f58-4be3-8a96-3849a465fc02'
def test_create_with_invalid_interval
invalid_name_list.each do |interval|
sync_plan = SyncPlan.new(valid_attributes.merge(:interval => interval))
refute sync_plan.valid?, "Validation succeed for create with invalid interval: '#{interval}'"
assert sync_plan.errors.key?(:interval)
end
end
test_attributes :pid => '325c0ef5-c0e8-4cb9-b85e-87eb7f42c2f8'
def test_update_enabled
@plan.save!
sync_plan_enabled = @plan.enabled
[!sync_plan_enabled, sync_plan_enabled].each do |enabled|
@plan.enabled = enabled
assert @plan.save
end
end
test_attributes :pid => 'dbfadf4f-50af-4aa8-8d7d-43988dc4528f'
def test_update_name
@plan.save!
valid_name_list.each do |name|
@plan.name = name
assert @plan.valid?, "Validation failed for update with valid name: '#{name}' length: #{name.length})"
assert_equal name, @plan.name
end
end
test_attributes :pid => '4769fe9c-9eec-40c8-b015-1e3d7e570bec'
def test_update_description
@plan.save!
valid_name_list.each do |description|
@plan.description = description
assert @plan.valid?, "Validation failed for update with valid description: '#{description}' length: #{description.length})"
assert_equal description, @plan.description
end
end
test_attributes :pid => 'cf2eddf8-b4db-430e-a9b0-83c626b45068'
def test_update_interval
@plan.save!
# create a new valid list of intervals with the current interval in the last position
intervals = valid_sync_intervals.reject { |interval| interval == @plan.interval }
intervals << @plan.interval
intervals.each do |interval|
@plan.interval = interval
assert @plan.save
assert_equal interval, @plan.interval
end
end
test_attributes :pid => 'fad472c7-01b4-453b-ae33-0845c9e0dfd4'
def test_update_sync_date
@plan.save!
valid_sync_dates.each do |sync_date|
@plan.sync_date = sync_date
assert_valid @plan
end
end
test_attributes :pid => 'ae502053-9d3c-4cad-aee4-821f846ceae5'
def test_update_with_invalid_name
@plan.save!
invalid_name_list.each do |name|
@plan.name = name
refute @plan.valid?, "Validation succeed for update with invalid name: '#{name}' length: #{name.length})"
assert @plan.errors.key?(:name)
end
end
def test_update_with_invalid_interval
@plan.save!
invalid_name_list.each do |interval|
@plan.interval = interval
refute_valid @plan
assert @plan.errors.key?(:interval)
end
end
def test_sync_date_future
......
@plan.interval = 'weekly'
@plan.sync_date = '1999-11-17 09:26:00 UTC'
Time.stubs(:now).returns(Time.new(2012, 1, 1, 9))
Time.stubs(:now).returns(Time.new(2012, 1, 1, 9, 0, 0, "+00:00"))
@plan.next_sync.must_equal(Time.new(2012, 1, 4, 9, 26, 0, "+00:00"))
Time.stubs(:now).returns(Time.new(2012, 1, 11, 9, 30))
Time.stubs(:now).returns(Time.new(2012, 1, 11, 9, 30, 0, "+00:00"))
@plan.next_sync.must_equal(Time.new(2012, 1, 18, 9, 26, 0, "+00:00"))
end

Also available in: Unified diff