123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- require __DIR__ . '/../../vendor/autoload.php';
- use GuzzleHttp\Client;
- use GuzzleHttp\Message\ResponseInterface;
- class ChecksumsContext implements \Behat\Behat\Context\Context {
-
- private $baseUrl;
-
- private $client;
-
- private $response;
-
- public function __construct($baseUrl) {
- $this->baseUrl = $baseUrl;
-
- $testServerUrl = getenv('TEST_SERVER_URL');
- if ($testServerUrl !== false) {
- $this->baseUrl = substr($testServerUrl, 0, -5);
- }
- }
-
- public function setUpScenario() {
- $this->client = new Client();
- }
-
- public function tearDownScenario() {
- }
-
- private function getPasswordForUser($userName) {
- if ($userName === 'admin') {
- return 'admin';
- }
- return '123456';
- }
-
- public function userUploadsFileToWithChecksum($user, $source, $destination, $checksum) {
- $file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r'));
- try {
- $this->response = $this->client->put(
- $this->baseUrl . '/remote.php/webdav' . $destination,
- [
- 'auth' => [
- $user,
- $this->getPasswordForUser($user)
- ],
- 'body' => $file,
- 'headers' => [
- 'OC-Checksum' => $checksum
- ]
- ]
- );
- } catch (\GuzzleHttp\Exception\ServerException $e) {
-
- $this->response = $e->getResponse();
- }
- }
-
- public function theWebdavResponseShouldHaveAStatusCode($statusCode) {
- if ((int)$statusCode !== $this->response->getStatusCode()) {
- throw new \Exception("Expected $statusCode, got ".$this->response->getStatusCode());
- }
- }
-
- public function userRequestTheChecksumOfViaPropfind($user, $path) {
- $this->response = $this->client->request(
- 'PROPFIND',
- $this->baseUrl . '/remote.php/webdav' . $path,
- [
- 'body' => '<?xml version="1.0"?>
- <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
- <d:prop>
- <oc:checksums />
- </d:prop>
- </d:propfind>',
- 'auth' => [
- $user,
- $this->getPasswordForUser($user),
- ]
- ]
- );
- }
-
- public function theWebdavChecksumShouldMatch($checksum) {
- $service = new Sabre\Xml\Service();
- $parsed = $service->parse($this->response->getBody()->getContents());
-
- $checksums = $parsed[0]['value'][1]['value'][0]['value'][0];
- if ($checksums['value'][0]['value'] !== $checksum) {
- throw new \Exception("Expected $checksum, got ".$checksums['value'][0]['value']);
- }
- }
-
- public function userDownloadsTheFile($user, $path) {
- $this->response = $this->client->get(
- $this->baseUrl . '/remote.php/webdav' . $path,
- [
- 'auth' => [
- $user,
- $this->getPasswordForUser($user),
- ]
- ]
- );
- }
-
- public function theHeaderChecksumShouldMatch($checksum) {
- if ($this->response->getHeader('OC-Checksum')[0] !== $checksum) {
- throw new \Exception("Expected $checksum, got ".$this->response->getHeader('OC-Checksum')[0]);
- }
- }
-
- public function userCopiedFileTo($user, $source, $destination) {
- $this->response = $this->client->request(
- 'MOVE',
- $this->baseUrl . '/remote.php/webdav' . $source,
- [
- 'auth' => [
- $user,
- $this->getPasswordForUser($user),
- ],
- 'headers' => [
- 'Destination' => $this->baseUrl . '/remote.php/webdav' . $destination,
- ],
- ]
- );
- }
-
- public function theWebdavChecksumShouldBeEmpty() {
- $service = new Sabre\Xml\Service();
- $parsed = $service->parse($this->response->getBody()->getContents());
-
- $status = $parsed[0]['value'][1]['value'][1]['value'];
- if ($status !== 'HTTP/1.1 404 Not Found') {
- throw new \Exception("Expected 'HTTP/1.1 404 Not Found', got ".$status);
- }
- }
-
- public function theOcChecksumHeaderShouldNotBeThere() {
- if ($this->response->hasHeader('OC-Checksum')) {
- throw new \Exception("Expected no checksum header but got ".$this->response->getHeader('OC-Checksum')[0]);
- }
- }
-
- public function userUploadsChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination, $checksum) {
- $num -= 1;
- $this->response = $this->client->put(
- $this->baseUrl . '/remote.php/webdav' . $destination . '-chunking-42-'.$total.'-'.$num,
- [
- 'auth' => [
- $user,
- $this->getPasswordForUser($user)
- ],
- 'body' => $data,
- 'headers' => [
- 'OC-Checksum' => $checksum,
- 'OC-Chunked' => '1',
- ]
- ]
- );
- }
- }
|