-
Notifications
You must be signed in to change notification settings - Fork 3
Add tests for WikiUserEmailChecker #1036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AndrewKostka
wants to merge
6
commits into
main
Choose a base branch
from
add-WikiUserEmailCheckerTest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+221
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4da5646
Added a coomand for checking if user email exist in api and MW db
rosalieper c167bf9
fix linting
rosalieper 5bdd21b
Added tests
rosalieper 585e740
Add more test
rosalieper 5fafbce
Refactor checking user email in a service to make testing easier
rosalieper 8b767fe
Add tests for WikiUserEmailChecker
AndrewKostka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| namespace App\Console\Commands\User; | ||
|
|
||
| use App\Services\WikiUserEmailChecker; | ||
| use App\User; | ||
| use Illuminate\Console\Command; | ||
|
|
||
| class CheckUserEmailExist extends Command { | ||
| protected $signature = 'wbs-user:check-email {emails*}'; | ||
|
|
||
| protected $description = 'Check if emails exist in apidb.users or any MediaWiki user table'; | ||
|
|
||
| public function handle(WikiUserEmailChecker $emailChecker): int { | ||
| $emails = $this->argument('emails'); | ||
| foreach ($emails as $email) { | ||
| $found = false; | ||
|
|
||
| if (User::whereEmail($email)->exists()) { | ||
| $this->line("FOUND: {$email} in apidb.users"); | ||
| $found = true; | ||
| } | ||
|
|
||
| $mwResults = $emailChecker->findEmail($email); | ||
|
|
||
| foreach ($mwResults as $location) { | ||
| $this->line("FOUND: {$email} in {$location}"); | ||
| $found = true; | ||
| } | ||
|
|
||
| if (!$found) { | ||
| $this->line("NOT FOUND: {$email}"); | ||
| } | ||
|
|
||
| $this->line('-------------------------------------------------'); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| namespace App\Services; | ||
|
|
||
| use Illuminate\Database\DatabaseManager; | ||
| use PDO; | ||
|
|
||
| class WikiUserEmailChecker { | ||
| public function __construct(private DatabaseManager $db) {} | ||
|
|
||
| public function findEmail(string $email): array { | ||
| $this->db->purge('mw'); | ||
| $pdo = $this->db->connection('mw')->getPdo(); | ||
|
|
||
| $mwDatabases = $pdo | ||
| ->query("SHOW DATABASES LIKE 'mwdb_%'") | ||
| ->fetchAll(PDO::FETCH_COLUMN); | ||
|
|
||
| $foundIn = []; | ||
|
|
||
| foreach ($mwDatabases as $dbName) { | ||
| $userTable = $this->findUserTable($pdo, $dbName); | ||
|
|
||
| if (!$userTable) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($this->emailExists($pdo, $dbName, $userTable, $email)) { | ||
| $foundIn[] = "{$dbName}.{$userTable}"; | ||
| } | ||
| } | ||
|
|
||
| return $foundIn; | ||
| } | ||
|
|
||
| private function findUserTable(PDO $pdo, string $dbName): ?string { | ||
| $stmt = $pdo->prepare(" | ||
| SELECT TABLE_NAME | ||
| FROM INFORMATION_SCHEMA.TABLES | ||
| WHERE TABLE_SCHEMA = :db | ||
| AND TABLE_NAME LIKE '%\_user' | ||
| LIMIT 1 | ||
| "); | ||
|
|
||
| $stmt->execute(['db' => $dbName]); | ||
|
|
||
| return $stmt->fetchColumn() ?: null; | ||
| } | ||
|
|
||
| private function emailExists(PDO $pdo, string $dbName, string $table, string $email): bool { | ||
| $stmt = $pdo->prepare(" | ||
| SELECT 1 | ||
| FROM {$dbName}.{$table} | ||
| WHERE user_email = :email | ||
| LIMIT 1 | ||
| "); | ||
|
|
||
| $stmt->execute(['email' => $email]); | ||
|
|
||
| return (bool) $stmt->fetch(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Commands; | ||
|
|
||
| use App\Services\WikiUserEmailChecker; | ||
| use App\User; | ||
| use Mockery; | ||
| use Tests\TestCase; | ||
|
|
||
| class CheckUserEmailExistTest extends TestCase { | ||
| public function testItFindsEmailInApiUsersTable() { | ||
| User::factory()->create([ | ||
| 'email' => 'user@example.com', | ||
| ]); | ||
|
|
||
| // Act & Assert | ||
| $this->artisan('wbs-user:check-email', ['emails' => ['user@example.com']]) | ||
| ->expectsOutput('FOUND: user@example.com in apidb.users') | ||
| ->assertExitCode(0); | ||
| } | ||
|
|
||
| public function testItReturnsNotFoundIfEmailDoesNotExist() { | ||
| $this->artisan('wbs-user:check-email', ['emails' => ['nonexistent@example.com']]) | ||
| ->expectsOutput('NOT FOUND: nonexistent@example.com') | ||
| ->assertExitCode(0); | ||
| } | ||
|
|
||
| public function testItChecksMultipleEmails() { | ||
| User::factory()->create(['email' => 'user1@example.com']); | ||
|
|
||
| $emails = ['user1@example.com', 'other@example.com']; | ||
|
|
||
| $this->artisan('wbs-user:check-email', ['emails' => $emails]) | ||
| ->expectsOutput('FOUND: user1@example.com in apidb.users') | ||
| ->expectsOutput('NOT FOUND: other@example.com') | ||
| ->assertExitCode(0); | ||
| } | ||
|
|
||
| public function testEmailFoundInWikiDb() { | ||
| $checker = Mockery::mock(WikiUserEmailChecker::class); | ||
|
|
||
| $checker->shouldReceive('findEmail') | ||
| ->with('test@example.com') | ||
| ->andReturn(['mwdb_test.mwdb_test_user']); | ||
|
|
||
| $this->app->instance(WikiUserEmailChecker::class, $checker); | ||
|
|
||
| $this->artisan('wbs-user:check-email', [ | ||
| 'emails' => ['test@example.com'], | ||
| ]) | ||
| ->expectsOutput('FOUND: test@example.com in mwdb_test.mwdb_test_user') | ||
| ->assertExitCode(0); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| namespace Tests; | ||
|
|
||
| use App\Services\WikiUserEmailChecker; | ||
| use App\WikiDb; | ||
| use Illuminate\Database\DatabaseManager; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
|
||
| class WikiUserEmailCheckerTest extends TestCase { | ||
| use RefreshDatabase; | ||
|
|
||
| private DatabaseManager $db; | ||
|
|
||
| private $databases = [ | ||
| ['prefix' => 'db_1', 'name' => 'mwdb_1', 'emails' => ['user1@email.localhost', 'user2@email.localhost']], | ||
| ['prefix' => 'db_2', 'name' => 'mwdb_2', 'emails' => ['user1@email.localhost']], | ||
| ['prefix' => 'db_3', 'name' => 'mwdb_3', 'emails' => []], | ||
| ]; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
| $this->db = $this->app->make('db'); | ||
| $this->deleteDatabases(); | ||
| $pdo = $this->db->connection('mw')->getPdo(); | ||
| foreach ($this->databases as $database) { | ||
| $pdo->exec("CREATE DATABASE {$database['name']}"); | ||
| $userTable = "{$database['name']}.{$database['prefix']}_user"; | ||
| $pdo->exec("CREATE TABLE {$userTable} (user_email TINYBLOB)"); | ||
| if ($database['emails']) { | ||
| $users = implode(',', array_map(fn ($email) => "('$email')", $database['emails'])); | ||
| $pdo->exec("INSERT INTO {$userTable} VALUES {$users}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected function tearDown(): void { | ||
| $this->deleteDatabases(); | ||
| WikiDb::query()->delete(); | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| private function deleteDatabases(): void { | ||
| $pdo = $this->db->connection('mw')->getPdo(); | ||
| foreach ($this->databases as $database) { | ||
| $pdo->exec("DROP DATABASE IF EXISTS {$database['name']};"); | ||
| } | ||
| } | ||
|
|
||
| public function testCorrectDatabaseFound(): void { | ||
| $checker = new WikiUserEmailChecker($this->db); | ||
| $this->assertEquals( | ||
| ['mwdb_1.db_1_user'], | ||
| $checker->findEmail('user2@email.localhost') | ||
| ); | ||
| } | ||
|
|
||
| public function testEmailFoundInMultipleDatabases(): void { | ||
| $checker = new WikiUserEmailChecker($this->db); | ||
| $this->assertEquals( | ||
| ['mwdb_1.db_1_user', 'mwdb_2.db_2_user'], | ||
| $checker->findEmail('user1@email.localhost') | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
RefreshDatabaseactually used? the test creates and delete databases using PDO.