From 83079ef8dd163adbd3f0e8a8f8692e2abc5133b4 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Wed, 27 Jun 2018 13:05:38 -0700 Subject: [PATCH 1/4] Adding documentation and tests for Logs endpoint; minor code quality tweaks --- composer.json | 3 +- phpcs-ruleset.xml | 15 +++-- src/API/Management/Logs.php | 48 ++++++++----- tests/API/Management/LogsTest.php | 108 ++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 20 deletions(-) create mode 100644 tests/API/Management/LogsTest.php diff --git a/composer.json b/composer.json index 104494ef..e9125efe 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ "test": "SHELL_INTERACTIVE=1 vendor/bin/phpunit --colors=always --verbose ", "test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml", "phpcs": "SHELL_INTERACTIVE=1 ./vendor/bin/phpcs --standard=phpcs-ruleset.xml -s", - "phpcbf": "SHELL_INTERACTIVE=1 ./vendor/bin/phpcbf --standard=phpcs-ruleset.xml .", + "phpcbf": "./vendor/bin/phpcbf --standard=phpcs-ruleset.xml .", + "phpcbf-path": "SHELL_INTERACTIVE=1 ./vendor/bin/phpcbf --standard=phpcs-ruleset.xml", "sniffs": "./vendor/bin/phpcs --standard=phpcs-ruleset.xml -e" } } diff --git a/phpcs-ruleset.xml b/phpcs-ruleset.xml index 9becbb9a..63f17085 100644 --- a/phpcs-ruleset.xml +++ b/phpcs-ruleset.xml @@ -26,6 +26,12 @@ + + + + + + @@ -51,6 +57,7 @@ + @@ -61,7 +68,7 @@ - + @@ -78,9 +85,9 @@ - - - + + + \ No newline at end of file diff --git a/src/API/Management/Logs.php b/src/API/Management/Logs.php index a0b106d3..d34cdadd 100644 --- a/src/API/Management/Logs.php +++ b/src/API/Management/Logs.php @@ -2,34 +2,52 @@ namespace Auth0\SDK\API\Management; +/** + * Class Logs. + * Access to the v2 Management API Logs endpoint. + * + * @package Auth0\SDK\API\Management + */ class Logs extends GenericResource { /** + * Get a single Log event. + * Required scope: "read:logs" + * + * @param string $log_id Log entry ID to get. * - * @param string $id * @return mixed + * + * @throws \Exception Thrown by Guzzle for API errors. + * + * @link https://auth0.com/docs/api/management/v2#!/Logs/get_logs_by_id */ - public function get($id) + public function get($log_id) { - return $this->apiClient->get() - ->logs($id) - ->call(); + return $this->apiClient->method('get') + ->addPath('logs', $log_id) + ->call(); } /** + * Retrieves log entries that match the specified search criteria (or list all entries if no criteria is used). + * Required scope: "read:logs" + * + * @param array $params Log search parameters to send: + * - Including a restricted "fields" parameter can speed up API calls significantly. + * - Results are paged by default; pass a "page" and "per_page" param to adjust what results are shown. * - * @param array $params * @return mixed + * + * @throws \Exception Thrown by Guzzle for API errors. + * + * @link https://auth0.com/docs/api/management/v2#!/Logs/get_logs */ - public function search($params = []) + public function search(array $params = []) { - $client = $this->apiClient->get() - ->logs(); - - foreach ($params as $param => $value) { - $client->withParam($param, $value); - } - - return $client->call(); + return $this->apiClient->method('get') + ->addPath('logs') + ->withDictParams($params) + ->call(); } } diff --git a/tests/API/Management/LogsTest.php b/tests/API/Management/LogsTest.php new file mode 100644 index 00000000..a35547b8 --- /dev/null +++ b/tests/API/Management/LogsTest.php @@ -0,0 +1,108 @@ + [ 'actions' => ['read'] ] ]); + $api = new Management($token, $env['DOMAIN']); + + self::$api = $api->logs; + } + + /** + * Test a general search. + * + * @return void + */ + public function testLogSearch() + { + $search_results = self::$api->search(); + $this->assertNotEmpty($search_results); + + // Get the 10th log entry to test pagination in self::testLogSearchPagination(). + self::$log_id = $search_results[9]['log_id']; + $this->assertNotEmpty(self::$log_id); + } + + /** + * Test fields parameter. + * + * @return void + */ + public function testLogSearchFields() + { + $search_results = self::$api->search([ + 'fields' => '_id,log_id,date', + 'include_fields' => true, + ]); + $this->assertNotEmpty($search_results); + $this->assertNotEmpty($search_results[0]['date']); + $this->assertNotEmpty($search_results[0]['log_id']); + $this->assertCount(3, $search_results[0]); + } + + /** + * Test pagination parameters. + * + * @return void + */ + public function testLogSearchPagination() + { + $expected_count = 10; + $search_results = self::$api->search([ + // Fields here to speed up API call. + 'fields' => '_id,log_id,date', + 'include_fields' => true, + + // First page of 10 results. + 'page' => 0, + 'per_page' => $expected_count, + ]); + $this->assertNotEmpty($search_results); + $this->assertCount($expected_count, $search_results); + } + + /** + * Test getting a single log entry with an ID. + * + * @return void + */ + public function testGetOne() + { + $one_log = self::$api->get(self::$log_id); + $this->assertNotEmpty($one_log); + $this->assertEquals(self::$log_id, $one_log['log_id']); + } +} From a4720855f22a3d0f2fe45059a39136b4d2e4eb0d Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Thu, 28 Jun 2018 10:48:19 -0700 Subject: [PATCH 2/4] PR feedback --- tests/API/Management/LogsTest.php | 66 +++++++++++-------------------- 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/tests/API/Management/LogsTest.php b/tests/API/Management/LogsTest.php index a35547b8..7175885c 100644 --- a/tests/API/Management/LogsTest.php +++ b/tests/API/Management/LogsTest.php @@ -20,13 +20,6 @@ class LogsTest extends ApiTests */ protected static $api; - /** - * Valid Log ID to test getter. - * - * @var string - */ - protected static $log_id; - /** * Sets up API client for entire testing class. * @@ -46,31 +39,22 @@ public static function setUpBeforeClass() * * @return void */ - public function testLogSearch() - { - $search_results = self::$api->search(); - $this->assertNotEmpty($search_results); - - // Get the 10th log entry to test pagination in self::testLogSearchPagination(). - self::$log_id = $search_results[9]['log_id']; - $this->assertNotEmpty(self::$log_id); - } - - /** - * Test fields parameter. - * - * @return void - */ - public function testLogSearchFields() + public function testLogSearchAndGetById() { $search_results = self::$api->search([ 'fields' => '_id,log_id,date', 'include_fields' => true, ]); $this->assertNotEmpty($search_results); - $this->assertNotEmpty($search_results[0]['date']); + $this->assertNotEmpty($search_results[0]['_id']); $this->assertNotEmpty($search_results[0]['log_id']); + $this->assertNotEmpty($search_results[0]['date']); $this->assertCount(3, $search_results[0]); + + // Test getting a single log result with a valid ID from above. + $one_log = self::$api->get($search_results[0]['log_id']); + $this->assertNotEmpty($one_log); + $this->assertEquals($search_results[0]['log_id'], $one_log['log_id']); } /** @@ -80,29 +64,25 @@ public function testLogSearchFields() */ public function testLogSearchPagination() { - $expected_count = 10; - $search_results = self::$api->search([ + $search_params = [ // Fields here to speed up API call. - 'fields' => '_id,log_id,date', + 'fields' => '_id,log_id', 'include_fields' => true, - // First page of 10 results. + // First page of 2 results. 'page' => 0, - 'per_page' => $expected_count, - ]); - $this->assertNotEmpty($search_results); - $this->assertCount($expected_count, $search_results); - } + 'per_page' => 2, + ]; - /** - * Test getting a single log entry with an ID. - * - * @return void - */ - public function testGetOne() - { - $one_log = self::$api->get(self::$log_id); - $this->assertNotEmpty($one_log); - $this->assertEquals(self::$log_id, $one_log['log_id']); + // Get one page of 2 results and check the count. + $search_results_1 = self::$api->search($search_params); + $this->assertCount(2, $search_results_1); + + // Now get one page of a single result and make sure it matches the second result above. + $search_params['page'] = 1; + $search_params['per_page'] = 1; + $search_results_2 = self::$api->search($search_params); + $this->assertCount(1, $search_results_2); + $this->assertEquals($search_results_1[1]['log_id'], $search_results_2[0]['log_id']); } } From 696e17874b0566c7c9f3e72a38ff3608153fff22 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Mon, 2 Jul 2018 09:02:14 -0700 Subject: [PATCH 3/4] extend timeoput for logs test; default to verified email for users to skip verify email sent --- tests/API/Management/LogsTest.php | 2 +- tests/API/Management/UsersTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/API/Management/LogsTest.php b/tests/API/Management/LogsTest.php index 7175885c..61640881 100644 --- a/tests/API/Management/LogsTest.php +++ b/tests/API/Management/LogsTest.php @@ -29,7 +29,7 @@ public static function setUpBeforeClass() { $env = self::getEnvStatic(); $token = self::getTokenStatic($env, [ 'logs' => [ 'actions' => ['read'] ] ]); - $api = new Management($token, $env['DOMAIN']); + $api = new Management($token, $env['DOMAIN'], ['timeout' => 30]); self::$api = $api->logs; } diff --git a/tests/API/Management/UsersTest.php b/tests/API/Management/UsersTest.php index 3089cafb..e204e57c 100644 --- a/tests/API/Management/UsersTest.php +++ b/tests/API/Management/UsersTest.php @@ -57,7 +57,7 @@ protected function getCreateBody() 'email' => 'test-create-user-'.$this->rand.'@auth0.com', 'password' => 'Y6t82hQjpXCMd3oD7Zsc', 'picture' => 'https://cdn.auth0.com/styleguide/components/1.0.8/media/logos/img/badge.png', - 'email_verified' => false, + 'email_verified' => true, 'user_metadata' => [ 'key1' => 'value1', 'key2' => 'value2', @@ -116,7 +116,7 @@ protected function afterCreate($entity) $expected = $this->getCreateBody(); $this->assertNotEmpty($this->getId($entity)); $this->assertEquals($expected['email'], $entity['email']); - $this->assertFalse($entity['email_verified']); + $this->assertTrue($entity['email_verified']); $this->assertEquals($expected['user_metadata']['key1'], $entity['user_metadata']['key1']); $this->assertEquals($expected['user_metadata']['key2'], $entity['user_metadata']['key2']); } @@ -130,7 +130,7 @@ protected function getUpdateBody() { return [ 'email' => 'test-update-user-'.$this->rand.'@auth0.com', - 'email_verified' => true, + 'email_verified' => false, 'user_metadata' => [ 'key1' => 'value4', 'key3' => 'value3', From 4e19efcdba46dddd20bb7243b072e783681d5444 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Mon, 2 Jul 2018 09:34:11 -0700 Subject: [PATCH 4/4] fix Logs test pagination --- tests/API/Management/LogsTest.php | 27 +++++++++++++-------------- tests/API/Management/UsersTest.php | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/API/Management/LogsTest.php b/tests/API/Management/LogsTest.php index 61640881..d5aa5a72 100644 --- a/tests/API/Management/LogsTest.php +++ b/tests/API/Management/LogsTest.php @@ -64,25 +64,24 @@ public function testLogSearchAndGetById() */ public function testLogSearchPagination() { - $search_params = [ + $expected_count = 5; + $search_results= self::$api->search([ // Fields here to speed up API call. 'fields' => '_id,log_id', 'include_fields' => true, - // First page of 2 results. - 'page' => 0, - 'per_page' => 2, - ]; + // Second page of 5 results. + 'page' => 1, + 'per_page' => $expected_count, - // Get one page of 2 results and check the count. - $search_results_1 = self::$api->search($search_params); - $this->assertCount(2, $search_results_1); + // Include totals to check pagination. + 'include_totals' => true, + ]); + + $this->assertCount($expected_count, $search_results['logs']); + $this->assertEquals($expected_count, $search_results['length']); - // Now get one page of a single result and make sure it matches the second result above. - $search_params['page'] = 1; - $search_params['per_page'] = 1; - $search_results_2 = self::$api->search($search_params); - $this->assertCount(1, $search_results_2); - $this->assertEquals($search_results_1[1]['log_id'], $search_results_2[0]['log_id']); + // Starting on 2nd page so starting result should be equal to the number per page. + $this->assertEquals($expected_count, $search_results['start']); } } diff --git a/tests/API/Management/UsersTest.php b/tests/API/Management/UsersTest.php index e204e57c..cee5c2e3 100644 --- a/tests/API/Management/UsersTest.php +++ b/tests/API/Management/UsersTest.php @@ -147,7 +147,7 @@ protected function afterUpdate($entity) { $expected = $this->getUpdateBody(); $this->assertEquals($expected['email'], $entity['email']); - $this->assertTrue($entity['email_verified']); + $this->assertFalse($entity['email_verified']); $this->assertEquals($expected['user_metadata']['key1'], $entity['user_metadata']['key1']); $this->assertEquals($expected['user_metadata']['key3'], $entity['user_metadata']['key3']); }