Example #1
0
    def test_args_lowercase(self, tmp_path):
        """
        Given
        - Postman collection.
        - Test report request which has variables with upper case.

        When
        - Generating config file.

        Then
        - Integration code has arguments as lowercase, but sends the arguments to requests as given.
        - Integration yml, the arguments are lower case.
        """
        path = tmp_path / 'test-collection.json'
        _testutil_create_postman_collection(
            dest_path=path,
            with_request={
                "name": "Test Report",
                "request": {
                    "method": "GET",
                    "header": [],
                    "body": {
                        "mode": "raw",
                        "raw":
                        "{\n    \"A\": 2,\n    \"B\": 3,\n    \"c\": 4\n}"
                    },
                    "url": {
                        "raw":
                        "{{url}}/vtapi/v2/test/{{FOO_A}}?RESOURCE_B=https://www.cobaltstrike.com/",
                        "host": ["{{url}}"],
                        "path": ["vtapi", "v2", "test", "{{FOO_A}}"],
                        "query": [{
                            "key": "RESOURCE_B",
                            "value": "https://www.cobaltstrike.com/"
                        }]
                    },
                    "description": "Test Report description"
                }
            })

        config = postman_to_autogen_configuration(collection=json.load(
            open(path)),
                                                  name='VirusTotal',
                                                  context_path_prefix=None,
                                                  command_prefix=None)

        integration_code = config.generate_integration_python_code()
        integration_obj = config.generate_integration_yml()
        integration_yml = yaml.dumps(integration_obj.to_dict())

        assert "foo_a = args.get('foo_a')" in integration_code
        assert "def test_report_request(self, foo_a, resource_b, a, b, c)" in integration_code
        assert 'assign_params(RESOURCE_B=resource_b' in integration_code
        assert "('GET', f'vtapi/v2/test/{foo_a}', params=params, json_data=data, headers=headers)" in integration_code

        assert 'name: foo_a' in integration_yml
        assert 'name: resource_b' in integration_yml
        assert 'name: a\n' in integration_yml
        assert 'name: b\n' in integration_yml
        assert 'name: c\n' in integration_yml
Example #2
0
    def test_config_generated_successfully(self, mocker):
        """
        This is general happy path test, the purpose of this test is not to test something specifically
        but to make sure if something changed in config file schema or broken, this test will fail because it is not
        identical with the actual result.
        If this test fails, validate that the reason for the failure is valid (like on purpose schema update) and then
        update the test file under resources folder.

        Given
        - Postman collection v2.1 of 4 Virus Total API commands

        When
        - generating config file from the postman collection

        Then
        - ensure the config file is generated
        - the config file should be identical to the one we have under resources folder
        """
        from demisto_sdk.commands.common.hook_validations.docker import \
            DockerImageValidator

        mocker.patch.object(DockerImageValidator,
                            'get_docker_image_latest_tag_request',
                            return_value='3.8.6.12176')

        autogen_config = postman_to_autogen_configuration(
            collection_path=self.postman_collection_path,
            name='VirusTotal Test',
            command_prefix='vt-test',
            context_path_prefix='VirusTotalTest')

        with open(self.autogen_config_path, 'r') as config_file:
            expected_config = json.load(config_file)

        assert expected_config == autogen_config.to_dict()
Example #3
0
    def test_url_contains_args(self, tmp_path):
        """
        Given
        - postman collection
        - with request Test Report which has variable {{foo}} in the url like:
        {{url}}/vtapi/v2/:Virus_name/test/{{foo}}?resource=https://www.cobaltstrike.com/

        When
        - generating config file

        Then
        - integration code, the command test-report will contain foo argument passed to the url
        - integration yml, the command test-report will contain foo arg
        - integration yml, the command test-report will contain virus_name arg.
        """
        path = tmp_path / 'test-collection.json'
        _testutil_create_postman_collection(
            dest_path=path,
            with_request={
                "name": "Test Report",
                "request": {
                    "method": "GET",
                    "header": [],
                    "url": {
                        "raw":
                        "{{url}}/vtapi/v2/:Virus_name/test/{{foo}}?resource=https://www.cobaltstrike.com/",
                        "host": ["{{url}}"],
                        "path":
                        ["vtapi", "v2", ":Virus_name", "test", "{{foo}}"],
                        "variable": [{
                            "key": "Virus_name",
                            "value": ""
                        }],
                        "query": [{
                            "key": "resource",
                            "value": "https://www.cobaltstrike.com/"
                        }]
                    },
                    "description": "Test Report description"
                }
            })

        config = postman_to_autogen_configuration(collection=json.load(
            open(path)),
                                                  name='VirusTotal',
                                                  context_path_prefix=None,
                                                  command_prefix=None)

        integration_code = config.generate_integration_python_code()
        integration_obj = config.generate_integration_yml()
        integration_yml = yaml.dumps(integration_obj.to_dict())

        assert "foo = args.get('foo')" in integration_code
        assert "virus_name = args.get('virus_name')" in integration_code
        assert "def test_report_request(self, foo, virus_name, resource):" in integration_code
        assert "'GET', f'vtapi/v2/{virus_name}/test/{foo}', params=params, headers=headers)" in integration_code

        assert 'name: foo' in integration_yml
        assert 'name: virus_name' in integration_yml
Example #4
0
    def test_context_output_path(self):
        """
        Given
        - postman collection with name Virus Total

        When
        - generating config file

        Then
        - ensure context_output_path of the whole integration is VirusTotal
        """
        autogen_config = postman_to_autogen_configuration(
            collection_path=self.postman_collection_path,
            context_path_prefix=None,
            command_prefix=None,
            name=None,
            category=None)

        assert autogen_config.context_path == 'VirusTotal'
Example #5
0
    def test_command_arguments_names_duplication(self):
        """
        Given
        - postman collection with one command, that has some arguments with the same name (suffix)

        When
        - generating config file

        Then
        - ensure the number of arguments generated is the same as the number of arguments in the command (if not, some arguments
        are generated as one with the same name)
        """
        autogen_config = postman_to_autogen_configuration(
            collection=self.arguments_check_collection,
            command_prefix=None,
            name=None,
            context_path_prefix=None,
            category=None)
        assert len(autogen_config.commands[0].arguments) == 15
Example #6
0
    def test_command_prefix(self):
        """
        Given
        - postman collection with name Virus Total

        When
        - generating config file

        Then
        - ensure command_prefix is virustotal-

        """
        autogen_config = postman_to_autogen_configuration(
            collection_path=self.postman_collection_path,
            command_prefix=None,
            name=None,
            context_path_prefix=None,
            category=None)

        assert autogen_config.command_prefix == 'virustotal'
Example #7
0
    def test_post_body_to_arguments(self, tmpdir):
        """
        If POST request requires data passed in the body, then command arguments should construct that data

        Given
        - postman collection
        - with POST request "test-create-group"
        - "test-create" requires data of the following structure
        {
            "test_name": "some name",
            "test_id": "some id",
            "test_filter": {
                "test_key": "this is nested object"
            }
        }

        When
        - creating config file

        Then
        - test-create command should contain arguments of "test_name" "test_id" "test_filter"

        When
        - generating code from the config file

        Then
        - "name", "id" and "filter" must be passed as request body
        - "name", "id" and "filter" should be arguments of "test-create-group" command in yml
        """
        path = Path(tmpdir, 'config.json')
        _testutil_create_postman_collection(
            dest_path=path,
            with_request={
                "name": "Test Create Group",
                "request": {
                    "method":
                    "POST",
                    "header": [{
                        "key": "Accept",
                        "value": "application/json"
                    }, {
                        "key": "Content-Type",
                        "value": "application/json"
                    }, {
                        "key": "Authorization",
                        "value": "SSWS {{apikey}}"
                    }],
                    "body": {
                        "mode":
                        "raw",
                        "raw":
                        """{"test_name":"some name","test_id":"some id","test_filter":{"test_key":"this is nested object"}}"""
                    },
                    "url": {
                        "raw": "{{url}}/api/v1/groups",
                        "host": ["{{url}}"],
                        "path": ["api", "v1", "groups"]
                    }
                },
                "response": []
            })

        config = postman_to_autogen_configuration(collection_path=path,
                                                  name=None,
                                                  command_prefix=None,
                                                  context_path_prefix=None,
                                                  category=None)

        command = _testutil_get_command(config, 'test-create-group')
        assert len(command.arguments) == 3
        assert command.arguments[0].name == 'test_name'
        assert command.arguments[0].in_ == 'body'
        assert not command.arguments[0].in_object

        assert command.arguments[1].name == 'test_id'
        assert command.arguments[1].in_ == 'body'
        assert not command.arguments[1].in_object

        assert command.arguments[2].name == 'test_key'
        assert command.arguments[2].in_ == 'body'
        assert command.arguments[2].in_object == ['test_filter']

        integration_code = config.generate_integration_python_code()
        integration_obj = config.generate_integration_yml()
        integration_yml = yaml.dump(integration_obj.to_dict())

        assert 'def test_create_group_request(self, test_name, test_id, test_key):' in integration_code
        assert 'data = {"test_filter": {"test_key": test_key}, "test_id": test_id, "test_name": test_name}' in integration_code
        assert 'response = self._http_request(\'POST\', \'api/v1/groups\', params=params, json_data=data, headers=headers)' in integration_code

        assert "name: test_id" in integration_yml
        assert "name: test_name" in integration_yml
        assert "name: test_key" in integration_yml
Example #8
0
    def test_apikey_passed_as_header(self, tmpdir):
        """
        Scenario: sometimes the auth method will not be defined under auth section, but as plain header Authorization

        Given
        - postman collection
        - with no auth defined
        - with request with headers:
            - "Authorization" header with value "SWSS {{apikey}}"
            - and other typical headers like Content-Type and Accept

        When
        - generating config file

        Then
        - config file should contain auth
        """
        path = Path(tmpdir, 'config.json')
        _testutil_create_postman_collection(dest_path=path,
                                            with_request={
                                                "name": "Test Report",
                                                "request": {
                                                    "method":
                                                    "GET",
                                                    "header": [{
                                                        "key":
                                                        "Accept",
                                                        "value":
                                                        "application/json"
                                                    }, {
                                                        "key":
                                                        "Content-Type",
                                                        "value":
                                                        "application/json"
                                                    }, {
                                                        "key":
                                                        "Authorization",
                                                        "value":
                                                        "SSWS {{apikey}}"
                                                    }],
                                                    "url": {
                                                        "raw": "{{url}}/test/",
                                                        "host": ["{{url}}"],
                                                        "path": [
                                                            "test",
                                                        ]
                                                    },
                                                    "description":
                                                    "Test Report description"
                                                }
                                            },
                                            no_auth=True)

        config = postman_to_autogen_configuration(collection_path=path,
                                                  name=None,
                                                  command_prefix=None,
                                                  context_path_prefix=None,
                                                  category=None)

        assert _testutil_get_param(config, 'api_key') is not None
        command = _testutil_get_command(config, 'test-report')
        assert command.headers == [{
            "Accept": "application/json"
        }, {
            "Content-Type": "application/json"
        }]
        assert config.auth == {
            "type":
            "apikey",
            "apikey": [{
                "key": "format",
                "value": "f'SSWS {params[\"api_key\"]}'",
                "type": "string"
            }, {
                "key": "in",
                "value": "header",
                "type": "string"
            }, {
                "key": "key",
                "value": "Authorization",
                "type": "string"
            }]
        }

        integration_code = config.generate_integration_python_code()

        assert "headers['Authorization'] = f'SSWS {params[\"api_key\"]}'" in integration_code