Ejemplo n.º 1
0
    def test_migrate_profile(self, tcex):
        """Test migrating profile."""
        os.environ['PYTEST_PWD'] = f'PYTEST_PWD_{randint(1000, 9999)}'
        profile_name = f'pytest-{randint(1000, 9999)}'

        # write temp test profile
        profile_filename = os.path.join(self.profile_dir,
                                        f'{profile_name}.json')
        with open(profile_filename, 'w') as fh:
            json.dump(self.profile_data, fh, indent=2)

        context = str(uuid4())
        profile = Profile(
            default_args=self.default_args.copy(),
            name=profile_name,
            redis_client=tcex.redis_client,
            tcex_testing_context=context,
        )

        # migrate profile to latest schema
        profile.migrate()

        # load profile
        profile_data = self.load_profile(f'{profile_name}.json')

        print('profile_data', profile_data)
Ejemplo n.º 2
0
    def test_profile_context(self, tcex):
        """Test profile contexts

        Args:
            tcex (TcEx, fixture): An instantiated instance of TcEx object.
        """
        default_args = {}
        feature = 'PyTest'
        name = f'pytest-{randint(1000, 9999)}'

        # initialize profile
        profile = Profile(default_args=default_args,
                          feature=feature,
                          name=name,
                          redis_client=tcex.redis_client)

        # add profile
        profile.add()

        # test context
        context = str(uuid4())
        profile.add_context(context)

        # assert context
        assert profile.context_tracker == [context]

        # clear context
        profile.clear_context(context)
        profile._context_tracker = []

        # assert context
        assert profile.context_tracker == []

        # cleanup
        os.remove(profile.filename)
Ejemplo n.º 3
0
 def _update_path_args(self, args):
     """Update path in args for each test profile."""
     # service Apps do not have a profile when this is needed.
     profile = self.profile or Profile(
         default_args=self.default_args.copy())
     args['tc_in_path'] = profile.tc_in_path
     args['tc_log_path'] = profile.tc_log_path
     args['tc_out_path'] = profile.tc_out_path
     args['tc_temp_path'] = profile.tc_temp_path
    def init_profile(self,
                     profile_name,
                     pytestconfig=None,
                     monkeypatch=None,
                     options=None):
        """Stages and sets up the profile given a profile name"""
        self._profile = Profile(
            default_args=self.default_args.copy(),
            name=profile_name,
            pytestconfig=pytestconfig,
            monkeypatch=monkeypatch,
            redis_client=self.redis_client,
            tcex_testing_context=self.tcex_testing_context,
            logger=self.log,
            options=options,
        )

        # Override test environments if specified
        os_environments = None
        if pytestconfig:
            os_environments = pytestconfig.option.environment

        # check profile environment
        self.check_environment(self._profile.environments, os_environments)

        # migrate profile to latest schema
        self._profile.migrate()

        # merge profile inputs (add new / remove non-defined)
        self._profile.merge_inputs()

        # populate profile (env vars, etc)
        self._profile.data = self._profile.populate.replace_env_variables(
            self._profile.data)

        # validate input fields, this method requires env vars to be populated
        valid, message = self._profile.validate_inputs()

        # stage ThreatConnect data based on current profile, also used in teardown method
        self._staged_tc_data = self.stager.threatconnect.entities(
            self._profile.stage_threatconnect, self._profile.owner)

        # insert staged data for replacement
        self._profile.tc_staged_data = self._staged_tc_data

        # Replace staged_data
        self._profile.data = self._profile.populate.replace_tc_variables(
            self._profile.data)

        # replace all references and all staged variable
        self._profile.init()

        # stage kvstore data based on current profile
        self.stager.redis.from_dict(self._profile.stage_kvstore)

        return valid, message
Ejemplo n.º 5
0
    def test_migrate_profile(self, tcex):
        """Test migrating profile."""
        os.environ['PYTEST_PWD'] = f'PYTEST_PWD_{randint(1000, 9999)}'
        profile_name = f'pytest-{randint(1000, 9999)}'

        # write temp test profile
        profile_filename = os.path.join(self.profile_dir, f'{profile_name}.json')
        with open(profile_filename, 'w') as fh:
            json.dump(self.profile_data, fh, indent=2)

        context = str(uuid4())
        profile = Profile(
            default_args=self.default_args.copy(),
            feature='PyTest',
            name=profile_name,
            redis_client=tcex.redis_client,
            tcex_testing_context=context,
        )

        # migrate profile to latest schema
        profile.migrate()

        # load profile
        profile_data = self.load_profile(f'{profile_name}.json')

        # validate redis was renamed to kvstore
        assert profile_data.get('stage', {}).get('redis') is None
        assert profile_data.get('stage', {}).get('kvstore') is not None
        # validate default input moved to default
        assert (
            profile_data.get('inputs', {}).get('defaults', {}).get('tc_proxy_host') == 'localhost'
        )
        # validate variable schema has been migrated
        assert (
            profile_data.get('inputs', {}).get('required', {}).get('vault_token')
            == '${env:PYTEST_PWD}'
        )
Ejemplo n.º 6
0
    def test_profile_add(self):
        """Test adding a profile.

        {
          "exit_codes": [
            0
          ],
          "exit_message": null,
          "inputs": {
            "optional": {
              "my_multi": [
                "one",
                "two"
              ]
            },
            "required": {
              "my_bool": false
            }
          },
          "options": {
            "autostage": {
              "enabled": false,
              "only_inputs": null
            },
            "session": {
              "blur": [],
              "enabled": false
            }
          },
          "outputs": null,
          "stage": {
            "kvstore": {}
          }
        }

        Args:
            tcex (TcEx, fixture): An instantiated instance of TcEx object.
        """
        default_args = {}
        feature = 'PyTest'
        name = f'pytest-{randint(1000, 9999)}'

        # initialize profile
        profile = Profile(default_args=default_args,
                          feature=feature,
                          name=name)

        # add profile
        profile.add()

        # load profile from disk
        profile_data = self.load_profile(f'{name}.json')

        # assert JSON file matches expected values
        assert profile_data.get('exit_codes') == [0]
        assert profile_data.get('exit_message') is None
        # inputs must match those defined in mock_app.py
        for k, _ in profile_data.get('inputs', {}).get('optional', {}).items():
            if k == 'my_multi':
                break
        else:
            assert False, 'Key my_multi not found in inputs, did someone change inputs in mock App?'
        for k, _ in profile_data.get('inputs', {}).get('required', {}).items():
            if k == 'my_bool':
                break
        else:
            assert False, 'Key my_bool not found in inputs, did someone change inputs in mock App?'
        assert profile_data.get('outputs') is None

        # cleanup
        os.remove(profile.filename)