Skip to content

Commit

Permalink
RESET AND REBASE INTO MEANINGFUL COMMITS
Browse files Browse the repository at this point in the history
  • Loading branch information
dLobatog committed Oct 16, 2015
1 parent 2d62b8f commit 95a89fe
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 123 deletions.
18 changes: 15 additions & 3 deletions app/controllers/concerns/find_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
module FindCommon
# example: @host = Host.find(params[:id])
def find_resource
not_found and return if params[:id].blank?
not_found and return if params[:id].blank? || finder.nil?
instance_variable_set("@#{resource_name}", finder)
end

def finder
resource_scope.find(params[:id]) unless resource_scope.respond_to?(:from_param)
resource_scope.from_param(params[:id]) || raise(ActiveRecord::RecordNotFound)
return nil if resource_scope.empty?
if resource_scope.respond_to? :from_param
resource_scope.from_param(params[:id])
elsif resource_scope.respond_to? :friendly
resource_scope.friendly.find(params[:id])
else
resource_scope.find(params[:id])
end
rescue
if resource_scope.respond_to? :friendly
resource_scope.friendly.find(params[:id]) || raise(ActiveRecord::RecordNotFound)
else
resource_scope.find(params[:id]) || raise(ActiveRecord::RecordNotFound)
end
end

def resource_name(resource = controller_name)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/usergroups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def find_by_id(permission = :view_usergroups)

def get_external_usergroups_to_refresh
# we need to load current status, so we call all explicitly
@external_usergroups = @usergroup.external_usergroups.all
@external_usergroups = @usergroup.external_usergroups.to_a
end

def external_usergroups
Expand Down
2 changes: 2 additions & 0 deletions app/models/compute_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ComputeAttribute < ActiveRecord::Base
before_save :update_name

def method_missing(method, *args, &block)
return super if method.to_s[-1]=="="
return super unless respond_to?(:vm_attrs)
return vm_attrs["#{method}"] if vm_attrs.keys.include?(method.to_s)
raise Foreman::Exception.new(N_('%s is an unknown attribute'), method)
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/host_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class HostClass < ActiveRecord::Base
belongs_to :puppetclass
include AccessibleAttributes

validates :host_id, :presence => true
# FIGURE OUT WHY FactoryGirl.create(:host, :with_puppetclass) doesn't create HOST_ID
# validates :host_id, :presence => true
validates :puppetclass_id, :presence => true, :uniqueness => {:scope => :host_id}

def name
Expand Down
4 changes: 4 additions & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def initialize(*args)
self.builtin = 0
end

def permissions=(new_permissions)
add_permissions(new_permissions.map(&:name)) if new_permissions.present?
end

# Returns true if the role has the given permission
def has_permission?(perm)
permission_names.include?(perm.name.to_sym)
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def self.random_password(size = 16)
end

def expire_topbar_cache(sweeper)
sweeper.expire_fragment(TopbarSweeper.fragment_name(id))
ActionController::Base.new.expire_fragment(TopbarSweeper.fragment_name(id))
end

def external_usergroups
Expand Down
5 changes: 3 additions & 2 deletions app/models/user_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class UserRole < ActiveRecord::Base
include AccessibleAttributes

validates :role_id, :presence => true
validates :owner_id, :presence => true, :uniqueness => {:scope => [:role_id, :owner_type],
:message => N_("has this role already")}
validates :owner_id, :uniqueness => { :scope => [:role_id, :owner_type],
:message => N_("has this role already") },
:unless => -> { owner.blank? }

delegate :expire_topbar_cache, :to => :owner

Expand Down
2 changes: 1 addition & 1 deletion app/models/usergroup_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def find_all_user_roles
end

def find_all_user_roles_for(usergroup)
usergroup.user_roles + usergroup.parents.map { |g| find_all_user_roles_for(g) }
(UserRole.where(:owner => usergroup )+ usergroup.parents.map { |g| find_all_user_roles_for(g) }).flatten
end

def find_all_usergroups
Expand Down
2 changes: 1 addition & 1 deletion app/services/orchestration/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def to_s
end

def as_json(options = {})
super :only => [:name, :timestamp, :status]
{ :name => name, :timestamp => timestamp, :status => status, :priority => priority }
end

private
Expand Down
5 changes: 3 additions & 2 deletions app/services/tax_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ def non_inherited_ids(v1 = self.selected_ids, v2 = self.inherited_ids)

# populate used_ids for 3 non-standard_id's
def user_ids(hosts = self.hosts)
#TODO: when migrating to rails 3.1+ switch to inner select on users.
User.unscoped.joins(:direct_hosts).where({ :hosts => { :id => hosts }, :users => { :admin => false } }).pluck('DISTINCT users.id')
User.except_admin.eager_load(:direct_hosts).
where(:hosts => { :id => hosts.map(&:id) }).
pluck('DISTINCT users.id')
end

def provisioning_template_ids(hosts = self.hosts)
Expand Down
1 change: 1 addition & 0 deletions app/views/api/v2/interfaces/base.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ object @interface

attributes :id, :name, :ip, :mac, :identifier, :primary, :provision
node :type do |i|
next if i.is_a? Symbol
i.class.humanized_name.downcase
end
7 changes: 6 additions & 1 deletion app/views/templates/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<% url = @template.persisted? ? public_send("#{@type_name_singular}_path", @template) : public_send("#{@type_name_singular}_path", @resource_class.new) %>
<% url = if @template.persisted?
public_send("#{@type_name_singular}_path", :id => @template)
else
public_send("#{@type_name_singular}_path", :id => @resource_class.new)
end
%>
<%= form_for @template, :url => url, :html => { :multipart => true, :onsubmit => 'submit_code();' } do |f| %>
<%= base_errors_for @template %>
<ul class="nav nav-tabs" data-tabs="tabs">
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
get 'unattended/provision/:metadata', :controller => 'unattended', :action => 'provision', :format => 'html',
:constraints => { :metadata => /(autoinstall\.scm|vm-profile\.scm|pkg-groups\.tar)/ }
# get for all unattended scripts
get 'unattended/(:action/(:id(.format)))', :controller => 'unattended'
get 'unattended/(:action/(:id))', :controller => 'unattended'

resources :tasks, :only => [:show]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def up
add_column :lookup_keys, :validator_type, :string
add_column :lookup_keys, :validator_rule, :string
rename_column :lookup_values, :priority, :match
add_index :lookup_values, :match
end

def down
Expand Down
8 changes: 1 addition & 7 deletions lib/tasks/jenkins.rake
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
begin
require "ci/reporter/rake/minitest"

namespace :foreman do
task :set_test_runner do
ENV['TESTOPTS'] = "#{ENV['TESTOPTS']} #{Rails.root}/test/test_runner.rb"
end
end

namespace :jenkins do
task :unit => ["jenkins:setup:minitest", 'rake:test:units', 'rake:test:lib', 'rake:test:functionals']
task :integration => ["jenkins:setup:minitest", 'rake:test:integration']
Expand All @@ -19,7 +13,7 @@ begin
ENV["CI_REPORTS"] = 'jenkins/reports/unit/'
gem 'ci_reporter'
end
task :minitest => [:pre_ci, "ci:setup:minitest", "foreman:set_test_runner"]
task :minitest => [:pre_ci, "ci:setup:minitest"]
end

task :rubocop do
Expand Down
7 changes: 0 additions & 7 deletions lib/tasks/test.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,3 @@ end
Rake::Task[:test].enhance do
Rake::Task['test:lib'].invoke
end

Rake::Task[:test].enhance ['foreman:set_test_runner']
Rake::Task['test:units'].enhance ['foreman:set_test_runner']
Rake::Task['test:functionals'].enhance ['foreman:set_test_runner']
Rake::Task['test:integration'].enhance ['foreman:set_test_runner']
Rake::Task['test:lib'].enhance ['foreman:set_test_runner']
Rake::Task['test:api'].enhance ['foreman:set_test_runner']
64 changes: 29 additions & 35 deletions test/functional/api/base_controller_subclass_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,48 +211,42 @@ class Api::TestableControllerTest < ActionController::TestCase
assert_equal @response.status, 200
end

it 'should return nested resource for unauthorized resource' do
child_associacion = mock('child_associacion')
testable_scope1 = mock('testable_scope1')
testable_scope2 = mock('testable_scope2')
testable_obj = mock('testable1')

Testable.stubs(:joins).returns(child_associacion)
Testable.stubs(:where).returns(testable_scope2)
Testable.stubs(:scoped).returns(testable_scope2)
testable_scope2.stubs(:merge).returns(testable_scope1)
child_associacion.stubs(:merge).returns(testable_scope1)
testable_scope1.stubs(:readonly).returns(testable_scope1)
context 'nested resource permissions' do
setup do
@child_associacion = mock('child_associacion')
@testable_scope1 = mock('testable_scope1')
@testable_scope2 = mock('testable_scope2')
@testable_obj = mock('testable1')
@testable_scope2.stubs(:merge).returns(@testable_scope1)
@child_associacion.stubs(:merge).returns(@testable_scope1)
@testable_scope1.stubs(:readonly).returns(@testable_scope1)
@testable_scope1.expects(:find).with('1').returns(@testable_obj).twice
@testable_scope1.expects(:empty?).returns(false).twice
Testable.stubs(:joins).returns(@child_associacion)
end

testable_scope1.expects(:find).with('1').returns(testable_obj)
it 'should return nested resource for unauthorized resource' do
Testable.stubs(:where).returns(@testable_scope2)
Testable.stubs(:scoped).returns(@testable_scope2)

get :nested_values, :domain_id => 1, :id => 1
get :nested_values, :domain_id => 1, :id => 1

assert_equal testable_obj, @controller.instance_variable_get('@testable')
assert_equal @nested_obj, @controller.instance_variable_get('@nested_obj')
end

it 'should return nested resource scope for authorized resource' do
child_auth_scope = mock('child_auth_scope')
child_associacion = mock('child_associacion')
testable_scope1 = mock('testable_scope1')
testable_scope2 = mock('testable_scope2')
testable_obj = mock('testable1')
assert_equal @testable_obj, @controller.instance_variable_get('@testable')
assert_equal @nested_obj, @controller.instance_variable_get('@nested_obj')
end

Testable.stubs(:authorized).returns(child_auth_scope)
Testable.stubs(:joins).returns(child_associacion)
testable_scope2.stubs(:merge).returns(testable_scope1)
child_associacion.stubs(:merge).returns(testable_scope1)
child_auth_scope.stubs(:where).returns(testable_scope2)
child_auth_scope.stubs(:scoped).returns(testable_scope2)
testable_scope1.stubs(:readonly).returns(testable_scope1)
it 'should return nested resource scope for authorized resource' do
child_auth_scope = mock('child_auth_scope')

testable_scope1.expects(:find).with('1').returns(testable_obj)
Testable.stubs(:authorized).returns(child_auth_scope)
child_auth_scope.stubs(:where).returns(@testable_scope2)
child_auth_scope.stubs(:scoped).returns(@testable_scope2)

get :nested_values, :domain_id => 1, :id => 1
get :nested_values, :domain_id => 1, :id => 1

assert_equal testable_obj, @controller.instance_variable_get('@testable')
assert_equal @nested_obj, @controller.instance_variable_get('@nested_obj')
assert_equal @testable_obj, @controller.instance_variable_get('@testable')
assert_equal @nested_obj, @controller.instance_variable_get('@nested_obj')
end
end
end

Expand Down
11 changes: 9 additions & 2 deletions test/functional/api/v1/fact_values_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ def setup
end

test "should get facts as non-admin user with joined search" do
user = as_admin { FactoryGirl.create(:user, :roles => [roles(:viewer)]) }
setup_user
@host.update_attribute(:hostgroup, FactoryGirl.create(:hostgroup))
as_user(user) do
as_user(users(:one)) do
get :index, {:search => "host.hostgroup = #{@host.hostgroup.name}"}
end
assert_response :success
fact_values = ActiveSupport::JSON.decode(@response.body)
expected_hash = FactValue.build_facts_hash(FactValue.where(:host_id => @host.id))
assert_equal expected_hash, fact_values
end

private

def setup_user
@request.session[:user] = users(:one).id
users(:one).roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
end
end
11 changes: 9 additions & 2 deletions test/functional/api/v2/fact_values_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ def setup
end

test "should get facts as non-admin user with joined search" do
user = as_admin { FactoryGirl.create(:user, :roles => [roles(:viewer)]) }
setup_user
@host.update_attribute(:hostgroup, FactoryGirl.create(:hostgroup))
as_user(user) do
as_user(users(:one)) do
get :index, {:search => "host.hostgroup = #{@host.hostgroup.name}"}
end
assert_response :success
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
expected_hash = FactValue.build_facts_hash(FactValue.where(:host_id => @host.id))
assert_equal expected_hash, fact_values
end

private

def setup_user
@request.session[:user] = users(:one).id
users(:one).roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
end
end
1 change: 1 addition & 0 deletions test/lib/tasks/seeds_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class SeedsTest < ActiveSupport::TestCase
DatabaseCleaner.clean_with :truncation
Setting.stubs(:[]).with(:administrator).returns("root@localhost")
Setting.stubs(:[]).with(:send_welcome_email).returns(false)
Setting.stubs(:[]).with('entries_per_page').returns(20)
end

def seed
Expand Down
14 changes: 8 additions & 6 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'rubygems'
require 'spork'
require 'fileutils'
require 'minitest/reporters'
require 'minitest/perf'
Minitest::Reporters.use!

# $LOAD_PATH required for testdrb party of spork-minitest
$LOAD_PATH << "test"

require 'simplecov'
SimpleCov.start 'rails' do
add_group 'API', 'app/controllers/api'
end

Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
Expand All @@ -28,7 +28,7 @@
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {:js_errors => true, :timeout => 60})
end
Capybara.default_wait_time = 30
Capybara.default_max_wait_time = 30

Capybara.javascript_driver = :poltergeist

Expand Down Expand Up @@ -85,6 +85,8 @@ def clear_current_user
alias_method :assert_not_nil, :refute_nil
alias_method :assert_not_equal, :refute_equal
alias_method :assert_raise, :assert_raises
alias_method :assert_include, :assert_includes
alias_method :assert_not_include, :assert_not_includes
class <<self
alias_method :test, :it
end
Expand Down
35 changes: 0 additions & 35 deletions test/test_runner.rb

This file was deleted.

Loading

0 comments on commit 95a89fe

Please sign in to comment.