Beispiel #1
0
    def addNode(self, name: str) -> TestNode:
        if name in self.nodes:
            error("{} already added".format(name))
        assert name in self.nodeReg
        ha, cliname, cliha = self.nodeReg[name]

        config_helper = PNodeConfigHelper(name, self.config, chroot=self.tmpdir)

        seed = randomSeed()
        if self.keyshare:
            learnKeysFromOthers(config_helper.keys_dir, name, self.nodes.values())

        testNodeClass = self.testNodeClass
        node = self.enter_context(
            testNodeClass(name=name,
                          ha=ha,
                          cliname=cliname,
                          cliha=cliha,
                          config_helper=config_helper,
                          primaryDecider=self.primaryDecider,
                          pluginPaths=self.pluginPaths,
                          seed=seed))

        if self.keyshare:
            tellKeysToOthers(node, self.nodes.values())

        self.nodes[name] = node
        self.__dict__[name] = node
        return node
Beispiel #2
0
 def __init__(self, length: int, **kwargs):
     if not isinstance(length, int):
         error('length should be integer', TypeError)
     if length < 1:
         error('should be greater than 0', ValueError)
     self.length = length
     super().__init__(**kwargs)
def checkDblImp():
    """
    Added this because I spent the better part of an evening troubleshooting an
    issue cause by double import. We were importing test.helper in one
    place, and test_helper in another, and python sees them as two different
    modules, and imported the same file twice. This caused genHa to be loaded
    twice, which caused overlapping ports to be assigned. Took a long time to
    track this down. I'm sure there's a better way to do this, but this seems
    to work for the basic testing I did.
    """
    logger.info("-------------checking for double imports-------------")
    ignore = {'posixpath.py',
              'helpers/pydev/pydevd.py',
              'importlib/_bootstrap.py',
              'importlib/_bootstrap_external.py',
              'helpers/pycharm/pytestrunner.py',
              'test/__init__.py',
              'site-packages/pytest.py',
              'python3.5/os.py',
              'python3.5/re.py'}

    files = [x.__file__ for x in list(sys.modules.values())
             if hasattr(x, "__file__")]
    dups = set([x for x in files if files.count(x) > 1])
    ignoreddups = {d for d in dups for i in ignore if i in d}
    filtereddups = dups - ignoreddups
    if filtereddups:
        error("Doubly imported files detected {}".format(filtereddups))
Beispiel #4
0
    def addNode(self, name: str) -> TestNode:
        if name in self.nodes:
            error("{} already added".format(name))
        assert name in self.nodeReg
        ha, cliname, cliha = self.nodeReg[name]

        config_helper = PNodeConfigHelper(name,
                                          self.config,
                                          chroot=self.tmpdir)

        seed = randomSeed()
        if self.keyshare:
            learnKeysFromOthers(config_helper.keys_dir, name,
                                self.nodes.values())

        testNodeClass = self.testNodeClass
        node = self.enter_context(
            testNodeClass(name=name,
                          ha=ha,
                          cliname=cliname,
                          cliha=cliha,
                          config_helper=config_helper,
                          primaryDecider=self.primaryDecider,
                          pluginPaths=self.pluginPaths,
                          seed=seed))

        if self.keyshare:
            tellKeysToOthers(node, self.nodes.values())

        self.nodes[name] = node
        self.__dict__[name] = node
        return node
Beispiel #5
0
def checkDblImp():
    """
    Added this because I spent the better part of an evening troubleshooting an
    issue cause by double import. We were importing test.helper in one
    place, and test_helper in another, and python sees them as two different
    modules, and imported the same file twice. This caused genHa to be loaded
    twice, which caused overlapping ports to be assigned. Took a long time to
    track this down. I'm sure there's a better way to do this, but this seems
    to work for the basic testing I did.
    """
    logger.info("-------------checking for double imports-------------")
    ignore = {
        'posixpath.py', 'helpers/pydev/pydevd.py', 'importlib/_bootstrap.py',
        'importlib/_bootstrap_external.py', 'helpers/pycharm/pytestrunner.py',
        'test/__init__.py', 'site-packages/pytest.py', 'python3.5/os.py',
        'python3.5/re.py'
    }

    files = [
        x.__file__ for x in list(sys.modules.values())
        if hasattr(x, "__file__")
    ]
    dups = set([x for x in files if files.count(x) > 1])
    ignoreddups = {d for d in dups for i in ignore if i in d}
    filtereddups = dups - ignoreddups
    if filtereddups:
        error("Doubly imported files detected {}".format(filtereddups))
Beispiel #6
0
 def __init__(self, length: int, **kwargs):
     if not isinstance(length, int):
         error('length should be integer', TypeError)
     if length < 1:
         error('should be greater than 0', ValueError)
     self.length = length
     super().__init__(**kwargs)
    def serialize(self, obj, level=0, objname=None, topLevelKeysToIgnore=None,
                  toBytes=True):
        """
        Create a string representation of the given object.

        Examples:
        ::
        >>> serialize("str")
        'str'
        >>> serialize([1,2,3,4,5])
        '1,2,3,4,5'
        >>> signing.serlize({1:'a', 2:'b'})
        '1:a|2:b'
        >>> signing.serlize({1:'a', 2:'b', 3:[1,{2:'k'}]})
        '1:a|2:b|3:1,2:k'

        :param obj: the object to serlize
        :param level: a parameter used internally for recursion to serialize nested
         data structures
         :param topLevelKeysToIgnore: the list of top level keys to ignore for
         serialization
        :return: a string representation of `obj`
        """
        res = None
        if not isinstance(obj, acceptableTypes):
            error("invalid type found {}: {}".format(objname, obj))
        elif isinstance(obj, str):
            res = obj
        elif isinstance(obj, dict):
            if level > 0:
                keys = list(obj.keys())
            else:
                topLevelKeysToIgnore = topLevelKeysToIgnore or []
                keys = [k for k in obj.keys() if k not in topLevelKeysToIgnore]
            keys.sort()
            strs = []
            for k in keys:
                onm = ".".join([str(objname), str(k)]) if objname else k
                strs.append(
                    str(k) + ":" + self.serialize(obj[k], level + 1, onm, toBytes=False))
            res = "|".join(strs)
        elif isinstance(obj, Iterable):
            strs = []
            for o in obj:
                strs.append(self.serialize(
                    o, level + 1, objname, toBytes=False))
            res = ",".join(strs)
        elif obj is None:
            res = ""
        else:
            res = str(obj)

        # logger.trace("serialized msg {} into {}".format(obj, res))

        if not toBytes:
            return res

        return res.encode('utf-8')
def genTestClient(nodes=None,
                  nodeReg=None,
                  tmpdir=None,
                  testClientClass=TestClient,
                  identifier: Identifier = None,
                  verkey: str = None,
                  bootstrapKeys=True,
                  ha=None,
                  usePoolLedger=False,
                  name=None,
                  sighex=None) -> (TestClient, Wallet):
    if not usePoolLedger:
        nReg = nodeReg
        if nodeReg:
            assert isinstance(nodeReg, dict)
        elif hasattr(nodes, "nodeReg"):
            nReg = nodes.nodeReg.extractCliNodeReg()
        else:
            error("need access to nodeReg")
        for k, v in nReg.items():
            assert isinstance(k, str)
            assert (isinstance(v, HA) or isinstance(v[0], HA))
    else:
        logger.debug("TestClient using pool ledger")
        nReg = None

    ha = genHa() if not ha else ha
    name = name or "testClient{}".format(ha.port)

    tc = testClientClass(name,
                         nodeReg=nReg,
                         ha=ha,
                         basedirpath=tmpdir,
                         sighex=sighex)

    if not usePoolLedger and nodes:
        for node in nodes:
            stack = node.clientstack
            initRemoteKeys(tc.name,
                           stack.name,
                           tc.keys_dir,
                           stack.verhex,
                           override=True)

    w = None  # type: Wallet
    if bootstrapKeys and nodes:
        if not identifier or not verkey:
            # no identifier or verkey were provided, so creating a wallet
            w = Wallet("test")
            w.addIdentifier()
            identifier = w.defaultId
            verkey = w.getVerkey()
        bootstrapClientKeys(identifier, verkey, nodes)
    return tc, w
def create_recommendation():
    database.database.ping(reconnect=True)
    if not request.json or not 'title' in request.json or not 'userId' in request.json:
        e = error(400, 'Must include a title and a userId')
        return e.toResponse()

    try:
        romaji = request.json['japaneseTitles']['romaji']
    except Exception:
        romaji = None

    try:
        native = request.json['japaneseTitles']['native']
    except Exception:
        native = None

    try:
        description = request.json['description']
    except Exception:
        description = None

    try:
        score = request.json['score']
    except Exception:
        score = None

    try:
        anilistLink = request.json['links']['anilist']
    except Exception:
        anilistLink = None

    try:
        malLink = request.json['links']['mal']
    except Exception:
        malLink = None

    try:
        image = request.json['image']
    except Exception:
        image = None

    anime = Anime(request.json['title'], romaji, native, description, score,
                  anilistLink, malLink, image, request.json['userId'])

    code = database.insert(anime)

    if code == 201:
        return jsonify({'anime': anime.toDict(idEndpoint)}), 201
    else:
        e = error(code, 'The anime already exists')
        return e.toResponse()
def delete_recommendation():
    database.database.ping(reconnect=True)
    title = request.args.get('title', default=None, type=str)
    userId = request.args.get('userId', default=None, type=int)
    if not title or not userId:
        e = error(400, 'Must supply a title and userId as a url parameter')
        return e.toResponse()

    database.delete(title, userId)
    return jsonify({'Result': 'Deleted'})
Beispiel #11
0
def getSymmetricallyEncryptedVal(
        val, secretKey: Union[str, bytes]=None) -> Tuple[str, str]:
    """
    Encrypt the provided value with symmetric encryption

    :param val: the value to encrypt
    :param secretKey: Optional key, if provided should be either in hex or bytes
    :return: Tuple of the encrypted value and secret key encoded in hex
    """
    if isinstance(val, str):
        val = val.encode("utf-8")
    if secretKey:
        if isHex(secretKey):
            secretKey = bytes(bytearray.fromhex(secretKey))
        elif not isinstance(secretKey, bytes):
            error("Secret key must be either in hex or bytes")
        box = libnacl.secret.SecretBox(secretKey)
    else:
        box = libnacl.secret.SecretBox()
    return box.encrypt(val).hex(), box.sk.hex()
def get_recommendation_byid(recommendationId):
    database.database.ping(reconnect=True)
    payload = []
    for a in database.get(params={'id': recommendationId}):
        payload.append(a.toDict(idEndpoint))

    if not payload:
        e = error(404, 'No recommendations were found with that id')
        return e.toResponse()

    return jsonify({'anime': payload})
Beispiel #13
0
def getSymmetricallyEncryptedVal(
        val, secretKey: Union[str, bytes]=None) -> Tuple[str, str]:
    """
    Encrypt the provided value with symmetric encryption

    :param val: the value to encrypt
    :param secretKey: Optional key, if provided should be either in hex or bytes
    :return: Tuple of the encrypted value and secret key encoded in hex
    """
    if isinstance(val, str):
        val = val.encode("utf-8")
    if secretKey:
        if isHex(secretKey):
            secretKey = bytes(bytearray.fromhex(secretKey))
        elif not isinstance(secretKey, bytes):
            error("Secret key must be either in hex or bytes")
        box = libnacl.secret.SecretBox(secretKey)
    else:
        box = libnacl.secret.SecretBox()
    return box.encrypt(val).hex(), box.sk.hex()
def get_anime_byId():
    Id = request.args.get('id', default=None, type=int)

    if not Id:
        e = error(400, "id url parameter is required")
        return e.toResponse()
    
    search_variables = {'id': Id}
    payload = {
        'query': anime_query,
        'variables': search_variables
    }

    response = req.post(url, json=payload).json()
    req.close()

    anime = parse(response)

    return jsonify({'anime': anime.toDict()})
def search_animes_bytitle():
    title = request.args.get('title', default=None, type=str)

    if not title:
        e = error(400, "title url parameter is required")
        return e.toResponse()

    search_variables = {
        'search': title
    }
    payload = {
        'query': search_query,
        'variables': search_variables
    }

    response = req.post(url, json=payload).json()
    req.close()

    result = parseSearch(response)

    return jsonify({'anime': result})
def get_recommendation():
    database.database.ping(reconnect=True)
    params = {}

    title = request.args.get('title', default=None, type=str)
    userId = request.args.get('userId', default=None, type=int)

    if title:
        params.update({'title': title})
    if userId:
        params.update({'userId': userId})

    payload = []
    for a in database.get(params=params):
        payload.append(a.toDict(idEndpoint))

    if (title or userId) and not payload:
        e = error(404, 'No recommendations were found')
        return e.toResponse()

    return jsonify({'anime': payload})
Beispiel #17
0
    def __init__(self,
                 config,
                 names: Iterable[str] = None,
                 count: int = None,
                 nodeReg=None,
                 tmpdir=None,
                 keyshare=True,
                 primaryDecider=None,
                 pluginPaths: Iterable[str] = None,
                 testNodeClass=TestNode):

        super().__init__()
        self.tmpdir = tmpdir
        assert config is not None
        self.config = config
        self.keyshare = keyshare
        self.primaryDecider = primaryDecider
        self.pluginPaths = pluginPaths

        self.testNodeClass = testNodeClass
        self.nodes = OrderedDict()  # type: Dict[str, TestNode]
        # Can use just self.nodes rather than maintaining a separate dictionary
        # but then have to pluck attributes from the `self.nodes` so keeping
        # it simple a the cost of extra memory and its test code so not a big
        # deal
        if nodeReg:
            self.nodeReg = nodeReg
        else:
            nodeNames = (names if names is not None and count is None else
            genNodeNames(count) if count is not None else
            error("only one of either names or count is required"))
            self.nodeReg = genNodeReg(
                names=nodeNames)  # type: Dict[str, NodeDetail]
        for name in self.nodeReg.keys():
            self.addNode(name)
        # The following lets us access the nodes by name as attributes of the
        # NodeSet. It's not a problem unless a node name shadows a member.
        self.__dict__.update(self.nodes)
Beispiel #18
0
    def __init__(self,
                 config,
                 names: Iterable[str] = None,
                 count: int = None,
                 nodeReg=None,
                 tmpdir=None,
                 keyshare=True,
                 primaryDecider=None,
                 pluginPaths: Iterable[str] = None,
                 testNodeClass=TestNode):

        super().__init__()
        self.tmpdir = tmpdir
        assert config is not None
        self.config = config
        self.keyshare = keyshare
        self.primaryDecider = primaryDecider
        self.pluginPaths = pluginPaths

        self.testNodeClass = testNodeClass
        self.nodes = OrderedDict()  # type: Dict[str, TestNode]
        # Can use just self.nodes rather than maintaining a separate dictionary
        # but then have to pluck attributes from the `self.nodes` so keeping
        # it simple a the cost of extra memory and its test code so not a big
        # deal
        if nodeReg:
            self.nodeReg = nodeReg
        else:
            nodeNames = (names if names is not None and count is None else
                         genNodeNames(count) if count is not None else error(
                             "only one of either names or count is required"))
            self.nodeReg = genNodeReg(
                names=nodeNames)  # type: Dict[str, NodeDetail]
        for name in self.nodeReg.keys():
            self.addNode(name)
        # The following lets us access the nodes by name as attributes of the
        # NodeSet. It's not a problem unless a node name shadows a member.
        self.__dict__.update(self.nodes)
Beispiel #19
0
 def getNodeName(node: NodeRef) -> str:
     return node if isinstance(node, str) \
         else node.name if isinstance(node, Node) \
         else error("Expected a node or node name")
Beispiel #20
0
 def getNode(self, node: NodeRef) -> TestNode:
     return node if isinstance(node, Node) \
         else self.nodes.get(node) if isinstance(node, str) \
         else error("Expected a node or node name")
 def core_authenticator(self):
     if not self._authenticators:
         error('No authenticator registered yet', RuntimeError)
     return self._authenticators[0]
 def register_hook(self, hook_id, hook: Callable):
     if hook_id not in self.hooks:
         error('Unknown hook id', KeyError)
     self.hooks[hook_id].append(hook)
Beispiel #23
0
 def getNode(self, node: NodeRef) -> TestNode:
     return node if isinstance(node, Node) \
         else self.nodes.get(node) if isinstance(node, str) \
         else error("Expected a node or node name")
Beispiel #24
0
    def serialize(self,
                  obj,
                  level=0,
                  objname=None,
                  topLevelKeysToIgnore=None,
                  toBytes=True):
        """
        Create a string representation of the given object.

        Examples:
        ::
        >>> serialize("str")
        'str'
        >>> serialize([1,2,3,4,5])
        '1,2,3,4,5'
        >>> signing.serlize({1:'a', 2:'b'})
        '1:a|2:b'
        >>> signing.serlize({1:'a', 2:'b', 3:[1,{2:'k'}]})
        '1:a|2:b|3:1,2:k'

        :param obj: the object to serlize
        :param level: a parameter used internally for recursion to serialize nested
         data structures
         :param topLevelKeysToIgnore: the list of top level keys to ignore for
         serialization
        :return: a string representation of `obj`
        """
        res = None
        if not isinstance(obj, acceptableTypes):
            error("invalid type found {}: {}".format(objname, obj))
        elif isinstance(obj, str):
            res = obj
        elif isinstance(obj, dict):
            if level > 0:
                keys = list(obj.keys())
            else:
                topLevelKeysToIgnore = topLevelKeysToIgnore or []
                keys = [k for k in obj.keys() if k not in topLevelKeysToIgnore]
            keys.sort()
            strs = []
            for k in keys:
                onm = ".".join([objname, k]) if objname else k
                strs.append(
                    str(k) + ":" +
                    self.serialize(obj[k], level + 1, onm, toBytes=False))
            res = "|".join(strs)
        elif isinstance(obj, Iterable):
            strs = []
            for o in obj:
                strs.append(
                    self.serialize(o, level + 1, objname, toBytes=False))
            res = ",".join(strs)
        elif obj is None:
            res = ""
        else:
            res = str(obj)

        # logger.trace("serialized msg {} into {}".format(obj, res))

        if not toBytes:
            return res

        return res.encode('utf-8')
Beispiel #25
0
 def getNodeName(node: NodeRef) -> str:
     return node if isinstance(node, str) \
         else node.name if isinstance(node, Node) \
         else error("Expected a node or node name")
Beispiel #26
0
 def register_hook(self, hook_id, hook: Callable):
     if hook_id not in self.hooks:
         error('Unknown hook id', KeyError)
     self.hooks[hook_id].append(hook)
 def core_authenticator(self):
     if not self._authenticators:
         error('No authenticator registered yet', RuntimeError)
     return self._authenticators[0]