bundle common test_meta
{
  vars:
      "description" string => "A user not present gets added with primary group id";
      "story_id" string => "5525";
      "covers" string => "operational_repaired";
}

#######################################################

body common control
{
      inputs => { "../../default.cf.sub",
                  "user_queries.cf.sub",
                  "disable_sudo_tty_requirement.cf.sub"
                };
      bundlesequence  => { default("$(this.promise_filename)") };
}

#######################################################

bundle agent init
{
  meta:
      # No GIDs on Windows.
      "test_skip_unsupported" string => "windows";

  # Remove him first, should he already be present.
  users:
    "johndoe"
      policy => "absent";

  files:
    "$(G.testfile)"
      delete => init_delete;

  methods:
      "any" usebundle => disable_sudo_tty_requirement;
}

body delete init_delete
{
  rmdirs => "false";
}

#######################################################

body contain test_contain_body
{
  useshell => "useshell";
}

bundle agent test
{
  users:
    "johndoe"
      policy => "present",
      group_primary => "$(user_tests.gid1)";

  commands:
    sudo_works::
      "sudo -u johndoe $(G.touch) $(G.testfile)"
        contain => test_contain_body;
}

#######################################################

body perms check_perms_body
{
  groups => { "$(user_tests.gid1)" };
}

body classes check_classes_body
{
  promise_repaired => { "perms_not_ok" };
  promise_kept => { "perms_ok" };
}

bundle agent check
{
  methods:
      "any" usebundle => remove_stale_groups;

  methods:
    "any" usebundle => user_is_in_primary_group("johndoe", "$(user_tests.group1)", "success", "failure"),
      classes => always("methods_run");

  files:
    "$(G.testfile)"
      perms => check_perms_body,
      classes => check_classes_body;

  classes:
      "ready" expression => "methods_run";
    sudo_works::
      "ok" and => { "success", "!failure", "perms_ok", "!perms_not_ok" };
    !sudo_works::
      "ok" and => { "success", "!failure" };

  reports:
    ok.ready::
      "$(this.promise_filename) Pass";
    !ok.ready::
      "$(this.promise_filename) FAIL";
}
