/**
  * test with post parameter requestType set to JSON
  */
 public function testServerPostParameter()
 {
     $request = \Zend\Http\PhpEnvironment\Request::fromString("POST /index.php?requestType=JSON HTTP/1.1\r\n" . "Host: localhost\r\n" . "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7");
     $request->setPost(new Zend\Stdlib\Parameters(array('requestType' => 'JSON')));
     $server = Tinebase_Core::getDispatchServer($request);
     $this->assertInstanceOf('Tinebase_Server_Json', $server);
 }
 public function testGetDispatchServerWebDAV()
 {
     $request = \Zend\Http\PhpEnvironment\Request::fromString("GET /index.php?frontend=webdav HTTP/1.1\r\n" . "User-Agent: SAMSUNG-GT-I9300/101.403");
     $request->setQuery(new \Zend\Stdlib\Parameters(array('frontend' => 'webdav')));
     $server = Tinebase_Core::getDispatchServer($request);
     $this->assertInstanceOf('Tinebase_Server_WebDAV', $server);
 }
    /**
     * @group ServerTests
     */
    public function testServer()
    {
        $this->markTestSkipped('FIXME unpack(): Type C: not enough input, need 1, have 0');
        $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
POST /Microsoft-Server-ActiveSync?User=abc1234&DeviceId=Appl7R743U8YWH9&DeviceType=iPhone&Cmd=FolderSync HTTP/1.1
Host: localhost
MS-ASProtocolVersion: 14.1
User-Agent: Apple-iPhone/705.18
EOS
);
        $credentials = $this->getTestCredentials();
        $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
        $request->getServer()->set('PHP_AUTH_PW', $credentials['password']);
        $request->getServer()->set('REMOTE_ADDR', 'localhost');
        $_SERVER['REQUEST_METHOD'] = $request->getMethod();
        $_SERVER['HTTP_MS_ASPROTOCOLVERSION'] = '14.1';
        $_SERVER['HTTP_USER_AGENT'] = 'Apple-iPhone/705.18';
        $body = new DOMDocument();
        $body->loadXML('<?xml version="1.0" encoding="utf-8"?>
            <!DOCTYPE AirSync PUBLIC "-//AIRSYNC//DTD AirSync//EN" "http://www.microsoft.com/">
            <FolderSync xmlns="uri:FolderHierarchy"><SyncKey>0</SyncKey></FolderSync>');
        ob_start();
        $server = new ActiveSync_Server_Http();
        $server->handle($request, $body);
        $result = ob_get_contents();
        ob_end_clean();
        //TODO needs to be improved (use XML document here)
        $this->assertContains('AwFqAAAHVkwDMQABUgMxAAFOVwM', base64_encode($result), 0, 30);
    }
 /**
  * test general functionality of Tinebase_Server_Plugin_WebDAV
  */
 public function testServerGetParameter()
 {
     $request = \Zend\Http\PhpEnvironment\Request::fromString("POST /index.php?frontend=webdav HTTP/1.1\r\n" . "Host: localhost\r\n" . "Depth: 0\r\n" . "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7");
     $request->setQuery(new Zend\Stdlib\Parameters(array('frontend' => 'webdav')));
     $server = Tinebase_Core::getDispatchServer($request);
     $this->assertInstanceOf('Tinebase_Server_WebDAV', $server);
 }
    /**
     * test general functionality of Tinebase_Server_Plugin_Http
     */
    public function testServer()
    {
        $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
POST /index.php?frontend=webdav HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
EOS
);
        $server = Tinebase_Server_Plugin_Http::getServer($request);
        $this->assertInstanceOf('Tinebase_Server_Http', $server);
    }
    /**
     * test general functionality of Voipmanager_Server_Plugin
     */
    public function testServerUserAgentAsterisk()
    {
        $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
POST /index.php?frontend=activesync HTTP/1.1
Host: localhost
User-Agent: asterisk-libcurl-agent/1.0
EOS
);
        $server = Tinebase_Core::getDispatchServer($request);
        $this->assertInstanceOf('Voipmanager_Server_Asterisk', $server);
    }
    /**
     * test general functionality of ActiveSync_Server_Plugin
     */
    public function testServerGetParameter()
    {
        $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
POST /index.php?frontend=activesync HTTP/1.1
Host: localhost
Depth: 0
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7
EOS
);
        $request->setQuery(new Zend\Stdlib\Parameters(array('frontend' => 'activesync')));
        $server = Tinebase_Core::getDispatchServer($request);
        $this->assertInstanceOf('ActiveSync_Server_Http', $server);
    }
 /**
  * @group ServerTests
  *
  * @see  0011760: create smd from model definition
  */
 public function testHandleRequestForDynamicAPI()
 {
     // handle jsonkey check
     $jsonkey = 'myawsomejsonkey';
     $_SERVER['HTTP_X_TINE20_JSONKEY'] = $jsonkey;
     $coreSession = Tinebase_Session::getSessionNamespace();
     $coreSession->jsonKey = $jsonkey;
     $server = new Tinebase_Server_Json();
     $request = \Zend\Http\PhpEnvironment\Request::fromString('POST /index.php?requestType=JSON HTTP/1.1' . "\r\n" . 'Host: localhost' . "\r\n" . 'User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7' . "\r\n" . 'Content-Type: application/json' . "\r\n" . 'X-Tine20-Transactionid: 18da265bc0eb66a36081bfd42689c1675ed68bab' . "\r\n" . 'X-Requested-With: XMLHttpRequest' . "\r\n" . 'Accept: */*' . "\r\n" . 'Referer: http://tine20.vagrant/' . "\r\n" . 'Accept-Encoding: gzip, deflate' . "\r\n" . 'Accept-Language: en-US,en;q=0.8,de-DE;q=0.6,de;q=0.4' . "\r\n" . "\r\n" . '{"jsonrpc":"2.0","method":"Inventory.searchInventoryItems","params":{"filter":[], "paging":{}},"id":6}' . "\r\n");
     ob_start();
     $server->handle($request);
     $out = ob_get_clean();
     //echo $out;
     $this->assertTrue(!empty($out), 'request should not be empty');
     $this->assertNotContains('Not Authorised', $out);
     $this->assertNotContains('Method not found', $out);
     $this->assertNotContains('No Application Controller found', $out);
     $this->assertNotContains('"error"', $out);
     $this->assertNotContains('PHP Fatal error', $out);
     $this->assertContains('"result"', $out);
 }
    /**
     * @group ServerTests
     */
    public function testAccountBlocking()
    {
        Zend_Session::$_unitTestEnabled = true;
        $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
POST /index.php HTTP/1.1
Content-Type: application/json
Content-Length: 122
Host: 192.168.122.158
Connection: keep-alive
Origin: http://192.168.1.158
X-Tine20-Request-Type: JSON
X-Tine20-Jsonkey: undefined
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36
X-Tine20-Transactionid: 9c7129898e9f8ab7e4621fddf7077a1eaa425aac
X-Requested-With: XMLHttpRequest
Accept: */*
Referer: http://192.168.122.158/tine20dev/
Accept-Encoding: gzip,deflate
Accept-Language: de-DE,de;q=0.8,en-GB;q=0.6,en;q=0.4
EOS
);
        $credentials = $this->getTestCredentials();
        $maxLoginFailures = Tinebase_Config::getInstance()->get(Tinebase_Config::MAX_LOGIN_FAILURES, 5);
        for ($i = 0; $i <= $maxLoginFailures; $i++) {
            $result = Tinebase_Controller::getInstance()->login($credentials['username'], 'foobar', $request);
            $this->assertFalse($result);
        }
        // account must be blocked now
        $result = Tinebase_Controller::getInstance()->login($credentials['username'], $credentials['password'], $request);
        $this->assertFalse($result);
    }
    /**
     * @group ServerTests
     *
     * @see 0011440: rework login failure handling
     */
    public function testAccountBlocking()
    {
        // NOTE: end transaction here as NOW() returns the start of the current transaction in pgsql
        //  and is used in user status statement (think about using statement_timestamp() instead of NOW() with pgsql)
        Tinebase_TransactionManager::getInstance()->commitTransaction($this->_transactionId);
        $this->_transactionId = null;
        $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
POST /index.php HTTP/1.1
Content-Type: application/json
Content-Length: 122
Host: 192.168.122.158
Connection: keep-alive
Origin: http://192.168.1.158
X-Tine20-Request-Type: JSON
X-Tine20-Jsonkey: undefined
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36
X-Tine20-Transactionid: 9c7129898e9f8ab7e4621fddf7077a1eaa425aac
X-Requested-With: XMLHttpRequest
Accept: */*
Referer: http://192.168.122.158/tine20dev/
Accept-Encoding: gzip,deflate
Accept-Language: de-DE,de;q=0.8,en-GB;q=0.6,en;q=0.4
EOS
);
        $credentials = $this->getTestCredentials();
        for ($i = 0; $i <= 3; $i++) {
            $result = Tinebase_Controller::getInstance()->login($credentials['username'], 'foobar', $request);
            $this->assertFalse($result);
        }
        $result = Tinebase_Controller::getInstance()->login($credentials['username'], $credentials['password'], $request);
        $this->assertFalse($result, 'account must be blocked now');
        // wait for some time (2^4 = 16 +1 seconds)
        $timeToWait = 17;
        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
            Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Waiting for ' . $timeToWait . ' seconds...');
        }
        sleep($timeToWait);
        $result = Tinebase_Controller::getInstance()->login($credentials['username'], $credentials['password'], $request);
        $this->assertTrue($result, 'account should be unblocked now');
    }
 public function testRequestHasHeadersMethod()
 {
     $baseRequest = BaseRequest::fromString("GET /index.php HTTP/1.1\r\nAccept: */*\r\n\r\nSome Content");
     $request = Request::createFromRequest($baseRequest);
     $this->assertEquals('Accept: */*', $request->headers('Accept'));
 }
    /**
     * test PROPFIND on calendar
     * 
     * @group ServerTests
     */
    public function testPropfindThundebird()
    {
        $credentials = $this->getTestCredentials();
        $account = $this->getAccountByName($credentials['username']);
        $this->assertInstanceOf('Tinebase_Model_FullUser', $account);
        $containerId = $this->getPersonalContainer($account, 'Calendar_Model_Event')->getFirstRecord()->getId();
        $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
PROPFIND /calendars/{$account->contact_id}/{$containerId}/ HTTP/1.1
Host: localhost
Depth: 1
Content-Type: application/xml; charset="utf-8"
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7

<?xml version="1.0" encoding="UTF-8"?>
<D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/" xmlns:C="urn:ietf:params:xml:ns:caldav"><D:prop><D:resourcetype/><D:owner/><D:current-user-principal/><D:supported-report-set/><C:supported-calendar-component-set/><CS:getctag/></D:prop></D:propfind>
EOS
);
        $_SERVER['REQUEST_METHOD'] = $request->getMethod();
        $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
        $_SERVER['HTTP_DEPTH'] = '0';
        $request->getServer()->set('PHP_AUTH_USER', $credentials['username']);
        $request->getServer()->set('PHP_AUTH_PW', $credentials['password']);
        $request->getServer()->set('REMOTE_ADDR', 'localhost');
        ob_start();
        $server = new Tinebase_Server_WebDAV();
        $server->handle($request);
        $result = ob_get_contents();
        ob_end_clean();
        $responseDoc = new DOMDocument();
        $responseDoc->loadXML($result);
        #$responseDoc->formatOutput = true; error_log($responseDoc->saveXML());
        $xpath = new DomXPath($responseDoc);
        $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
        $xpath->registerNamespace('cs', 'http://calendarserver.org/ns/');
        $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/d:current-user-principal');
        $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
    }