Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FeatureContext.php « bootstrap « features « integration « tests - github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49ae2f0b60adf122ca33963c1f8d089fe504e500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<?php
/**
 * @author Joas Schilling <coding@schilljs.com>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 *
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */
require __DIR__ . '/../../vendor/autoload.php';

use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Message\ResponseInterface;

/**
 * Defines application features from the specific context.
 */
class FeatureContext implements Context, SnippetAcceptingContext {

	/** @var array[] */
	protected static $identifierToToken;
	/** @var array[] */
	protected static $tokenToIdentifier;
	/** @var array[] */
	protected static $sessionIdToUser;

	/** @var string */
	protected $currentUser;

	/** @var ResponseInterface */
	private $response;

	/** @var CookieJar[] */
	private $cookieJars;

	/** @var string */
	protected $baseUrl;

	/** @var string */
	protected $lastEtag;

	/**
	 * FeatureContext constructor.
	 */
	public function __construct() {
		$this->cookieJars = [];
		$this->baseUrl = getenv('TEST_SERVER_URL');
	}

	/**
	 * @Then /^user "([^"]*)" is participant of the following rooms$/
	 *
	 * @param string $user
	 * @param TableNode|null $formData
	 */
	public function userIsParticipantOfRooms($user, TableNode $formData = null) {
		$this->setCurrentUser($user);
		$this->sendRequest('GET', '/apps/spreed/api/v1/room');
		$this->assertStatusCode($this->response, 200);

		$rooms = $this->getDataFromResponse($this->response);
		if ($formData === null) {
			PHPUnit_Framework_Assert::assertEmpty($rooms);
			return;
		}


		PHPUnit_Framework_Assert::assertCount(count($formData->getHash()), $rooms, 'Room count does not match');
		PHPUnit_Framework_Assert::assertEquals($formData->getHash(), array_map(function($room, $expectedRoom) {
			$participantNames = array_map(function($participant) {
				return $participant['name'];
			}, $room['participants']);

			// When participants have the same last ping the order in which they
			// are returned from the server is undefined. That is the most
			// common case during the tests, so by default the list of
			// participants returned by the server is sorted alphabetically. In
			// order to check the exact order of participants returned by the
			// server " [exact order]" can be appended in the test definition to
			// the list of expected participants of the room.
			if (strpos($expectedRoom['participants'], ' [exact order]') === false) {
				sort($participantNames);
			} else {
				// "end(array_keys(..." would generate the Strict Standards
				// error "Only variables should be passed by reference".
				$participantNamesKeys = array_keys($participantNames);
				$lastParticipantKey = end($participantNamesKeys);

				// Append " [exact order]" to the last participant so the
				// imploded string is the same as the expected one.
				$participantNames[$lastParticipantKey] .= ' [exact order]';
			}

			return [
				'id' => self::$tokenToIdentifier[$room['token']],
				'type' => (string) $room['type'],
				'participantType' => (string) $room['participantType'],
				'participants' => implode(', ', $participantNames),
			];
		}, $rooms, $formData->getHash()));
	}

	/**
	 * @Then /^user "([^"]*)" (is|is not) participant of room "([^"]*)"$/
	 *
	 * @param string $user
	 * @param string $isParticipant
	 * @param string $identifier
	 */
	public function userIsParticipantOfRoom($user, $isParticipant, $identifier) {
		$this->setCurrentUser($user);
		$this->sendRequest('GET', '/apps/spreed/api/v1/room');
		$this->assertStatusCode($this->response, 200);

		$isParticipant = $isParticipant === 'is';

		$rooms = $this->getDataFromResponse($this->response);

		if ($isParticipant) {
			PHPUnit_Framework_Assert::assertNotEmpty($rooms);
		}

		foreach ($rooms as $room) {
			if (self::$tokenToIdentifier[$room['token']] === $identifier) {
				PHPUnit_Framework_Assert::assertEquals($isParticipant, true, 'Room ' . $identifier . ' found in user´s room list');
				return;
			}
		}

		PHPUnit_Framework_Assert::assertEquals($isParticipant, false, 'Room ' . $identifier . ' not found in user´s room list');
	}

	/**
	 * @Then /^user "([^"]*)" creates room "([^"]*)"$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param TableNode|null $formData
	 */
	public function userCreatesRoom($user, $identifier, TableNode $formData = null) {
		$this->setCurrentUser($user);
		$this->sendRequest('POST', '/apps/spreed/api/v1/room', $formData);
		$this->assertStatusCode($this->response, 201);

		$response = $this->getDataFromResponse($this->response);
		self::$identifierToToken[$identifier] = $response['token'];
		self::$tokenToIdentifier[$response['token']] = $identifier;
	}

	/**
	 * @Then /^user "([^"]*)" tries to create room with (\d+)$/
	 *
	 * @param string $user
	 * @param int $statusCode
	 * @param TableNode|null $formData
	 */
	public function userTriesToCreateRoom($user, $statusCode, TableNode $formData = null) {
		$this->setCurrentUser($user);
		$this->sendRequest('POST', '/apps/spreed/api/v1/room', $formData);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" joins room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $statusCode
	 * @param TableNode|null $formData
	 */
	public function userJoinsRoom($user, $identifier, $statusCode, TableNode $formData = null) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			'POST', '/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier] . '/participants/active',
			$formData
		);
		$this->assertStatusCode($this->response, $statusCode);

		$response = $this->getDataFromResponse($this->response);
		if (array_key_exists('sessionId', $response)) {
			// In the chat guest users are identified by their sessionId. The
			// sessionId is larger than the size of the actorId column in the
			// database, though, so the ID stored in the database and returned
			// in chat messages is a hashed version instead.
			self::$sessionIdToUser[sha1($response['sessionId'])] = $user;
		}
	}

	/**
	 * @Then /^user "([^"]*)" exits room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userExitsRoom($user, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest('DELETE', '/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier] . '/participants/active');
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" leaves room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userLeavesRoom($user, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest('DELETE', '/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier] . '/participants/self');
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" removes "([^"]*)" from room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $toRemove
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userRemovesUserFromRoom($user, $toRemove, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			'DELETE', '/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier] . '/participants',
			new TableNode([['participant', $toRemove]])
		);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" deletes room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userDeletesRoom($user, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest('DELETE', '/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier]);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" renames room "([^"]*)" to "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $newName
	 * @param string $statusCode
	 */
	public function userRenamesRoom($user, $identifier, $newName, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			'PUT', '/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier],
			new TableNode([['roomName', $newName]])
		);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @When /^user "([^"]*)" sets password "([^"]*)" for room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $password
	 * @param string $identifier
	 * @param string $statusCode
	 * @param TableNode
	 */
	public function userSetsTheRoomPassword($user, $password, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			'PUT', '/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier] . '/password',
			new TableNode([['password', $password]])
		);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" makes room "([^"]*)" (public|private) with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $newType
	 * @param string $statusCode
	 */
	public function userChangesTypeOfTheRoom($user, $identifier, $newType, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			$newType === 'public' ? 'POST' : 'DELETE',
			'/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier] . '/public'
		);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" adds "([^"]*)" to room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $newUser
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userAddUserToRoom($user, $newUser, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			'POST', '/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier] . '/participants',
			new TableNode([['newParticipant', $newUser]])
		);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" (promotes|demotes) "([^"]*)" in room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $isPromotion
	 * @param string $participant
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userPromoteDemoteInRoom($user, $isPromotion, $participant, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			$isPromotion === 'promotes' ? 'POST' : 'DELETE',
			'/apps/spreed/api/v1/room/' . self::$identifierToToken[$identifier] . '/moderators',
			new TableNode([['participant', $participant]])
		);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" pings call "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userPingsCall($user, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest('POST', '/apps/spreed/api/v1/call/' . self::$identifierToToken[$identifier] . '/ping');
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" joins call "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $statusCode
	 * @param TableNode|null $formData
	 */
	public function userJoinsCall($user, $identifier, $statusCode, TableNode $formData = null) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			'POST', '/apps/spreed/api/v1/call/' . self::$identifierToToken[$identifier],
			$formData
		);
		$this->assertStatusCode($this->response, $statusCode);

		$response = $this->getDataFromResponse($this->response);
		if (array_key_exists('sessionId', $response)) {
			// In the chat guest users are identified by their sessionId. The
			// sessionId is larger than the size of the actorId column in the
			// database, though, so the ID stored in the database and returned
			// in chat messages is a hashed version instead.
			self::$sessionIdToUser[sha1($response['sessionId'])] = $user;
		}
	}

	/**
	 * @Then /^user "([^"]*)" leaves call "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userLeavesCall($user, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest('DELETE', '/apps/spreed/api/v1/call/' . self::$identifierToToken[$identifier]);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" sees (\d+) peers in call "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $numPeers
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userSeesPeersInCall($user, $numPeers, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest('GET', '/apps/spreed/api/v1/call/' . self::$identifierToToken[$identifier]);
		$this->assertStatusCode($this->response, $statusCode);

		if ($statusCode === '200') {
			$response = $this->getDataFromResponse($this->response);
			PHPUnit_Framework_Assert::assertCount((int) $numPeers, $response);
		} else {
			PHPUnit_Framework_Assert::assertEquals((int) $numPeers, 0);
		}
	}

	/**
	 * @Then /^user "([^"]*)" sends message "([^"]*)" to room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $message
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userSendsMessageToRoom($user, $message, $identifier, $statusCode) {
		$this->setCurrentUser($user);
		$this->sendRequest(
			'POST', '/apps/spreed/api/v1/chat/' . self::$identifierToToken[$identifier],
			new TableNode([['message', $message]])
		);
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @Then /^user "([^"]*)" sees the following messages in room "([^"]*)" with (\d+)$/
	 *
	 * @param string $user
	 * @param string $message
	 * @param string $identifier
	 * @param string $statusCode
	 */
	public function userSeesTheFollowingMessagesInRoom($user, $identifier, $statusCode, TableNode $formData = null) {
		$this->setCurrentUser($user);
		$this->sendRequest('GET', '/apps/spreed/api/v1/chat/' . self::$identifierToToken[$identifier]);
		$this->assertStatusCode($this->response, $statusCode);

		$messages = $this->getDataFromResponse($this->response);
		if ($formData === null) {
			PHPUnit_Framework_Assert::assertEmpty($messages);
			return;
		}

		PHPUnit_Framework_Assert::assertCount(count($formData->getHash()), $messages, 'Message count does not match');
		PHPUnit_Framework_Assert::assertEquals($formData->getHash(), array_map(function($message) {
			return [
				'room' => self::$tokenToIdentifier[$message['token']],
				'actorType' => (string) $message['actorType'],
				'actorId' => ($message['actorType'] == 'guests')? self::$sessionIdToUser[$message['actorId']]: (string) $message['actorId'],
				'actorDisplayName' => (string) $message['actorDisplayName'],
				// TODO test timestamp; it may require using Runkit, php-timecop
				// or something like that.
				'message' => (string) $message['message'],
			];
		}, $messages));
	}

	/**
	 * Parses the xml answer to get the array of users returned.
	 * @param ResponseInterface $response
	 * @return array
	 */
	protected function getDataFromResponse(ResponseInterface $response) {
		return $response->json()['ocs']['data'];
	}

	/**
	 * @Then /^status code is ([0-9]*)$/
	 *
	 * @param int $statusCode
	 */
	public function isStatusCode($statusCode) {
		$this->assertStatusCode($this->response, $statusCode);
	}

	/**
	 * @BeforeScenario
	 * @AfterScenario
	 */
	public function resetSpreedAppData() {
		$client = new Client();
		$options = [
			'auth' => ['admin', 'admin'],
		];

		try {
			return $client->send($client->createRequest('DELETE', getenv('TEST_SERVER_URL') . 'ocs/v2.php/apps/spreedcheats/', $options));
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			return $ex->getResponse();
		}
	}

	/*
	 * User management
	 */

	/**
	 * @Given /^as user "([^"]*)"$/
	 * @param string $user
	 */
	public function setCurrentUser($user) {
		$this->currentUser = $user;
	}

	/**
	 * @Given /^user "([^"]*)" exists$/
	 * @param string $user
	 */
	public function assureUserExists($user) {
		try {
			$this->userExists($user);
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->createUser($user);
			// Set a display name different than the user ID to be able to
			// ensure in the tests that the right value was returned.
			$this->setUserDisplayName($user);
		}
		$response = $this->userExists($user);
		$this->assertStatusCode($response, 200);
	}

	private function userExists($user) {
		$client = new Client();
		$options = [
			'auth' => ['admin', 'admin'],
			'headers' => [
				'OCS-APIREQUEST' => 'true',
			],
		];
		return $client->get($this->baseUrl . 'ocs/v2.php/cloud/users/' . $user, $options);
	}

	private function createUser($user) {
		$userProvisioningUrl = $this->baseUrl . 'ocs/v2.php/cloud/users';
		$client = new Client();
		$options = [
			'auth' => ['admin', 'admin'],
			'body' => [
				'userid' => $user,
				'password' => '123456'
			],
			'headers' => [
				'OCS-APIREQUEST' => 'true',
			],
		];
		$client->send($client->createRequest('POST', $userProvisioningUrl, $options));

		//Quick hack to login once with the current user
		$options2 = [
			'auth' => [$user, '123456'],
			'headers' => [
				'OCS-APIREQUEST' => 'true',
			],
		];
		$client->send($client->createRequest('GET', $userProvisioningUrl . '/' . $user, $options2));
	}

	private function setUserDisplayName($user) {
		$userProvisioningUrl = $this->baseUrl . 'ocs/v2.php/cloud/users/' . $user;
		$client = new Client();
		$options = [
			'auth' => ['admin', 'admin'],
			'body' => [
				'key' => 'displayname',
				'value' => $user . '-displayname'
			],
			'headers' => [
				'OCS-APIREQUEST' => 'true',
			],
		];
		$client->send($client->createRequest('PUT', $userProvisioningUrl, $options));
	}

	/**
	 * @Given /^group "([^"]*)" exists$/
	 * @param string $group
	 */
	public function assureGroupExists($group) {
		try {
			$this->groupExists($group);
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->createGroup($group);
		}
		$response = $this->groupExists($group);
		$this->assertStatusCode($response, 200);
	}

	private function groupExists($group) {
		$client = new Client();
		$options = [
			'auth' => ['admin', 'admin'],
			'headers' => [
				'OCS-APIREQUEST' => 'true',
			],
		];
		return $client->get($this->baseUrl . 'ocs/v2.php/cloud/groups/' . $group, $options);
	}

	private function createGroup($group) {
		$userProvisioningUrl = $this->baseUrl . 'ocs/v2.php/cloud/groups';
		$client = new Client();
		$options = [
			'auth' => ['admin', 'admin'],
			'body' => [
				'groupid' => $group,
			],
			'headers' => [
				'OCS-APIREQUEST' => 'true',
			],
		];
		$client->send($client->createRequest('POST', $userProvisioningUrl, $options));
	}

	/**
	 * @When /^user "([^"]*)" is member of group "([^"]*)"$/
	 * @param string $user
	 * @param string $group
	 */
	public function addingUserToGroup($user, $group) {
		$userProvisioningUrl = $this->baseUrl . "ocs/v2.php/cloud/users/$user/groups";
		$client = new Client();
		$options = [
			'auth' => ['admin', 'admin'],
			'body' => [
				'groupid' => $group,
			],
			'headers' => [
				'OCS-APIREQUEST' => 'true',
			],
		];

		$this->response = $client->send($client->createRequest('POST', $userProvisioningUrl, $options));
	}

	/*
	 * Requests
	 */

	/**
	 * @When /^sending "([^"]*)" to "([^"]*)" with$/
	 * @param string $verb
	 * @param string $url
	 * @param TableNode $body
	 * @param array $headers
	 */
	public function sendRequest($verb, $url, $body = null, array $headers = []) {
		$fullUrl = $this->baseUrl . 'ocs/v2.php' . $url;
		$client = new Client();
		$options = ['cookies'  => $this->getUserCookieJar($this->currentUser)];
		if ($this->currentUser === 'admin') {
			$options['auth'] = ['admin', 'admin'];
		} else if (strpos($this->currentUser, 'guest') !== 0) {
			$options['auth'] = [$this->currentUser, '123456'];
		}
		if ($body instanceof TableNode) {
			$fd = $body->getRowsHash();
			$options['body'] = $fd;
		}

		$options['headers'] = array_merge($headers, [
			'OCS-ApiRequest' => 'true',
			'Accept' => 'application/json',
		]);

		try {
			$this->response = $client->send($client->createRequest($verb, $fullUrl, $options));
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
		}
	}

	protected function getUserCookieJar($user) {
		if (!isset($this->cookieJars[$user])) {
			$this->cookieJars[$user] = new CookieJar();
		}
		return $this->cookieJars[$user];
	}

	/**
	 * @param ResponseInterface $response
	 * @param int $statusCode
	 */
	protected function assertStatusCode(ResponseInterface $response, $statusCode) {
		PHPUnit_Framework_Assert::assertEquals($statusCode, $response->getStatusCode());
	}

	protected function typeStringToInt($roomType) {
		switch ($roomType) {
			case 'one2one':
				return 1;
			case 'group':
				return 2;
			case 'public':
				return 3;
		}

		throw new \RuntimeException('Invalid room type');
	}
}