Completed
Push — master ( 3a2657...9dd439 )
by Joas
28:56
created
build/integration/features/bootstrap/Auth.php 1 patch
Indentation   +218 added lines, -218 removed lines patch added patch discarded remove patch
@@ -13,244 +13,244 @@
 block discarded – undo
13 13
 require __DIR__ . '/../../vendor/autoload.php';
14 14
 
15 15
 trait Auth {
16
-	/** @var string */
17
-	private $unrestrictedClientToken;
18
-	/** @var string */
19
-	private $restrictedClientToken;
20
-	/** @var Client */
21
-	private $client;
22
-	/** @var string */
23
-	private $responseXml;
16
+    /** @var string */
17
+    private $unrestrictedClientToken;
18
+    /** @var string */
19
+    private $restrictedClientToken;
20
+    /** @var Client */
21
+    private $client;
22
+    /** @var string */
23
+    private $responseXml;
24 24
 
25
-	/** @BeforeScenario */
26
-	public function setUpScenario() {
27
-		$this->client = new Client();
28
-		$this->responseXml = '';
29
-		$this->cookieJar = new CookieJar();
30
-	}
25
+    /** @BeforeScenario */
26
+    public function setUpScenario() {
27
+        $this->client = new Client();
28
+        $this->responseXml = '';
29
+        $this->cookieJar = new CookieJar();
30
+    }
31 31
 
32
-	/**
33
-	 * @When requesting :url with :method
34
-	 */
35
-	public function requestingWith($url, $method) {
36
-		$this->sendRequest($url, $method);
37
-	}
32
+    /**
33
+     * @When requesting :url with :method
34
+     */
35
+    public function requestingWith($url, $method) {
36
+        $this->sendRequest($url, $method);
37
+    }
38 38
 
39
-	private function sendRequest($url, $method, $authHeader = null, $useCookies = false) {
40
-		$fullUrl = substr($this->baseUrl, 0, -5) . $url;
41
-		try {
42
-			if ($useCookies) {
43
-				$options = [
44
-					'cookies' => $this->cookieJar,
45
-				];
46
-			} else {
47
-				$options = [];
48
-			}
49
-			if ($authHeader) {
50
-				$options['headers'] = [
51
-					'Authorization' => $authHeader
52
-				];
53
-			}
54
-			$options['headers']['OCS_APIREQUEST'] = 'true';
55
-			$options['headers']['requesttoken'] = $this->requestToken;
56
-			$this->response = $this->client->request($method, $fullUrl, $options);
57
-		} catch (ClientException $ex) {
58
-			$this->response = $ex->getResponse();
59
-		} catch (ServerException $ex) {
60
-			$this->response = $ex->getResponse();
61
-		}
62
-	}
39
+    private function sendRequest($url, $method, $authHeader = null, $useCookies = false) {
40
+        $fullUrl = substr($this->baseUrl, 0, -5) . $url;
41
+        try {
42
+            if ($useCookies) {
43
+                $options = [
44
+                    'cookies' => $this->cookieJar,
45
+                ];
46
+            } else {
47
+                $options = [];
48
+            }
49
+            if ($authHeader) {
50
+                $options['headers'] = [
51
+                    'Authorization' => $authHeader
52
+                ];
53
+            }
54
+            $options['headers']['OCS_APIREQUEST'] = 'true';
55
+            $options['headers']['requesttoken'] = $this->requestToken;
56
+            $this->response = $this->client->request($method, $fullUrl, $options);
57
+        } catch (ClientException $ex) {
58
+            $this->response = $ex->getResponse();
59
+        } catch (ServerException $ex) {
60
+            $this->response = $ex->getResponse();
61
+        }
62
+    }
63 63
 
64
-	/**
65
-	 * @When the CSRF token is extracted from the previous response
66
-	 */
67
-	public function theCsrfTokenIsExtractedFromThePreviousResponse() {
68
-		$this->requestToken = substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $this->response->getBody()->getContents()), 0, 89);
69
-	}
64
+    /**
65
+     * @When the CSRF token is extracted from the previous response
66
+     */
67
+    public function theCsrfTokenIsExtractedFromThePreviousResponse() {
68
+        $this->requestToken = substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $this->response->getBody()->getContents()), 0, 89);
69
+    }
70 70
 
71
-	/**
72
-	 * @param bool $loginViaWeb
73
-	 * @return object
74
-	 */
75
-	private function createClientToken($loginViaWeb = true) {
76
-		if ($loginViaWeb) {
77
-			$this->loggingInUsingWebAs('user0');
78
-		}
71
+    /**
72
+     * @param bool $loginViaWeb
73
+     * @return object
74
+     */
75
+    private function createClientToken($loginViaWeb = true) {
76
+        if ($loginViaWeb) {
77
+            $this->loggingInUsingWebAs('user0');
78
+        }
79 79
 
80
-		$fullUrl = substr($this->baseUrl, 0, -5) . '/index.php/settings/personal/authtokens';
81
-		$client = new Client();
82
-		$options = [
83
-			'auth' => [
84
-				'user0',
85
-				$loginViaWeb ? '123456' : $this->restrictedClientToken,
86
-			],
87
-			'form_params' => [
88
-				'requesttoken' => $this->requestToken,
89
-				'name' => md5(microtime()),
90
-			],
91
-			'cookies' => $this->cookieJar,
92
-		];
80
+        $fullUrl = substr($this->baseUrl, 0, -5) . '/index.php/settings/personal/authtokens';
81
+        $client = new Client();
82
+        $options = [
83
+            'auth' => [
84
+                'user0',
85
+                $loginViaWeb ? '123456' : $this->restrictedClientToken,
86
+            ],
87
+            'form_params' => [
88
+                'requesttoken' => $this->requestToken,
89
+                'name' => md5(microtime()),
90
+            ],
91
+            'cookies' => $this->cookieJar,
92
+        ];
93 93
 
94
-		try {
95
-			$this->response = $client->request('POST', $fullUrl, $options);
96
-		} catch (\GuzzleHttp\Exception\ServerException $e) {
97
-			$this->response = $e->getResponse();
98
-		}
99
-		return json_decode($this->response->getBody()->getContents());
100
-	}
94
+        try {
95
+            $this->response = $client->request('POST', $fullUrl, $options);
96
+        } catch (\GuzzleHttp\Exception\ServerException $e) {
97
+            $this->response = $e->getResponse();
98
+        }
99
+        return json_decode($this->response->getBody()->getContents());
100
+    }
101 101
 
102
-	/**
103
-	 * @Given a new restricted client token is added
104
-	 */
105
-	public function aNewRestrictedClientTokenIsAdded() {
106
-		$tokenObj = $this->createClientToken();
107
-		$newCreatedTokenId = $tokenObj->deviceToken->id;
108
-		$fullUrl = substr($this->baseUrl, 0, -5) . '/index.php/settings/personal/authtokens/' . $newCreatedTokenId;
109
-		$client = new Client();
110
-		$options = [
111
-			'auth' => ['user0', '123456'],
112
-			'headers' => [
113
-				'requesttoken' => $this->requestToken,
114
-			],
115
-			'json' => [
116
-				'name' => md5(microtime()),
117
-				'scope' => [
118
-					'filesystem' => false,
119
-				],
120
-			],
121
-			'cookies' => $this->cookieJar,
122
-		];
123
-		$this->response = $client->request('PUT', $fullUrl, $options);
124
-		$this->restrictedClientToken = $tokenObj->token;
125
-	}
102
+    /**
103
+     * @Given a new restricted client token is added
104
+     */
105
+    public function aNewRestrictedClientTokenIsAdded() {
106
+        $tokenObj = $this->createClientToken();
107
+        $newCreatedTokenId = $tokenObj->deviceToken->id;
108
+        $fullUrl = substr($this->baseUrl, 0, -5) . '/index.php/settings/personal/authtokens/' . $newCreatedTokenId;
109
+        $client = new Client();
110
+        $options = [
111
+            'auth' => ['user0', '123456'],
112
+            'headers' => [
113
+                'requesttoken' => $this->requestToken,
114
+            ],
115
+            'json' => [
116
+                'name' => md5(microtime()),
117
+                'scope' => [
118
+                    'filesystem' => false,
119
+                ],
120
+            ],
121
+            'cookies' => $this->cookieJar,
122
+        ];
123
+        $this->response = $client->request('PUT', $fullUrl, $options);
124
+        $this->restrictedClientToken = $tokenObj->token;
125
+    }
126 126
 
127
-	/**
128
-	 * @Given a new unrestricted client token is added
129
-	 */
130
-	public function aNewUnrestrictedClientTokenIsAdded() {
131
-		$this->unrestrictedClientToken = $this->createClientToken()->token;
132
-	}
127
+    /**
128
+     * @Given a new unrestricted client token is added
129
+     */
130
+    public function aNewUnrestrictedClientTokenIsAdded() {
131
+        $this->unrestrictedClientToken = $this->createClientToken()->token;
132
+    }
133 133
 
134
-	/**
135
-	 * @When a new unrestricted client token is added using restricted basic token auth
136
-	 */
137
-	public function aNewUnrestrictedClientTokenIsAddedUsingRestrictedBasicTokenAuth() {
138
-		$this->createClientToken(false);
139
-	}
134
+    /**
135
+     * @When a new unrestricted client token is added using restricted basic token auth
136
+     */
137
+    public function aNewUnrestrictedClientTokenIsAddedUsingRestrictedBasicTokenAuth() {
138
+        $this->createClientToken(false);
139
+    }
140 140
 
141
-	/**
142
-	 * @When requesting :url with :method using basic auth
143
-	 *
144
-	 * @param string $url
145
-	 * @param string $method
146
-	 */
147
-	public function requestingWithBasicAuth($url, $method) {
148
-		$this->sendRequest($url, $method, 'basic ' . base64_encode('user0:123456'));
149
-	}
141
+    /**
142
+     * @When requesting :url with :method using basic auth
143
+     *
144
+     * @param string $url
145
+     * @param string $method
146
+     */
147
+    public function requestingWithBasicAuth($url, $method) {
148
+        $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:123456'));
149
+    }
150 150
 
151
-	/**
152
-	 * @When requesting :url with :method using unrestricted basic token auth
153
-	 *
154
-	 * @param string $url
155
-	 * @param string $method
156
-	 */
157
-	public function requestingWithUnrestrictedBasicTokenAuth($url, $method) {
158
-		$this->sendRequest($url, $method, 'basic ' . base64_encode('user0:' . $this->unrestrictedClientToken), true);
159
-	}
151
+    /**
152
+     * @When requesting :url with :method using unrestricted basic token auth
153
+     *
154
+     * @param string $url
155
+     * @param string $method
156
+     */
157
+    public function requestingWithUnrestrictedBasicTokenAuth($url, $method) {
158
+        $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:' . $this->unrestrictedClientToken), true);
159
+    }
160 160
 
161
-	/**
162
-	 * @When requesting :url with :method using restricted basic token auth
163
-	 *
164
-	 * @param string $url
165
-	 * @param string $method
166
-	 */
167
-	public function requestingWithRestrictedBasicTokenAuth($url, $method) {
168
-		$this->sendRequest($url, $method, 'basic ' . base64_encode('user0:' . $this->restrictedClientToken), true);
169
-	}
161
+    /**
162
+     * @When requesting :url with :method using restricted basic token auth
163
+     *
164
+     * @param string $url
165
+     * @param string $method
166
+     */
167
+    public function requestingWithRestrictedBasicTokenAuth($url, $method) {
168
+        $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:' . $this->restrictedClientToken), true);
169
+    }
170 170
 
171
-	/**
172
-	 * @When requesting :url with :method using an unrestricted client token
173
-	 *
174
-	 * @param string $url
175
-	 * @param string $method
176
-	 */
177
-	public function requestingWithUsingAnUnrestrictedClientToken($url, $method) {
178
-		$this->sendRequest($url, $method, 'Bearer ' . $this->unrestrictedClientToken);
179
-	}
171
+    /**
172
+     * @When requesting :url with :method using an unrestricted client token
173
+     *
174
+     * @param string $url
175
+     * @param string $method
176
+     */
177
+    public function requestingWithUsingAnUnrestrictedClientToken($url, $method) {
178
+        $this->sendRequest($url, $method, 'Bearer ' . $this->unrestrictedClientToken);
179
+    }
180 180
 
181
-	/**
182
-	 * @When requesting :url with :method using a restricted client token
183
-	 *
184
-	 * @param string $url
185
-	 * @param string $method
186
-	 */
187
-	public function requestingWithUsingARestrictedClientToken($url, $method) {
188
-		$this->sendRequest($url, $method, 'Bearer ' . $this->restrictedClientToken);
189
-	}
181
+    /**
182
+     * @When requesting :url with :method using a restricted client token
183
+     *
184
+     * @param string $url
185
+     * @param string $method
186
+     */
187
+    public function requestingWithUsingARestrictedClientToken($url, $method) {
188
+        $this->sendRequest($url, $method, 'Bearer ' . $this->restrictedClientToken);
189
+    }
190 190
 
191
-	/**
192
-	 * @When requesting :url with :method using browser session
193
-	 *
194
-	 * @param string $url
195
-	 * @param string $method
196
-	 */
197
-	public function requestingWithBrowserSession($url, $method) {
198
-		$this->sendRequest($url, $method, null, true);
199
-	}
191
+    /**
192
+     * @When requesting :url with :method using browser session
193
+     *
194
+     * @param string $url
195
+     * @param string $method
196
+     */
197
+    public function requestingWithBrowserSession($url, $method) {
198
+        $this->sendRequest($url, $method, null, true);
199
+    }
200 200
 
201
-	/**
202
-	 * @Given a new browser session is started
203
-	 *
204
-	 * @param bool $remember
205
-	 */
206
-	public function aNewBrowserSessionIsStarted($remember = false) {
207
-		$baseUrl = substr($this->baseUrl, 0, -5);
208
-		$loginUrl = $baseUrl . '/login';
209
-		// Request a new session and extract CSRF token
210
-		$client = new Client();
211
-		$response = $client->get($loginUrl, [
212
-			'cookies' => $this->cookieJar,
213
-		]);
214
-		$this->extracRequestTokenFromResponse($response);
201
+    /**
202
+     * @Given a new browser session is started
203
+     *
204
+     * @param bool $remember
205
+     */
206
+    public function aNewBrowserSessionIsStarted($remember = false) {
207
+        $baseUrl = substr($this->baseUrl, 0, -5);
208
+        $loginUrl = $baseUrl . '/login';
209
+        // Request a new session and extract CSRF token
210
+        $client = new Client();
211
+        $response = $client->get($loginUrl, [
212
+            'cookies' => $this->cookieJar,
213
+        ]);
214
+        $this->extracRequestTokenFromResponse($response);
215 215
 
216
-		// Login and extract new token
217
-		$client = new Client();
218
-		$response = $client->post(
219
-			$loginUrl, [
220
-				'form_params' => [
221
-					'user' => 'user0',
222
-					'password' => '123456',
223
-					'rememberme' => $remember ? '1' : '0',
224
-					'requesttoken' => $this->requestToken,
225
-				],
226
-				'cookies' => $this->cookieJar,
227
-				'headers' => [
228
-					'Origin' => $baseUrl,
229
-				],
230
-			]
231
-		);
232
-		$this->extracRequestTokenFromResponse($response);
233
-	}
216
+        // Login and extract new token
217
+        $client = new Client();
218
+        $response = $client->post(
219
+            $loginUrl, [
220
+                'form_params' => [
221
+                    'user' => 'user0',
222
+                    'password' => '123456',
223
+                    'rememberme' => $remember ? '1' : '0',
224
+                    'requesttoken' => $this->requestToken,
225
+                ],
226
+                'cookies' => $this->cookieJar,
227
+                'headers' => [
228
+                    'Origin' => $baseUrl,
229
+                ],
230
+            ]
231
+        );
232
+        $this->extracRequestTokenFromResponse($response);
233
+    }
234 234
 
235
-	/**
236
-	 * @Given a new remembered browser session is started
237
-	 */
238
-	public function aNewRememberedBrowserSessionIsStarted() {
239
-		$this->aNewBrowserSessionIsStarted(true);
240
-	}
235
+    /**
236
+     * @Given a new remembered browser session is started
237
+     */
238
+    public function aNewRememberedBrowserSessionIsStarted() {
239
+        $this->aNewBrowserSessionIsStarted(true);
240
+    }
241 241
 
242 242
 
243
-	/**
244
-	 * @Given the cookie jar is reset
245
-	 */
246
-	public function theCookieJarIsReset() {
247
-		$this->cookieJar = new CookieJar();
248
-	}
243
+    /**
244
+     * @Given the cookie jar is reset
245
+     */
246
+    public function theCookieJarIsReset() {
247
+        $this->cookieJar = new CookieJar();
248
+    }
249 249
 
250
-	/**
251
-	 * @When the session cookie expires
252
-	 */
253
-	public function whenTheSessionCookieExpires() {
254
-		$this->cookieJar->clearSessionCookies();
255
-	}
250
+    /**
251
+     * @When the session cookie expires
252
+     */
253
+    public function whenTheSessionCookieExpires() {
254
+        $this->cookieJar->clearSessionCookies();
255
+    }
256 256
 }
Please login to merge, or discard this patch.