Skip to content

Commit

Permalink
add basic integration tests for the api v2 table endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Steffens <florian.steffens@nextcloud.com>
  • Loading branch information
Florian Steffens committed Oct 26, 2023
1 parent 404ba7d commit 8dd2d00
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 2 deletions.
16 changes: 14 additions & 2 deletions tests/integration/features/APIv2.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@ Feature: APIv2

@api2
Scenario: Test initial setup
# Then user "participant1" has the following tables new
# | Tutorial |
Then user "participant1-v2" has the following tables via v2
| Tutorial |
Then user "participant1-v2" has the following resources via v2
| Tutorial |

@api2
Scenario: Basic table actions
Given table "Table 1 via api v2" with emoji "👋" exists for user "participant1-v2" as "t1" via v2
Then user "participant1-v2" has the following tables via v2
| Table 1 via api v2 |
Then user "participant1-v2" updates table "t1" set title "updated title" and emoji "⛵︎" via v2
Then user "participant1-v2" has the following tables via v2
| updated title |
Then user "participant1-v2" deletes table "t1" via v2
190 changes: 190 additions & 0 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,196 @@ public function cleanupUsers() {
}


/**
* @Given table :table with emoji :emoji exists for user :user as :tableName via v2
*
* @param string $user
* @param string $title
* @param string $tableName
* @param string|null $emoji
* @throws Exception
*/
public function createTableV2(string $user, string $title, string $tableName, string $emoji = null): void {
$this->setCurrentUser($user);
$this->sendOcsRequest('post', '/apps/tables/api/2/tables',
[
'title' => $title,
'emoji' => $emoji
]
);

$newTable = $this->getDataFromResponse($this->response)['ocs']['data'];
$this->tableIds[$tableName] = $newTable['id'];

Assert::assertEquals(200, $this->response->getStatusCode());
Assert::assertEquals($newTable['title'], $title);
Assert::assertEquals($newTable['emoji'], $emoji);
Assert::assertEquals($newTable['ownership'], $user);

$this->sendOcsRequest(
'GET',
'/apps/tables/api/2/tables/'.$newTable['id'],
);

$tableToVerify = $this->getDataFromResponse($this->response)['ocs']['data'];
Assert::assertEquals(200, $this->response->getStatusCode());
Assert::assertEquals($tableToVerify['title'], $title);
Assert::assertEquals($tableToVerify['emoji'], $emoji);
Assert::assertEquals($tableToVerify['ownership'], $user);
}

/**
* @Then user :user has the following tables via v2
*
* @param string $user
* @param TableNode|null $body
* @throws Exception
*/
public function userTablesV2(string $user, TableNode $body = null): void {
$this->setCurrentUser($user);
$this->sendOcsRequest(
'GET',
'/apps/tables/api/2/tables'
);

$data = $this->getDataFromResponse($this->response)['ocs']['data'];
Assert::assertEquals(200, $this->response->getStatusCode());

// check if tables are empty
if ($body === null) {
Assert::assertCount(0, $data);
return;
}

// check if given tables exists
$titles = [];
foreach ($data as $d) {
$titles[] = $d['title'];
}
foreach ($body->getRows()[0] as $tableTitle) {
Assert::assertTrue(in_array($tableTitle, $titles, true));
}
}

/**
* @Then user :user has the following resources via v2
*
* first row contains tables, second views
* | first table | second table |
* | first shared view | second shared view |
*
* @param string $user
* @param TableNode|null $body
* @throws Exception
*/
public function initialResourcesV2(string $user, TableNode $body = null): void {
$this->setCurrentUser($user);
$this->sendOcsRequest(
'GET',
'/apps/tables/api/2/init'
);

$data = $this->getDataFromResponse($this->response)['ocs']['data'];
Assert::assertEquals(200, $this->response->getStatusCode());

// check if table is empty
if ($body === null) {
Assert::assertCount(0, $data);
return;
}

// check if given tables exists
var_dump($data);
$tableTitles = [];
foreach ($data['tables'] as $d) {
$tableTitles[] = $d['title'];
}
$viewTitles = [];
foreach ($data['views'] as $d) {
$tableTitles[] = $d['title'];
}

if (@$body->getRows()[0]) {
foreach ($body->getRows()[0] as $tableTitle) {
Assert::assertTrue(in_array($tableTitle, $tableTitles, true));
}
}
if (@$body->getRows()[1]) {
foreach ($body->getRows()[1] as $viewTitle) {
Assert::assertTrue(in_array($viewTitle, $viewTitles, true));
}
}
}

/**
* @Then user :user updates table :tableName set title :title and emoji :emoji via v2
*
* @param string $user
* @param string $title
* @param string|null $emoji
* @param string $tableName
* @throws Exception
*/
public function updateTableV2(string $user, string $title, ?string $emoji, string $tableName): void {
$this->setCurrentUser($user);

$data = ['title' => $title];
if ($emoji !== null) {
$data['emoji'] = $emoji;
}

$this->sendOcsRequest(
'PUT',
'/apps/tables/api/2/tables/'.$this->tableIds[$tableName],
$data
);

$updatedTable = $this->getDataFromResponse($this->response)['ocs']['data'];

Assert::assertEquals(200, $this->response->getStatusCode());
Assert::assertEquals($updatedTable['title'], $title);
Assert::assertEquals($updatedTable['emoji'], $emoji);
Assert::assertEquals($updatedTable['ownership'], $user);

$this->sendOcsRequest(
'GET',
'/apps/tables/api/2/tables/'.$updatedTable['id'],
);

$tableToVerify = $this->getDataFromResponse($this->response)['ocs']['data'];
Assert::assertEquals(200, $this->response->getStatusCode());
Assert::assertEquals($tableToVerify['title'], $title);
Assert::assertEquals($tableToVerify['emoji'], $emoji);
Assert::assertEquals($tableToVerify['ownership'], $user);
}

/**
* @Then user :user deletes table :tableName via v2
*
* @param string $user
* @param string $tableName
* @throws Exception
*/
public function deleteTableV2(string $user, string $tableName): void {
$this->setCurrentUser($user);

$this->sendOcsRequest(
'DELETE',
'/apps/tables/api/2/tables/'.$this->tableIds[$tableName]
);

$deletedTable = $this->getDataFromResponse($this->response)['ocs']['data'];
Assert::assertEquals(200, $this->response->getStatusCode());
Assert::assertEquals($deletedTable['id'], $this->tableIds[$tableName]);

$this->sendOcsRequest(
'GET',
'/apps/tables/api/2/tables/'.$deletedTable['id'],
);
Assert::assertEquals(404, $this->response->getStatusCode());

unset($this->tableIds[$tableName]);
}

// IMPORT --------------------------

Expand Down

0 comments on commit 8dd2d00

Please sign in to comment.