Skip to content

Commit

Permalink
Merge pull request #19 from LibreSign/feature/make-possible-change-ap…
Browse files Browse the repository at this point in the history
…p-config

Make possible change app config
  • Loading branch information
vitormattos authored Apr 22, 2023
2 parents 02f0c9a + fe60713 commit d4f5030
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 73 deletions.
21 changes: 7 additions & 14 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@

require __DIR__ . '/../../vendor/autoload.php';

class FeatureContext extends NextcloudApiContext
{
class FeatureContext extends NextcloudApiContext {
protected MockWebServer $mockServer;
public function __construct(?array $parameters = [])
{
public function __construct(?array $parameters = []) {
parent::__construct($parameters);
$this->mockServer = new MockWebServer();
$this->mockServer->start();
Expand All @@ -23,17 +21,15 @@ public function __construct(?array $parameters = [])
/**
* @inheritDoc
*/
public function setCurrentUser(?string $user): void
{
public function setCurrentUser(?string $user): void {
parent::setCurrentUser($user);
Assert::assertEquals($this->currentUser, $user);
}

/**
* @inheritDoc
*/
public function assureUserExists(string $user): void
{
public function assureUserExists(string $user): void {
parent::assureUserExists($user);
$lastRequest = $this->getLastREquest();
$headers = $lastRequest->getHeaders();
Expand All @@ -45,8 +41,7 @@ public function assureUserExists(string $user): void
Assert::assertEquals('application/json', $headers['Accept']);
}

private function getLastRequest(): RequestInfo
{
private function getLastRequest(): RequestInfo {
$lastRequest = $this->mockServer->getLastRequest();
if (!$lastRequest instanceof RequestInfo) {
throw new Exception('Invalid response');
Expand All @@ -57,8 +52,7 @@ private function getLastRequest(): RequestInfo
/**
* @inheritDoc
*/
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void
{
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
parent::sendRequest($verb, $url, $body, $headers, $options);
$lastRequest = $this->getLastRequest();

Expand All @@ -84,8 +78,7 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
/**
* @inheritDoc
*/
public function theResponseShouldContainTheInitialStateWithTheFollowingValues(string $name, TableNode $table): void
{
public function theResponseShouldContainTheInitialStateWithTheFollowingValues(string $name, TableNode $table): void {
switch ($name) {
case 'appid-string':
$value = base64_encode($table->getRow(0)[0]);
Expand Down
82 changes: 38 additions & 44 deletions src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
/**
* Defines application features from the specific context.
*/
class NextcloudApiContext implements Context
{
class NextcloudApiContext implements Context {
protected string $testPassword = '123456';
protected string $adminPassword = 'admin';
protected string $baseUrl;
Expand All @@ -32,8 +31,7 @@ class NextcloudApiContext implements Context
protected $cookieJars;
protected array $requestOptions = [];

public function __construct(?array $parameters = [])
{
public function __construct(?array $parameters = []) {
$this->server = RunServerListener::getInstance();
$this->baseUrl = RunServerListener::getServerRoot();
$this->response = new Response();
Expand All @@ -49,26 +47,23 @@ public function __construct(?array $parameters = [])
/**
* @BeforeScenario
*/
public function setUp(): void
{
public function setUp(): void {
$this->createdUsers = [];
}

/**
* @Given as user :user
* @param string $user
*/
public function setCurrentUser(?string $user): void
{
public function setCurrentUser(?string $user): void {
$this->currentUser = $user;
}

/**
* @Given user :user exists
* @param string $user
*/
public function assureUserExists(string $user): void
{
public function assureUserExists(string $user): void {
$response = $this->userExists($user);
if ($response->getStatusCode() !== 200) {
$this->createUser($user);
Expand All @@ -80,17 +75,15 @@ public function assureUserExists(string $user): void
}
}

protected function userExists(string $user): ResponseInterface
{
protected function userExists(string $user): ResponseInterface {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
$this->sendOCSRequest('GET', '/cloud/users/' . $user);
$this->setCurrentUser($currentUser);
return $this->response;
}

protected function createUser(string $user): void
{
protected function createUser(string $user): void {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
$this->sendOCSRequest('POST', '/cloud/users', [
Expand All @@ -109,8 +102,7 @@ protected function createUser(string $user): void
$this->setCurrentUser($currentUser);
}

protected function setUserDisplayName(string $user): void
{
protected function setUserDisplayName(string $user): void {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
$this->sendOCSRequest('PUT', '/cloud/users/' . $user, [
Expand All @@ -121,8 +113,7 @@ protected function setUserDisplayName(string $user): void
}

/** @Given /^set the email of user "([^"]*)" to "([^"]*)"$/ */
public function setUserEmail(string $user, string $email): void
{
public function setUserEmail(string $user, string $email): void {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
$this->sendOCSRequest('PUT', '/cloud/users/' . $user, [
Expand All @@ -137,8 +128,7 @@ public function setUserEmail(string $user, string $email): void
* @param string $url
* @param TableNode|array|null $body
*/
public function sendOCSRequest(string $verb, string $url, $body = null, array $headers = []): void
{
public function sendOCSRequest(string $verb, string $url, $body = null, array $headers = []): void {
$url = '/ocs/v2.php' . $url;
$headers['OCS-ApiRequest'] = 'true';
$this->sendRequest($verb, $url, $body, $headers);
Expand All @@ -151,8 +141,7 @@ public function sendOCSRequest(string $verb, string $url, $body = null, array $h
* @param TableNode|array|null $body
* @param array $headers
*/
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void
{
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
if (!str_starts_with($url, '/')) {
$url = '/' . $url;
}
Expand Down Expand Up @@ -197,13 +186,11 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
}
}

protected function beforeRequest(string $fullUrl, array $options): array
{
protected function beforeRequest(string $fullUrl, array $options): array {
return [$fullUrl, $options];
}

protected function decodeIfIsJsonString(array $list): array
{
protected function decodeIfIsJsonString(array $list): array {
foreach ($list as $key => $value) {
if ($this->isJson($value)) {
$list[$key] = json_decode($value);
Expand All @@ -212,14 +199,12 @@ protected function decodeIfIsJsonString(array $list): array
return $list;
}

private function isJson(string $string): bool
{
private function isJson(string $string): bool {
json_decode($string);
return json_last_error() === JSON_ERROR_NONE;
}

protected function getUserCookieJar(string $user): CookieJar
{
protected function getUserCookieJar(string $user): CookieJar {
if (!isset($this->cookieJars[$user])) {
$this->cookieJars[$user] = new CookieJar();
}
Expand All @@ -231,8 +216,7 @@ protected function getUserCookieJar(string $user): CookieJar
* @param int $statusCode
* @param string $message
*/
protected function assertStatusCode(ResponseInterface $response, int $statusCode, string $message = ''): void
{
protected function assertStatusCode(ResponseInterface $response, int $statusCode, string $message = ''): void {
Assert::assertEquals($statusCode, $response->getStatusCode(), $message);
}

Expand All @@ -241,8 +225,7 @@ protected function assertStatusCode(ResponseInterface $response, int $statusCode
* @param string $code
* @throws \InvalidArgumentException
*/
public function theResponseShouldHaveStatusCode($code): void
{
public function theResponseShouldHaveStatusCode($code): void {
$currentCode = $this->response->getStatusCode();
Assert::assertEquals($code, $currentCode);
}
Expand All @@ -252,8 +235,7 @@ public function theResponseShouldHaveStatusCode($code): void
* @param TableNode $table
* @throws \InvalidArgumentException
*/
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void
{
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
$this->response->getBody()->seek(0);
$expectedValues = $table->getColumnsHash();
$realResponseArray = json_decode($this->response->getBody()->getContents(), true);
Expand All @@ -266,8 +248,7 @@ public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(Tab
/**
* @Given the response should contain the initial state :name with the following values:
*/
public function theResponseShouldContainTheInitialStateWithTheFollowingValues(string $name, TableNode $table): void
{
public function theResponseShouldContainTheInitialStateWithTheFollowingValues(string $name, TableNode $table): void {
$html = $this->response->getBody()->getContents();
$dom = new DOMDocument();
if (empty($html) || !$dom->loadHTML($html)) {
Expand Down Expand Up @@ -303,23 +284,36 @@ public function theResponseShouldContainTheInitialStateWithTheFollowingValues(st
}
}

protected function parseText(string $text): string
{
/**
* @Given the following :appId app config is set
*
* @param TableNode $formData
*/
public function setAppConfig(string $appId, TableNode $formData): void {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
foreach ($formData->getRows() as $row) {
$this->sendOCSRequest('POST', '/apps/provisioning_api/api/v1/config/apps/' . $appId . '/' . $row[0], [
'value' => $row[1],
]);
}
$this->setCurrentUser($currentUser);
}

protected function parseText(string $text): string {
return $text;
}

/**
* @AfterScenario
*/
public function tearDown(): void
{
public function tearDown(): void {
foreach ($this->createdUsers as $user) {
$this->deleteUser($user);
}
}

protected function deleteUser(string $user): ResponseInterface
{
protected function deleteUser(string $user): ResponseInterface {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
$this->sendOCSRequest('DELETE', '/cloud/users/' . $user);
Expand Down
2 changes: 1 addition & 1 deletion vendor-bin/coding-standard/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require-dev": {
"nextcloud/coding-standard": "^1.0"
"nextcloud/coding-standard": "^1.1"
}
}
16 changes: 8 additions & 8 deletions vendor-bin/coding-standard/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions vendor-bin/psalm/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4f5030

Please sign in to comment.