Example #1
0
 def test_can_accept_config_object(self):
     # create a config object
     config1 = Config(foo='bar')
     # create a config object out of that object
     config2 = Config(config1)
     # validate the config object
     self.check_configuration(config2,
         "Configuration object could not accept other config objects."
     )
Example #2
0
 def test_can_set_keys_as_attrbutes(self):
     # create a config object to test
     config = Config(foo='bar')
     # update the attrbute
     config.foo = 'quz'
     # validate the config object
     assert config['foo'] == 'quz', (
         "Attributes could not be updated."
     )
Example #3
0
    def test_can_update_with_another_config(self):
        # create a config object
        config1 = Config(foo='bar')
        # create a config object out of that object
        config2 = Config(bar='baz')

        # merge the two configs
        config1.update({'bar':'baz'})
        # make sure one can be applied on the other
        assert config1 == {'foo': 'bar', 'bar': 'baz'}, (
            "Config could not be updated with another."
        )
Example #4
0
    def __init__(
        self,
        name=None,
        schema=None,
        action_handler=None,
        config=None,
        auth=True,
    ):

        self.name = name or self.name or type(self).name
        self.app = None
        self.__name__ = self.name
        self.event_broker = None
        self.schema = schema or self.schema

        # wrap the given configuration in the nautilus wrapper
        self.config = Config(self.config, config)

        # initialize the service
        self.init_app()
        self.init_routes()
        self.init_action_handler()

        # placeholders
        self._http_server = None
        self._server_handler = None
Example #5
0
 def test_can_accept_kwds(self):
     # create a config object to test
     config = Config(foo='bar')
     # validate the config object
     self.check_configuration(config,
         "Configuration object could not accept keywords."
     )
Example #6
0
 def test_can_accept_multiple_arguments(self):
     # create a config object with two arguments
     config = Config({'foo': 'bar'}, {'bar': 'baz'})
     # make sure both applied
     assert config['foo'] == 'bar' and config['bar'] == 'baz', (
         "Config could not mix in multiple values."
     )
Example #7
0
 def test_can_read_keys_as_attribute(self):
     # create a config object to test
     config = Config(foo='bar')
     # validate the config object
     assert config.foo == 'bar', (
         "Attribute could not be read"
     )
Example #8
0
 def test_can_accept_none(self):
     # create a config with nothing
     config = Config(None)
     # make sure it created an empty config
     assert config == {}, (
         "Config(None) did not create an empty config."
     )
Example #9
0
 def test_can_accept_dict(self):
     # the configuration dictionary
     config_dict = dict(foo='bar')
     # create a config object out of the dictionary
     config = Config(config_dict)
     # validate the config object
     self.check_configuration(config,
         "Configuration object could not accept dictionaries."
     )
Example #10
0
    def test_can_accept_type(self):
        # the configuration type
        class ConfigType:
            foo = 'bar'
            # add a function to the test too
            def func(self): pass

        # create the config object from the type
        config = Config(ConfigType)
        # validate the config object
        self.check_configuration(config,
            "Configuration object could not accept types."
        )
Example #11
0
    async def _handle_query(self, query):

        # if there hasn't been a schema generated yet
        if not self.schema:
            # yell loudly
            result = json.dumps({
                'data': {},
                'errors': ['No schema for this service.']
            })
            # send the result of the introspection to the user
            return Response(body=result.encode())

        try:
            # figure out if the query is an introspection
            is_introspection = graphql.parse(
                query).definitions[0].name.value == 'IntrospectionQuery'
        # if something went wrong
        except AttributeError:
            # its not
            is_introspection = False

        # if the query is an introspection
        if is_introspection:
            # handle it using the schema
            introspection = self.service.schema.execute(query)
            result = json.dumps({
                'data':
                {key: value
                 for key, value in introspection.data.items()},
                'errors': introspection.errors
            })
            # send the result of the introspection to the user
            return Response(body=result.encode())

        # by default there is no current user
        current_user = None

        # if there is an authorization header
        if 'Authorization' in self.request.headers:
            # the authorization header value
            auth_header = self.request.headers['Authorization']
            # the name of the token method
            method = 'Bearer'
            # only accept bearer tokens
            if method in auth_header:
                # pull the session token out from the header value
                session_token = auth_header.replace(method, '').strip()
                # create a config object from the current user session
                current_user = Config(
                    self.service._read_session_token(session_token))

        # otherwise its a normal query/mutation so walk it like normal
        response = await parse_string(query,
                                      self.service.object_resolver,
                                      self.service.connection_resolver,
                                      self.service.mutation_resolver,
                                      extra_mutations={
                                          'loginUser':
                                          self.service.login_user,
                                          'registerUser':
                                          self.service.register_user
                                      },
                                      current_user=current_user)

        # pass the result to the request
        return Response(body=json.dumps(response).encode())