Esempio n. 1
0
def test_subcommand():
    app = Application('foo', 'tests.test_subcommand:Foo.quickstart')
    with Given(app):
        assert stdout == EXPECTED_HELP
        assert status == 0

        when(['-h'])
        assert status == 0
        assert stderr == ''
        assert stdout == EXPECTED_HELP

        when(['bar'])
        assert stderr == ''
        assert stdout == 'Bar done: False\n'
        assert status == 0

        when(['ba'])
        assert stderr == ''
        assert stdout == 'Bar done: False\n'
        assert status == 0

        when(['b'])
        assert stderr == ''
        assert stdout == 'Bar done: False\n'
        assert status == 0

        when(['bar', '--baz'])
        assert stderr == ''
        assert stdout == 'Bar done: True\n'
        assert status == 1
Esempio n. 2
0
    def test_database_schema_basedata_mockup(self):

        self.db.drop_database()
        self.db.create_database()
        assert self.db.database_exists()

        with Given(app, ['db', 'schema']):
            assert stderr == ''
            assert status == 0
            assert self.db.table_exists(FooModel.__tablename__)

            when(['db', 'basedata'])
            assert stderr == ''
            assert status == 0
            with self.db.cursor(
                    f'SELECT count(*) FROM foo_model WHERE title = %s',
                ('FooBase', )) as c:
                assert c.fetchone()[0] == 1

            when(['db', 'mockup'])
            assert stderr == ''
            assert status == 0
            with self.db.cursor(
                    f'SELECT count(*) FROM foo_model WHERE title = %s',
                ('FooMock', )) as c:
                assert c.fetchone()[0] == 1
Esempio n. 3
0
def test_verb():
    with Given(app, 'example.com'):
        assert status == 0
        assert stdout == 'example.com'

        when(given + 'POST')
        assert status == 0
        assert stdout == '-XPOST example.com'
Esempio n. 4
0
def test_restfulpy_cli(db):
    with Given(app):
        assert stdout.startswith('usage')
        assert status == 0

        when(given + '--version')
        assert stdout == f'{restfulpy.__version__}\n'
        assert status == 0
Esempio n. 5
0
def test_working_directory():
    with Given(app):
        assert f'{os.getcwd()}\n' == stdout
        if os.name == "nt":
            tmp_dir = os.getenv('TEMP')
        else:
            tmp_dir = '/tmp'
        when(working_directory=tmp_dir)
        assert stdout == tmp_dir + '\n'
def test_appcli_root():
    with Given(app):
        assert stderr == ''
        assert status == 0
        assert stdout == EXPECTED_HELP

        when(given + '-V')
        assert stderr == ''
        assert status == 0
        assert stdout == '0.1.0a0\n'
Esempio n. 7
0
def test_jwtcli():
    cliapp = CLIApplication('example', 'tests.test_cli:app.climain')
    with Given(cliapp, 'jwt --help'):
        assert status == 0
        assert stderr == ''

        when('jwt create \'{"foo": "bar"}\'')
        print(stderr)
        assert status == 0
        assert len(stdout.split('.')) == 3
Esempio n. 8
0
def test_versioncli():
    cliapp = CLIApplication('foo', 'tests.test_builtincli_version:app.climain')

    with Given(cliapp, '--version'):
        assert status == 0
        assert stdout.strip() == __version__
        assert stderr == ''

        when('')
        assert status == 0
        assert stderr == ''
def test_configuration_encrypt():

    with Given(app, 'configuration encrypt', stdin=b'abc'):
        assert stderr == b''
        assert status == 0
        assert stdout.startswith(b'#enc')
        binary = stdout.proxied_object
        when('configuration decrypt', stdin=binary)
        assert stderr == b''
        assert status == 0
        assert stdout == b'abc'
Esempio n. 10
0
def test_json():
    with Given(app, 'example.com'):
        assert status == 0
        assert stdout == 'example.com'

        when('-c json example.com post {foo: "bar"}')
        assert status == 0
        assert stdout == \
            '-XPOST --data \'{"foo": "bar"}\' ' \
            '-H"Content-Type: application/json" ' \
            'example.com'
Esempio n. 11
0
def test_signal():
    with Given(app, nowait=True) as s:
        # Wait some moments
        time.sleep(1)
        s.wait('bar')
        assert stdout == 'bar\n'
        assert status == 0

        when(stdin='baz')
        s.wait()
        assert stdout == 'baz\n'
        assert status == 0
Esempio n. 12
0
def test_configuration_dump():

    with Given(app, 'configuration dump'):
        assert stderr == ''
        assert status == 0
        assert len(stdout.proxied_object) > 100

        filename = tempfile.mktemp()
        when(given + filename)
        assert stderr == ''
        assert status == 0
        assert path.exists(filename)
Esempio n. 13
0
def test_querystring():
    with Given(app, 'example.com'):
        assert status == 0
        assert stdout == 'example.com'

        when(given + 'POST ?foo=bar')
        assert status == 0
        assert stdout == '-XPOST example.com?foo=bar'

        when(given + 'POST ?foo=bar ?baz=qux')
        assert status == 0
        assert stdout == '-XPOST example.com?foo=bar&baz=qux'
Esempio n. 14
0
def test_jwt():
    with Given(app, 'jwt create'):
        assert stderr == ''
        assert status == 0
        assert len(stdout) > 10

        when(given + '\'{"foo": 1}\'')
        assert stderr == ''
        assert status == 0
        header, payload, signature = stdout.encode().split(b'.')
        payload = base64.urlsafe_b64decode(payload)
        assert json.loads(payload) == {'foo': 1}
Esempio n. 15
0
def test_basic_pipeline():
    app = Application('foo', 'tests.test_pipeline:foo')
    with Given(app):
        assert status == 0
        assert stdout == ''
        assert stderr == ''

        when(stdin='bar')
        assert status == 0
        assert stdout == f'input: bar\n'

        when(stdin='bad')
        assert status == 1
        assert stderr == f'error: bad\n'
Esempio n. 16
0
def test_applicationcli_default():
    cliapp = CLIApplication('foo', 'tests.test_applicationcli:app.climain')
    with Given(cliapp, '--help'):
        assert status == 0

        when('foo')
        assert status == 0
        assert stdout == 'foo\n'

        with tempfile.NamedTemporaryFile() as f:
            f.write(b'title: bar')
            f.flush()
            when(f'--configuration-file {f.name} foo')
            assert status == 0
            assert stdout == 'bar\n'
Esempio n. 17
0
def test_signal():
    with Given(app, nowait=True) as s:
        # Wait some moments
        time.sleep(1)
        s.kill()
        s.wait()
        assert stdout == 'Signal received: 15\n'
        assert status == -15

        when()
        time.sleep(1)
        s.kill(signal.SIGINT)
        s.wait()
        assert stdout == 'Signal received: 2\n'
        assert status == -2
Esempio n. 18
0
def test_simple():
    app = Application('foo', 'tests.test_simple:Foo.quickstart')
    with Given(app):
        assert stderr == ''
        assert stdout == 'foo done\n'
        assert status == 0

        when(['-h'])
        assert status == 0
        assert stderr == ''
        assert stdout == EXPECTED_HELP

        when(['--invalid'])
        assert status == 2
        assert stderr == EXPECTED_USAGE
        assert stdout == ''
def test_appcli_mule_start(db):
    session = db()
    task = WorkerTask()
    session.add(task)
    session.commit()

    with Given(app, 'mule start', nowait=True):
        time.sleep(2)
        story.kill()
        story.wait()
        assert status == -15

        when(given + '--query-interval 1')
        time.sleep(2)
        story.kill()
        story.wait()
        assert status == -15
Esempio n. 20
0
def test_appcli_worker_start(db):
    session = db()
    task = WorkerTask()
    session.add(task)
    session.commit()

    with Given(app, 'worker start', nowait=True):
        time.sleep(2)
        story.kill()
        story.wait()
        assert status == -15

        when(given + '--gap 1')
        time.sleep(2)
        story.kill()
        story.wait()
        assert status == -15
Esempio n. 21
0
def test_jwt():
    foo.configure(force=True)
    pirincipal = TimedJSONWebSignatureSerializer(
        settings.jwt.secret, algorithm_name=settings.jwt.algorithm)

    with Given(app, ['jwt', 'create']):
        assert stderr == ''
        token = f'{stdout}'[:-1]
        assert pirincipal.loads(token) == {}

        # Create a jwt token with a payload
        payload = dict(a=1)
        when(given + f'\'{json.dumps(payload)}\'')
        assert stderr == ''
        token = f'{stdout}'[:-1]
        assert pirincipal.loads(token) == payload

        # Create a expired token
        when(given + '-e -1')
        assert stderr == ''
        token = f'{stdout}'[:-1]
        with pytest.raises(SignatureExpired):
            pirincipal.loads(token)
Esempio n. 22
0
def test_signal():
    with Given(app, nowait=True) as s:
        # Wait some moments
        if os.name == "nt":
            # Windows doesn't support the Unix signals.
            # Simply check if the process didn't timeout.
            time.sleep(1)
            s.kill()
            s.wait()
            assert stdout == ''
        else:
            time.sleep(1)
            s.kill()
            s.wait()
            assert stdout == 'Signal received: 15\n'
            assert status == -15

            when()
            time.sleep(1)
            s.kill(signal.SIGINT)
            s.wait()
            assert stdout == 'Signal received: 2\n'
            assert status == -2
Esempio n. 23
0
def test_bash_autocompletion_virtualenv():
    app = Application('foo', 'tests.test_completion:Foo.quickstart')
    with tempfile.TemporaryDirectory() as venvdir:
        os.mkdir(path.join(venvdir, 'bin'))
        with Given(app, ['completion'], environ={'VIRTUAL_ENV': venvdir}):
            assert stdout == EXPECTED_HELP
            assert status == 0

            when(given + ['install', '-s'])
            assert stderr == 'The -s/--system-wide flag can not be used ' \
                'within virtualenv\n'
            assert status == 1
            when(['completion', 'install'])

            when(given + ['uninstall', '-s'])
            assert stderr == 'The -s/--system-wide flag can not be used ' \
                'within virtualenv\n'
            assert status == 1
            when(['completion', 'uninstall'])
Esempio n. 24
0
def test_environ():
    with Given(app, environ={'bar': 'baz'}):
        assert stdout == 'bar: baz\n'

        when(environ=given - 'bar')
        assert stdout == '\n'

        when(environ=given + {'qux': 'quux'})
        assert stdout == 'bar: baz qux: quux\n'

        when(environ=given | {'bar': 'quux'})
        assert stdout == 'bar: quux\n'
Esempio n. 25
0
def test_contenttypes():
    with Given(app, 'example.com'):
        assert status == 0
        assert stdout == 'example.com'

        when(given + '-cplain')
        assert status == 0
        assert stdout == '-H"Content-Type: text/plain" example.com'

        when(given + '-cjson')
        assert status == 0
        assert stdout == '-H"Content-Type: application/json" example.com'

        when(given + '-cmultipart')
        assert status == 0
        assert stdout == '-H"Content-Type: multipart/form-data" example.com'

        when(given + '-curlencoded')
        assert status == 0
        assert stdout == \
            '-H"Content-Type: application/x-www-form-urlencoded" example.com'
Esempio n. 26
0
def test_applicationcli():
    cliapp = CLIApplication('example', 'tests.test_cli:app.climain')
    with Given(cliapp, 'db --help'):
        assert status == 0
        assert stderr == ''

        when('db drop')
        when('db create')
        assert status == 0
        assert stderr == ''

        when('db drop')
        assert status == 0
        assert stderr == ''
Esempio n. 27
0
def test_arguments():
    with Given(app, ['bar']):
        assert stdout == 'foo bar\n'
        assert status == 0

        when(given - 'bar')
        assert stdout == 'foo\n'

        when(given + 'baz')
        assert stdout == 'foo bar baz\n'

    with Given(app, 'bar'):
        assert stdout == 'foo bar\n'

        when('baz')
        assert stdout == 'foo baz\n'

    # Given without arguments
    with Given(app):

        # Then, when with the given
        when(given + 'qux')
        assert stdout == 'foo qux\n'
Esempio n. 28
0
def test_multipart():
    with Given(app, 'example.com'):
        assert status == 0
        assert stdout == 'example.com'

        when('example.com post [email protected]')
        assert status == 0
        assert stdout == \
            '-XPOST [email protected] example.com'

        when(given + 'POST foo=bar')
        assert status == 0
        assert stdout == '-XPOST -Ffoo=bar example.com'

        when(given + 'POST foo=bar baz=qux')
        assert status == 0
        assert stdout == '-XPOST -Ffoo=bar -Fbaz=qux example.com'
Esempio n. 29
0
def test_mutex():
    app = Application('foo', 'tests.test_mutex:Foo.quickstart')
    with Given(app):
        assert stderr == ''
        assert stdout == 'foo done\n'
        assert status == 0

        when(['--bar'])
        assert status == 0
        assert stderr == ''
        assert stdout == 'foo done\n'

        when(['--baz'])
        assert status == 0
        assert stderr == ''
        assert stdout == 'foo done\n'

        when(['--bar --baz'])
        assert status == 2
        assert stderr == EXPECTED_USAGE
        assert stdout == ''
Esempio n. 30
0
def test_list_manipulators():
    with Given(app, ['bar']):
        assert stderr == 'foo bar\n'

        with pytest.raises(ValueError):
            when(given + {'invalid': 'qux'})

        with pytest.raises(ValueError):
            when(given | {'bar': 'qux'})

        when(given - 'bar')
        assert stderr == 'foo\n'

        with pytest.raises(ValueError):
            when(given - 'missing')

        when(given + ['baz', 'qux', 'quux'])
        assert stderr == 'foo bar baz qux quux\n'

        when(given - ['bar'])
        assert stderr == 'foo\n'

        class InvalidType:
            pass

        with pytest.raises(TypeError):
            when(given - InvalidType())

        with pytest.raises(TypeError):
            when(given | InvalidType())