<commit_before>require 'test/unit'
require 'webmock/test_unit'
require 'vaultconf.rb'
require 'vault'

class TestLogin < Test::Unit::TestCase
  @@server = 'localhost:8200'
  @@user = 'mike'
  @@password = '<PASSWORD>'
  Vault.address = "http://#{@@server}"
  Vault.token = 'test<PASSWORD>'

  def test_login

    expected_auth_token = '<PASSWORD>'
    stub_request(:post, "http://#{@@server}/v1/auth/userpass/login/#{@@user}").
        with(:body => "{\"password\":\"#{@@password}\"}").
        to_return(:status => 200, :body => '{"lease_id":"","renewable":false,"lease_duration":0,"data":null,
          "auth":{"client_token":"'+expected_auth_token+'","policies":["root"],"metadata":{"username":"user"},
          "lease_duration":0,"renewable":false}}', :headers => {})

    auth_token = Vaultconf.get_auth_token(@@user, @@password, @@server)

    assert auth_token = expected_auth_token
  end

  def test_add_policies_to_vault
    policies_path = File.expand_path('../resources/policies', __FILE__)

    reader_policy_stub = stub_request(:put, "http://#{@@server}/v1/sys/policy/reader").
        with(:body => "{\"rules\":\"{\\n  \\\"path\\\": {\\n    \\\"secret/*\\\": {\\n      \\\"policy\\\": \\\"read\\\"\\n    }\\n  }\\n}\"}").
        to_return(:status => 200, :body => "", :headers => {})

    writer_policy_stub = stub_request(:put, "http://#{@@server}/v1/sys/policy/writer").
        with(:body => "{\"rules\":\"{\\n  \\\"path\\\": {\\n    \\\"secret/*\\\": {\\n      \\\"policy\\\": \\\"write\\\"\\n    }\\n  }\\n}\"}").
        to_return(:status => 200, :body => "", :headers => {})

    Vaultconf.add_policies_to_vault(Vault, policies_path)

    assert_requested(writer_policy_stub)
    assert_requested(reader_policy_stub)

  end

  def test_add_users_to_vault
    users_path = File.expand_path('../resources/users/users.yaml', __FILE__)

    user_request_stub_writer = stub_request(:put, "http://#{@@server}/v1/auth/userpass/users/MrWrite").
        with(:body => /{\"password\":\".*\",\"policies\":\"writer,reader\"}/).
        to_return(:status => 200, :body => "", :headers => {})

    user_request_stub_reader = stub_request(:put, "http://#{@@server}/v1/auth/userpass/users/MrRead").
        with(:body => /{\"password\":\".*\",\"policies\":\"reader\"}/).
        to_return(:status => 200, :body => "", :headers => {})

    Vaultconf.add_users_to_vault(Vault, users_path)
    assert_requested(user_request_stub_writer)
    assert_requested(user_request_stub_reader)<commit_msg>Simplified unit test stubs to make them less brittle. If http calls are wrong acceptance tests will pick this up<commit_after>require 'test/unit'
require 'webmock/test_unit'
require 'vaultconf.rb'
require 'vault'

class TestLogin < Test::Unit::TestCase
  @@server = 'localhost:8200'
  @@user = 'mike'
  @@password = '<PASSWORD>'
  Vault.address = "http://#{@@server}"
  Vault.token = 'testtoken'

  def test_login

    expected_auth_token = 'autht<PASSWORD>'
    stub_request(:post, "http://#{@@server}/v1/auth/userpass/login/#{@@user}").
        with(:body => "{\"password\":\"#{@@password}\"}").
        to_return(:status => 200, :body => '{"lease_id":"","renewable":false,"lease_duration":0,"data":null,
          "auth":{"client_token":"'+expected_auth_token+'","policies":["root"],"metadata":{"username":"user"},
          "lease_duration":0,"renewable":false}}', :headers => {})

    auth_token = Vaultconf.get_auth_token(@@user, @@password, @@server)

    assert auth_token = expected_auth_token
  end

  def test_add_policies_to_vault
    policies_path = File.expand_path('../resources/policies', __FILE__)

    reader_policy_stub = stub_request(:put, "http://#{@@server}/v1/sys/policy/reader").
        to_return(:status => 200, :body => "", :headers => {})

    writer_policy_stub = stub_request(:put, "http://#{@@server}/v1/sys/policy/writer").
        to_return(:status => 200, :body => "", :headers => {})

    Vaultconf.add_policies_to_vault(Vault, policies_path)

    assert_requested(writer_policy_stub)
    assert_requested(reader_policy_stub)

  end

  def test_add_users_to_vault
    users_path = File.expand_path('../resources/users/users.yaml', __FILE__)

    user_request_stub_writer = stub_request(:put, "http://#{@@server}/v1/auth/userpass/users/MrWrite").
        with(:body => /{\"password\":\".*\",\"policies\":\"writer,reader\"}/).
        to_return(:status => 200, :body => "", :headers => {})

    user_request_stub_reader = stub_request(:put, "http://#{@@server}/v1/auth/userpass/users/MrRead").
        with(:body => /{\"password\":\".*\",\"policies\":\"reader\"}/).
        to_return(:status => 200, :body => "", :headers => {})

    Vaultconf.add_users_to_vault(Vault, users_path)
    assert_requested(user_request_stub_writer)
    assert_requested(user_request_stub_reader)
{'fraction_non_alphanumeric': 0.2190099833610649, 'fraction_numerical': 0.010815307820299502, 'mean_word_length': 3.882233502538071, 'pattern_counts': {'":': 40, '<': 10, '<?xml version=': 0, '>': 46, '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': '573473', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1652, 'n_words': 228}