summaryrefslogtreecommitdiffstats
path: root/tests/lib/api.php
blob: 1d1f97c4942e33f986afeb3d14b5e83e80401e4c (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
<?php
/**
 * Copyright (c) 2013 Tom Needham <tom@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

class Test_API extends \Test\TestCase {

	// Helps build a response variable

	/**
	 * @param string $message
	 */
	function buildResponse($shipped, $data, $code, $message=null) {
		$resp = new OC_OCS_Result($data, $code, $message);
		$resp->addHeader('KEY', 'VALUE');
		return [
			'shipped' => $shipped,
			'response' => $resp,
			'app' => $this->getUniqueID('testapp_'),
		];
	}

	// Validate details of the result

	/**
	 * @param OC_OCS_Result $result
	 */
	function checkResult($result, $success) {
		// Check response is of correct type
		$this->assertInstanceOf('OC_OCS_Result', $result);
		// Check if it succeeded
		/** @var $result OC_OCS_Result */
		$this->assertEquals($success, $result->succeeded());
	}

	/**
	 * @return array
	 */
	public function versionDataScriptNameProvider() {
		return [
			// Valid script name
			[
				'/master/ocs/v2.php',
				true,
			],

			// Invalid script names
			[
				'/master/ocs/v2.php/someInvalidPathName',
				false,
			],
			[
				'/master/ocs/v1.php',
				false,
			],
			[
				'',
				false,
			],
		];
	}

	/**
	 * @dataProvider versionDataScriptNameProvider
	 * @param string $scriptName
	 * @param bool $expected
	 */
	public function testIsV2($scriptName, $expected) {
		$request = $this->getMockBuilder('\OCP\IRequest')
			->disableOriginalConstructor()
			->getMock();
		$request
			->expects($this->once())
			->method('getScriptName')
			->will($this->returnValue($scriptName));

		$this->assertEquals($expected, $this->invokePrivate(new \OC_API, 'isV2', [$request]));
	}

	function dataProviderTestOneResult() {
		return [
			[100, true],
			[101, false],
			[997, false],
		];
	}

	/**
	 * @dataProvider dataProviderTestOneResult
	 *
	 * @param $statusCode
	 * @param $succeeded
	 */
	public function testOneResult($statusCode, $succeeded) {
		// Setup some data arrays
		$data1 = [
			'users' => [
				'tom' => [
					'key' => 'value',
				],
				'frank' => [
					'key' => 'value',
				],
			]];

		// Test merging one success result
		$response = $this->buildResponse(true, $data1, $statusCode);
		$result = OC_API::mergeResponses([$response]);
		$this->assertEquals($response['response'], $result);
		$this->checkResult($result, $succeeded);
	}

	function dataProviderTestMergeResponses() {
		return [
			// Two shipped success results
			[true, 100, true, 100, true],
			// Two shipped results, one success and one failure
			[true, 100, true, 998, false],
			// Two shipped results, both failure
			[true, 997, true, 998, false],
			// Two third party success results
			[false, 100, false, 100, true],
			// Two third party results, one success and one failure
			[false, 100, false, 998, false],
			// Two third party results, both failure
			[false, 997, false, 998, false],
			// One of each, both success
			[false, 100, true, 100, true],
			[true, 100, false, 100, true],
			// One of each, both failure
			[false, 997, true, 998, false],
			// One of each, shipped success
			[false, 997, true, 100, true],
			// One of each, third party success
			[false, 100, true, 998, false],
		];
	}
	/**
	 * @dataProvider dataProviderTestMergeResponses
	 *
	 * Test the merging of multiple responses
	 * @param $statusCode1
	 * @param $statusCode2
	 * @param $succeeded
	 */
	public function testMultipleMergeResponses($shipped1, $statusCode1, $shipped2, $statusCode2, $succeeded){
		// Tests that app responses are merged correctly
		// Setup some data arrays
		$data1 = array(
			'users' => array(
				'tom' => array(
					'key' => 'value',
				),
				'frank' => array(
					'key' => 'value',
				),
			));

		$data2 = array(
			'users' => array(
				'tom' => array(
					'key' => 'newvalue',
				),
				'jan' => array(
					'key' => 'value',
				),
			));

		// Two shipped success results
		$result = OC_API::mergeResponses(array(
			$this->buildResponse($shipped1, $data1, $statusCode1, "message1"),
			$this->buildResponse($shipped2, $data2, $statusCode2, "message2"),
		));
		$this->checkResult($result, $succeeded);
		$resultData = $result->getData();
		$resultMeta = $result->getMeta();
		$resultHeaders = $result->getHeaders();
		$resultStatusCode = $result->getStatusCode();

		$this->assertArrayHasKey('jan', $resultData['users']);
		$this->assertArrayHasKey('KEY', $resultHeaders);

		// check if the returned status message matches the selected status code
		if ($resultStatusCode === 997) {
			$this->assertEquals('message1', $resultMeta['message']);
		} elseif ($resultStatusCode === 998) {
			$this->assertEquals('message2', $resultMeta['message']);
		} elseif ($resultStatusCode === 100) {
			$this->assertEquals(null, $resultMeta['message']);
		}

	}

}