|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Acceptance; |
| 4 | + |
| 5 | +use Codeception\Attribute\Group; |
| 6 | +use Tests\Support\AcceptanceTester; |
| 7 | +use Tests\Support\Page\Acceptance\Install; |
| 8 | +use Tests\Support\Page\Acceptance\Login; |
| 9 | + |
| 10 | +class ApiCest |
| 11 | +{ |
| 12 | + private string $apiKey; |
| 13 | + |
| 14 | + private Login $loginPage; |
| 15 | + |
| 16 | + private Install $installPage; |
| 17 | + |
| 18 | + public function _before(AcceptanceTester $I, Login $loginPage, Install $installPage) |
| 19 | + { |
| 20 | + $this->loginPage = $loginPage; |
| 21 | + $this->installPage = $installPage; |
| 22 | + |
| 23 | + // Ensure database is installed before running API tests |
| 24 | + $this->installPage->install( |
| 25 | + |
| 26 | + 'test', |
| 27 | + 'John', |
| 28 | + 'Smith', |
| 29 | + 'Smith & Co' |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + #[Group('api')] |
| 34 | + public function createAPIKey(AcceptanceTester $I) |
| 35 | + { |
| 36 | + |
| 37 | + $this-> loginPage-> login( '[email protected]', 'test'); |
| 38 | + |
| 39 | + // Generate API key if not exists |
| 40 | + $I->amOnPage('setting/editCompanySettings#/api/newApiKey'); |
| 41 | + $I->waitForElementVisible('#firstname', 120); |
| 42 | + |
| 43 | + $I->fillField(['id' => 'firstname'], 'APIUser'); |
| 44 | + $I->selectOption(['id' => 'role'], 'Administrator'); |
| 45 | + $I->checkOption('Leantime Onboarding'); |
| 46 | + $I->clickWithRetry('#save'); |
| 47 | + |
| 48 | + $I->waitForElement('#apiKey'); |
| 49 | + |
| 50 | + $this->apiKey = $I->grabValueFrom('#apiKey'); |
| 51 | + |
| 52 | + codecept_debug($this->apiKey); |
| 53 | + } |
| 54 | + |
| 55 | + #[Group('api')] |
| 56 | + public function testJsonRpcEndpoint(AcceptanceTester $I) |
| 57 | + { |
| 58 | + |
| 59 | + $I->haveHttpHeader('Content-Type', 'application/json'); |
| 60 | + $I->haveHttpHeader('x-api-key', $this->apiKey); |
| 61 | + |
| 62 | + $I->sendPost('/api/jsonrpc', [ |
| 63 | + 'jsonrpc' => '2.0', |
| 64 | + 'method' => 'leantime.rpc.Comments.pollComments', |
| 65 | + 'params' => ['projectId' => 1], |
| 66 | + 'id' => 1, |
| 67 | + ]); |
| 68 | + |
| 69 | + $I->seeResponseCodeIs(200); |
| 70 | + $I->seeResponseIsJson(); |
| 71 | + $I->seeResponseMatchesJsonType([ |
| 72 | + 'jsonrpc' => 'string', |
| 73 | + 'result' => 'array', |
| 74 | + 'id' => 'string', |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + #[Group('api')] |
| 79 | + public function testInvalidJsonRpcRequest(AcceptanceTester $I) |
| 80 | + { |
| 81 | + $I->haveHttpHeader('Content-Type', 'application/json'); |
| 82 | + $I->haveHttpHeader('x-api-key', $this->apiKey); |
| 83 | + |
| 84 | + $I->sendPost('/api/jsonrpc', [ |
| 85 | + 'jsonrpc' => '2.0', |
| 86 | + 'method' => 'invalid.method', |
| 87 | + 'params' => ['projectId' => 1], |
| 88 | + 'id' => 1, |
| 89 | + ]); |
| 90 | + |
| 91 | + $I->seeResponseCodeIs(200); |
| 92 | + $I->seeResponseIsJson(); |
| 93 | + $I->seeResponseMatchesJsonType([ |
| 94 | + 'jsonrpc' => 'string', |
| 95 | + 'error' => [ |
| 96 | + 'code' => 'integer', |
| 97 | + 'message' => 'string', |
| 98 | + 'data' => 'string' |
| 99 | + ], |
| 100 | + 'id' => 'integer', |
| 101 | + ]); |
| 102 | + |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + #[Group('api')] |
| 107 | + public function testValidReturnId(AcceptanceTester $I) |
| 108 | + { |
| 109 | + $I->haveHttpHeader('Content-Type', 'application/json'); |
| 110 | + $I->haveHttpHeader('x-api-key', $this->apiKey); |
| 111 | + |
| 112 | + $I->sendPost('/api/jsonrpc', [ |
| 113 | + 'jsonrpc' => '2.0', |
| 114 | + 'method' => 'leantime.rpc.Comments.pollComments', |
| 115 | + 'params' => ['projectId' => 1], |
| 116 | + 'id' => 123, |
| 117 | + ]); |
| 118 | + |
| 119 | + $I->seeResponseCodeIs(200); |
| 120 | + $I->seeResponseIsJson([ |
| 121 | + 'jsonrpc' => '2.0', |
| 122 | + 'method' => 'leantime.rpc.Comments.pollComments', |
| 123 | + 'params' => ['projectId' => 1], |
| 124 | + 'id' => 'integer', |
| 125 | + ]); |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + public function testMissingApiKey(AcceptanceTester $I) |
| 130 | + { |
| 131 | + $I->haveHttpHeader('Content-Type', 'application/json'); |
| 132 | + |
| 133 | + $I->sendPost('/api/jsonrpc', [ |
| 134 | + 'jsonrpc' => '2.0', |
| 135 | + 'method' => 'leantime.rpc.Comments.pollComments', |
| 136 | + 'params' => ['projectId' => 1], |
| 137 | + 'id' => 1, |
| 138 | + ]); |
| 139 | + |
| 140 | + $I->seeResponseCodeIs(401); |
| 141 | + } |
| 142 | +} |
0 commit comments