Exemplo n.º 1
0
    def test_claim_collection_fails_if_key_hash_not_set(self):
        coll, key = collection.make("socrates")
        assert_equals(coll["owner"], "socrates")
        del coll["key_hash"]

        coll = collection.claim_collection(coll, "plato", key)
        assert_equals(coll["owner"], "plato")
Exemplo n.º 2
0
def collection_create():
    """
    POST /collection
    creates new collection
    """
    response_code = None
    coll, key = collection.make(request.json.get("owner", None))
    refset_metadata = request.json.get("refset_metadata", None)
    if refset_metadata:
        coll["refset_metadata"] = refset_metadata
    coll["ip_address"] = request.remote_addr
    try:
        coll["title"] = request.json["title"]
        aliases = request.json["aliases"]
        (tiids, new_items) = item_module.create_or_update_items_from_aliases(aliases, myredis, mydao)
        for item in new_items:
            namespaces = item["aliases"].keys()
            mixpanel.track("Create:Item", {"Namespace":namespaces[0], "Source":"collection"}, request)

        if not tiids:
            abort(404, "POST /collection requires a list of [namespace, id] pairs.")
    except (AttributeError, TypeError):
        # we got missing or improperly formated data.
        logger.error(
            "we got missing or improperly formated data: '{id}' with {json}.".format(
                id=coll["_id"],
                json=str(request.json)))
        abort(404, "Missing arguments.")

    aliases_strings = [namespace+":"+nid for (namespace, nid) in aliases]
    # save dict of alias:tiid
    coll["alias_tiids"] = dict(zip(aliases_strings, tiids))

    logger.info(json.dumps(coll, sort_keys=True, indent=4))

    mydao.save(coll)
    response_code = 201 # Created
    logger.info(
        "saved new collection '{id}' with {num_items} items.".format(
            id=coll["_id"],
            num_items=len(coll["alias_tiids"])
        ))

    mixpanel.track("Create:Report", {"Report Id":coll["_id"]}, request)

    resp = make_response(json.dumps({"collection":coll, "key":key},
            sort_keys=True, indent=4), response_code)
    resp.mimetype = "application/json"
    return resp
Exemplo n.º 3
0
def collection_create():
    """
    POST /collection
    creates new collection
    """
    response_code = None
    coll, key = collection.make(request.json.get("owner", None))
    refset_metadata = request.json.get("refset_metadata", None)
    if refset_metadata:
        coll["refset_metadata"] = refset_metadata
    coll["ip_address"] = request.remote_addr
    try:
        coll["title"] = request.json["title"]
        aliases = request.json["aliases"]
        tiids = prep_collection_items(aliases)
        aliases_strings = [namespace + ":" + nid for (namespace, nid) in aliases]
    except (AttributeError, TypeError):
        # we got missing or improperly formated data.
        logger.error(
            "we got missing or improperly formated data: '{id}' with {json}.".format(
                id=coll["_id"], json=str(request.json)
            )
        )
        abort(404, "Missing arguments.")

    # save dict of alias:tiid
    coll["alias_tiids"] = dict(zip(aliases_strings, tiids))

    logger.info(json.dumps(coll, sort_keys=True, indent=4))

    mydao.save(coll)
    response_code = 201  # Created
    logger.info(
        "saved new collection '{id}' with {num_items} items.".format(id=coll["_id"], num_items=len(coll["alias_tiids"]))
    )

    resp = make_response(json.dumps({"collection": coll, "key": key}, sort_keys=True, indent=4), response_code)
    resp.mimetype = "application/json"
    return resp
Exemplo n.º 4
0
    def test_claim_collection_fails_with_wrong_key(self):
        coll, key = collection.make("socrates")
        assert_equals(coll["owner"], "socrates")

        coll = collection.claim_collection(coll, "plato", "wrong key")
        assert_equals(coll["owner"], "plato")
Exemplo n.º 5
0
    def test_claim_collection(self):
        coll, key = collection.make("socrates")
        assert_equals(coll["owner"], "socrates")

        coll = collection.claim_collection(coll, "plato", key)
        assert_equals(coll["owner"], "plato")
Exemplo n.º 6
0
 def test_create_returns_collection_and_update_key(self):
     coll, key = collection.make()
     assert collection.check_password_hash(coll["key_hash"], key)
Exemplo n.º 7
0
    def test_make_sets_owner(self):
        coll, key = collection.make()
        assert_equals(coll["owner"], None)

        coll, key = collection.make("socrates")
        assert_equals(coll["owner"], "socrates")
Exemplo n.º 8
0
 def test_make_creates_identifier(self):
     coll, key = collection.make()
     assert_equals(len(coll["_id"]), 6)