1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace aiprovider_azureai;
|
|
|
18 |
|
|
|
19 |
use core_ai\aiactions\base;
|
|
|
20 |
use core_ai\manager;
|
|
|
21 |
use GuzzleHttp\Psr7\Response;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Test response_base Azure AI provider methods.
|
|
|
25 |
*
|
|
|
26 |
* @package aiprovider_azureai
|
|
|
27 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
* @covers \aiprovider_azureai\process_generate_image
|
|
|
30 |
*/
|
|
|
31 |
final class process_generate_image_test extends \advanced_testcase {
|
|
|
32 |
/** @var string A successful response in JSON format. */
|
|
|
33 |
protected string $responsebodyjson;
|
|
|
34 |
|
|
|
35 |
/** @var manager $manager */
|
|
|
36 |
private manager $manager;
|
|
|
37 |
|
|
|
38 |
/** @var provider The provider that will process the action. */
|
|
|
39 |
protected provider $provider;
|
|
|
40 |
|
|
|
41 |
/** @var base The action to process. */
|
|
|
42 |
protected base $action;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Set up the test.
|
|
|
46 |
*/
|
|
|
47 |
protected function setUp(): void {
|
|
|
48 |
parent::setUp();
|
|
|
49 |
$this->resetAfterTest();
|
|
|
50 |
// Load a response body from a file.
|
|
|
51 |
$this->responsebodyjson = file_get_contents(self::get_fixture_path('aiprovider_azureai', 'image_request_success.json'));
|
|
|
52 |
$this->create_provider();
|
|
|
53 |
$this->create_action();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Create the provider object.
|
|
|
58 |
*/
|
|
|
59 |
private function create_provider(): void {
|
|
|
60 |
$this->manager = \core\di::get(\core_ai\manager::class);
|
|
|
61 |
$config = [
|
|
|
62 |
'apikey' => '123',
|
|
|
63 |
'endpoint' => 'https://api.example.com',
|
|
|
64 |
'enableuserratelimit' => true,
|
|
|
65 |
'userratelimit' => 1,
|
|
|
66 |
'enableglobalratelimit' => true,
|
|
|
67 |
'globalratelimit' => 1,
|
|
|
68 |
];
|
|
|
69 |
$this->provider = $this->manager->create_provider_instance(
|
|
|
70 |
classname: '\aiprovider_azureai\provider',
|
|
|
71 |
name: 'dummy',
|
|
|
72 |
config: $config,
|
|
|
73 |
);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Create the action object.
|
|
|
78 |
*
|
|
|
79 |
* @param int $userid The user id to use in the action.
|
|
|
80 |
*/
|
|
|
81 |
private function create_action(int $userid = 1): void {
|
|
|
82 |
$this->action = new \core_ai\aiactions\generate_image(
|
|
|
83 |
contextid: 1,
|
|
|
84 |
userid: $userid,
|
|
|
85 |
prompttext: 'This is a test prompt',
|
|
|
86 |
quality: 'hd',
|
|
|
87 |
aspectratio: 'square',
|
|
|
88 |
numimages: 1,
|
|
|
89 |
style: 'vivid',
|
|
|
90 |
);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Test calculate_size.
|
|
|
95 |
*/
|
|
|
96 |
public function test_calculate_size(): void {
|
|
|
97 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
98 |
|
|
|
99 |
// We're working with a private method here, so we need to use reflection.
|
|
|
100 |
$method = new \ReflectionMethod($processor, 'calculate_size');
|
|
|
101 |
|
|
|
102 |
$ratio = 'square';
|
|
|
103 |
$size = $method->invoke($processor, $ratio);
|
|
|
104 |
$this->assertEquals('1024x1024', $size);
|
|
|
105 |
|
|
|
106 |
$ratio = 'portrait';
|
|
|
107 |
$size = $method->invoke($processor, $ratio);
|
|
|
108 |
$this->assertEquals('1024x1792', $size);
|
|
|
109 |
|
|
|
110 |
$ratio = 'landscape';
|
|
|
111 |
$size = $method->invoke($processor, $ratio);
|
|
|
112 |
$this->assertEquals('1792x1024', $size);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Test create_request_object
|
|
|
117 |
*/
|
|
|
118 |
public function test_create_request_object(): void {
|
|
|
119 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
120 |
|
|
|
121 |
// We're working with a private method here, so we need to use reflection.
|
|
|
122 |
$method = new \ReflectionMethod($processor, 'create_request_object');
|
|
|
123 |
$request = $method->invoke($processor, 1);
|
|
|
124 |
|
|
|
125 |
$requestdata = (object) json_decode($request->getBody()->getContents());
|
|
|
126 |
|
|
|
127 |
$this->assertEquals('This is a test prompt', $requestdata->prompt);
|
|
|
128 |
$this->assertEquals('1', $requestdata->n);
|
|
|
129 |
$this->assertEquals('hd', $requestdata->quality);
|
|
|
130 |
$this->assertEquals('1024x1024', $requestdata->size);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Test the API error response handler method.
|
|
|
135 |
*/
|
|
|
136 |
public function test_handle_api_error(): void {
|
|
|
137 |
$responses = [
|
|
|
138 |
500 => new Response(500, ['Content-Type' => 'application/json']),
|
|
|
139 |
503 => new Response(503, ['Content-Type' => 'application/json']),
|
|
|
140 |
401 => new Response(401, ['Content-Type' => 'application/json'],
|
|
|
141 |
'{"error": {"message": "Invalid Authentication"}}'),
|
|
|
142 |
404 => new Response(404, ['Content-Type' => 'application/json'],
|
|
|
143 |
'{"error": {"message": "You must be a member of an organization to use the API"}}'),
|
|
|
144 |
429 => new Response(429, ['Content-Type' => 'application/json'],
|
|
|
145 |
'{"error": {"message": "Rate limit reached for requests"}}'),
|
|
|
146 |
];
|
|
|
147 |
|
|
|
148 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
149 |
$method = new \ReflectionMethod($processor, 'handle_api_error');
|
|
|
150 |
|
|
|
151 |
foreach ($responses as $status => $response) {
|
|
|
152 |
$result = $method->invoke($processor, $response);
|
|
|
153 |
$this->assertEquals($status, $result['errorcode']);
|
|
|
154 |
if ($status == 500) {
|
|
|
155 |
$this->assertEquals('Internal Server Error', $result['errormessage']);
|
|
|
156 |
} else if ($status == 503) {
|
|
|
157 |
$this->assertEquals('Service Unavailable', $result['errormessage']);
|
|
|
158 |
} else {
|
|
|
159 |
$this->assertStringContainsString($response->getBody()->getContents(), $result['errormessage']);
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Test the API success response handler method.
|
|
|
166 |
*/
|
|
|
167 |
public function test_handle_api_success(): void {
|
|
|
168 |
$response = new Response(
|
|
|
169 |
200,
|
|
|
170 |
['Content-Type' => 'application/json'],
|
|
|
171 |
$this->responsebodyjson
|
|
|
172 |
);
|
|
|
173 |
|
|
|
174 |
// We're testing a private method, so we need to setup reflector magic.
|
|
|
175 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
176 |
$method = new \ReflectionMethod($processor, 'handle_api_success');
|
|
|
177 |
|
|
|
178 |
$result = $method->invoke($processor, $response);
|
|
|
179 |
|
|
|
180 |
$this->stringContains('An image that represents the concept of a \'test\'.', $result['revisedprompt']);
|
|
|
181 |
$this->stringContains('oaidalleapiprodscus.blob.core.windows.net', $result['sourceurl']);
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Test query_ai_api for a successful call.
|
|
|
186 |
*/
|
|
|
187 |
public function test_query_ai_api_success(): void {
|
|
|
188 |
// Mock the http client to return a successful response.
|
|
|
189 |
['mock' => $mock] = $this->get_mocked_http_client();
|
|
|
190 |
|
|
|
191 |
// The response from Azure AI.
|
|
|
192 |
$mock->append(new Response(
|
|
|
193 |
200,
|
|
|
194 |
['Content-Type' => 'application/json'],
|
|
|
195 |
$this->responsebodyjson,
|
|
|
196 |
));
|
|
|
197 |
|
|
|
198 |
$mock->append(new Response(
|
|
|
199 |
200,
|
|
|
200 |
['Content-Type' => 'image/jpeg'],
|
|
|
201 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(
|
|
|
202 |
self::get_fixture_path('aiprovider_azureai', 'test.jpg'),
|
|
|
203 |
'r',
|
|
|
204 |
)),
|
|
|
205 |
));
|
|
|
206 |
|
|
|
207 |
$this->setAdminUser();
|
|
|
208 |
|
|
|
209 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
210 |
$method = new \ReflectionMethod($processor, 'query_ai_api');
|
|
|
211 |
$result = $method->invoke($processor);
|
|
|
212 |
|
|
|
213 |
$this->stringContains('An image that represents the concept of a \'test\'.', $result['revisedprompt']);
|
|
|
214 |
$this->stringContains('oaidalleapiprodscus.blob.core.windows.net', $result['sourceurl']);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Test prepare_response success.
|
|
|
219 |
*/
|
|
|
220 |
public function test_prepare_response_success(): void {
|
|
|
221 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
222 |
|
|
|
223 |
// We're working with a private method here, so we need to use reflection.
|
|
|
224 |
$method = new \ReflectionMethod($processor, 'prepare_response');
|
|
|
225 |
|
|
|
226 |
$response = [
|
|
|
227 |
'success' => true,
|
|
|
228 |
'revisedprompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
229 |
'imageurl' => 'oaidalleapiprodscus.blob.core.windows.net',
|
|
|
230 |
];
|
|
|
231 |
|
|
|
232 |
$result = $method->invoke($processor, $response);
|
|
|
233 |
|
|
|
234 |
$this->assertInstanceOf(\core_ai\aiactions\responses\response_base::class, $result);
|
|
|
235 |
$this->assertTrue($result->get_success());
|
|
|
236 |
$this->assertEquals('generate_image', $result->get_actionname());
|
|
|
237 |
$this->assertEquals($response['success'], $result->get_success());
|
|
|
238 |
$this->assertEquals($response['revisedprompt'], $result->get_response_data()['revisedprompt']);
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Test prepare_response error.
|
|
|
243 |
*/
|
|
|
244 |
public function test_prepare_response_error(): void {
|
|
|
245 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
246 |
|
|
|
247 |
// We're working with a private method here, so we need to use reflection.
|
|
|
248 |
$method = new \ReflectionMethod($processor, 'prepare_response');
|
|
|
249 |
|
|
|
250 |
$response = [
|
|
|
251 |
'success' => false,
|
|
|
252 |
'errorcode' => 500,
|
|
|
253 |
'errormessage' => 'Internal server error.',
|
|
|
254 |
];
|
|
|
255 |
|
|
|
256 |
$result = $method->invoke($processor, $response);
|
|
|
257 |
|
|
|
258 |
$this->assertInstanceOf(\core_ai\aiactions\responses\response_base::class, $result);
|
|
|
259 |
$this->assertFalse($result->get_success());
|
|
|
260 |
$this->assertEquals('generate_image', $result->get_actionname());
|
|
|
261 |
$this->assertEquals($response['errorcode'], $result->get_errorcode());
|
|
|
262 |
$this->assertEquals($response['errormessage'], $result->get_errormessage());
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Test url_to_file.
|
|
|
267 |
*/
|
|
|
268 |
public function test_url_to_file(): void {
|
|
|
269 |
// Log in user.
|
|
|
270 |
$this->setUser($this->getDataGenerator()->create_user());
|
|
|
271 |
|
|
|
272 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
273 |
// We're working with a private method here, so we need to use reflection.
|
|
|
274 |
$method = new \ReflectionMethod($processor, 'url_to_file');
|
|
|
275 |
|
|
|
276 |
$contextid = 1;
|
|
|
277 |
$url = $this->getExternalTestFileUrl('/test.jpg', false);
|
|
|
278 |
$fileobj = $method->invoke($processor, $contextid, $url);
|
|
|
279 |
|
|
|
280 |
$this->assertEquals('user', $fileobj->get_component());
|
|
|
281 |
$this->assertEquals('draft', $fileobj->get_filearea());
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* Test process.
|
|
|
286 |
*/
|
|
|
287 |
public function test_process(): void {
|
|
|
288 |
// Log in user.
|
|
|
289 |
$this->setUser($this->getDataGenerator()->create_user());
|
|
|
290 |
|
|
|
291 |
// Mock the http client to return a successful response.
|
|
|
292 |
['mock' => $mock] = $this->get_mocked_http_client();
|
|
|
293 |
|
|
|
294 |
$url = 'https://example.com/test.jpg';
|
|
|
295 |
|
|
|
296 |
// The response from Azure AI.
|
|
|
297 |
$mock->append(new Response(
|
|
|
298 |
200,
|
|
|
299 |
['Content-Type' => 'application/json'],
|
|
|
300 |
json_encode([
|
|
|
301 |
'created' => 1719140500,
|
|
|
302 |
'data' => [
|
|
|
303 |
(object) [
|
|
|
304 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
305 |
'url' => $url,
|
|
|
306 |
],
|
|
|
307 |
],
|
|
|
308 |
]),
|
|
|
309 |
));
|
|
|
310 |
|
|
|
311 |
// The image downloaded from the server successfully.
|
|
|
312 |
$mock->append(new Response(
|
|
|
313 |
200,
|
|
|
314 |
['Content-Type' => 'image/jpeg'],
|
|
|
315 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
316 |
));
|
|
|
317 |
|
|
|
318 |
// Create a request object.
|
|
|
319 |
$contextid = 1;
|
|
|
320 |
$userid = 1;
|
|
|
321 |
$prompttext = 'This is a test prompt';
|
|
|
322 |
$aspectratio = 'square';
|
|
|
323 |
$quality = 'hd';
|
|
|
324 |
$numimages = 1;
|
|
|
325 |
$style = 'vivid';
|
|
|
326 |
$this->action = new \core_ai\aiactions\generate_image(
|
|
|
327 |
contextid: $contextid,
|
|
|
328 |
userid: $userid,
|
|
|
329 |
prompttext: $prompttext,
|
|
|
330 |
quality: $quality,
|
|
|
331 |
aspectratio: $aspectratio,
|
|
|
332 |
numimages: $numimages,
|
|
|
333 |
style: $style,
|
|
|
334 |
);
|
|
|
335 |
|
|
|
336 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
337 |
$result = $processor->process();
|
|
|
338 |
|
|
|
339 |
$this->assertInstanceOf(\core_ai\aiactions\responses\response_base::class, $result);
|
|
|
340 |
$this->assertTrue($result->get_success());
|
|
|
341 |
$this->assertEquals('generate_image', $result->get_actionname());
|
|
|
342 |
$this->assertEquals('An image that represents the concept of a \'test\'.', $result->get_response_data()['revisedprompt']);
|
|
|
343 |
$this->assertEquals($url, $result->get_response_data()['sourceurl']);
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
* Test process method with error.
|
|
|
348 |
*/
|
|
|
349 |
public function test_process_error(): void {
|
|
|
350 |
// Log in user.
|
|
|
351 |
$this->setUser($this->getDataGenerator()->create_user());
|
|
|
352 |
|
|
|
353 |
// Mock the http client to return a successful response.
|
|
|
354 |
['mock' => $mock] = $this->get_mocked_http_client();
|
|
|
355 |
|
|
|
356 |
// The response from Azure AI.
|
|
|
357 |
$mock->append(new Response(
|
|
|
358 |
401,
|
|
|
359 |
['Content-Type' => 'application/json'],
|
|
|
360 |
json_encode(['error' => ['message' => 'Invalid Authentication']]),
|
|
|
361 |
));
|
|
|
362 |
|
|
|
363 |
$processor = new process_generate_image($this->provider, $this->action);
|
|
|
364 |
$result = $processor->process();
|
|
|
365 |
|
|
|
366 |
$this->assertInstanceOf(\core_ai\aiactions\responses\response_base::class, $result);
|
|
|
367 |
$this->assertFalse($result->get_success());
|
|
|
368 |
$this->assertEquals('generate_image', $result->get_actionname());
|
|
|
369 |
$this->assertEquals(401, $result->get_errorcode());
|
|
|
370 |
$this->assertEquals('Invalid Authentication', $result->get_errormessage());
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
/**
|
|
|
374 |
* Test process method with user rate limiter.
|
|
|
375 |
*/
|
|
|
376 |
public function test_process_with_user_rate_limiter(): void {
|
|
|
377 |
// Create users.
|
|
|
378 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
379 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
380 |
// Log in user1.
|
|
|
381 |
$this->setUser($user1);
|
|
|
382 |
// Mock clock.
|
|
|
383 |
$clock = $this->mock_clock_with_frozen();
|
|
|
384 |
|
|
|
385 |
// Set the user rate limiter.
|
|
|
386 |
$config = [
|
|
|
387 |
'apikey' => '123',
|
|
|
388 |
'endpoint' => 'https://api.example.com',
|
|
|
389 |
'enableuserratelimit' => true,
|
|
|
390 |
'userratelimit' => 1,
|
|
|
391 |
];
|
|
|
392 |
$actionconfig = [
|
|
|
393 |
'core_ai\\aiactions\\generate_image' => [
|
|
|
394 |
'enabled' => true,
|
|
|
395 |
'settings' => [
|
|
|
396 |
'deployment' => 'test',
|
|
|
397 |
'apiversion' => '2024-06-01',
|
|
|
398 |
],
|
|
|
399 |
],
|
|
|
400 |
];
|
|
|
401 |
$provider = $this->manager->create_provider_instance(
|
|
|
402 |
classname: provider::class,
|
|
|
403 |
name: 'dummy',
|
|
|
404 |
config: $config,
|
|
|
405 |
);
|
|
|
406 |
$provider = $this->manager->update_provider_instance(
|
|
|
407 |
provider: $provider,
|
|
|
408 |
actionconfig: $actionconfig,
|
|
|
409 |
);
|
|
|
410 |
|
|
|
411 |
// Mock the http client to return a successful response.
|
|
|
412 |
['mock' => $mock] = $this->get_mocked_http_client();
|
|
|
413 |
$url = 'https://example.com/test.jpg';
|
|
|
414 |
|
|
|
415 |
// Case 1: User rate limit has not been reached.
|
|
|
416 |
$this->create_action($user1->id);
|
|
|
417 |
// The response from Azure AI.
|
|
|
418 |
$mock->append(new Response(
|
|
|
419 |
200,
|
|
|
420 |
['Content-Type' => 'application/json'],
|
|
|
421 |
json_encode([
|
|
|
422 |
'created' => 1719140500,
|
|
|
423 |
'data' => [
|
|
|
424 |
(object) [
|
|
|
425 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
426 |
'url' => $url,
|
|
|
427 |
],
|
|
|
428 |
],
|
|
|
429 |
]),
|
|
|
430 |
));
|
|
|
431 |
// The image downloaded from the server successfully.
|
|
|
432 |
$mock->append(new Response(
|
|
|
433 |
200,
|
|
|
434 |
['Content-Type' => 'image/jpeg'],
|
|
|
435 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
436 |
));
|
|
|
437 |
$processor = new process_generate_image($provider, $this->action);
|
|
|
438 |
$result = $processor->process();
|
|
|
439 |
$this->assertTrue($result->get_success());
|
|
|
440 |
|
|
|
441 |
// Case 2: User rate limit has been reached.
|
|
|
442 |
$clock->bump(HOURSECS - 10);
|
|
|
443 |
// The response from Azure AI.
|
|
|
444 |
$mock->append(new Response(
|
|
|
445 |
200,
|
|
|
446 |
['Content-Type' => 'application/json'],
|
|
|
447 |
json_encode([
|
|
|
448 |
'created' => 1719140500,
|
|
|
449 |
'data' => [
|
|
|
450 |
(object) [
|
|
|
451 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
452 |
'url' => $url,
|
|
|
453 |
],
|
|
|
454 |
],
|
|
|
455 |
]),
|
|
|
456 |
));
|
|
|
457 |
// The image downloaded from the server successfully.
|
|
|
458 |
$mock->append(new Response(
|
|
|
459 |
200,
|
|
|
460 |
['Content-Type' => 'image/jpeg'],
|
|
|
461 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
462 |
));
|
|
|
463 |
$this->create_action($user1->id);
|
|
|
464 |
$processor = new process_generate_image($provider, $this->action);
|
|
|
465 |
$result = $processor->process();
|
|
|
466 |
$this->assertEquals(429, $result->get_errorcode());
|
|
|
467 |
$this->assertEquals('User rate limit exceeded', $result->get_errormessage());
|
|
|
468 |
$this->assertFalse($result->get_success());
|
|
|
469 |
|
|
|
470 |
// Case 3: User rate limit has not been reached for a different user.
|
|
|
471 |
// Log in user2.
|
|
|
472 |
$this->setUser($user2);
|
|
|
473 |
$this->create_action($user2->id);
|
|
|
474 |
// The response from Azure AI.
|
|
|
475 |
$mock->append(new Response(
|
|
|
476 |
200,
|
|
|
477 |
['Content-Type' => 'application/json'],
|
|
|
478 |
json_encode([
|
|
|
479 |
'created' => 1719140500,
|
|
|
480 |
'data' => [
|
|
|
481 |
(object) [
|
|
|
482 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
483 |
'url' => $url,
|
|
|
484 |
],
|
|
|
485 |
],
|
|
|
486 |
]),
|
|
|
487 |
));
|
|
|
488 |
// The image downloaded from the server successfully.
|
|
|
489 |
$mock->append(new Response(
|
|
|
490 |
200,
|
|
|
491 |
['Content-Type' => 'image/jpeg'],
|
|
|
492 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
493 |
));
|
|
|
494 |
$processor = new process_generate_image($provider, $this->action);
|
|
|
495 |
$result = $processor->process();
|
|
|
496 |
$this->assertTrue($result->get_success());
|
|
|
497 |
|
|
|
498 |
// Case 4: Time window has passed, user rate limit should be reset.
|
|
|
499 |
$clock->bump(11);
|
|
|
500 |
// Log in user1.
|
|
|
501 |
$this->setUser($user1);
|
|
|
502 |
// The response from Azure AI.
|
|
|
503 |
$mock->append(new Response(
|
|
|
504 |
200,
|
|
|
505 |
['Content-Type' => 'application/json'],
|
|
|
506 |
json_encode([
|
|
|
507 |
'created' => 1719140500,
|
|
|
508 |
'data' => [
|
|
|
509 |
(object) [
|
|
|
510 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
511 |
'url' => $url,
|
|
|
512 |
],
|
|
|
513 |
],
|
|
|
514 |
]),
|
|
|
515 |
));
|
|
|
516 |
// The image downloaded from the server successfully.
|
|
|
517 |
$mock->append(new Response(
|
|
|
518 |
200,
|
|
|
519 |
['Content-Type' => 'image/jpeg'],
|
|
|
520 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
521 |
));
|
|
|
522 |
$this->create_action($user1->id);
|
|
|
523 |
$processor = new process_generate_image($provider, $this->action);
|
|
|
524 |
$result = $processor->process();
|
|
|
525 |
$this->assertTrue($result->get_success());
|
|
|
526 |
}
|
|
|
527 |
|
|
|
528 |
/**
|
|
|
529 |
* Test process method with global rate limiter.
|
|
|
530 |
*/
|
|
|
531 |
public function test_process_with_global_rate_limiter(): void {
|
|
|
532 |
// Create users.
|
|
|
533 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
534 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
535 |
// Log in user1.
|
|
|
536 |
$this->setUser($user1);
|
|
|
537 |
// Mock clock.
|
|
|
538 |
$clock = $this->mock_clock_with_frozen();
|
|
|
539 |
|
|
|
540 |
// Set the global rate limiter.
|
|
|
541 |
$config = [
|
|
|
542 |
'apikey' => '123',
|
|
|
543 |
'endpoint' => 'https://api.example.com',
|
|
|
544 |
'enableglobalratelimit' => true,
|
|
|
545 |
'globalratelimit' => 1,
|
|
|
546 |
];
|
|
|
547 |
$actionconfig = [
|
|
|
548 |
'core_ai\\aiactions\\generate_image' => [
|
|
|
549 |
'enabled' => true,
|
|
|
550 |
'settings' => [
|
|
|
551 |
'deployment' => 'test',
|
|
|
552 |
'apiversion' => '2024-06-01',
|
|
|
553 |
],
|
|
|
554 |
],
|
|
|
555 |
];
|
|
|
556 |
$provider = $this->manager->create_provider_instance(
|
|
|
557 |
classname: provider::class,
|
|
|
558 |
name: 'dummy',
|
|
|
559 |
config: $config,
|
|
|
560 |
);
|
|
|
561 |
$provider = $this->manager->update_provider_instance(
|
|
|
562 |
provider: $provider,
|
|
|
563 |
actionconfig: $actionconfig,
|
|
|
564 |
);
|
|
|
565 |
|
|
|
566 |
// Mock the http client to return a successful response.
|
|
|
567 |
['mock' => $mock] = $this->get_mocked_http_client();
|
|
|
568 |
$url = 'https://example.com/test.jpg';
|
|
|
569 |
|
|
|
570 |
// Case 1: Global rate limit has not been reached.
|
|
|
571 |
$this->create_action($user1->id);
|
|
|
572 |
// The response from Azure AI.
|
|
|
573 |
$mock->append(new Response(
|
|
|
574 |
200,
|
|
|
575 |
['Content-Type' => 'application/json'],
|
|
|
576 |
json_encode([
|
|
|
577 |
'created' => 1719140500,
|
|
|
578 |
'data' => [
|
|
|
579 |
(object) [
|
|
|
580 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
581 |
'url' => $url,
|
|
|
582 |
],
|
|
|
583 |
],
|
|
|
584 |
]),
|
|
|
585 |
));
|
|
|
586 |
// The image downloaded from the server successfully.
|
|
|
587 |
$mock->append(new Response(
|
|
|
588 |
200,
|
|
|
589 |
['Content-Type' => 'image/jpeg'],
|
|
|
590 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
591 |
));
|
|
|
592 |
$processor = new process_generate_image($provider, $this->action);
|
|
|
593 |
$result = $processor->process();
|
|
|
594 |
$this->assertTrue($result->get_success());
|
|
|
595 |
|
|
|
596 |
// Case 2: Global rate limit has been reached.
|
|
|
597 |
$clock->bump(HOURSECS - 10);
|
|
|
598 |
// The response from Azure AI.
|
|
|
599 |
$mock->append(new Response(
|
|
|
600 |
200,
|
|
|
601 |
['Content-Type' => 'application/json'],
|
|
|
602 |
json_encode([
|
|
|
603 |
'created' => 1719140500,
|
|
|
604 |
'data' => [
|
|
|
605 |
(object) [
|
|
|
606 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
607 |
'url' => $url,
|
|
|
608 |
],
|
|
|
609 |
],
|
|
|
610 |
]),
|
|
|
611 |
));
|
|
|
612 |
// The image downloaded from the server successfully.
|
|
|
613 |
$mock->append(new Response(
|
|
|
614 |
200,
|
|
|
615 |
['Content-Type' => 'image/jpeg'],
|
|
|
616 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
617 |
));
|
|
|
618 |
$this->create_action($user1->id);
|
|
|
619 |
$processor = new process_generate_image($provider, $this->action);
|
|
|
620 |
$result = $processor->process();
|
|
|
621 |
$this->assertEquals(429, $result->get_errorcode());
|
|
|
622 |
$this->assertEquals('Global rate limit exceeded', $result->get_errormessage());
|
|
|
623 |
$this->assertFalse($result->get_success());
|
|
|
624 |
|
|
|
625 |
// Case 3: Global rate limit has been reached for a different user too.
|
|
|
626 |
// Log in user2.
|
|
|
627 |
$this->setUser($user2);
|
|
|
628 |
$this->create_action($user2->id);
|
|
|
629 |
// The response from Azure AI.
|
|
|
630 |
$mock->append(new Response(
|
|
|
631 |
200,
|
|
|
632 |
['Content-Type' => 'application/json'],
|
|
|
633 |
json_encode([
|
|
|
634 |
'created' => 1719140500,
|
|
|
635 |
'data' => [
|
|
|
636 |
(object) [
|
|
|
637 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
638 |
'url' => $url,
|
|
|
639 |
],
|
|
|
640 |
],
|
|
|
641 |
]),
|
|
|
642 |
));
|
|
|
643 |
// The image downloaded from the server successfully.
|
|
|
644 |
$mock->append(new Response(
|
|
|
645 |
200,
|
|
|
646 |
['Content-Type' => 'image/jpeg'],
|
|
|
647 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
648 |
));
|
|
|
649 |
$processor = new process_generate_image($provider, $this->action);
|
|
|
650 |
$result = $processor->process();
|
|
|
651 |
$this->assertFalse($result->get_success());
|
|
|
652 |
|
|
|
653 |
// Case 4: Time window has passed, global rate limit should be reset.
|
|
|
654 |
$clock->bump(11);
|
|
|
655 |
// Log in user1.
|
|
|
656 |
$this->setUser($user1);
|
|
|
657 |
// The response from Azure AI.
|
|
|
658 |
$mock->append(new Response(
|
|
|
659 |
200,
|
|
|
660 |
['Content-Type' => 'application/json'],
|
|
|
661 |
json_encode([
|
|
|
662 |
'created' => 1719140500,
|
|
|
663 |
'data' => [
|
|
|
664 |
(object) [
|
|
|
665 |
'revised_prompt' => 'An image that represents the concept of a \'test\'.',
|
|
|
666 |
'url' => $url,
|
|
|
667 |
],
|
|
|
668 |
],
|
|
|
669 |
]),
|
|
|
670 |
));
|
|
|
671 |
// The image downloaded from the server successfully.
|
|
|
672 |
$mock->append(new Response(
|
|
|
673 |
200,
|
|
|
674 |
['Content-Type' => 'image/jpeg'],
|
|
|
675 |
\GuzzleHttp\Psr7\Utils::streamFor(fopen(self::get_fixture_path('aiprovider_azureai', 'test.jpg'), 'r')),
|
|
|
676 |
));
|
|
|
677 |
$this->create_action($user1->id);
|
|
|
678 |
$processor = new process_generate_image($provider, $this->action);
|
|
|
679 |
$result = $processor->process();
|
|
|
680 |
$this->assertTrue($result->get_success());
|
|
|
681 |
}
|
|
|
682 |
}
|