コード例 #1
0
ファイル: test_command.py プロジェクト: cosphere-org/lily
    def test_conf__is_saved_on_function(self):

        source = TestCommands.post.command_conf.pop('source')
        assert TestCommands.post.command_conf == {
            'name':
            'MAKE_IT',
            'method':
            'post',
            'meta':
            Meta(title='hi there',
                 description='it is made for all',
                 domain=Domain(id='test', name='test management')),
            'access':
            Access(access_list=['PREMIUM', 'SUPER_PREMIUM']),
            'input':
            Input(body_parser=TestCommands.BodyParser),
            'output':
            Output(serializer=TestCommands.ClientSerializer),
            'is_atomic':
            False,
            'fn':
            TestCommands.post.command_conf['fn'],
        }

        assert source.filepath == '/tests/test_base/test_command.py'
        assert source.start_line == 117
        assert source.end_line == 131
コード例 #2
0
ファイル: test_test.py プロジェクト: cosphere-org/lily
class HttpView(View):
    @command(name='HTTP_IT',
             meta=Meta(title='http it',
                       description='http it',
                       domain=Domain(id='d', name='d')),
             access=Access(access_list=[]))
    def get(self, request):
        return HttpResponse('http it!')
コード例 #3
0
ファイル: test_access.py プロジェクト: cosphere-org/lily
    def test_serialize(self):

        a = Access(access_list=['SUPER_USER'], is_private=True)

        assert AccessSerializer(a).data == {
            '@type': 'access',
            'is_private': True,
            'access_list': ['SUPER_USER'],
        }
コード例 #4
0
ファイル: test_command.py プロジェクト: cosphere-org/lily
class DoAndGoCommands(HTTPCommands):
    @command(name='DO_AND_GO',
             meta=Meta(title='do and go',
                       domain=Domain(id='test', name='test management')),
             access=Access(access_list=['PREMIUM']))
    def post(self, request):

        raise self.event.Redirect('DONE_SO_GO',
                                  redirect_uri='http://go.there.org')
コード例 #5
0
ファイル: test_command.py プロジェクト: cosphere-org/lily
class BrokenSerializerCommands(HTTPCommands):
    class ClientSerializer(serializers.Serializer):

        _type = 'client'

        card_id = serializers.SerializerMethodField()

        def get_card_id(self, instance):
            raise EventFactory.AccessDenied('GO_AWAY')

    @command(name='BREAK_SERIALIZER',
             meta=Meta(title='break it',
                       domain=Domain(id='test', name='test management')),
             output=Output(serializer=ClientSerializer),
             access=Access(access_list=['PREMIUM', 'SUPER_PREMIUM']))
    def post(self, request):
        raise self.event.Executed(event='NEVER_REACH_IT',
                                  instance=FakeClient(name="Jake"))
コード例 #6
0
ファイル: test_access.py プロジェクト: cosphere-org/lily
    def test_defaults(self):

        a = Access()

        assert a.access_list is None
        assert a.is_private is False
コード例 #7
0
ファイル: test_access.py プロジェクト: cosphere-org/lily
    def test_arguments_are_saved(self):

        a = Access(access_list=['SUPER_USER'], is_private=True)

        assert a.access_list == ['SUPER_USER']
        assert a.is_private is True
コード例 #8
0
class EntryPointCommands(HTTPCommands):
    class EntryPointSerializer(serializers.Serializer):
        class VersionInfoSerializer(serializers.Serializer):

            _type = 'version_info'

            deployed = serializers.CharField()

            displayed = serializers.CharField()

            available = serializers.ListField(child=serializers.CharField())

        _type = 'entrypoint'

        version_info = VersionInfoSerializer()

        name = serializers.CharField()

        commands = serializers.DictField(child=CommandSerializer())

        enums = serializers.ListField(child=serializers.DictField())

    class QueryParser(parsers.Parser):

        commands = parsers.ListField(child=parsers.CharField(), default=None)

        with_schemas = parsers.BooleanField(default=True)

        is_private = parsers.BooleanField(default=None)

        domain_id = parsers.CharField(default=None)

        version = parsers.CharField(default=None)

    @command(
        name=name.Read('ENTRY_POINT'),
        meta=Meta(title='Read Entry Point',
                  description='''
                Serve Service Entry Point data:
                - current or chosen version of the service
                - list of all available commands together with their
                  configurations
                - examples collected for a given service.

            ''',
                  domain=Domain(id='docs', name='Docs Management')),
        access=Access(
            is_private=True,
            access_list=settings.LILY_ENTRYPOINT_COMMANDS_ACCESS_LIST),
        input=Input(query_parser=QueryParser),
        output=Output(serializer=EntryPointSerializer),
    )
    def get(self, request):

        command_names = request.input.query['commands']

        is_private = request.input.query['is_private']

        domain_id = request.input.query['domain_id']

        version = request.input.query['version']

        config = Config()

        commands = self.get_commands(version)
        enums = commands.pop('enums')

        if command_names:
            commands = {
                command_name: commands[command_name]
                for command_name in command_names
            }

        if is_private is not None:
            commands = {
                name: command
                for name, command in commands.items()
                if command['access']['is_private'] == is_private
            }

        if domain_id:
            commands = {
                name: command
                for name, command in commands.items() if command['meta']
                ['domain']['id'].lower() == domain_id.lower()
            }

        raise self.event.Read({
            'name': config.name,
            'version_info': {
                'deployed': config.version,
                'displayed': version or config.version,
                'available': self.get_available_versions(),
            },
            'commands': commands,
            'enums': enums,
        })

    def get_available_versions(self):

        commands_dir_path = os.path.join(Config.get_lily_path(), 'commands')

        return sorted([
            commands_file.replace('.json', '')
            for commands_file in os.listdir(commands_dir_path)
        ],
                      key=lambda x: [int(e) for e in x.split('.')],
                      reverse=True)

    def get_commands(self, version=None):

        config = Config()
        version = version or config.version
        commands_dir_path = os.path.join(Config.get_lily_path(), 'commands')

        commands_path = os.path.join(commands_dir_path, f'{version}.json')
        with open(commands_path, 'r') as f:
            return json.loads(f.read())
コード例 #9
0
ファイル: test_test.py プロジェクト: cosphere-org/lily
class SampleView(View):
    @command(name='POST_IT',
             meta=Meta(title='post it',
                       description='post it',
                       domain=Domain(id='d', name='d')),
             access=Access(access_list=[]),
             output=Output(serializer=SampleSerializer))
    def post(self, request):
        raise self.event.Created(event='CREATED',
                                 context=request,
                                 data={'hello': 'post'})

    @command(name='GET_IT',
             meta=Meta(title='get it',
                       description='get it',
                       domain=Domain(id='d', name='d')),
             access=Access(access_list=[]),
             output=Output(serializer=SampleSerializer))
    def get(self, request):

        _type = request.GET.get('type')
        if _type in ['a', None]:
            raise self.event.Executed(event='LISTED',
                                      context=request,
                                      data={'hello': 'get.a'})

        if _type == 'b':
            raise self.event.Executed(event='LISTED',
                                      context=request,
                                      data={'hello': 'get.b'})

        if _type == 'c':
            raise self.event.Executed(event='LISTED_AGAIN',
                                      context=request,
                                      data={'hello': 'get.c'})

        if _type == 'd':
            raise self.event.DoesNotExist(event='ERROR_LISTED',
                                          context=request,
                                          data={'hello': 'get.d'})

    @command(name='PUT_IT',
             meta=Meta(title='put it',
                       description='put it',
                       domain=Domain(id='d', name='d')),
             access=Access(access_list=[]),
             output=Output(serializer=SampleSerializer))
    def put(self, request):
        raise self.event.Executed(event='UPDATED',
                                  context=request,
                                  data={'hello': 'put'})

    @command(name='DELETE_IT',
             meta=Meta(title='delete it',
                       description='delete it',
                       domain=Domain(id='d', name='d')),
             access=Access(access_list=[]),
             output=Output(serializer=SampleSerializer))
    def delete(self, request):
        raise self.event.Executed(event='DELETED',
                                  context=request,
                                  data={'hello': 'delete'})
コード例 #10
0
ファイル: test_command.py プロジェクト: cosphere-org/lily
class TestCommands(HTTPCommands):
    class BodyParser(parsers.Parser):

        name = parsers.CharField()

        age = parsers.IntegerField()

    class ClientSerializer(serializers.ModelSerializer):

        _type = 'client'

        card_id = serializers.SerializerMethodField()

        def get_access(self, instance):
            return [(TestCommands.put, True)]

        class Meta:
            model = FakeClient
            fields = ('name', 'card_id')

        def get_card_id(self, instance):
            return 190

    class SimpleSerializer(serializers.Serializer):
        _type = 'simple'

        amount = serializers.IntegerField()

    @command(name='MAKE_IT',
             meta=Meta(title='hi there',
                       description='it is made for all',
                       domain=Domain(id='test', name='test management')),
             input=Input(body_parser=BodyParser),
             output=Output(serializer=ClientSerializer),
             access=Access(access_list=['PREMIUM', 'SUPER_PREMIUM']))
    def post(self, request, user_id):
        raise self.event.Executed(event='MADE_IT',
                                  instance=FakeClient(name="Jake"))

    @command(name='GET_IT',
             meta=Meta(title='get',
                       description='get it...',
                       domain=Domain(id='get', name='get')),
             output=Output(serializer=ClientSerializer))
    def get(self, request):
        raise self.event.Executed(event='GET_IT',
                                  instance=FakeClient(name="Jake"))

    @command(name='BREAK',
             meta=Meta(title='break',
                       description='break it...',
                       domain=Domain(id='break', name='break')),
             output=Output(serializer=SimpleSerializer))
    def put(self, request):

        raise self.event.Executed(event='BROKEN',
                                  context=request,
                                  data=json.loads(request.body.decode('utf8')))

    @command(name='ATOMIC',
             meta=Meta(title='atomic',
                       description='atomic it...',
                       domain=Domain(id='atomic', name='atomic')),
             is_atomic='default')
    def delete(self, request):

        self.some_stuff()

        raise self.event.Executed(event='ATOMIC', context=request, data={})

    def some_stuff(self):
        pass