static void userAddTest(Node& node, TestRunner& tr) { tr.group("user add"); Messenger* messenger = node.getMessenger(); tr.test("add (valid)"); { Url url("/api/3.0/users"); // password update User user; user["email"] = "*****@*****.**"; user["username"] = "******"; user["password"] = "******"; user["confirm"] = "password"; user["tosAgree"] = "agree"; assertNoException( messenger->postSecureToBitmunk( &url, &user, NULL, node.getDefaultUserId())); printf("\nUser added.\n"); } tr.passIfNoException(); tr.ungroup(); }
static bool _doReverseNetTest( Node* node, Config& cfg, UserId userId, const char* token, string& serverUrl) { bool rval = false; // start creating public server URL serverUrl = "https://"; // setup post data, use blank host to indicate to the reverse netaccess // service that our public IP should be used in the netaccess test // use a 15 seconds timeout in waiting for a response DynamicObject out; out["host"] = ""; out["port"] = cfg["port"]; out["path"] = "/api/3.0/catalog/netaccess/test"; out["sellerId"] = userId; out["token"] = token; out["timeout"] = 15; DynamicObject in; // post to bitmunk Url url; url.format("/api/3.0/catalog/netaccess/rtest"); Messenger* m = node->getMessenger(); if(m->postSecureToBitmunk(&url, &out, &in, userId)) { rval = true; serverUrl.append(in["ip"]->getString()); serverUrl.push_back(':'); serverUrl.append(cfg["port"]->getString()); // send event Event ev; ev["type"] = EVENT_NET_ACCESS_SUCCESS; ev["details"]["userId"] = userId; ev["details"]["serverUrl"] = serverUrl.c_str(); node->getEventController()->schedule(ev); } return rval; }
static void acquireLicenseTest(Node& node, TestRunner& tr) { tr.group("media license"); Messenger* messenger = node.getMessenger(); tr.test("acquire signed media"); { // create url for obtaining media license Url url("/api/3.0/sva/contracts/media/2"); // get signed media Media media; assertNoException( messenger->postSecureToBitmunk( &url, NULL, &media, node.getDefaultUserId())); printf("\nSigned Media:\n"); dumpDynamicObject(media, false); } tr.passIfNoException(); tr.ungroup(); }
void ListingUpdater::sendUpdate(SellerListingUpdate& update) { MO_CAT_DEBUG(BM_CUSTOMCATALOG_CAT, "ListingUpdater sending seller listing update for user %" PRIu64 ": %s", getUserId(), JsonWriter::writeToString(update).c_str()); // create message to send after update DynamicObject msg; msg["updateResponse"] = true; msg["scheduleUpdateRequest"] = false; msg["error"] = false; // post to bitmunk DynamicObject result; Url url("/api/3.0/catalog/listings"); Messenger* m = mNode->getMessenger(); if(m->postSecureToBitmunk(&url, &update, &result, getUserId())) { // send result in message, include update ID separately so that // handling heartbeats above is simpler -- it doesn't matter if // the current update ID was valid or not, it will be stored in // "updateId" field msg["update"] = update; msg["updateResult"] = result; msg["updateId"] = result["updateId"]->getString(); // if the update wasn't a heartbeat, include a note that another // update should be run immediately after this result is processed if(update["payeeSchemes"]["updates"]->length() > 0 || update["payeeSchemes"]["removals"]->length() > 0 || update["listings"]["updates"]->length() > 0 || update["listings"]["removals"]->length() > 0) { msg["scheduleUpdateRequest"] = true; } } else { // if the error was an invalid update ID, do not record it as an error // and see if we can recover from it ExceptionRef ex = Exception::get(); if(ex->isType("bitmunk.mastercatalog.UpdateIdNotCurrent")) { msg["updateIdNotCurrent"] = true; msg["updateId"] = ex->getDetails()["currentUpdateId"]->getString(); } else { // FIXME: determine if error is a network error or if there // were just some bad wares that weren't accepted/bad remote update ID // failed to update ExceptionRef e = new Exception( "Could not send seller listing update.", "bitmunk.catalog.SellerListingUpdateError"); Exception::push(e); DynamicObject d = Exception::getAsDynamicObject(); msg["error"] = true; msg["exception"] = d; // see if we need to register for a new server ID because our catalog // expired and we no longer exist as a seller // FIXME: we want a different exception name for this if(ex->hasType("bitmunk.database.NotFound") || ex->hasType("bitmunk.database.Catalog.InvalidServerToken")) { // log message ... do new server registration MO_CAT_DEBUG(BM_CUSTOMCATALOG_CAT, "ListingUpdater user's (user ID %" PRIu64 ") seller does not exist, " "has expired, or server token is invalid: %s", getUserId(), JsonWriter::writeToString(d).c_str()); msg["reset"] = true; } else { // log generic error MO_CAT_ERROR(BM_CUSTOMCATALOG_CAT, "ListingUpdater: %s", JsonWriter::writeToString(d).c_str()); } } } // send message to self messageSelf(msg); // schedule a pending test net access request if(mTestNetAccessPending) { mTestNetAccessPending = false; DynamicObject msg; msg["testNetAccess"] = true; messageSelf(msg); } }
void ListingUpdater::updateServerUrl(Seller& seller) { // get user ID UserId userId = getUserId(); MO_CAT_DEBUG(BM_CUSTOMCATALOG_CAT, "ListingUpdater testing public internet access to catalog for user %" PRIu64, userId); // create message to send after test and update DynamicObject msg; msg["testNetAccessResponse"] = true; // do net access test string serverUrl; bool pass = false; if(testNetAccess(serverUrl)) { // if server URL has NOT changed, we're done if(strcmp(seller["url"]->getString(), serverUrl.c_str()) == 0) { pass = true; MO_CAT_DEBUG(BM_CUSTOMCATALOG_CAT, "ListingUpdater net access test passed and server URL has not " "changed for user ID: %" PRIu64 ", serverUrl: '%s'", userId, serverUrl.c_str()); } // server URL has changed, post to bitmunk to update it else { MO_CAT_DEBUG(BM_CUSTOMCATALOG_CAT, "ListingUpdater net access test passed and server URL has " "changed for user ID: %" PRIu64 ", " "oldServerUrl: '%s', newServerUrl: '%s'", userId, seller["url"]->getString(), serverUrl.c_str()); DynamicObject out; out["url"] = serverUrl.c_str(); DynamicObject in; // post to bitmunk to update server URL Url url; url.format("/api/3.0/catalog/sellers/%" PRIu64 "/%" PRIu32, userId, BM_SERVER_ID(seller["serverId"])); Messenger* m = mNode->getMessenger(); if(m->postSecureToBitmunk(&url, &out, &in, userId)) { // save new server URL pass = mCatalog->setConfigValue( userId, "serverUrl", serverUrl.c_str()); } } } if(!pass) { // failed to update server URL ExceptionRef e = new Exception( "Could not test public internet access and update server URL.", "bitmunk.catalog.UpdateServerUrlError"); Exception::push(e); // log error MO_CAT_ERROR(BM_CUSTOMCATALOG_CAT, "ListingUpdater: %s", JsonWriter::writeToString(Exception::getAsDynamicObject()).c_str()); } // send message to self messageSelf(msg); // schedule a pending update request if(mUpdateRequestPending) { mUpdateRequestPending = false; DynamicObject msg; msg["updateRequest"] = true; messageSelf(msg); } }
void ListingUpdater::registerServerId() { MO_CAT_DEBUG(BM_CUSTOMCATALOG_CAT, "ListingUpdater registering for new server ID for user %" PRIu64, getUserId()); // create message to send after registration DynamicObject msg; msg["registerResponse"] = true; msg["error"] = false; // do net access test string serverUrl; bool pass = false; if(testNetAccess(serverUrl)) { DynamicObject out; out["url"] = serverUrl.c_str(); out["listingUpdateId"] = "0"; DynamicObject in; // post to bitmunk Url url; url.format("/api/3.0/catalog/sellers/%" PRIu64, getUserId()); Messenger* m = mNode->getMessenger(); if(m->postSecureToBitmunk(&url, &out, &in, getUserId())) { pass = true; // send registration data in message msg["serverId"] = in["serverId"]; msg["serverToken"] = in["serverToken"]; msg["serverUrl"] = out["url"]; } } if(!pass) { // failed to register ExceptionRef e = new Exception( "Could not register for new server ID.", "bitmunk.catalog.ServerIdRegistrationError"); Exception::push(e); DynamicObject d = Exception::getAsDynamicObject(); msg["error"] = true; msg["exception"] = d; // log error MO_CAT_ERROR(BM_CUSTOMCATALOG_CAT, "ListingUpdater: %s", JsonWriter::writeToString(d).c_str()); } // send message to self messageSelf(msg); // schedule a pending test net access request if(mTestNetAccessPending) { mTestNetAccessPending = false; DynamicObject msg; msg["testNetAccess"] = true; messageSelf(msg); } }