From 84c4e8dd6e643ab21eb4e6da12c8a3d40b921804 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Fri, 13 Nov 2020 02:55:17 -0500 Subject: [PATCH] Ensure that search AND list endpoints are tested for users and discussions --- .../integration/api/discussions/ListTest.php | 120 ++----------- .../api/discussions/SearchTest.php | 166 ++++++++++++++++++ tests/integration/api/users/SearchTest.php | 83 +++++++++ 3 files changed, 263 insertions(+), 106 deletions(-) create mode 100644 tests/integration/api/discussions/SearchTest.php create mode 100644 tests/integration/api/users/SearchTest.php diff --git a/tests/integration/api/discussions/ListTest.php b/tests/integration/api/discussions/ListTest.php index 3e56c4150..56741861e 100644 --- a/tests/integration/api/discussions/ListTest.php +++ b/tests/integration/api/discussions/ListTest.php @@ -56,111 +56,19 @@ class ListTest extends TestCase $this->assertEquals(1, count($data['data'])); } - /** - * @test - */ - public function can_search_for_author() - { - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => 'author:normal foo'], - 'include' => 'mostRelevantPost', - ]) - ); + // /** + // * @test + // */ + // public function can_search_for_author() + // { + // $response = $this->send( + // $this->request('GET', '/api/search/discussions') + // ->withQueryParams([ + // 'filter' => ['q' => 'author:normal foo'], + // 'include' => 'mostRelevantPost', + // ]) + // ); - $this->assertEquals(200, $response->getStatusCode()); - } - - /** - * @test - */ - public function can_search_for_word_in_post() - { - $this->database()->table('discussions')->insert([ - ['id' => 2, 'title' => 'lightsail in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], - ['id' => 3, 'title' => 'not in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], - ]); - - $this->database()->table('posts')->insert([ - ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

not in text

'], - ['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

lightsail in text

'], - ]); - - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => 'lightsail'], - 'include' => 'mostRelevantPost', - ]) - ); - - $data = json_decode($response->getBody()->getContents(), true); - $ids = array_map(function ($row) { - return $row['id']; - }, $data['data']); - - // Order-independent comparison - $this->assertEquals(['3'], $ids, 'IDs do not match', 0.0, 10, true); - } - - /** - * @test - */ - public function ignores_non_word_characters_when_searching() - { - $this->database()->table('discussions')->insert([ - ['id' => 2, 'title' => 'lightsail in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], - ['id' => 3, 'title' => 'not in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], - ]); - - $this->database()->table('posts')->insert([ - ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

not in text

'], - ['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

lightsail in text

'], - ]); - - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => 'lightsail+'], - 'include' => 'mostRelevantPost', - ]) - ); - - $data = json_decode($response->getBody()->getContents(), true); - $ids = array_map(function ($row) { - return $row['id']; - }, $data['data']); - - // Order-independent comparison - $this->assertEquals(['3'], $ids, 'IDs do not match', 0.0, 10, true); - } - - /** - * @test - */ - public function search_for_special_characters_gives_empty_result() - { - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => '*'], - 'include' => 'mostRelevantPost', - ]) - ); - - $data = json_decode($response->getBody()->getContents(), true); - $this->assertEquals([], $data['data']); - - $response = $this->send( - $this->request('GET', '/api/discussions') - ->withQueryParams([ - 'filter' => ['q' => '@'], - 'include' => 'mostRelevantPost', - ]) - ); - - $data = json_decode($response->getBody()->getContents(), true); - $this->assertEquals([], $data['data']); - } + // $this->assertEquals(200, $response->getStatusCode()); + // } } diff --git a/tests/integration/api/discussions/SearchTest.php b/tests/integration/api/discussions/SearchTest.php new file mode 100644 index 000000000..5919c3c73 --- /dev/null +++ b/tests/integration/api/discussions/SearchTest.php @@ -0,0 +1,166 @@ +prepareDatabase([ + 'discussions' => [ + ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 1], + ], + 'posts' => [ + ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

foo bar

'], + ], + 'users' => [ + $this->normalUser(), + ], + 'groups' => [ + $this->memberGroup(), + $this->guestGroup(), + ], + 'group_permission' => [ + ['permission' => 'viewDiscussions', 'group_id' => 2], + ] + ]); + } + + /** + * @test + */ + public function shows_index_for_guest() + { + $response = $this->send( + $this->request('GET', '/api/search/discussions') + ); + + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($response->getBody()->getContents(), true); + + $this->assertEquals(1, count($data['data'])); + } + + /** + * @test + */ + public function can_search_for_author() + { + $response = $this->send( + $this->request('GET', '/api/search/discussions') + ->withQueryParams([ + 'filter' => ['q' => 'author:normal foo'], + 'include' => 'mostRelevantPost', + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @test + */ + public function can_search_for_word_in_post() + { + $this->database()->table('discussions')->insert([ + ['id' => 2, 'title' => 'lightsail in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], + ['id' => 3, 'title' => 'not in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], + ]); + + $this->database()->table('posts')->insert([ + ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

not in text

'], + ['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

lightsail in text

'], + ]); + + $response = $this->send( + $this->request('GET', '/api/search/discussions') + ->withQueryParams([ + 'filter' => ['q' => 'lightsail'], + 'include' => 'mostRelevantPost', + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true); + $ids = array_map(function ($row) { + return $row['id']; + }, $data['data']); + + // Order-independent comparison + $this->assertEquals(['3'], $ids, 'IDs do not match', 0.0, 10, true); + } + + /** + * @test + */ + public function ignores_non_word_characters_when_searching() + { + $this->database()->table('discussions')->insert([ + ['id' => 2, 'title' => 'lightsail in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], + ['id' => 3, 'title' => 'not in title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'comment_count' => 1], + ]); + + $this->database()->table('posts')->insert([ + ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

not in text

'], + ['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

lightsail in text

'], + ]); + + $response = $this->send( + $this->request('GET', '/api/search/discussions') + ->withQueryParams([ + 'filter' => ['q' => 'lightsail+'], + 'include' => 'mostRelevantPost', + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true); + $ids = array_map(function ($row) { + return $row['id']; + }, $data['data']); + + // Order-independent comparison + $this->assertEquals(['3'], $ids, 'IDs do not match', 0.0, 10, true); + } + + /** + * @test + */ + public function search_for_special_characters_gives_empty_result() + { + $response = $this->send( + $this->request('GET', '/api/search/discussions') + ->withQueryParams([ + 'filter' => ['q' => '*'], + 'include' => 'mostRelevantPost', + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true); + $this->assertEquals([], $data['data']); + + $response = $this->send( + $this->request('GET', '/api/search/discussions') + ->withQueryParams([ + 'filter' => ['q' => '@'], + 'include' => 'mostRelevantPost', + ]) + ); + + $data = json_decode($response->getBody()->getContents(), true); + $this->assertEquals([], $data['data']); + } +} diff --git a/tests/integration/api/users/SearchTest.php b/tests/integration/api/users/SearchTest.php new file mode 100644 index 000000000..9a33728e1 --- /dev/null +++ b/tests/integration/api/users/SearchTest.php @@ -0,0 +1,83 @@ +prepareDatabase([ + 'users' => [ + $this->adminUser(), + ], + 'groups' => [ + $this->adminGroup(), + $this->guestGroup(), + ], + 'group_permission' => [], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ], + ]); + } + + /** + * @test + */ + public function disallows_index_for_guest() + { + $response = $this->send( + $this->request('GET', '/api/search/users') + ); + + $this->assertEquals(403, $response->getStatusCode()); + } + + /** + * @test + */ + public function shows_index_for_guest_when_they_have_permission() + { + Permission::unguarded(function () { + Permission::create([ + 'permission' => 'viewUserList', + 'group_id' => 2, + ]); + }); + + $response = $this->send( + $this->request('GET', '/api/search/users') + ); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @test + */ + public function shows_index_for_admin() + { + $response = $this->send( + $this->request('GET', '/api/search/users', [ + 'authenticatedAs' => 1, + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + } +}