<commit_before>#
# Cookbook Name:: sys
# File:: test/unit/recipes/pam_spec.rb
#
# Copyright 2015-2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
#
# Authors:
#  <NAME> <<EMAIL>>
#  <NAME> <<EMAIL>>
#  <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

describe 'sys::pam' do

  let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe) }

  context 'node.sys.pam is empty' do
    it 'does nothing' do
      expect(chef_run.run_context.resource_collection
               .to_hash.keep_if { |x| x['updated'] }).to be_empty
    end
  end

  context 'with basic attributes' do
    cached(:chef_run) do
      ChefSpec::SoloRunner.new do |node|
        fqdn = 'node.example.com'
        node.default['sys']['pam']['rules'] = %w(rule_1 rule_2 rule_3)
        node.default['sys']['pam']['access'] = %w(access_1 access_2 access_3)
        node.default['sys']['pamd']['sshd'] = "sshd_1\nsshd_2\nsshd_3"
        node.default['sys']['pamd']['login'] = "login_1\nlogin_2\nlogin_3"
        node.default['sys']['pam']['limits'] = %w(limit_1 limit_2 limit_3)
        node.default['sys']['pam']['group'] = [{}, {}, {}]
        node.automatic['fqdn'] = fqdn
        node.automatic['domain'] = "example.com"
      end.converge(described_recipe)
    end

    it 'manages /etc/security/access.conf' do
      expect(chef_run).to create_template('/etc/security/access.conf').with_mode('0600').with(
        :variables => {
          rules: %w(access_1 access_2 access_3),
          default: nil
        }
      )

      expect(chef_run).to render_file('/etc/security/access.conf').with_content(
        "access_1\naccess_2\naccess_3"
      )
    end

    it 'manages /etc/pam.d/sshd' do
      expect(chef_run).to create_template('/etc/pam.d/sshd').with_mode('0644')
    end

    it 'manages /etc/pam.d/login' do
      expect(chef_run).to create_template('/etc/pam.d/login').with_mode('0644')
    end

    it 'manages /etc/security/limits.conf' do
      expect(chef_run).to create_template('/etc/security/limits.conf').with_mode('0644').with(
        :variables => {
          :rules => %w(limit_1 limit_2 limit_3)
        }
      )

      expect(chef_run).to render_file('/etc/security/limits.conf').with_content(
        "limit_1\nlimit_2\nlimit_3"<commit_msg>Adapt test to changed file permissions for /etc/security/access.conf<commit_after>#
# Cookbook Name:: sys
# File:: test/unit/recipes/pam_spec.rb
#
# Copyright 2015-2020 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
#
# Authors:
#  <NAME> <<EMAIL>>
#  <NAME>     <<EMAIL>>
#  <NAME>  <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

describe 'sys::pam' do

  let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe) }

  context 'node.sys.pam is empty' do
    it 'does nothing' do
      expect(chef_run.run_context.resource_collection
               .to_hash.keep_if { |x| x['updated'] }).to be_empty
    end
  end

  context 'with basic attributes' do
    cached(:chef_run) do
      ChefSpec::SoloRunner.new do |node|
        fqdn = 'node.example.com'
        node.default['sys']['pam']['rules'] = %w(rule_1 rule_2 rule_3)
        node.default['sys']['pam']['access'] = %w(access_1 access_2 access_3)
        node.default['sys']['pamd']['sshd'] = "sshd_1\nsshd_2\nsshd_3"
        node.default['sys']['pamd']['login'] = "login_1\nlogin_2\nlogin_3"
        node.default['sys']['pam']['limits'] = %w(limit_1 limit_2 limit_3)
        node.default['sys']['pam']['group'] = [{}, {}, {}]
        node.automatic['fqdn'] = fqdn
        node.automatic['domain'] = "example.com"
      end.converge(described_recipe)
    end

    it 'manages /etc/security/access.conf' do
      expect(chef_run).to create_template('/etc/security/access.conf')
                            .with_mode('0640')
                            .with(variables: {
                                    rules: %w(access_1 access_2 access_3),
                                    default: nil
                                  }
                                 )

      expect(chef_run).to render_file('/etc/security/access.conf').with_content(
        "access_1\naccess_2\naccess_3"
      )
    end

    it 'manages /etc/pam.d/sshd' do
      expect(chef_run).to create_template('/etc/pam.d/sshd').with_mode('0644')
    end

    it 'manages /etc/pam.d/login' do
      expect(chef_run).to create_template('/etc/pam.d/login').with_mode('0644')
    end

    it 'manages /etc/security/limits.conf' do
      expect(chef_run).to create_template('/etc/security/limits.conf').with_mode('0644').with(
        :variables => {
          :rules => %w(limit_1 limit_2 limit_3)
        }
      )

      expect(chef_run).to render_file('/etc/security/limits.conf').with_content(
        "limit_1\nlimit_2\nlimit_3"
{'fraction_non_alphanumeric': 0.14243529616119507, 'fraction_numerical': 0.019107173875282265, 'mean_word_length': 2.9010840108401084, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '641412', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1902, 'n_words': 407}