<commit_before># -*- coding: utf-8 -*-

'''
tests for user state
user absent
user present
user present with custom homedir
'''

# Import python libs
from __future__ import absolute_import
import os
import sys
from random import randint
import grp

# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.unit import skipIf
from tests.support.helpers import destructiveTest, requires_system_grains, skip_if_not_root
from tests.support.mixins import SaltReturnAssertsMixin

# Import salt libs
import salt.utils

if salt.utils.is_darwin():
    USER = 'macuser'
    GROUP = 'macuser'
    GID = randint(400, 500)
    NOGROUPGID = randint(400, 500)
else:
    USER = 'nobody'
    GROUP = 'nobody'
    GID = 'nobody'
    NOGROUPGID = 'nogroup'


@destructiveTest
@skip_if_not_root
class UserTest(ModuleCase, SaltReturnAssertsMixin):
    '''
    test for user absent
    '''
    user_name = 'salt_test'
    user_home = '/var/lib/salt_test'

    def setUp(self):
        if salt.utils.is_darwin():
            #on mac we need to add user, because there is
            #no creationtime for nobody user.
            add_user = self.run_function('user.add', [USER], gid=GID)

    def test_user_absent(self):
        ret = self.run_state('user.absent', name='unpossible')
        self.assertSaltTrueReturn(ret)

    def test_user_if_present(self):
        ret = self.run_state('user.present', name=USER)
        self.assertSaltTrueReturn(ret)

    def test_user_if_present_with_gid(self):
        if self.run_function('group.info', [USER]):
            ret = self.run_state('user.present', name=USER, gid=GID)
        elif self.run_function('group.info', ['nogroup']):
            ret = self.run_state('user.present', name=USER, gid=NOGROUPGID)
        else:
            self.skipTest(
                'Neither \'nobody\' nor \'nogroup\' are valid groups'
            )
        self.assertSaltTrueReturn(ret)

    def test_user_not_present(self):
        '''
        This is a DESTRUCTIVE TEST it creates a new user on the minion.
        And then destroys that user.
        Assume that it will break any system you run it on.
        '''
        ret = self.run_state('user.present', name=self.user_name)
        self.assertSaltTrueReturn(ret)

    def test_user_present_when_home_dir_does_not_18843(self):
        '''
        This is a DESTRUCTIVE TEST it creates a new user on the minion.
        And then destroys that user.
        Assume that it will break any system you run it on.
        '''
        if salt.utils.is_darwin():
            HOMEDIR = '/Users/home_of_' + self.user_name
        else:
            HOMEDIR = '/home/home_of_' + self.user_name
        ret = self.run_state('user.present', name=self.user_name,
                             home=HOMEDIR)
        self.assertSaltTrueReturn(ret)

        self.run_function('file.absent', name=HOMEDIR)
        ret = self.run_state('user.present', name=self.user_name,
                             home=HOMEDIR)
        self.assertSaltTrueReturn(ret)

    def test_user_present_nondefault(self):
        '''
        This is a DESTRUCTIVE TEST it creates a new user on the on the minion.
        '''
        ret = self.run_state('user.present', name=self.user_name,
                             home=self.user_home)
        self.assertSaltTrueReturn(ret)
        self.assertTrue(os.path.isdir(self.user_home))

    @requires_system_grains
    def test_user_present_gid_from_name_default(self, grains=None):
        '''
        This is a DESTRUCTIVE TEST. It creates a new user on the on the minion.
        This is an integration test. Not all systems will automatically create
        a group of the same name as the user, but I don't have access to any.
        If you run the test and it fails, please fix the code it's testing to
        work on your operating system.
        '''
        # MacOS users' primary group defaults to staff (20), not the name of
        # user
        gid_from_name = False if grains['os_family'] == 'MacOS' else True

        ret = self.run_state('user.present', name=self.user_name,
                             gid_from_name=gid_from_name, home=self.user_home)
        self.assertSaltTrueReturn(ret)

        ret = self.run_function('user.info', [self.user_name])
        self.assertReturnNonEmptySaltType(ret)
        group_name = grp.getgrgid(ret['gid']).gr_name

        self.assertTrue(os.path.isdir(self.user_home))
        if grains['os_family'] in ('Suse',):
            self.assertEqual(group_name, 'users')
        elif grains['os_family'] == 'MacOS':<commit_msg>Disable home dir check for mac in test_user<commit_after># -*- coding: utf-8 -*-

'''
tests for user state
user absent
user present
user present with custom homedir
'''

# Import python libs
from __future__ import absolute_import
import os
import sys
from random import randint
import grp

# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.unit import skipIf
from tests.support.helpers import destructiveTest, requires_system_grains, skip_if_not_root
from tests.support.mixins import SaltReturnAssertsMixin

# Import salt libs
import salt.utils

if salt.utils.is_darwin():
    USER = 'macuser'
    GROUP = 'macuser'
    GID = randint(400, 500)
    NOGROUPGID = randint(400, 500)
else:
    USER = 'nobody'
    GROUP = 'nobody'
    GID = 'nobody'
    NOGROUPGID = 'nogroup'


@destructiveTest
@skip_if_not_root
class UserTest(ModuleCase, SaltReturnAssertsMixin):
    '''
    test for user absent
    '''
    user_name = 'salt_test'
    user_home = '/var/lib/salt_test'

    def setUp(self):
        if salt.utils.is_darwin():
            #on mac we need to add user, because there is
            #no creationtime for nobody user.
            add_user = self.run_function('user.add', [USER], gid=GID)

    def test_user_absent(self):
        ret = self.run_state('user.absent', name='unpossible')
        self.assertSaltTrueReturn(ret)

    def test_user_if_present(self):
        ret = self.run_state('user.present', name=USER)
        self.assertSaltTrueReturn(ret)

    def test_user_if_present_with_gid(self):
        if self.run_function('group.info', [USER]):
            ret = self.run_state('user.present', name=USER, gid=GID)
        elif self.run_function('group.info', ['nogroup']):
            ret = self.run_state('user.present', name=USER, gid=NOGROUPGID)
        else:
            self.skipTest(
                'Neither \'nobody\' nor \'nogroup\' are valid groups'
            )
        self.assertSaltTrueReturn(ret)

    def test_user_not_present(self):
        '''
        This is a DESTRUCTIVE TEST it creates a new user on the minion.
        And then destroys that user.
        Assume that it will break any system you run it on.
        '''
        ret = self.run_state('user.present', name=self.user_name)
        self.assertSaltTrueReturn(ret)

    def test_user_present_when_home_dir_does_not_18843(self):
        '''
        This is a DESTRUCTIVE TEST it creates a new user on the minion.
        And then destroys that user.
        Assume that it will break any system you run it on.
        '''
        if salt.utils.is_darwin():
            HOMEDIR = '/Users/home_of_' + self.user_name
        else:
            HOMEDIR = '/home/home_of_' + self.user_name
        ret = self.run_state('user.present', name=self.user_name,
                             home=HOMEDIR)
        self.assertSaltTrueReturn(ret)

        self.run_function('file.absent', name=HOMEDIR)
        ret = self.run_state('user.present', name=self.user_name,
                             home=HOMEDIR)
        self.assertSaltTrueReturn(ret)

    def test_user_present_nondefault(self):
        '''
        This is a DESTRUCTIVE TEST it creates a new user on the on the minion.
        '''
        ret = self.run_state('user.present', name=self.user_name,
                             home=self.user_home)
        self.assertSaltTrueReturn(ret)
        if not salt.utils.is_darwin():
            self.assertTrue(os.path.isdir(self.user_home))

    @requires_system_grains
    def test_user_present_gid_from_name_default(self, grains=None):
        '''
        This is a DESTRUCTIVE TEST. It creates a new user on the on the minion.
        This is an integration test. Not all systems will automatically create
        a group of the same name as the user, but I don't have access to any.
        If you run the test and it fails, please fix the code it's testing to
        work on your operating system.
        '''
        # MacOS users' primary group defaults to staff (20), not the name of
        # user
        gid_from_name = False if grains['os_family'] == 'MacOS' else True

        ret = self.run_state('user.present', name=self.user_name,
                             gid_from_name=gid_from_name, home=self.user_home)
        self.assertSaltTrueReturn(ret)

        ret = self.run_function('user.info', [self.user_name])
        self.assertReturnNonEmptySaltType(ret)
        group_name = grp.getgrgid(ret['gid']).gr_name

        self.assertTrue(os.path.isdir(self.user_home))
        if grains['os_family'] in ('Suse',):
            self.assertEqual(group_name, 'users')
        elif grains['os_family'] == 'MacOS':
{'fraction_non_alphanumeric': 0.09653010485352935, 'fraction_numerical': 0.004323856880337261, 'mean_word_length': 2.527258863896302, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2217649', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2812, 'n_words': 755}