/** * @inheritdoc */ protected function _before() { $this->account = new NostoAccount('platform-00000000'); // The first 16 chars of the SSO token are used as the encryption key. $token = new NostoApiToken('sso', '01098d0fc84ded7c4226820d5d1207c69243cbb3637dc4bc2a216dafcf09d783'); $this->account->addApiToken($token); }
/** * Test the account deletion with the required SSO token. */ public function testDeletingAccountWithToken() { $account = new NostoAccount('platform-test'); $token = new NostoApiToken('sso', '123'); $account->addApiToken($token); $this->specify('account is deleted', function () use($account) { $account->delete(); }); }
/** * Tests that we can build an authenticated url for the config iframe. */ public function testIframeUrlAuthentication() { /** @var NostoAccount $account */ $account = new NostoAccount('platform-00000000'); $meta = new NostoAccountMetaDataIframe(); $baseUrl = isset($_ENV['NOSTO_WEB_HOOK_BASE_URL']) ? $_ENV['NOSTO_WEB_HOOK_BASE_URL'] : NostoHttpRequest::$baseUrl; $url = $account->getIframeUrl($meta); $this->specify('install iframe url was created', function () use($url, $baseUrl) { $validUrl = $baseUrl . '/hub/platform/install?lang=en&ps_version=1.0.0&nt_version=1.0.0&product_pu=http%3A%2F%2Fmy.shop.com%2Fproducts%2Fproduct123%3Fnostodebug%3Dtrue&category_pu=http%3A%2F%2Fmy.shop.com%2Fproducts%2Fcategory123%3Fnostodebug%3Dtrue&search_pu=http%3A%2F%2Fmy.shop.com%2Fsearch%3Fquery%3Dred%3Fnostodebug%3Dtrue&cart_pu=http%3A%2F%2Fmy.shop.com%2Fcart%3Fnostodebug%3Dtrue&front_pu=http%3A%2F%2Fmy.shop.com%3Fnostodebug%3Dtrue&shop_lang=en&shop_name=Shop+Name&unique_id=123&fname=James&lname=Kirk&email=james.kirk%40example.com'; $this->assertEquals($validUrl, $url); }); $tokenSso = new NostoApiToken(NostoApiToken::API_SSO, '01098d0fc84ded7c4226820d5d1207c69243cbb3637dc4bc2a216dafcf09d783'); $tokenProducts = new NostoApiToken(NostoApiToken::API_PRODUCTS, 'X1098d0fc84ded7c4226820d5d1207c69243cbb3637dc4bc2a216dafcf09d783'); $account->addApiToken($tokenSso); $account->addApiToken($tokenProducts); $url = $account->getIframeUrl($meta); $this->specify('auth iframe url was created', function () use($url, $baseUrl) { $validUrl = 'https://nosto.com/auth/sso/sso%2Bplatform-00000000@nostosolutions.com/xAd1RXcmTMuLINVYaIZJJg?lang=en&ps_version=1.0.0&nt_version=1.0.0&product_pu=http%3A%2F%2Fmy.shop.com%2Fproducts%2Fproduct123%3Fnostodebug%3Dtrue&category_pu=http%3A%2F%2Fmy.shop.com%2Fproducts%2Fcategory123%3Fnostodebug%3Dtrue&search_pu=http%3A%2F%2Fmy.shop.com%2Fsearch%3Fquery%3Dred%3Fnostodebug%3Dtrue&cart_pu=http%3A%2F%2Fmy.shop.com%2Fcart%3Fnostodebug%3Dtrue&front_pu=http%3A%2F%2Fmy.shop.com%3Fnostodebug%3Dtrue&shop_lang=en&shop_name=Shop+Name&unique_id=123&fname=James&lname=Kirk&email=james.kirk%40example.com'; $this->assertEquals($validUrl, $url); }); }
/** * Tests the "getApiToken" method for the NostoAccount class. */ public function testAccountApiToken() { $account = new NostoAccount('platform-test'); $this->specify('account does not have sso token', function () use($account) { $this->assertNull($account->getApiToken('sso')); }); $token = new NostoApiToken('sso', '123'); $account->addApiToken($token); $this->specify('account has sso token', function () use($account) { $this->assertEquals('123', $account->getApiToken('sso')->getValue()); }); }
/** * Returns the account with associated api tokens for the store view scope. * * @param Mage_Core_Model_Store|null $store the account store view. * * @return NostoAccount|null the account or null if not found. */ public function find(Mage_Core_Model_Store $store = null) { if ($store === null) { $store = Mage::app()->getStore(); } $accountName = $store->getConfig(self::XML_PATH_ACCOUNT); if (!empty($accountName)) { $account = new NostoAccount($accountName); $tokens = json_decode($store->getConfig(self::XML_PATH_TOKENS), true); if (is_array($tokens) && !empty($tokens)) { foreach ($tokens as $name => $value) { $token = new NostoApiToken($name, $value); $account->addApiToken($token); } } return $account; } return null; }
/** * Finds and returns an account for given criteria. * * @param null|int $lang_id the ID of the language. * @param null|int $id_shop_group the ID of the shop context. * @param null|int $id_shop the ID of the shop. * @return NostoAccount|null the account with loaded API tokens, or null if not found. */ public function find($lang_id = null, $id_shop_group = null, $id_shop = null) { /** @var NostoTaggingHelperConfig $helper_config */ $helper_config = Nosto::helper('nosto_tagging/config'); $account_name = $helper_config->getAccountName($lang_id, $id_shop_group, $id_shop); if (!empty($account_name)) { $account = new NostoAccount($account_name); $tokens = array(); foreach (NostoApiToken::getApiTokenNames() as $token_name) { $token_value = $helper_config->getToken($token_name, $lang_id, $id_shop_group, $id_shop); if (!empty($token_value)) { $tokens[$token_name] = $token_value; } } if (!empty($tokens)) { foreach ($tokens as $name => $value) { $account->addApiToken(new NostoApiToken($name, $value)); } } return $account; } return null; }
/** * Syncs an existing Nosto account via OAuth. * * Requires that the OAuth cycle has already completed the first step in getting the authorization code. * * @param NostoOauthClientMetaInterface $meta the OAuth client meta data to use for connection to Nosto. * @param string $authCode the authorization code that grants access to transfer data from Nosto. * @return NostoAccount the synced account. * * @throws NostoException on failure. */ public function sync(NostoOauthClientMetaInterface $meta, $authCode) { $oauthClient = new NostoOAuthClient($meta); $token = $oauthClient->authenticate($authCode); $request = new NostoHttpRequest(); // The request is currently not made according the the OAuth2 spec with the access token in the // Authorization header. This is due to the authentication server not implementing the full OAuth2 spec yet. $request->setUrl(NostoOAuthClient::$baseUrl . '/exchange'); $request->setQueryParams(array('access_token' => $token->getAccessToken())); $response = $request->get(); if ($response->getCode() !== 200) { throw Nosto::createHttpException('Failed to send account sync to Nosto.', $request, $response); } $result = $response->getJsonResult(true); $account = new NostoAccount($token->getMerchantName()); $tokens = NostoApiToken::parseTokens($result, 'api_'); foreach ($tokens as $token) { $account->addApiToken($token); } if (!$account->isConnectedToNosto()) { throw new NostoException('Failed to sync all account details from Nosto. Unknown error.'); } return $account; }
/** * Tests that batch product re-crawl API requests can be made. */ public function testSendingBatchProductReCrawl() { $account = new NostoAccount('platform-00000000'); $product = new NostoProduct(); $collection = new NostoExportProductCollection(); $collection[] = $product; $token = new NostoApiToken('products', '01098d0fc84ded7c4226820d5d1207c69243cbb3637dc4bc2a216dafcf09d783'); $account->addApiToken($token); $result = NostoProductReCrawl::sendBatch($collection, $account); $this->specify('successful batch product re-crawl', function () use($result) { $this->assertTrue($result); }); }
/** * Tests that product delete API requests can be made. */ public function testSendingProductDelete() { $account = new NostoAccount('platform-00000000'); $product = new NostoProduct(); $token = new NostoApiToken('products', '01098d0fc84ded7c4226820d5d1207c69243cbb3637dc4bc2a216dafcf09d783'); $account->addApiToken($token); $op = new NostoOperationProduct($account); $op->addProduct($product); $result = $op->delete(); $this->specify('successful product delete', function () use($result) { $this->assertTrue($result); }); }
/** * Returns the account with associated api tokens for the store. * * @param Store $store the store. * * @return \NostoAccount|null the account or null if not found. */ public function findAccount(Store $store) { $accountName = $store->getConfig(self::XML_PATH_ACCOUNT); if (!empty($accountName)) { $account = new \NostoAccount($accountName); $tokens = json_decode($store->getConfig(self::XML_PATH_TOKENS), true); if (is_array($tokens) && !empty($tokens)) { foreach ($tokens as $name => $value) { try { $account->addApiToken(new \NostoApiToken($name, $value)); } catch (\NostoInvalidArgumentException $e) { } } } return $account; } return null; }