/**
  * @todo Figure out how to handle authentication and authorization
  * using human intervention and outside of the test functions
  */
 public function testGetToken()
 {
     /**
      * Scrape the web site to authenticate and just get delete
      * permissions so all operations will be allowed
      */
     $url = self::$_rtm->getAuthUrl(Zend_Service_RememberTheMilk::PERMS_DELETE, self::$_frob);
     $parsed = parse_url($url);
     $query = $parsed['query'];
     $params = array('username' => self::$_username, 'password' => self::$_password, 'remember' => 'on', 'login' => 'Login', 'continue' => 'home', 'api' => $query, 'u' => '1');
     $config = array('useragent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11');
     $http = new Zend_Http_Client();
     $http->setCookieJar();
     $http->setMethod(Zend_Http_Client::POST);
     $http->setConfig($config);
     $http->setUri('https://www.rememberthemilk.com/auth.rtm');
     $http->setHeaders('Referer', $url);
     $http->setParameterPost($params);
     $response = $http->request();
     /*if (!$response->isSuccessful()) {
                 throw new Zend_Service_Exception('Authentication failed');
             }
     
             sleep(1);
     
             $params = array(
                 'authorize_yes' => 'Yes, go for it!'
             );
     
             $http->setUri('http://www.rememberthemilk.com/services/auth/?' . $query);
             $http->setParameterPost($params);
             $response = $http->request();*/
     if (!$response->isSuccessful() || !strpos($response->getBody(), 'Application successfully authorized')) {
         throw new Zend_Service_Exception('Authorization failed');
     }
     sleep(1);
     /**
      * Authentication and authorization logic ends here
      */
     self::$_token = self::$_rtm->getToken(self::$_frob);
     $this->assertTrue(self::$_token instanceof Zend_Service_RememberTheMilk_Token, 'Returned token is not an instance');
     $this->assertRegExp('/^[a-f0-9]{40}$/', self::$_token->getToken(), 'Returned token string appears to be invalid');
     $this->assertEquals(self::$_token->getPerms(), Zend_Service_RememberTheMilk::PERMS_DELETE, 'Permissions are inconsistent');
     $user = self::$_token->getUser();
     $this->assertTrue($user instanceof Zend_Service_RememberTheMilk_Contact, 'Token user is not an instance');
 }
예제 #2
0
 /**
  * Executes an API request and returns the response.
  *
  * @param Zend_Service_RememberTheMilk_Request $request
  * @throws Zend_Service_Exception
  * @return array Associative array containing the response data
  */
 protected function _request($request)
 {
     $params = $request->getParameters();
     $params['api_key'] = $this->_apiKey;
     $params['format'] = 'json';
     $params['method'] = $request->getMethod();
     if ($request->requiresTimeline()) {
         if ($this->_timeline == null) {
             $timeline = new Zend_Service_RememberTheMilk_Request();
             $timeline->setMethod('rtm.timelines.create');
             $timeline->useTimeline(false);
             $response = $this->_request($timeline);
             $this->_timeline = $response->timeline;
         }
         $params['timeline'] = $this->_timeline;
     }
     if ($request->requiresAuth()) {
         if (!$this->_token instanceof Zend_Service_RememberTheMilk_Token) {
             throw new Zend_Service_Exception('Authentication token not set');
         }
         $params['auth_token'] = $this->_token->getToken();
     }
     if (strpos($params['method'], 'rtm.time.') !== 0) {
         $this->_sign($params);
     }
     $time = time();
     $elapsed = $time - $this->_lastRequest;
     if ($elapsed < $this->_throttle) {
         sleep($this->_throttle - $elapsed);
     }
     $this->_lastRequest = $time;
     /**
      * @see Zend_Service_Exception
      */
     require_once 'Zend/Service/Exception.php';
     $response = $this->_rest->restGet('/services/rest/', $params);
     if ($response->isSuccessful()) {
         $body = $response->getBody();
         if ($body === null) {
             throw new Zend_Service_Exception('Service appears to be unavailable');
         }
         $body = Zend_Json::decode($body, Zend_Json::TYPE_OBJECT);
         $body = $body->rsp;
         if ($body->stat == 'fail') {
             throw new Zend_Service_Exception($body->err->msg, $body->err->code);
         } else {
             if (isset($body->transaction) && isset($body->transaction->undoable) && $body->transaction->undoable == '1') {
                 $this->_transactions[] = $body->transaction->id;
             }
         }
         return $body;
     }
     throw new Zend_Service_Exception('HTTP ' . $response->getStatus());
 }