Ejemplo n.º 1
0
    def test_api_hello_api_008():
        """Test server hello response.

        Send GET /api/snippy/rest/' to get server hello response. In this case
        the server base path is incorrect because it contains two slashes. This
        configuration error results the default base path configuration.
        """

        expect_headers = {
            'content-type': 'application/vnd.api+json; charset=UTF-8',
            'content-length': '246'
        }
        expect_body = {'meta': Content.get_api_meta()}
        server = Snippy([
            'snippy', 'server', '--server-host', 'localhost:8080',
            '--server-base-path-rest', '/api//snippy'
        ])
        server.run()
        result = testing.TestClient(
            server.server.api).simulate_get('/api/snippy/rest')
        assert result.status == falcon.HTTP_200
        assert result.headers == expect_headers
        Content.assert_restapi(result.json, expect_body)
        server.release()
        Content.delete()
Ejemplo n.º 2
0
    def test_api_hello_api_004():
        """Test server hello response.

        Send GET /api/snippy to get server hello response. In this case the
        server base path is changed from default and it is set in correct
        format.
        """

        expect_headers = {
            'content-type': 'application/vnd.api+json; charset=UTF-8',
            'content-length': '246'
        }
        expect_body = {'meta': Content.get_api_meta()}
        server = Snippy([
            'snippy', 'server', '--server-host', 'localhost:8080',
            '--server-base-path-rest', '/api/snippy/'
        ])
        server.run()
        result = testing.TestClient(
            server.server.api).simulate_get('/api/snippy')
        assert result.status == falcon.HTTP_200
        assert result.headers == expect_headers
        Content.assert_restapi(result.json, expect_body)
        server.release()
        Content.delete()
Ejemplo n.º 3
0
    def test_help_option_009(capsys, caplog):
        """Test printing test documentation from console.

        Print test cases. The ``--no-ansi`` option must work when set after
        the ``--help`` option.
        """

        output = (
            'test case reference list:',
            '',
            '   $ snippy import --filter .*(\\$\\s.*)',
            '   # Import all snippets. File name is not defined in commmand line.',
            '   # This should result tool internal default file name',
            '   # ./snippets.yaml being used by default.',
            '',
            '   $ snippy import --filter .*(\\$\\s.*)',
            '   # Import all snippets. File name is not defined in commmand line.',
            '   # This should result tool internal default file name',
            '   # ./snippets.yaml being used by default.',
            '',
            ''
        )
        snippy = Snippy(['snippy', '--help', 'tests', '--no-ansi'])
        snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert out == Const.NEWLINE.join(output)
        assert not err
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 4
0
    def test_api_hello_api_007():
        """Test server hello response.

        Send GET /api/snippy/ to get server hello response. In this case the
        server base path configuration is incorrect. The server base path must
        contain leading and trailing slashes which are missing from this test.
        In this case the configuration must be updated automatically and the
        API call must work.
        """

        expect_headers = {
            'content-type': 'application/vnd.api+json; charset=UTF-8',
            'content-length': '246'
        }
        expect_body = {'meta': Content.get_api_meta()}
        server = Snippy([
            'snippy', 'server', '--server-host', 'localhost:8080',
            '--server-base-path-rest', 'api/snippy'
        ])
        server.run()
        result = testing.TestClient(
            server.server.api).simulate_get('/api/snippy')
        assert result.status == falcon.HTTP_200
        assert result.headers == expect_headers
        Content.assert_restapi(result.json, expect_body)
        server.release()
        Content.delete()
Ejemplo n.º 5
0
    def test_help_option_003(capsys, caplog):
        """Test printing help from console.

        Generate help text by giving only the tool name.
        """

        snippy = Snippy(['snippy'])
        snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert out == Const.NEWLINE.join(TestCliOptions.HELP)
        assert not err
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 6
0
    def test_help_option_002(capsys, caplog):
        """Test printing help from console.

        Print help with short option.
        """

        snippy = Snippy(['snippy', '-h'])
        snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert out == Const.NEWLINE.join(TestCliOptions.HELP)
        assert not err
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 7
0
    def test_help_option_007(capsys, caplog):
        """Test printing examples from console.

        Print command examples from help.
        """

        snippy = Snippy(['snippy', '--help', 'examples'])
        snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert out == Const.NEWLINE.join(TestCliOptions.EXAMPLES)
        assert not err
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 8
0
    def test_snippy_main(capsys, caplog):
        """Test running program main with profile option.

        Run program main with the profile option. Test checks that there is
        more than randomly picked largish number of rows. This just verifies
        that the profile option prints lots for data.
        """

        with pytest.raises(SystemExit):
            main(['snippy', 'search', '--sall', '.', '--profile'])
        out, err = capsys.readouterr()
        assert 'Ordered by: cumulative time' in out
        assert not err
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 9
0
    def test_version_option_002(capsys, caplog):
        """Test printing tool version.

        Output tool version with short option. Only the version must be
        printed and nothing else. The print must be send to stdout.
        """

        snippy = Snippy(['snippy', '-v'])
        snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert out == __version__ + Const.NEWLINE
        assert not err
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 10
0
    def test_help_option_005(capsys, caplog):
        """Test printing help from console.

        Suppress tool help text with quiet mode even when there are no other
        parameters and the help should be printed.
        """

        snippy = Snippy(['snippy', '-q'])
        snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert out == Const.EMPTY
        assert not err
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 11
0
    def test_help_option_010(capsys, caplog):
        """Print test documentation when testing package does not exist.

        Try to print tool test case reference documentation when tests are
        not packaged with the release.
        """

        snippy = Snippy(['snippy', '--help', 'tests'])
        cause = snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert cause == 'NOK: test cases are not packaged with release No module named \'tests\''
        assert out == 'NOK: test cases are not packaged with release No module named \'tests\'' + Const.NEWLINE
        assert not err
        assert not caplog.records[:]
        snippy.release()
        Content.delete()
Ejemplo n.º 12
0
    def test_help_option_006(capsys, caplog):
        """Test invalid command line option.

        Try to run snippy with invalid command line option.
        """

        output = (
            'usage: snippy [-v, --version] [-h, --help] <operation> [<options>] [-vv] [-q]',
            'snippy: error: unrecognized arguments: -a', '')
        snippy = Snippy(['snippy', '-a'])
        snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert out == Const.EMPTY
        assert err == Const.NEWLINE.join(output)
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 13
0
    def test_help_option_004(capsys, caplog, osenviron):
        """Test running only the snippy.

        In this case the SNIPPY_SERVER_HOST configuration variable is set.
        This should just start the server without printing help. This is a
        use case for server installation in Docker containers where all
        variables are coming from environment variables.
        """

        osenviron.setenv('SNIPPY_SERVER_HOST', '127.0.0.1:8081')
        snippy = Snippy(['snippy'])
        snippy.run()
        snippy.release()
        out, err = capsys.readouterr()
        assert out == Const.EMPTY
        assert not err
        assert not caplog.records[:]
        Content.delete()
Ejemplo n.º 14
0
    def test_api_hello_api_016(caplog, osenviron):
        """Test server configuration.

        Send GET /api/snippy/rest/v2 to get server hello response. In this
        case the server variables are changed with environment variables.
        """

        osenviron.setenv('SNIPPY_SERVER_BASE_PATH_REST', '/api/snippy/rest/v2')
        osenviron.setenv('SNIPPY_SERVER_HOST', '127.0.0.1:8081')
        osenviron.setenv('SNIPPY_SERVER_MINIFY_JSON', 'True')
        osenviron.setenv('SNIPPY_STORAGE_TYPE',
                         'misconfig')  # Must be mapped to default.
        osenviron.setenv('SNIPPY_LOG_MSG_MAX',
                         'misconfig')  # Must be mapped to default.
        osenviron.setenv('SNIPPY_LOG_JSON', '1')  # Must be mapped to True.
        osenviron.setenv('SNIPPY_Q', 'True')  # Must be mapped to True.
        osenviron.setenv('SNIPPY_PROFILE', 'False')
        expect_headers = {
            'content-type': 'application/vnd.api+json; charset=UTF-8',
            'content-length': '202'
        }
        expect_body = {'meta': Content.get_api_meta()}
        server = Snippy(['snippy', '--debug'])
        server.run()
        result = testing.TestClient(
            server.server.api).simulate_get('/api/snippy/rest/v2')
        assert 'server_base_path_rest=/api/snippy/rest/v2' in caplog.text
        assert 'server_host=127.0.0.1:8081' in caplog.text
        assert 'server_minify_json=True' in caplog.text
        assert 'storage_type=sqlite' in caplog.text
        assert 'log_json=True' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'json logs: True' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'log_msg_max=80' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'log msg max: 80' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'quiet=True' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'quiet: True' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'profiler=False' in caplog.text
        assert result.status == falcon.HTTP_200
        assert result.headers == expect_headers
        Content.assert_restapi(result.json, expect_body)
        server.release()
        Content.delete()
Ejemplo n.º 15
0
    def test_api_hello_api_009(caplog):
        """Test server hello response.

        Send GET /api/snippy/rest to get server hello response. In this case
        the server host is changed from the default with command line option.
        """

        expect_headers = {
            'content-type': 'application/vnd.api+json; charset=UTF-8',
            'content-length': '246'
        }
        expect_body = {'meta': Content.get_api_meta()}
        server = Snippy(
            ['snippy', 'server', '--server-host', 'localhost:8081', '--debug'])
        server.run()
        result = testing.TestClient(
            server.server.api).simulate_get('/api/snippy/rest')
        assert 'server_host=localhost:8081' in caplog.text
        assert result.status == falcon.HTTP_200
        assert result.headers == expect_headers
        Content.assert_restapi(result.json, expect_body)
        server.release()
        Content.delete()
Ejemplo n.º 16
0
    def test_api_hello_api_017(caplog, osenviron):
        """Test server configuration.

        Send GET /api/snippy/rest/v3 to get server hello response. In this
        case the server options are configured with environment variables
        and command line options. The command line option has higher
        precedence and they must be used.
        """

        osenviron.setenv('SNIPPY_SERVER_BASE_PATH_REST', '/api/snippy/rest/v2')
        osenviron.setenv('SNIPPY_SERVER_HOST', '127.0.0.1:8081')
        osenviron.setenv('SNIPPY_SERVER_MINIFY_JSON', 'False')
        osenviron.setenv('SNIPPY_LOG_MSG_MAX', '100')
        osenviron.setenv('SNIPPY_LOG_JSON', '0')  # Must be mapped to False.
        expect_headers = {
            'content-type': 'application/vnd.api+json; charset=UTF-8',
            'content-length': '202'
        }
        expect_body = {'meta': Content.get_api_meta()}
        server = Snippy(['snippy', 'server', '--server-host', 'localhost:8080', '--server-base-path-rest', '/api/snippy/rest/v3', '--server-minify-json', '--log-msg-max', '20', '--debug'])  # noqa pylint: disable=line-too-long
        server.run()
        result = testing.TestClient(
            server.server.api).simulate_get('/api/snippy/rest/v3')
        assert 'server_base_path_rest=/api/snippy/rest/v3' in caplog.text
        assert 'server_host=localhost:8080' in caplog.text
        assert 'server_minify_json=True' in caplog.text
        assert 'storage_type=sqlite' in caplog.text
        assert 'log_msg_max=20' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'log msg max: 20' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'log_json=False' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert 'json logs: False' in caplog.text  # Debug log settings are twice and both parsing must be correct.
        assert result.status == falcon.HTTP_200
        assert result.headers == expect_headers
        Content.assert_restapi(result.json, expect_body)
        server.release()
        Content.delete()
Ejemplo n.º 17
0
    def teardown_class(cls):
        """Teardown class."""

        Content.delete()