Best Inspec_ruby code snippet using UserManagementSelector.select_user_manager
users.rb
Source:users.rb
...11 # specific users with certain properties, use the `users` resource.12 module UserManagementSelector13 # select user provider based on the operating system14 # returns nil, if no user manager was found for the operating system15 def select_user_manager(os)16 if os.linux?17 LinuxUser.new(inspec)18 elsif os.windows?19 WindowsUser.new(inspec)20 elsif ['darwin'].include?(os[:family])21 DarwinUser.new(inspec)22 elsif ['freebsd'].include?(os[:family])23 FreeBSDUser.new(inspec)24 elsif ['aix'].include?(os[:family])25 AixUser.new(inspec)26 elsif os.solaris?27 SolarisUser.new(inspec)28 elsif ['hpux'].include?(os[:family])29 HpuxUser.new(inspec)30 end31 end32 end33 # The InSpec users resources looksup all local users available on a system.34 # TODO: the current version of the users resource will use eg. /etc/passwd35 # on Linux to parse all usernames. Therefore the resource may not return36 # users managed on other systems like LDAP/ActiveDirectory. Please open37 # a feature request at https://github.com/chef/inspec if you need that38 # functionality39 #40 # This resource allows complex filter mechanisms41 #42 # describe users.where(uid: 0).entries do43 # it { should eq ['root'] }44 # its('uids') { should eq [1234] }45 # its('gids') { should eq [1234] }46 # end47 #48 # describe users.where { uid =~ /S\-1\-5\-21\-\d+\-\d+\-\d+\-500/ } do49 # it { should exist }50 # end51 class Users < Inspec.resource(1)52 include UserManagementSelector53 name 'users'54 desc 'Use the users InSpec audit resource to test local user profiles. Users can be filtered by groups to which they belong, the frequency of required password changes, the directory paths to home and shell.'55 example "56 describe users.where(uid: 0).entries do57 it { should eq ['root'] }58 its('uids') { should eq [1234] }59 its('gids') { should eq [1234] }60 end61 "62 def initialize63 # select user provider64 @user_provider = select_user_manager(inspec.os)65 return skip_resource 'The `users` resource is not supported on your OS yet.' if @user_provider.nil?66 end67 filter = FilterTable.create68 filter.add_accessor(:where)69 .add_accessor(:entries)70 .add(:usernames, field: :username)71 .add(:uids, field: :uid)72 .add(:gids, field: :gid)73 .add(:groupnames, field: :groupname)74 .add(:groups, field: :groups)75 .add(:homes, field: :home)76 .add(:shells, field: :shell)77 .add(:mindays, field: :mindays)78 .add(:maxdays, field: :maxdays)79 .add(:warndays, field: :warndays)80 .add(:disabled, field: :disabled)81 .add(:exists?) { |x| !x.entries.empty? }82 .add(:disabled?) { |x| x.where { disabled == false }.entries.empty? }83 .add(:enabled?) { |x| x.where { disabled == true }.entries.empty? }84 filter.connect(self, :collect_user_details)85 def to_s86 'Users'87 end88 private89 # method to get all available users90 def list_users91 @username_cache ||= @user_provider.list_users unless @user_provider.nil?92 end93 # collects information about every user94 def collect_user_details95 @users_cache ||= @user_provider.collect_user_details unless @user_provider.nil?96 end97 end98 # The `user` resource handles the special case where only one resource is required99 #100 # describe user('root') do101 # it { should exist }102 # its('uid') { should eq 0 }103 # its('gid') { should eq 0 }104 # its('group') { should eq 'root' }105 # its('groups') { should eq ['root', 'wheel']}106 # its('home') { should eq '/root' }107 # its('shell') { should eq '/bin/bash' }108 # its('mindays') { should eq 0 }109 # its('maxdays') { should eq 99 }110 # its('warndays') { should eq 5 }111 # end112 #113 # The following Serverspec matchers are deprecated in favor for direct value access114 #115 # describe user('root') do116 # it { should belong_to_group 'root' }117 # it { should have_uid 0 }118 # it { should have_home_directory '/root' }119 # it { should have_login_shell '/bin/bash' }120 # its('minimum_days_between_password_change') { should eq 0 }121 # its('maximum_days_between_password_change') { should eq 99 }122 # end123 #124 # ServerSpec tests that are not supported:125 #126 # describe user('root') do127 # it { should have_authorized_key 'ssh-rsa ADg54...3434 user@example.local' }128 # its(:encrypted_password) { should eq 1234 }129 # end130 class User < Inspec.resource(1)131 include UserManagementSelector132 name 'user'133 desc 'Use the user InSpec audit resource to test user profiles, including the groups to which they belong, the frequency of required password changes, the directory paths to home and shell.'134 example "135 describe user('root') do136 it { should exist }137 its('uid') { should eq 1234 }138 its('gid') { should eq 1234 }139 end140 "141 def initialize(username = nil)142 @username = username143 # select user provider144 @user_provider = select_user_manager(inspec.os)145 return skip_resource 'The `user` resource is not supported on your OS yet.' if @user_provider.nil?146 end147 def exists?148 !identity.nil? && !identity[:username].nil?149 end150 def disabled?151 identity[:disabled] == true unless identity.nil?152 end153 def enabled?154 identity[:disabled] == false unless identity.nil?155 end156 def username157 identity[:username] unless identity.nil?158 end...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!