<commit_before>require 'test_helper'

class TestSecurity < Minitest::Test

  include TestHelper

  # When solita_jenkins_security_realm is set to "none", Jenkins is unsecured.
  def test_realm_none
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      vars:
        solita_jenkins_security_realm: none
      roles:
        - solita.jenkins
    EOF
    open_dashboard
    assert can_manage_jenkins?
  end

  # When solita_jenkins_security_realm is set to "jenkins", Jenkins is secured.
  def test_realm_jenkins
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      vars:
        solita_jenkins_security_realm: jenkins
      roles:
        - solita.jenkins
    EOF
    assert_forbidden { open_dashboard }
  end

  # When solita_jenkins_security_realm is undefined, an unsecured Jenkins
  # remains unsecured.
  def test_realm_undefined_unsecured
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      vars:
        solita_jenkins_security_realm: none
      roles:
        - solita.jenkins
    EOF
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      roles:
        - solita.jenkins
    EOF
    open_dashboard
    assert can_manage_jenkins?
  end

  # When solita_jenkins_security_realm is undefined, a secured Jenkins remains
  # secured.
  def test_realm_undefined_secured
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      vars:
        solita_jenkins_security_realm: jenkins
      roles:
        - solita.jenkins
    EOF
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      roles:
        - solita.jenkins
    EOF
    assert_forbidden { open_dashboard }
  end

  # It's an error to use user variables when solita_jenkins_security_realm is
  # not jenkins.
  def test_users_with_wrong_security_realm
    assert_raises do
      ansible_playbook '--tags solita_jenkins_security', <<-EOF
      ---
      - hosts: vagrant
        vars:
          solita_jenkins_users:
            - foo<commit_msg>TestSecurity: Run playbook that is expected to fail silently<commit_after>require 'test_helper'

class TestSecurity < Minitest::Test

  include TestHelper

  # When solita_jenkins_security_realm is set to "none", Jenkins is unsecured.
  def test_realm_none
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      vars:
        solita_jenkins_security_realm: none
      roles:
        - solita.jenkins
    EOF
    open_dashboard
    assert can_manage_jenkins?
  end

  # When solita_jenkins_security_realm is set to "jenkins", Jenkins is secured.
  def test_realm_jenkins
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      vars:
        solita_jenkins_security_realm: jenkins
      roles:
        - solita.jenkins
    EOF
    assert_forbidden { open_dashboard }
  end

  # When solita_jenkins_security_realm is undefined, an unsecured Jenkins
  # remains unsecured.
  def test_realm_undefined_unsecured
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      vars:
        solita_jenkins_security_realm: none
      roles:
        - solita.jenkins
    EOF
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      roles:
        - solita.jenkins
    EOF
    open_dashboard
    assert can_manage_jenkins?
  end

  # When solita_jenkins_security_realm is undefined, a secured Jenkins remains
  # secured.
  def test_realm_undefined_secured
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      vars:
        solita_jenkins_security_realm: jenkins
      roles:
        - solita.jenkins
    EOF
    ansible_playbook '--tags solita_jenkins_security', <<-EOF
    ---
    - hosts: vagrant
      roles:
        - solita.jenkins
    EOF
    assert_forbidden { open_dashboard }
  end

  # It's an error to use user variables when solita_jenkins_security_realm is
  # not jenkins.
  def test_users_with_wrong_security_realm
    assert_raises do
      ansible_playbook '--tags solita_jenkins_security', <<-EOF, :silent => true
      ---
      - hosts: vagrant
        vars:
          solita_jenkins_users:
            - foo
{'fraction_non_alphanumeric': 0.07247370827617741, 'fraction_numerical': 0.0, 'mean_word_length': 2.5453808752025933, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1165678', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1429, 'n_words': 321}