public function testGetAccounts()
 {
     $this->createConfig();
     $this->createAccount();
     $this->httpClient->request('GET', $this->componentName . '/accounts');
     $responseJson = $this->httpClient->getResponse()->getContent();
     $response = json_decode($responseJson, true);
     $this->assertNotEmpty($response);
     $accountArr = array_shift($response);
     $this->assertAccount($this->configuration->getAccountBy('accountId', $accountArr['accountId']));
 }
 /**
  * @param Request $request
  * @return JsonResponse
  */
 public function postConfigsAction(Request $request)
 {
     $params = $this->getPostJson($request);
     $this->checkParams(['name'], $params);
     try {
         $this->configuration->exists();
     } catch (ConfigurationException $e) {
         $this->configuration->create();
     }
     $account = $this->configuration->getAccountBy('accountId', $this->configuration->getIdFromName($params['name']));
     if (null != $account) {
         throw new ConfigurationException('Account already exists');
     }
     $account = $this->configuration->addAccount($params);
     return $this->createJsonResponse(['id' => $account->getAccountId(), 'name' => $account->getAccountName(), 'description' => $account->getDescription()]);
 }
 public function oauthCallbackAction()
 {
     $session = $this->get('session');
     $token = $session->get('token');
     $accountId = $session->get('account');
     $referrer = $session->get('referrer');
     $external = $session->get('external');
     if ($token == null) {
         throw new UserException("Your session expired, please try again");
     }
     $code = $this->get('request')->query->get('code');
     if (empty($code)) {
         throw new UserException('Could not read from Google API, please try again');
     }
     $googleApi = $this->getGoogleApi();
     try {
         $storageApi = new StorageApi(array('token' => $token, 'userAgent' => $this->componentName));
         /** @var ObjectEncryptor $encryptor */
         $encryptor = $this->get('syrup.object_encryptor');
         $configuration = new Configuration($this->componentName, $encryptor);
         $configuration->setStorageApi($storageApi);
         $tokens = $googleApi->authorize($code, $this->container->get('router')->generate('keboola_google_drive_writer_oauth_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL));
         $googleApi->setCredentials($tokens['access_token'], $tokens['refresh_token']);
         $userData = json_decode($googleApi->request('/oauth2/v2/userinfo')->getBody(), true);
         if ($external) {
             $this->container->get('session')->clear();
             $referrer .= strstr($referrer, '?') ? '&' : '?';
             $referrer .= 'access-token=' . urlencode($this->container->get('syrup.encryptor')->encrypt($tokens['access_token'])) . '&refresh-token=' . urlencode($this->container->get('syrup.encryptor')->encrypt($tokens['refresh_token'])) . '&email=' . $userData['email'];
             return new RedirectResponse($referrer);
         } else {
             $account = $configuration->getAccountBy('accountId', $accountId);
             if (null == $account) {
                 throw new ConfigurationException("Account doesn't exist");
             }
             $account->setGoogleId($userData['id'])->setGoogleName($userData['name'])->setEmail($userData['email'])->setAccessToken($tokens['access_token'])->setRefreshToken($tokens['refresh_token'])->save();
         }
         $this->container->get('session')->clear();
         if ($referrer) {
             return new RedirectResponse($referrer);
         } else {
             return new JsonResponse(array('status' => 'ok'));
         }
     } catch (\Exception $e) {
         throw new SyrupComponentException(500, 'Could not save API tokens', $e);
     }
 }