Add a second parameter to BaseApi::getRequest to enable API tests

This commit is contained in:
Hypolite Petovan 2021-11-27 18:30:41 -05:00
parent d83ebe6031
commit 9cc675233d
40 changed files with 45 additions and 41 deletions

View file

@ -47,7 +47,7 @@ class Activity extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'id' => 0, // Id of the post 'id' => 0, // Id of the post
]); ], $request);
$res = Item::performActivity($request['id'], $this->parameters['verb'], $uid); $res = Item::performActivity($request['id'], $this->parameters['verb'], $uid);

View file

@ -37,7 +37,7 @@ class Setseen extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'id' => 0, // Id of the direct message 'id' => 0, // Id of the direct message
]); ], $request);
// return error if id is zero // return error if id is zero
if (empty($request['id'])) { if (empty($request['id'])) {

View file

@ -41,7 +41,7 @@ class Index extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'since_id' => 0, 'since_id' => 0,
'count' => 0, 'count' => 0,
]); ], $request);
$condition = ["`id` > ? AND `uid` = ?", $request['since_id'], $uid]; $condition = ["`id` > ? AND `uid` = ?", $request['since_id'], $uid];
$params = ['limit' => $request['count']]; $params = ['limit' => $request['count']];

View file

@ -40,7 +40,7 @@ class Delete extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'gid' => 0, 'gid' => 0,
'name' => '' 'name' => ''
]); ], $request);
// params // params

View file

@ -40,7 +40,7 @@ class Delete extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'photo_id' => null, // Photo id 'photo_id' => null, // Photo id
]); ], $request);
// do several checks on input parameters // do several checks on input parameters
// we do not allow calls without photo id // we do not allow calls without photo id

View file

@ -41,7 +41,7 @@ class Delete extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'album' => '', // Album name 'album' => '', // Album name
]); ], $request);
// we do not allow calls without album string // we do not allow calls without album string
if (empty($request['album'])) { if (empty($request['album'])) {

View file

@ -40,7 +40,7 @@ class Update extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'album' => '', // Current album name 'album' => '', // Current album name
'album_new' => '', // New album name 'album_new' => '', // New album name
]); ], $request);
// we do not allow calls without album string // we do not allow calls without album string
if (empty($request['album'])) { if (empty($request['album'])) {

View file

@ -53,7 +53,7 @@ class Followers extends BaseApi
'since_id' => 0, // Return results newer than this id 'since_id' => 0, // Return results newer than this id
'min_id' => 0, // Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
'limit' => 40, // Maximum number of results to return. Defaults to 40. 'limit' => 40, // Maximum number of results to return. Defaults to 40.
]); ], $request);
$params = ['order' => ['relation-cid' => true], 'limit' => $request['limit']]; $params = ['order' => ['relation-cid' => true], 'limit' => $request['limit']];

View file

@ -53,7 +53,7 @@ class Following extends BaseApi
'since_id' => 0, // Return results newer than this id 'since_id' => 0, // Return results newer than this id
'min_id' => 0, // Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
'limit' => 40, // Maximum number of results to return. Defaults to 40. 'limit' => 40, // Maximum number of results to return. Defaults to 40.
]); ], $request);
$params = ['order' => ['cid' => true], 'limit' => $request['limit']]; $params = ['order' => ['cid' => true], 'limit' => $request['limit']];

View file

@ -43,7 +43,7 @@ class Note extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'comment' => '', 'comment' => '',
]); ], $request);
$cdata = Contact::getPublicAndUserContactID($this->parameters['id'], $uid); $cdata = Contact::getPublicAndUserContactID($this->parameters['id'], $uid);
if (empty($cdata['user'])) { if (empty($cdata['user'])) {

View file

@ -41,7 +41,7 @@ class Relationships extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'id' => [], 'id' => [],
]); ], $request);
if (empty($request['id'])) { if (empty($request['id'])) {
DI::mstdnError()->UnprocessableEntity(); DI::mstdnError()->UnprocessableEntity();

View file

@ -47,7 +47,7 @@ class Search extends BaseApi
'limit' => 40, // Maximum number of results. Defaults to 40. 'limit' => 40, // Maximum number of results. Defaults to 40.
'resolve' => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address. 'resolve' => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address.
'following' => false, // Only who the user is following. Defaults to false. 'following' => false, // Only who the user is following. Defaults to false.
]); ], $request);
$accounts = []; $accounts = [];

View file

@ -63,7 +63,7 @@ class Statuses extends BaseApi
'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
'exclude_reblogs' => false, // Undocumented parameter 'exclude_reblogs' => false, // Undocumented parameter
'tagged' => false, // Undocumented parameter 'tagged' => false, // Undocumented parameter
]); ], $request);
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];

View file

@ -42,7 +42,7 @@ class Apps extends BaseApi
'redirect_uris' => '', 'redirect_uris' => '',
'scopes' => 'read', 'scopes' => 'read',
'website' => '', 'website' => '',
]); ], $request);
// Workaround for AndStatus, see issue https://github.com/andstatus/andstatus/issues/538 // Workaround for AndStatus, see issue https://github.com/andstatus/andstatus/issues/538
$postdata = Network::postdata(); $postdata = Network::postdata();

View file

@ -53,7 +53,7 @@ class Blocks extends BaseApi
'since_id' => 0, // Return results newer than this id 'since_id' => 0, // Return results newer than this id
'min_id' => 0, // Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
'limit' => 40, // Maximum number of results. Defaults to 40. 'limit' => 40, // Maximum number of results. Defaults to 40.
]); ], $request);
$params = ['order' => ['cid' => true], 'limit' => $request['limit']]; $params = ['order' => ['cid' => true], 'limit' => $request['limit']];

View file

@ -47,7 +47,7 @@ class Bookmarks extends BaseApi
'since_id' => 0, // Return results newer than id 'since_id' => 0, // Return results newer than id
'min_id' => 0, // Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
]); ], $request);
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];

View file

@ -59,7 +59,7 @@ class Conversations extends BaseApi
'max_id' => 0, // Return results older than this ID. Use HTTP Link header to paginate. 'max_id' => 0, // Return results older than this ID. Use HTTP Link header to paginate.
'since_id' => 0, // Return results newer than this ID. Use HTTP Link header to paginate. 'since_id' => 0, // Return results newer than this ID. Use HTTP Link header to paginate.
'min_id' => 0, // Return results immediately newer than this ID. Use HTTP Link header to paginate. 'min_id' => 0, // Return results immediately newer than this ID. Use HTTP Link header to paginate.
]); ], $request);
$params = ['order' => ['id' => true], 'limit' => $request['limit']]; $params = ['order' => ['id' => true], 'limit' => $request['limit']];

View file

@ -46,7 +46,7 @@ class Directory extends BaseApi
'limit' => 40, // How many accounts to load. Default 40. 'limit' => 40, // How many accounts to load. Default 40.
'order' => 'active', // active to sort by most recently posted statuses (default) or new to sort by most recently created profiles. 'order' => 'active', // active to sort by most recently posted statuses (default) or new to sort by most recently created profiles.
'local' => false, // Only return local accounts. 'local' => false, // Only return local accounts.
]); ], $request);
Logger::info('directory', ['offset' => $request['offset'], 'limit' => $request['limit'], 'order' => $request['order'], 'local' => $request['local']]); Logger::info('directory', ['offset' => $request['offset'], 'limit' => $request['limit'], 'order' => $request['order'], 'local' => $request['local']]);

View file

@ -47,7 +47,7 @@ class Favourited extends BaseApi
'min_id' => 0, // Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
'max_id' => 0, // Return results older than id 'max_id' => 0, // Return results older than id
'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
]); ], $request);
$params = ['order' => ['thr-parent-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['thr-parent-id' => true], 'limit' => $request['limit']];

View file

@ -91,7 +91,7 @@ class FollowRequests extends BaseApi
'min_id' => 0, 'min_id' => 0,
'max_id' => 0, 'max_id' => 0,
'limit' => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header. 'limit' => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header.
]); ], $request);
$introductions = DI::intro()->selectForUser($uid, $request['min_id'], $request['max_id'], $request['limit']); $introductions = DI::intro()->selectForUser($uid, $request['min_id'], $request['max_id'], $request['limit']);

View file

@ -58,7 +58,7 @@ class Lists extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'title' => '', 'title' => '',
]); ], $request);
if (empty($request['title'])) { if (empty($request['title'])) {
DI::mstdnError()->UnprocessableEntity(); DI::mstdnError()->UnprocessableEntity();

View file

@ -66,7 +66,7 @@ class Accounts extends BaseApi
'since_id' => 0, // Return results newer than this id 'since_id' => 0, // Return results newer than this id
'min_id' => 0, // Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
'limit' => 40, // Maximum number of results. Defaults to 40. Max 40. Set to 0 in order to get all accounts without pagination. 'limit' => 40, // Maximum number of results. Defaults to 40. Max 40. Set to 0 in order to get all accounts without pagination.
]); ], $request);
$params = ['order' => ['contact-id' => true]]; $params = ['order' => ['contact-id' => true]];

View file

@ -53,7 +53,7 @@ class Mutes extends BaseApi
'since_id' => 0, // Return results newer than this id 'since_id' => 0, // Return results newer than this id
'min_id' => 0, // Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
'limit' => 40, // Maximum number of results. Defaults to 40. 'limit' => 40, // Maximum number of results. Defaults to 40.
]); ], $request);
$params = ['order' => ['cid' => true], 'limit' => $request['limit']]; $params = ['order' => ['cid' => true], 'limit' => $request['limit']];

View file

@ -64,7 +64,7 @@ class Notifications extends BaseApi
'account_id' => 0, // Return only notifications received from this account 'account_id' => 0, // Return only notifications received from this account
'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
'count' => 0, // Unknown parameter 'count' => 0, // Unknown parameter
]); ], $request);
$params = ['order' => ['id' => true]]; $params = ['order' => ['id' => true]];

View file

@ -42,7 +42,7 @@ class PushSubscription extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'subscription' => [], 'subscription' => [],
'data' => [], 'data' => [],
]); ], $request);
$subscription = [ $subscription = [
'application-id' => $application['id'], 'application-id' => $application['id'],

View file

@ -76,7 +76,7 @@ class ScheduledStatuses extends BaseApi
'max_id' => 0, // Return results older than ID 'max_id' => 0, // Return results older than ID
'since_id' => 0, // Return results newer than ID 'since_id' => 0, // Return results newer than ID
'min_id' => 0, // Return results immediately newer than ID 'min_id' => 0, // Return results immediately newer than ID
]); ], $request);
$params = ['order' => ['id' => true], 'limit' => $request['limit']]; $params = ['order' => ['id' => true], 'limit' => $request['limit']];

View file

@ -56,7 +56,7 @@ class Search extends BaseApi
'limit' => 20, // Maximum number of results to load, per type. Defaults to 20. Max 40. 'limit' => 20, // Maximum number of results to load, per type. Defaults to 20. Max 40.
'offset' => 0, // Offset in search results. Used for pagination. Defaults to 0. 'offset' => 0, // Offset in search results. Used for pagination. Defaults to 0.
'following' => false, // Only include accounts that the user is following. Defaults to false. 'following' => false, // Only include accounts that the user is following. Defaults to false.
]); ], $request);
if (empty($request['q'])) { if (empty($request['q'])) {
DI::mstdnError()->UnprocessableEntity(); DI::mstdnError()->UnprocessableEntity();

View file

@ -56,7 +56,7 @@ class Statuses extends BaseApi
'visibility' => '', // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct". 'visibility' => '', // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct".
'scheduled_at' => '', // ISO 8601 Datetime at which to schedule a status. Providing this paramter will cause ScheduledStatus to be returned instead of Status. Must be at least 5 minutes in the future. 'scheduled_at' => '', // ISO 8601 Datetime at which to schedule a status. Providing this paramter will cause ScheduledStatus to be returned instead of Status. Must be at least 5 minutes in the future.
'language' => '', // ISO 639 language code for this status. 'language' => '', // ISO 639 language code for this status.
]); ], $request);
$owner = User::getOwnerDataById($uid); $owner = User::getOwnerDataById($uid);

View file

@ -45,7 +45,7 @@ class Context extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'limit' => 40, // Maximum number of results to return. Defaults to 40. 'limit' => 40, // Maximum number of results to return. Defaults to 40.
]); ], $request);
$id = $this->parameters['id']; $id = $this->parameters['id'];

View file

@ -41,7 +41,7 @@ class Suggestions extends BaseApi
$request = self::getRequest([ $request = self::getRequest([
'limit' => 40, // Maximum number of results to return. Defaults to 40. 'limit' => 40, // Maximum number of results to return. Defaults to 40.
]); ], $request);
$suggestions = Contact\Relation::getSuggestions($uid, 0, $request['limit']); $suggestions = Contact\Relation::getSuggestions($uid, 0, $request['limit']);

View file

@ -45,7 +45,7 @@ class Direct extends BaseApi
'since_id' => 0, // Return results newer than id 'since_id' => 0, // Return results newer than id
'min_id' => 0, // Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
'limit' => 20, // Maximum number of results to return. Defaults to 20. 'limit' => 20, // Maximum number of results to return. Defaults to 20.
]); ], $request);
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];

View file

@ -51,7 +51,7 @@ class Home extends BaseApi
'only_media' => false, // Show only statuses with media attached? Defaults to false. 'only_media' => false, // Show only statuses with media attached? Defaults to false.
'remote' => false, // Show only remote statuses? Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false.
'exclude_replies' => false, // Don't show comments 'exclude_replies' => false, // Don't show comments
]); ], $request);
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];

View file

@ -55,7 +55,7 @@ class ListTimeline extends BaseApi
'local' => false, // Show only local statuses? Defaults to false. 'local' => false, // Show only local statuses? Defaults to false.
'remote' => false, // Show only remote statuses? Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false.
'exclude_replies' => false, // Don't show comments 'exclude_replies' => false, // Don't show comments
]); ], $request);
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];

View file

@ -53,7 +53,7 @@ class PublicTimeline extends BaseApi
'limit' => 20, // Maximum number of results to return. Defaults to 20. 'limit' => 20, // Maximum number of results to return. Defaults to 20.
'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
'exclude_replies' => false, // Don't show comments 'exclude_replies' => false, // Don't show comments
]); ], $request);
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];

View file

@ -63,7 +63,7 @@ class Tag extends BaseApi
'limit' => 20, // Maximum number of results to return. Defaults to 20. 'limit' => 20, // Maximum number of results to return. Defaults to 20.
'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users. 'with_muted' => false, // Pleroma extension: return activities by muted (not by blocked!) users.
'exclude_replies' => false, // Don't show comments 'exclude_replies' => false, // Don't show comments
]); ], $request);
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];

View file

@ -38,7 +38,7 @@ class Trends extends BaseApi
{ {
$request = self::getRequest([ $request = self::getRequest([
'limit' => 20, // Maximum number of results to return. Defaults to 10. 'limit' => 20, // Maximum number of results to return. Defaults to 10.
]); ], $request);
$trending = []; $trending = [];
$tags = Tag::getGlobalTrendingHashtags(24, 20); $tags = Tag::getGlobalTrendingHashtags(24, 20);

View file

@ -108,12 +108,16 @@ class BaseApi extends BaseModule
/** /**
* Processes data from GET requests and sets defaults * Processes data from GET requests and sets defaults
* *
* @param array $defaults Associative array of expected request keys and their default typed value. A null
* value will remove the request key from the resulting value array.
* @param array|null $request Custom REQUEST array, superglobal instead
* @return array request data * @return array request data
* @throws \Exception
*/ */
public static function getRequest(array $defaults) public static function getRequest(array $defaults, array $request = null): array
{ {
$httpinput = HTTPInputData::process(); $httpinput = HTTPInputData::process();
$input = array_merge($httpinput['variables'], $httpinput['files'], $_REQUEST); $input = array_merge($httpinput['variables'], $httpinput['files'], $request ?? $_REQUEST);
self::$request = $input; self::$request = $input;
self::$boundaries = []; self::$boundaries = [];

View file

@ -47,7 +47,7 @@ class Authorize extends BaseApi
'redirect_uri' => '', // Set a URI to redirect the user to. If this parameter is set to "urn:ietf:wg:oauth:2.0:oob" then the authorization code will be shown instead. Must match one of the redirect URIs declared during app registration. 'redirect_uri' => '', // Set a URI to redirect the user to. If this parameter is set to "urn:ietf:wg:oauth:2.0:oob" then the authorization code will be shown instead. Must match one of the redirect URIs declared during app registration.
'scope' => 'read', // List of requested OAuth scopes, separated by spaces (or by pluses, if using query parameters). Must be a subset of scopes declared during app registration. If not provided, defaults to "read". 'scope' => 'read', // List of requested OAuth scopes, separated by spaces (or by pluses, if using query parameters). Must be a subset of scopes declared during app registration. If not provided, defaults to "read".
'state' => '', 'state' => '',
]); ], $request);
if ($request['response_type'] != 'code') { if ($request['response_type'] != 'code') {
Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]); Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]);

View file

@ -38,7 +38,7 @@ class Revoke extends BaseApi
'client_id' => '', // Client ID, obtained during app registration 'client_id' => '', // Client ID, obtained during app registration
'client_secret' => '', // Client secret, obtained during app registration 'client_secret' => '', // Client secret, obtained during app registration
'token' => '', // The previously obtained token, to be invalidated 'token' => '', // The previously obtained token, to be invalidated
]); ], $request);
$condition = ['client_id' => $request['client_id'], 'client_secret' => $request['client_secret'], 'access_token' => $request['token']]; $condition = ['client_id' => $request['client_id'], 'client_secret' => $request['client_secret'], 'access_token' => $request['token']];
$token = DBA::selectFirst('application-view', ['id'], $condition); $token = DBA::selectFirst('application-view', ['id'], $condition);

View file

@ -43,7 +43,7 @@ class Token extends BaseApi
'scope' => 'read', // List of requested OAuth scopes, separated by spaces. Must be a subset of scopes declared during app registration. If not provided, defaults to "read". 'scope' => 'read', // List of requested OAuth scopes, separated by spaces. Must be a subset of scopes declared during app registration. If not provided, defaults to "read".
'code' => '', // A user authorization code, obtained via /oauth/authorize 'code' => '', // A user authorization code, obtained via /oauth/authorize
'grant_type' => '', // Set equal to "authorization_code" if code is provided in order to gain user-level access. Otherwise, set equal to "client_credentials" to obtain app-level access only. 'grant_type' => '', // Set equal to "authorization_code" if code is provided in order to gain user-level access. Otherwise, set equal to "client_credentials" to obtain app-level access only.
]); ], $request);
// AndStatus transmits the client data in the AUTHORIZATION header field, see https://github.com/andstatus/andstatus/issues/530 // AndStatus transmits the client data in the AUTHORIZATION header field, see https://github.com/andstatus/andstatus/issues/530
$authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? ''; $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';