Project

General

Profile

« Previous | Next » 

Revision 305ee6f0

Added by Timo Goebel about 5 years ago

fixes #26528 - graphql: refactor AuthorizedModelQuery to resolver

View differences:

app/graphql/queries/authorized_model_query.rb
module Queries
class AuthorizedModelQuery
def initialize(model_class:, user:)
@model_class = model_class
@user = user
end
def results(params = {})
if params[:search].present?
authorized_scope.search_for(*search_options(params))
elsif params[:order_by].present?
ordered_results(order_field: params[:order_by], order_direction: params[:order])
else
authorized_scope.all
end
end
private
attr_reader :model_class, :user
def authorized_scope
return model_class unless model_class.respond_to?(:authorized)
permission = model_class.find_permission_name(:view)
model_class.authorized_as(user, permission, model_class)
end
def search_options(params)
search_options = [params[:search]]
if params[:order_by].present?
search_options << { :order => "#{params[:order_by]} #{params[:order]}".strip }
end
search_options
end
def ordered_results(order_field:, order_direction:)
order_direction = order_direction.presence || :ASC
authorized_scope.order(order_field => order_direction).all
end
end
end
app/graphql/resolvers/concerns/collection.rb
type ["Types::#{self::MODEL_CLASS}".safe_constantize], null: false
argument :search, String, 'Search query', required: false
argument :order_by, String, 'Order by this field', required: false
argument :order, String, 'Order direction', required: false
argument :sort_by, String, 'Sort by this searchable field', required: false
argument :sort_direction, Types::SortDirectionEnum, 'Sort direction', required: false
if has_taxonomix?
argument :location, String, required: false
argument :location_id, String, required: false
argument :organization, String, required: false
argument :organization_id, String, required: false
end
delegate :has_taxonomix?, to: :class
end
def resolve(**kwargs)
filters = filter_list(**kwargs)
base_scope = authorized_scope.all
filters.reduce(base_scope) { |scope, filter| filter.call(scope) }
end
private
def user
context[:current_user]
end
def model_class
self.class::MODEL_CLASS
end
def authorized_scope
return model_class unless model_class.respond_to?(:authorized)
permission = model_class.find_permission_name(:view)
model_class.authorized_as(user, permission, model_class)
end
def search_options(search:, sort_by:, sort_direction:)
(sort_direction.to_s.upcase == 'DESC') ? 'DESC' : 'ASC'
search_options = [search]
if sort_by.present?
search_options << { order: "#{sort_by} #{sort_direction}".strip }
end
search_options
end
def filter_list(search: nil, sort_by: nil, sort_direction: 'ASC',
first: nil, skip: nil,
location: nil, location_id: nil,
organization: nil, organization_id: nil)
filters = []
if search.present? || sort_by.present?
filters << search_and_sort_filter(search: search, sort_by: sort_by, sort_direction: sort_direction)
end
if has_taxonomix?
taxonomy_ids = taxonomy_ids_for_filter(location_id: location_id, organization_id: organization_id,
location: location, organization: organization)
filters << taxonomy_id_filter(taxonomy_ids) if taxonomy_ids.any?
end
filters
end
def search_and_sort_filter(search:, sort_by:, sort_direction:)
lambda do |scope|
begin
scope.search_for(*search_options(search: search, sort_by: sort_by, sort_direction: sort_direction))
rescue ScopedSearch::QueryNotSupported => error
raise GraphQL::ExecutionError.new(error.message)
end
end
end
def resolve(search: nil, order: nil, order_by: nil)
params = {
order_by: order_by,
order: order,
search: search
}
def taxonomy_id_filter(taxonomy_ids)
lambda do |scope|
scope.joins(:taxable_taxonomies)
.where(taxable_taxonomies: {taxonomy_id: taxonomy_ids}).distinct
end
end
def taxonomy_ids_for_filter(location_id: nil, organization_id: nil,
location: nil, organization: nil)
taxonomy_ids = []
if location_id.present?
taxonomy_ids << resolve_taxonomy_global_id(Location, location_id)
elsif location.present?
taxonomy_ids << resolve_taxonomy_name_to_id(Location, location)
end
if organization_id.present?
taxonomy_ids << resolve_taxonomy_global_id(Organization, organization_id)
elsif organization.present?
taxonomy_ids << resolve_taxonomy_name_to_id(Organization, organization)
end
taxonomy_ids.uniq
end
def resolve_taxonomy_global_id(taxonomy_class, global_id)
taxonomy_name = taxonomy_class.name
_, type_name, id = Foreman::GlobalId.decode(global_id)
raise GraphQL::ExecutionError.new("Global ID for #{taxonomy_name} filter is for other graphql type (expected '#{taxonomy_name}', got '#{type_name}')") unless taxonomy_name == type_name
id.to_i
end
def resolve_taxonomy_name_to_id(taxonomy_class, taxonomy_name)
id = taxonomy_class.find_by(name: taxonomy_name)
raise GraphQL::ExecutionError.new("#{taxonomy_class.name} could not be found my name #{name}.") unless id
id
end
Queries::AuthorizedModelQuery.new(
model_class: self.class::MODEL_CLASS,
user: context[:current_user]
).results(params)
class_methods do
def has_taxonomix?
self::MODEL_CLASS.include?(Taxonomix)
end
end
end
end
app/graphql/types/sort_direction_enum.rb
module Types
class SortDirectionEnum < Types::BaseEnum
value 'ASC', description: 'Sort ascending'
value 'DESC', description: 'Sort descending'
end
end
app/services/foreman/global_id.rb
ID_SEPARATOR = '-'
VERSION_SEPARATOR = ':'
DEFAULT_VERSION = 1
BASE64_FORMAT = %r(\A([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\Z)
def self.encode(type_name, object_value, version: DEFAULT_VERSION)
object_value_str = object_value.to_s
......
end
def self.decode(node_id)
raise InvalidGlobalIdException unless base64_encoded?(node_id)
decoded = Base64.decode64(node_id)
version, payload = decoded.split(VERSION_SEPARATOR, 2)
raise InvalidGlobalIdException unless version.present? && payload.present?
type_name, object_value = payload.split(ID_SEPARATOR, 2)
raise InvalidGlobalIdException unless type_name.present? && object_value.present?
[version.to_i, type_name, object_value]
end
def self.for(obj)
encode(obj.class.name, obj.id)
end
def self.base64_encoded?(string)
!!string.match(BASE64_FORMAT)
end
class InvalidGlobalIdException < Foreman::Exception
def initialize
super('Invalid Global ID. Can not decode.')
end
end
end
end
test/graphql/queries/architecture_query_test.rb
require 'test_helper'
class Queries::ArchitectureQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class ArchitectureQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
) {
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:hosts) { FactoryBot.create_list(:host, 2) }
let(:architecture) { FactoryBot.create(:architecture, hosts: hosts) }
let(:hosts) { FactoryBot.create_list(:host, 2) }
let(:architecture) { FactoryBot.create(:architecture, hosts: hosts) }
let(:global_id) { Foreman::GlobalId.for(architecture) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['architecture'] }
let(:global_id) { Foreman::GlobalId.for(architecture) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['architecture'] }
test 'fetching architecture attributes' do
assert_empty result['errors']
test 'fetching architecture attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal architecture.created_at.utc.iso8601, data['createdAt']
assert_equal architecture.updated_at.utc.iso8601, data['updatedAt']
assert_equal architecture.name, data['name']
assert_equal global_id, data['id']
assert_equal architecture.created_at.utc.iso8601, data['createdAt']
assert_equal architecture.updated_at.utc.iso8601, data['updatedAt']
assert_equal architecture.name, data['name']
assert_collection architecture.hosts, data['hosts'], type_name: 'Host'
assert_collection architecture.hosts, data['hosts'], type_name: 'Host'
end
end
end
test/graphql/queries/architectures_query_test.rb
require 'test_helper'
class Queries::ArchitecturesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class ArchitecturesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
architectures {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['architectures'] }
let(:data) { result['data']['architectures'] }
setup do
FactoryBot.create_list(:architecture, 2)
end
setup do
FactoryBot.create_list(:architecture, 2)
end
test 'fetching architectures attributes' do
assert_empty result['errors']
test 'fetching architectures attributes' do
assert_empty result['errors']
expected_count = Architecture.count
expected_count = Architecture.count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
end
end
end
test/graphql/queries/authorized_model_query_test.rb
require 'test_helper'
class Queries::AuthorizedModelQueryTest < GraphQLQueryTestCase
describe '#results' do
test 'does not return records for missing user' do
as_admin { FactoryBot.create(:host) }
result = Queries::AuthorizedModelQuery.new(model_class: Host::Managed, user: nil)
.results
assert_empty result
end
test 'does not return records for not authorized user' do
user = as_admin { FactoryBot.create(:user) }
as_admin { FactoryBot.create(:host) }
result = Queries::AuthorizedModelQuery.new(model_class: Host::Managed, user: user)
.results
assert_empty result
end
test 'returns records for authorized user' do
user = as_admin { setup_user 'view', 'hosts' }
host = as_admin { FactoryBot.create(:host) }
result = Queries::AuthorizedModelQuery.new(model_class: Host::Managed, user: user)
.results
assert_equal [host], result
end
test 'searches results when search param present' do
user = as_admin { setup_user 'view', 'hosts' }
excluded_host = as_admin { FactoryBot.create(:host) }
included_host = as_admin { FactoryBot.create(:host, hostname: 'sample host 1') }
result = Queries::AuthorizedModelQuery.new(
model_class: Host::Managed, user: user
).results(search: 'name ~ "sample"')
refute_includes result, excluded_host
assert_equal [included_host], result
end
test 'searches and order results when search param present' do
user = as_admin { setup_user 'view', 'hosts' }
excluded_host = as_admin { FactoryBot.create(:host) }
included_hosts = as_admin do
[
FactoryBot.create(:host, hostname: 'sample host 1'),
FactoryBot.create(:host, hostname: 'a sample host 1')
]
end
result = Queries::AuthorizedModelQuery.new(
model_class: Host::Managed, user: user
).results(search: 'name ~ "sample"', order_by: 'name', order: 'DESC')
refute_includes result, excluded_host
assert_equal included_hosts, result
end
test 'returns ordered records for authorized user by given order_by' do
user = as_admin { setup_user 'view', 'hosts' }
old_host = as_admin { FactoryBot.create(:host, created_at: Time.zone.now - 2.days) }
new_host = as_admin { FactoryBot.create(:host, created_at: Time.zone.now) }
result = Queries::AuthorizedModelQuery.new(model_class: Host::Managed, user: user)
.results(order_by: 'created_at')
assert_equal [old_host, new_host], result
end
test 'returns ordered records for authorized user by given order_by and order' do
user = as_admin { setup_user 'view', 'hosts' }
old_host = as_admin { FactoryBot.create(:host, created_at: Time.zone.now - 2.days) }
new_host = as_admin { FactoryBot.create(:host, created_at: Time.zone.now) }
result = Queries::AuthorizedModelQuery.new(model_class: Host::Managed, user: user)
.results(order_by: 'created_at', order: 'desc')
assert_equal [new_host, old_host], result
end
end
end
test/graphql/queries/compute_attribute_query_test.rb
require 'test_helper'
class Queries::ComputeAttributeQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class ComputeAttributeQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
) {
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:compute_resource) { FactoryBot.create(:compute_resource, :vmware, uuid: 'Solutions') }
let(:compute_attribute) { compute_resource.compute_attributes.first }
let(:compute_resource) { FactoryBot.create(:compute_resource, :vmware, uuid: 'Solutions') }
let(:compute_attribute) { compute_resource.compute_attributes.first }
let(:global_id) { Foreman::GlobalId.for(compute_attribute) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['computeAttribute'] }
let(:global_id) { Foreman::GlobalId.for(compute_attribute) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['computeAttribute'] }
setup do
FactoryBot.create(:compute_profile, :with_compute_attribute, compute_resource: compute_resource)
end
setup do
FactoryBot.create(:compute_profile, :with_compute_attribute, compute_resource: compute_resource)
end
test 'fetching compute attribute attributes' do
assert_empty result['errors']
test 'fetching compute attribute attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal compute_attribute.created_at.utc.iso8601, data['createdAt']
assert_equal compute_attribute.updated_at.utc.iso8601, data['updatedAt']
assert_equal compute_attribute.name, data['name']
assert_equal global_id, data['id']
assert_equal compute_attribute.created_at.utc.iso8601, data['createdAt']
assert_equal compute_attribute.updated_at.utc.iso8601, data['updatedAt']
assert_equal compute_attribute.name, data['name']
assert_record compute_attribute.compute_resource, data['computeResource']
assert_record compute_attribute.compute_resource, data['computeResource']
end
end
end
test/graphql/queries/compute_attributes_query_test.rb
require 'test_helper'
class Queries::ComputeAttributesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class ComputeAttributesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
computeAttributes {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['computeAttributes'] }
let(:data) { result['data']['computeAttributes'] }
setup do
compute_resource = FactoryBot.create(:compute_resource, :vmware, uuid: 'Solutions')
FactoryBot.create(:compute_profile, :with_compute_attribute, compute_resource: compute_resource)
end
setup do
compute_resource = FactoryBot.create(:compute_resource, :vmware, uuid: 'Solutions')
FactoryBot.create(:compute_profile, :with_compute_attribute, compute_resource: compute_resource)
end
test 'fetching compute resources attributes' do
assert_empty result['errors']
test 'fetching compute resources attributes' do
assert_empty result['errors']
expected_count = ComputeAttribute.count
expected_count = ComputeAttribute.count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
end
end
end
test/graphql/queries/compute_resource_query_test.rb
require 'test_helper'
class Queries::ComputeResourceQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class ComputeResourceQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
) {
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:hosts) { FactoryBot.create_list(:host, 2) }
let(:compute_resource) { FactoryBot.create(:vmware_cr, uuid: 'Solutions', hosts: hosts) }
let(:hosts) { FactoryBot.create_list(:host, 2) }
let(:compute_resource) { FactoryBot.create(:vmware_cr, uuid: 'Solutions', hosts: hosts) }
let(:global_id) { Foreman::GlobalId.encode('ComputeResource', compute_resource.id) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['computeResource'] }
let(:global_id) { Foreman::GlobalId.encode('ComputeResource', compute_resource.id) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['computeResource'] }
setup do
RecordLoader.any_instance.expects(:load_by_global_id).returns(compute_resource)
Fog.mock!
FactoryBot.create(:compute_profile, :with_compute_attribute, compute_resource: compute_resource)
end
setup do
RecordLoader.any_instance.expects(:load_by_global_id).returns(compute_resource)
Fog.mock!
FactoryBot.create(:compute_profile, :with_compute_attribute, compute_resource: compute_resource)
end
teardown { Fog.unmock! }
teardown { Fog.unmock! }
test 'fetching compute resource attributes' do
assert_empty result['errors']
test 'fetching compute resource attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal compute_resource.created_at.utc.iso8601, data['createdAt']
assert_equal compute_resource.updated_at.utc.iso8601, data['updatedAt']
assert_equal compute_resource.name, data['name']
assert_equal compute_resource.description, data['description']
assert_equal compute_resource.url, data['url']
assert_equal compute_resource.provider, data['provider']
assert_equal compute_resource.provider_friendly_name, data['providerFriendlyName']
assert_equal global_id, data['id']
assert_equal compute_resource.created_at.utc.iso8601, data['createdAt']
assert_equal compute_resource.updated_at.utc.iso8601, data['updatedAt']
assert_equal compute_resource.name, data['name']
assert_equal compute_resource.description, data['description']
assert_equal compute_resource.url, data['url']
assert_equal compute_resource.provider, data['provider']
assert_equal compute_resource.provider_friendly_name, data['providerFriendlyName']
assert_collection compute_resource.compute_attributes, data['computeAttributes']
assert_collection compute_resource.hosts, data['hosts'], type_name: 'Host'
end
assert_collection compute_resource.compute_attributes, data['computeAttributes']
assert_collection compute_resource.hosts, data['hosts'], type_name: 'Host'
end
test 'fetching compute resource VMWare networks' do
assert compute_resource.networks.any?
assert_equal compute_resource.networks.count, data['networks']['totalCount']
assert_same_elements compute_resource.networks.map(&:id), data['networks']['edges'].map { |e| e['node']['id'] }
test 'fetching compute resource VMWare networks' do
assert compute_resource.networks.any?
assert_equal compute_resource.networks.count, data['networks']['totalCount']
assert_same_elements compute_resource.networks.map(&:id), data['networks']['edges'].map { |e| e['node']['id'] }
network = compute_resource.networks.first
edge = data['networks']['edges'].find { |e| e['node']['id'] == network.id }['node']
network = compute_resource.networks.first
edge = data['networks']['edges'].find { |e| e['node']['id'] == network.id }['node']
assert_equal 'Vmware', edge['__typename']
assert_equal network.id, edge['id']
assert_equal network.name, edge['name']
assert_equal network.virtualswitch, edge['virtualswitch']
assert_equal network.datacenter, edge['datacenter']
assert_equal network.accessible, edge['accessible']
assert_equal network.vlanid, edge['vlanid']
assert_equal 'Vmware', edge['__typename']
assert_equal network.id, edge['id']
assert_equal network.name, edge['name']
assert_equal network.virtualswitch, edge['virtualswitch']
assert_equal network.datacenter, edge['datacenter']
assert_equal network.accessible, edge['accessible']
assert_equal network.vlanid, edge['vlanid']
end
end
end
test/graphql/queries/compute_resources_query_test.rb
require 'test_helper'
class Queries::ComputeResourcesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class ComputeResourcesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
computeResources {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['computeResources'] }
let(:data) { result['data']['computeResources'] }
setup do
FactoryBot.create_list(:compute_resource, 2, :vmware, uuid: 'Solutions')
end
setup do
FactoryBot.create_list(:compute_resource, 2, :vmware, uuid: 'Solutions')
end
test 'fetching compute resources attributes' do
assert_empty result['errors']
test 'fetching compute resources attributes' do
assert_empty result['errors']
expected_count = ComputeResource.count
expected_count = ComputeResource.count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
end
end
end
test/graphql/queries/domain_query_test.rb
require 'test_helper'
class Queries::DomainQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class DomainQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
$subnetsLocation: String!
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:hosts) { FactoryBot.create_list(:host, 2) }
let(:subnet_location) { FactoryBot.create(:location) }
let(:expected_subnet) { FactoryBot.create(:subnet_ipv4, locations: [subnet_location]) }
let(:unexpected_subnet) { FactoryBot.create(:subnet_ipv4, locations: []) }
let(:domain) { FactoryBot.create(:domain, hosts: hosts, subnets: [expected_subnet, unexpected_subnet]) }
let(:hosts) { FactoryBot.create_list(:host, 2) }
let(:subnet_location) { FactoryBot.create(:location) }
let(:expected_subnet) { FactoryBot.create(:subnet_ipv4, locations: [subnet_location]) }
let(:unexpected_subnet) { FactoryBot.create(:subnet_ipv4, locations: []) }
let(:domain) { FactoryBot.create(:domain, hosts: hosts, subnets: [expected_subnet, unexpected_subnet]) }
let(:global_id) { Foreman::GlobalId.for(domain) }
let(:variables) {{ id: global_id, subnetsLocation: subnet_location.name }}
let(:data) { result['data']['domain'] }
let(:global_id) { Foreman::GlobalId.for(domain) }
let(:variables) {{ id: global_id, subnetsLocation: subnet_location.name }}
let(:data) { result['data']['domain'] }
test 'fetching domain attributes' do
assert_empty result['errors']
test 'fetching domain attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal domain.created_at.utc.iso8601, data['createdAt']
assert_equal domain.updated_at.utc.iso8601, data['updatedAt']
assert_equal domain.name, data['name']
assert_equal domain.fullname, data['fullname']
assert_equal global_id, data['id']
assert_equal domain.created_at.utc.iso8601, data['createdAt']
assert_equal domain.updated_at.utc.iso8601, data['updatedAt']
assert_equal domain.name, data['name']
assert_equal domain.fullname, data['fullname']
assert_collection [expected_subnet], data['subnets'], type_name: 'Subnet'
assert_collection domain.hosts, data['hosts'], type_name: 'Host'
assert_collection [expected_subnet], data['subnets'], type_name: 'Subnet'
assert_collection domain.hosts, data['hosts'], type_name: 'Host'
end
end
end
test/graphql/queries/domains_query_test.rb
require 'test_helper'
class Queries::DomainsQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class DomainsQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
domains {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['domains'] }
let(:data) { result['data']['domains'] }
setup do
FactoryBot.create_list(:domain, 2)
end
setup do
FactoryBot.create_list(:domain, 2)
end
test 'fetching domains attributes' do
assert_empty result['errors']
test 'fetching domains attributes' do
assert_empty result['errors']
expected_count = Domain.count
expected_count = Domain.count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
end
end
end
test/graphql/queries/environment_query_test.rb
require 'test_helper'
class Queries::EnvironmentQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class EnvironmentQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
) {
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:environment) { FactoryBot.create(:environment) }
let(:environment) { FactoryBot.create(:environment) }
let(:global_id) { Foreman::GlobalId.for(environment) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['environment'] }
let(:global_id) { Foreman::GlobalId.for(environment) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['environment'] }
test 'fetching environment attributes' do
assert_empty result['errors']
test 'fetching environment attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal environment.created_at.utc.iso8601, data['createdAt']
assert_equal environment.updated_at.utc.iso8601, data['updatedAt']
assert_equal environment.name, data['name']
assert_equal global_id, data['id']
assert_equal environment.created_at.utc.iso8601, data['createdAt']
assert_equal environment.updated_at.utc.iso8601, data['updatedAt']
assert_equal environment.name, data['name']
assert_collection environment.locations, data['locations']
assert_collection environment.organizations, data['organizations']
assert_collection environment.locations, data['locations']
assert_collection environment.organizations, data['organizations']
end
end
end
test/graphql/queries/environments_query_test.rb
require 'test_helper'
class Queries::EnvironmentsQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class EnvironmentsQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
environments {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['environments'] }
let(:data) { result['data']['environments'] }
setup do
FactoryBot.create_list(:environment, 2)
end
setup do
FactoryBot.create_list(:environment, 2)
end
test 'fetching environments attributes' do
assert_empty result['errors']
test 'fetching environments attributes' do
assert_empty result['errors']
expected_count = Environment.count
expected_count = Environment.count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
end
end
end
test/graphql/queries/fact_name_query_test.rb
require 'test_helper'
class Queries::FactNameQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class FactNameQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
) {
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:fact_value) { FactoryBot.create(:fact_value) }
let(:fact_name) { fact_value.fact_name }
let(:fact_value) { FactoryBot.create(:fact_value) }
let(:fact_name) { fact_value.fact_name }
let(:global_id) { Foreman::GlobalId.for(fact_name) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['factName'] }
let(:global_id) { Foreman::GlobalId.for(fact_name) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['factName'] }
test 'fetching fact name attributes' do
assert_empty result['errors']
test 'fetching fact name attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal fact_name.created_at.utc.iso8601, data['createdAt']
assert_equal fact_name.updated_at.utc.iso8601, data['updatedAt']
assert_equal fact_name.short_name, data['shortName']
assert_equal fact_name.type, data['type']
assert_equal global_id, data['id']
assert_equal fact_name.created_at.utc.iso8601, data['createdAt']
assert_equal fact_name.updated_at.utc.iso8601, data['updatedAt']
assert_equal fact_name.short_name, data['shortName']
assert_equal fact_name.type, data['type']
assert_collection fact_name.fact_values, data['factValues']
assert_collection fact_name.hosts, data['hosts'], type_name: 'Host'
assert_collection fact_name.fact_values, data['factValues']
assert_collection fact_name.hosts, data['hosts'], type_name: 'Host'
end
end
end
test/graphql/queries/fact_names_query_test.rb
require 'test_helper'
class Queries::FactNamesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class FactNamesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
factNames {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['factNames'] }
let(:data) { result['data']['factNames'] }
setup do
FactoryBot.create_list(:fact_name, 2)
end
setup do
FactoryBot.create_list(:fact_name, 2)
end
test 'fetching fact names attributes' do
assert_empty result['errors']
test 'fetching fact names attributes' do
assert_empty result['errors']
expected_count = FactName.count
expected_count = FactName.count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
end
end
end
test/graphql/queries/fact_value_query_test.rb
require 'test_helper'
class Queries::FactValueQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class FactValueQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
) {
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:fact_value) { FactoryBot.create(:fact_value) }
let(:fact_value) { FactoryBot.create(:fact_value) }
let(:global_id) { Foreman::GlobalId.for(fact_value) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['factValue'] }
let(:global_id) { Foreman::GlobalId.for(fact_value) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['factValue'] }
test 'fetching fact value attributes' do
assert_empty result['errors']
test 'fetching fact value attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal fact_value.created_at.utc.iso8601, data['createdAt']
assert_equal fact_value.updated_at.utc.iso8601, data['updatedAt']
assert_equal fact_value.value, data['value']
assert_equal global_id, data['id']
assert_equal fact_value.created_at.utc.iso8601, data['createdAt']
assert_equal fact_value.updated_at.utc.iso8601, data['updatedAt']
assert_equal fact_value.value, data['value']
assert_record fact_value.fact_name, data['factName']
assert_record fact_value.host, data['host'], type_name: 'Host'
assert_record fact_value.fact_name, data['factName']
assert_record fact_value.host, data['host'], type_name: 'Host'
end
end
end
test/graphql/queries/fact_values_query_test.rb
require 'test_helper'
class Queries::FactValuesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class FactValuesQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
factValues {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['factValues'] }
let(:data) { result['data']['factValues'] }
setup do
FactoryBot.create_list(:fact_value, 2)
end
setup do
FactoryBot.create_list(:fact_value, 2)
end
test 'fetching fact values attributes' do
assert_empty result['errors']
test 'fetching fact values attributes' do
assert_empty result['errors']
expected_count = FactValue.count
expected_count = FactValue.count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
end
end
end
test/graphql/queries/host_query_test.rb
require 'test_helper'
class Queries::HostQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class HostQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
) {
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:hostgroup) { FactoryBot.create(:hostgroup, :with_compute_resource) }
let(:host) do
FactoryBot.create(:host, :managed,
:with_environment,
:with_model,
:with_facts,
:with_puppet,
:with_puppet_ca,
hostgroup: hostgroup,
uuid: Foreman.uuid,
last_report: Time.now)
end
let(:global_id) { Foreman::GlobalId.encode('Host', host.id) }
let(:variables) { { id: Foreman::GlobalId.encode('Host', host.id) } }
let(:data) { result['data']['host'] }
let(:hostgroup) { FactoryBot.create(:hostgroup, :with_compute_resource) }
let(:host) do
FactoryBot.create(:host, :managed,
:with_environment,
:with_model,
:with_facts,
:with_puppet,
:with_puppet_ca,
hostgroup: hostgroup,
uuid: Foreman.uuid,
last_report: Time.now)
end
let(:global_id) { Foreman::GlobalId.encode('Host', host.id) }
let(:variables) { { id: Foreman::GlobalId.encode('Host', host.id) } }
let(:data) { result['data']['host'] }
test 'fetching host attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal host.created_at.utc.iso8601, data['createdAt']
assert_equal host.updated_at.utc.iso8601, data['updatedAt']
assert_equal host.name, data['name']
assert_equal host.build, data['build']
assert_equal host.ip, data['ip']
assert_equal host.ip6, data['ip6']
assert_equal Rails.application.routes.url_helpers.host_path(host), data['path']
assert_equal host.mac, data['mac']
assert_equal host.last_report.utc.iso8601, data['lastReport']
assert_equal host.domain_name, data['domainName']
assert_equal host.pxe_loader, data['pxeLoader']
assert_equal host.enabled, data['enabled']
assert_equal host.uuid, data['uuid']
test 'fetching host attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal host.created_at.utc.iso8601, data['createdAt']
assert_equal host.updated_at.utc.iso8601, data['updatedAt']
assert_equal host.name, data['name']
assert_equal host.build, data['build']
assert_equal host.ip, data['ip']
assert_equal host.ip6, data['ip6']
assert_equal Rails.application.routes.url_helpers.host_path(host), data['path']
assert_equal host.mac, data['mac']
assert_equal host.last_report.utc.iso8601, data['lastReport']
assert_equal host.domain_name, data['domainName']
assert_equal host.pxe_loader, data['pxeLoader']
assert_equal host.enabled, data['enabled']
assert_equal host.uuid, data['uuid']
assert_record host.environment, data['environment']
assert_record host.compute_resource, data['computeResource'], type_name: 'ComputeResource'
assert_record host.architecture, data['architecture']
assert_record host.ptable, data['ptable']
assert_record host.domain, data['domain']
assert_record host.location, data['location']
assert_record host.model, data['model']
assert_record host.operatingsystem, data['operatingsystem']
assert_record host.puppet_ca_proxy, data['puppetCaProxy']
assert_record host.puppet_proxy, data['puppetProxy']
assert_record host.medium, data['medium']
assert_record host.environment, data['environment']
assert_record host.compute_resource, data['computeResource'], type_name: 'ComputeResource'
assert_record host.architecture, data['architecture']
assert_record host.ptable, data['ptable']
assert_record host.domain, data['domain']
assert_record host.location, data['location']
assert_record host.model, data['model']
assert_record host.operatingsystem, data['operatingsystem']
assert_record host.puppet_ca_proxy, data['puppetCaProxy']
assert_record host.puppet_proxy, data['puppetProxy']
assert_record host.medium, data['medium']
assert_collection host.fact_names, data['factNames']
assert_collection host.fact_values, data['factValues']
end
assert_collection host.fact_names, data['factNames']
assert_collection host.fact_values, data['factValues']
end
context 'with user without view_models permission' do
let(:context_user) { setup_user 'view', 'hosts' }
context 'with user without view_models permission' do
let(:context_user) { setup_user 'view', 'hosts' }
test 'does not load associated model' do
assert_empty result['errors']
test 'does not load associated model' do
assert_empty result['errors']
assert_nil data['model']
assert_nil data['model']
end
end
end
context 'with user without view_hosts permission' do
let(:context_user) { setup_user 'view', 'models' }
context 'with user without view_hosts permission' do
let(:context_user) { setup_user 'view', 'models' }
test 'does not load any hosts' do
assert_empty result['errors']
test 'does not load any hosts' do
assert_empty result['errors']
assert_nil data
assert_nil data
end
end
end
end
test/graphql/queries/hosts_query_test.rb
require 'test_helper'
class Queries::HostsQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class HostsQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
hosts {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['hosts'] }
let(:data) { result['data']['hosts'] }
setup do
FactoryBot.create_list(:host, 2, :managed)
end
setup do
FactoryBot.create_list(:host, 2, :managed)
end
test 'fetching hosts attributes' do
assert_empty result['errors']
test 'fetching hosts attributes' do
assert_empty result['errors']
expected_count = Host.count
expected_count = Host.count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
assert_not_equal 0, expected_count
assert_equal expected_count, data['totalCount']
assert_equal expected_count, data['edges'].count
end
end
end
test/graphql/queries/location_query_test.rb
require 'test_helper'
class Queries::LocationQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class LocationQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query (
$id: String!
) {
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:environment) { FactoryBot.create(:environment) }
let(:hosts) { FactoryBot.create_list(:host, 2) }
let(:location_object) { FactoryBot.create(:location, hosts: hosts, environments: [environment]) }
let(:environment) { FactoryBot.create(:environment) }
let(:hosts) { FactoryBot.create_list(:host, 2) }
let(:location_object) { FactoryBot.create(:location, hosts: hosts, environments: [environment]) }
let(:global_id) { Foreman::GlobalId.for(location_object) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['location'] }
let(:global_id) { Foreman::GlobalId.for(location_object) }
let(:variables) {{ id: global_id }}
let(:data) { result['data']['location'] }
setup do
FactoryBot.create(:puppetclass, :environments => [environment])
end
setup do
FactoryBot.create(:puppetclass, :environments => [environment])
end
test 'fetching location attributes' do
assert_empty result['errors']
test 'fetching location attributes' do
assert_empty result['errors']
assert_equal global_id, data['id']
assert_equal location_object.created_at.utc.iso8601, data['createdAt']
assert_equal location_object.updated_at.utc.iso8601, data['updatedAt']
assert_equal location_object.name, data['name']
assert_equal location_object.title, data['title']
assert_equal global_id, data['id']
assert_equal location_object.created_at.utc.iso8601, data['createdAt']
assert_equal location_object.updated_at.utc.iso8601, data['updatedAt']
assert_equal location_object.name, data['name']
assert_equal location_object.title, data['title']
assert_collection location_object.environments, data['environments']
assert_collection location_object.puppetclasses, data['puppetclasses']
assert_collection location_object.hosts, data['hosts'], type_name: 'Host'
assert_collection location_object.environments, data['environments']
assert_collection location_object.puppetclasses, data['puppetclasses']
assert_collection location_object.hosts, data['hosts'], type_name: 'Host'
end
end
end
test/graphql/queries/locations_query_test.rb
require 'test_helper'
class Queries::LocationsQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
module Queries
class LocationsQueryTest < GraphQLQueryTestCase
let(:query) do
<<-GRAPHQL
query {
locations {
totalCount
......
}
}
}
GRAPHQL
end
GRAPHQL
end
let(:data) { result['data']['locations'] }
let(:data) { result['data']['locations'] }
setup do
FactoryBot.create_list(:location, 2)
end
setup do
FactoryBot.create_list(:location, 2)
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff