blob: 3ba30d6adfa3791d12b82a24d4c56ae7dda8c969 (
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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Marketplace\tests\Integration;
use Piwik\Plugins\Marketplace\API;
use Piwik\Plugins\Marketplace\LicenseKey;
use Piwik\Plugins\Marketplace\tests\Framework\Mock\Service;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Plugins\Marketplace\Api\Service\Exception as ServiceException;
use Exception;
/**
* @group Marketplace
* @group ApiTest
* @group Api
* @group Plugins
*/
class ApiTest extends IntegrationTestCase
{
/**
* @var API
*/
private $api;
/**
* @var Service
*/
private $service;
public function setUp(): void
{
parent::setUp();
API::unsetInstance();
$this->api = API::getInstance();
Fixture::createSuperUser();
if (!Fixture::siteCreated(1)) {
Fixture::createWebsite('2012-01-01 00:00:00');
}
$this->setSuperUser();
}
public function test_deleteLicenseKey_requiresSuperUserAccess_IfUser()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('checkUserHasSuperUserAccess');
$this->setUser();
$this->api->deleteLicenseKey();
}
public function test_deleteLicenseKey_requiresSuperUserAccess_IfAnonymous()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('checkUserHasSuperUserAccess');
$this->setAnonymousUser();
$this->api->deleteLicenseKey();
}
public function test_deleteLicenseKey_shouldRemoveAnExistingKey()
{
$this->buildLicenseKey()->set('key');
$this->assertHasLicenseKey();
$this->api->deleteLicenseKey();
$this->assertNotHasLicenseKey();
}
public function test_saveLicenseKey_requiresSuperUserAccess_IfUser()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('checkUserHasSuperUserAccess');
$this->setUser();
$this->api->saveLicenseKey('key');
}
public function test_saveLicenseKey_requiresSuperUserAccess_IfAnonymous()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('checkUserHasSuperUserAccess');
$this->setAnonymousUser();
$this->api->saveLicenseKey('key');
}
public function test_saveLicenseKey_shouldThrowException_IfTokenIsNotValid()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Marketplace_ExceptionLinceseKeyIsNotValid');
$this->service->returnFixture('v2.0_consumer_validate-access_token-notexistingtoken.json');
$this->api->saveLicenseKey('key');
}
public function test_saveLicenseKey_shouldCallTheApiTheCorrectWay()
{
$this->service->returnFixture('v2.0_consumer-access_token-valid_but_expired.json');
try {
$this->api->saveLicenseKey('key123');
} catch (Exception $e) {
}
// make sure calls API the correct way
$this->assertSame('consumer/validate', $this->service->action);
$this->assertSame(array(), $this->service->params);
$this->assertSame('key123', $this->service->getAccessToken());
$this->assertNotHasLicenseKey();
}
public function test_saveLicenseKey_shouldActuallySaveToken_IfValid()
{
$this->service->returnFixture('v2.0_consumer_validate-access_token-consumer1_paid2_custom1.json');
$success = $this->api->saveLicenseKey('123licensekey');
$this->assertTrue($success);
$this->assertHasLicenseKey();
$this->assertSame('123licensekey', $this->buildLicenseKey()->get());
}
public function test_saveLicenseKey_shouldThrowException_IfConnectionToMarketplaceFailed()
{
$this->expectException(\Piwik\Plugins\Marketplace\Api\Service\Exception::class);
$this->expectExceptionMessage('Host not reachable');
$this->service->throwException(new ServiceException('Host not reachable', ServiceException::HTTP_ERROR));
$success = $this->api->saveLicenseKey('123licensekey');
$this->assertTrue($success);
$this->assertHasLicenseKey();
$this->assertSame('123licensekey', $this->buildLicenseKey()->get());
}
public function provideContainerConfig()
{
$this->service = new Service();
return array(
'Piwik\Access' => new FakeAccess(),
'Piwik\Plugins\Marketplace\Api\Service' => $this->service
);
}
protected function setSuperUser()
{
FakeAccess::clearAccess(true);
}
protected function setUser()
{
FakeAccess::clearAccess(false);
FakeAccess::$idSitesView = array(1);
FakeAccess::$idSitesAdmin = array();
FakeAccess::$identity = 'aUser';
}
protected function setAnonymousUser()
{
FakeAccess::clearAccess();
FakeAccess::$identity = 'anonymous';
}
protected function buildLicenseKey()
{
return new LicenseKey();
}
private function assertHasLicenseKey()
{
$this->assertTrue($this->buildLicenseKey()->has());
}
private function assertNotHasLicenseKey()
{
$this->assertFalse($this->buildLicenseKey()->has());
}
}
|