public function testEnvironmentEventFuncional()
{
$db = \Scalr::getDb();
$testName = str_replace('-', '', static::getTestName());
$events = null;
$uri = self::getUserApiUrl('/events');
static::createEntity(new EventDefinition(), ['name' => 'testEnvironment', 'description' => 'testEnvironment', 'envId' => $this->getEnvironment()->id]);
// test describe pagination
do {
$query = [];
if (isset($events->pagination->next)) {
$parts = parse_url($events->pagination->next);
parse_str($parts['query'], $query);
}
$describe = $this->request($uri, Request::METHOD_GET, $query);
$this->assertDescribeResponseNotEmpty($describe);
$this->assertNotEmpty($describe->getBody());
$events = $describe->getBody();
foreach ($events->data as $event) {
$this->assertEventObjectNotEmpty($event);
if ($event->id == $testName) {
$delete = $this->request($uri . '/' . $event->id, Request::METHOD_DELETE);
$this->assertEquals(200, $delete->status, $this->printResponseError($delete));
}
}
} while (!empty($events->pagination->next));
// test create action
$create = $this->postEvent(null, self::$testEnvId);
$create = $this->request($uri, Request::METHOD_POST);
$this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Invalid body');
$create = $this->postEvent(['id' => $testName, 'invalid' => 'value'], self::$testEnvId);
$this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'You are trying to set');
$create = $this->postEvent(['scope' => ScopeInterface::SCOPE_ENVIRONMENT], self::$testEnvId);
$this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Required field');
$create = $this->postEvent(['id' => 'invalid*^'], self::$testEnvId);
$this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_VALUE, 'Invalid id of the Event');
$create = $this->postEvent(['id' => $testName, 'description' => '<br>tags'], self::$testEnvId);
$this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_VALUE);
$create = $this->postEvent(['id' => $testName, 'description' => $testName, 'scope' => ScopeInterface::SCOPE_ENVIRONMENT], self::$testEnvId);
$createSame = $this->postEvent(['id' => $testName, 'description' => $testName, 'scope' => ScopeInterface::SCOPE_ENVIRONMENT], self::$testEnvId);
$this->assertErrorMessageContains($createSame, 409, ErrorMessage::ERR_UNICITY_VIOLATION);
$body = $create->getBody();
$this->assertEquals(201, $create->response->getStatus());
$this->assertFetchResponseNotEmpty($create);
$this->assertEventObjectNotEmpty($body->data);
$this->assertNotEmpty($body->data->id);
$this->assertEquals($testName, $body->data->id);
$this->assertEquals($testName, $body->data->description);
$this->assertEquals(ScopeInterface::SCOPE_ENVIRONMENT, $body->data->scope);
// test filtering
$describe = $this->request($uri, Request::METHOD_GET, ['description' => $testName]);
$this->assertErrorMessageContains($describe, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Unsupported filter');
$describe = $this->request($uri, Request::METHOD_GET, ['scope' => 'wrong<br>']);
$this->assertErrorMessageContains($describe, 400, ErrorMessage::ERR_INVALID_VALUE, 'Invalid scope value');
$describe = $this->request($uri, Request::METHOD_GET, ['scope' => ScopeInterface::SCOPE_ENVIRONMENT]);
$this->assertDescribeResponseNotEmpty($describe);
foreach ($describe->getBody()->data as $data) {
$this->assertEventObjectNotEmpty($data);
$this->assertEquals(ScopeInterface::SCOPE_ENVIRONMENT, $data->scope);
}
$describe = $this->request($uri, Request::METHOD_GET, ['id' => $testName]);
$this->assertDescribeResponseNotEmpty($describe);
foreach ($describe->getBody()->data as $data) {
$this->assertEventObjectNotEmpty($data);
$this->assertEquals($testName, $data->id);
}
// test fetch action
$eventId = $body->data->id;
$fetch = $this->request($uri . '/' . $eventId . 'invalid', Request::METHOD_GET);
$this->assertErrorMessageContains($fetch, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND, 'The Event either does not exist');
$fetch = $this->request($uri . '/' . $eventId, Request::METHOD_GET);
$fetchBody = $fetch->getBody();
$this->assertEquals(200, $fetch->response->getStatus());
$this->assertFetchResponseNotEmpty($fetch);
$this->assertEventObjectNotEmpty($fetchBody->data);
$this->assertEquals($testName, $fetchBody->data->id);
$this->assertEquals($testName, $fetchBody->data->description);
$this->assertEquals(ScopeInterface::SCOPE_ENVIRONMENT, $fetchBody->data->scope);
// test modify action
$modify = $this->request($uri . '/' . $eventId, Request::METHOD_PATCH);
$this->assertErrorMessageContains($modify, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Invalid body');
$accountEventId = $db->GetOne("SELECT e.name FROM event_definitions e WHERE e.env_id IS NULL AND e.account_id IS NOT NULL");
if (!empty($accountEventId)) {
$fetch = $this->request($uri . '/' . $accountEventId, Request::METHOD_PATCH, [], ['description' => '']);
$this->assertErrorMessageContains($fetch, 403, ErrorMessage::ERR_SCOPE_VIOLATION);
}
$modify = $this->request($uri . '/' . $eventId, Request::METHOD_PATCH, [], ['scope' => ScopeInterface::SCOPE_ACCOUNT]);
$this->assertErrorMessageContains($modify, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'You are trying to set the property');
$modify = $this->request($uri . '/' . $eventId, Request::METHOD_PATCH, [], ['description' => '']);
$modifyBody = $modify->getBody();
$this->assertEquals(200, $modify->response->getStatus());
$this->assertFetchResponseNotEmpty($modify);
$this->assertEventObjectNotEmpty($modifyBody->data);
$this->assertEquals($testName, $modifyBody->data->id);
$this->assertEquals('', $modifyBody->data->description);
$this->assertEquals(ScopeInterface::SCOPE_ENVIRONMENT, $modifyBody->data->scope);
// test delete action
if (!empty($accountEventId)) {
$delete = $this->request($uri . '/' . $accountEventId, Request::METHOD_DELETE);
$this->assertErrorMessageContains($delete, 403, ErrorMessage::ERR_SCOPE_VIOLATION);
}
$delete = $this->request($uri . '/' . $eventId, Request::METHOD_DELETE);
$this->assertEquals(200, $delete->status);
}