Project

General

Profile

Download (9.12 KB) Statistics
| Branch: | Tag: | Revision:
dc38aad3 Joseph Magen
require 'test_helper'

class Api::V2::PtablesControllerTest < ActionController::TestCase
valid_attrs = { :name => 'ptable_test', :layout => 'd-i partman-auto/disk' }

510d53cd Marek Hulan
def setup
8c6bc83e Marek Hulan
@ptable = FactoryBot.create(:ptable)
510d53cd Marek Hulan
end

dc38aad3 Joseph Magen
test "should get index" do
d041d4bb Dominic Cleal
get :index
dc38aad3 Joseph Magen
assert_response :success
assert_not_nil assigns(:ptables)
ptables = ActiveSupport::JSON.decode(@response.body)
assert !ptables.empty?
end

test "should show individual record" do
d041d4bb Dominic Cleal
get :show, params: { :id => @ptable.to_param }
dc38aad3 Joseph Magen
assert_response :success
show_response = ActiveSupport::JSON.decode(@response.body)
assert !show_response.empty?
end

d0644ecc Djebran Lezzoum
test_attributes :pid => 'f774051a-8ad4-48dc-b652-0e3c382b6043'
dc38aad3 Joseph Magen
test "should create ptable" do
5f606e11 Daniel Lobato Garcia
assert_difference('Ptable.unscoped.count') do
d041d4bb Dominic Cleal
post :create, params: { :ptable => valid_attrs }
dc38aad3 Joseph Magen
end
d575926a alongoldboim
assert_response :created
74e9b19a ldjebran
response = JSON.parse(@response.body)
assert response.key?('name')
assert response.key?('layout')
assert_equal response['name'], valid_attrs[:name]
assert_equal response['layout'], valid_attrs[:layout]
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '7a07d70c-6130-4357-81c3-4f1254e519d2'
74e9b19a ldjebran
test "create with layout length" do
# :BZ: 1270181
valid_params = valid_attrs.merge(:layout => RFauxFactory.gen_alpha(5000))
assert_difference('Ptable.unscoped.count') do
post :create, params: { :ptable => valid_params }
end
assert_response :created
response = JSON.parse(@response.body)
assert response.key?('layout')
assert_equal response['layout'], valid_params[:layout]
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '71601d96-8ce8-4ecb-b053-af6f26a246ea'
74e9b19a ldjebran
test "create with one character name" do
# :BZ: 1229384
valid_params = valid_attrs.merge(:name => RFauxFactory.gen_alpha(1))
assert_difference('Ptable.unscoped.count') do
post :create, params: { :ptable => valid_params }
end
assert_response :created
response = JSON.parse(@response.body)
assert response.key?('name')
assert_equal response['name'], valid_params[:name]
end

d0644ecc Djebran Lezzoum
test_attributes :pid => 'ebd55ed6-5fb2-4f17-ac73-b56661ee5254'
74e9b19a ldjebran
test "should create ptable with os family" do
valid_params = valid_attrs.merge(:os_family => 'Redhat')
assert_difference('Ptable.unscoped.count') do
post :create, params: { :ptable => valid_params }
end
assert_response :created
response = JSON.parse(@response.body)
assert response.key?('os_family')
assert_equal response['os_family'], valid_params[:os_family]
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '5f97b180-3708-4e1c-8407-42977459d4b6'
74e9b19a ldjebran
test "should create ptable with organization" do
organization_id = Organization.first.id
assert_difference('Ptable.unscoped.count') do
post :create, params: { :ptable => valid_attrs.merge(:organization_ids => [organization_id]) }
end
assert_response :created
response = JSON.parse(@response.body)
assert response.key?('organizations')
organization_ids = response['organizations'].map { |org| org['id']}
assert_equal organization_ids.length, 1
assert_include organization_ids, organization_id
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '8bde5a54-21a8-420e-b6cb-1d81c381d0b2'
74e9b19a ldjebran
test "should update name" do
new_name = 'new ptable name'
put :update, params: { :id => @ptable.id, :ptable => { :name => new_name } }
assert_response :success
response = JSON.parse(@response.body)
assert response.key?('name')
assert_equal response['name'], new_name
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '329eea6e-3474-4cc1-87d4-15e765e0a255'
74e9b19a ldjebran
test "should update layout" do
new_layout = 'new ptable layout'
put :update, params: { :id => @ptable.id, :ptable => { :layout => new_layout } }
assert_response :success
response = JSON.parse(@response.body)
assert response.key?('layout')
assert_equal response['layout'], new_layout
end

d0644ecc Djebran Lezzoum
test_attributes :pid => 'bf03d80c-3527-4b0a-b6c7-4629a8eaefb2'
74e9b19a ldjebran
test "should update os family" do
@ptable.os_family = 'Redhat'
assert @ptable.save
new_os_family = 'Coreos'
put :update, params: { :id => @ptable.id, :ptable => { :os_family => new_os_family } }
assert_response :success
response = JSON.parse(@response.body)
assert response.key?('os_family')
assert_equal response['os_family'], new_os_family
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '02631917-2f7a-4cf7-bb2a-783349a04758'
74e9b19a ldjebran
test "should not create with invalid name" do
assert_difference('Ptable.unscoped.count', 0) do
post :create, params: { :ptable => valid_attrs.merge(:name => '') }
end
assert_response :unprocessable_entity
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '03cb7a35-e4c3-4874-841b-0760c3b8d6af'
74e9b19a ldjebran
test "should not create with invalid layout" do
assert_difference('Ptable.unscoped.count', 0) do
post :create, params: { :ptable => valid_attrs.merge(:layout => '') }
end
assert_response :unprocessable_entity
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '7e9face8-2c20-450e-890c-6def6de570ca'
74e9b19a ldjebran
test "should not update with invalid name" do
put :update, params: { :id => @ptable.id, :ptable => { :name => ''} }
assert_response :unprocessable_entity
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '35c84c8f-b802-4076-89f2-4ec04cf43a31'
74e9b19a ldjebran
test "should not update with invalid layout" do
put :update, params: { :id => @ptable.id, :ptable => { :layout => ''} }
assert_response :unprocessable_entity
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '08520746-444b-47c9-a8a3-438170147453'
74e9b19a ldjebran
test "search ptable" do
9d43fc71 Michael Moll
get :index, params: { :search => @ptable.name, :format => 'json' }
3cd8c84b Michael Moll
assert_response :success, "search ptable name: '#{@ptable.name}' failed with code: #{@response.code}"
74e9b19a ldjebran
response = JSON.parse(@response.body)
assert_equal response['results'].length, 1
assert_equal response['results'][0]['id'], @ptable.id
end

d0644ecc Djebran Lezzoum
test_attributes :pid => 'cdbc5d5a-c924-4cb3-8b54-d84fc6bbb651'
74e9b19a ldjebran
test "search ptable by name and organization" do
# :BZ: 1375788
org = Organization.first
@ptable.organizations = [org]
assert @ptable.save
630061d2 Michael Moll
get :index, params: {:search => @ptable.name, :organization_id => org.id, :format => 'json' }
3cd8c84b Michael Moll
assert_response :success, "search ptable by name and organization failed with code: #{@response.code}"
74e9b19a ldjebran
response = JSON.parse(@response.body)
assert_equal response['results'].length, 1
assert_equal response['results'][0]['id'], @ptable.id
dc38aad3 Joseph Magen
end

eb9a7a49 Shlomi Zadok
test "should created ptable with unwrapped 'layout'" do
5f606e11 Daniel Lobato Garcia
assert_difference('Ptable.unscoped.count') do
d041d4bb Dominic Cleal
post :create, params: valid_attrs
eb9a7a49 Shlomi Zadok
end
assert_response :created
end

dc38aad3 Joseph Magen
test "should update ptable" do
d041d4bb Dominic Cleal
put :update, params: { :id => @ptable.to_param, :ptable => valid_attrs }
dc38aad3 Joseph Magen
assert_response :success
end

5fc2aed6 Martin Bačovský
test "should assign operating system" do
d041d4bb Dominic Cleal
put :update, params: { :id => @ptable.to_param, :ptable => {
5fc2aed6 Martin Bačovský
:operatingsystem_ids => [operatingsystems(:redhat).to_param] } }
assert_response :success
d041d4bb Dominic Cleal
get :show, params: { :id => @ptable.to_param }
5fc2aed6 Martin Bačovský
show_response = ActiveSupport::JSON.decode(@response.body)
assert_equal operatingsystems(:redhat).id, show_response["operatingsystems"].first["id"]
end

dc38aad3 Joseph Magen
test "should NOT destroy ptable in use" do
8c6bc83e Marek Hulan
FactoryBot.create(:host, :ptable_id => @ptable.id)
5f606e11 Daniel Lobato Garcia
assert_difference('Ptable.unscoped.count', 0) do
d041d4bb Dominic Cleal
delete :destroy, params: { :id => @ptable.to_param }
dc38aad3 Joseph Magen
end
assert_response :unprocessable_entity
end

d0644ecc Djebran Lezzoum
test_attributes :pid => '36133202-8849-432e-838b-3d13d088ef28'
dc38aad3 Joseph Magen
test "should destroy ptable that is NOT in use" do
5f606e11 Daniel Lobato Garcia
assert_difference('Ptable.unscoped.count', -1) do
d041d4bb Dominic Cleal
delete :destroy, params: { :id => @ptable.to_param }
dc38aad3 Joseph Magen
end
assert_response :success
end
510d53cd Marek Hulan
test "should add audit comment" do
Ptable.auditing_enabled = true
Ptable.any_instance.stubs(:valid?).returns(true)
8c6bc83e Marek Hulan
ptable = FactoryBot.create(:ptable)
d041d4bb Dominic Cleal
put :update, params: { :id => ptable.to_param,
:ptable => { :audit_comment => "aha", :template => "tmp" } }
510d53cd Marek Hulan
assert_response :success
assert_equal "aha", ptable.audits.last.comment
end

test 'should clone template' do
8c6bc83e Marek Hulan
original_ptable = FactoryBot.create(:ptable)
d041d4bb Dominic Cleal
post :clone, params: { :id => original_ptable.to_param,
:ptable => {:name => 'MyClone'} }
510d53cd Marek Hulan
assert_response :success
template = ActiveSupport::JSON.decode(@response.body)
assert_equal(template['name'], 'MyClone')
assert_equal(template['template'], original_ptable.template)
end

404ead2a Marek Hulan
test 'export should export the erb of the template' do
8c6bc83e Marek Hulan
ptable = FactoryBot.create(:ptable)
d041d4bb Dominic Cleal
get :export, params: { :id => ptable.to_param }
404ead2a Marek Hulan
assert_response :success
assert_equal 'text/plain', response.content_type
assert_equal ptable.to_erb, response.body
end

510d53cd Marek Hulan
test 'clone name should not be blank' do
d041d4bb Dominic Cleal
post :clone, params: { :id => FactoryBot.create(:ptable).to_param,
:ptable => {:name => ''} }
510d53cd Marek Hulan
assert_response :unprocessable_entity
end
ca51c513 Marek Hulan
test "should import partition table" do
ptable = FactoryBot.create(:ptable, :template => 'a')
post :import, params: { :ptable => { :name => ptable.name, :template => 'b'} }
assert_response :success
assert_equal 'b', Ptable.unscoped.find_by_name(ptable.name).template
end
dc38aad3 Joseph Magen
end