From ef2aa0654b60560b94c848c35b37cd8610f2a170 Mon Sep 17 00:00:00 2001 From: joverthegrey Date: Thu, 8 Sep 2016 12:11:02 +0200 Subject: [PATCH] Fixed unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new factories, causes the unit tests to fail. The tests are expecting a specific seeded data set, with the new method we don’t have that set anymore. - updated test to use data that is available instead of hardcooded values --- .gitignore | 2 ++ database/factories/ModelFactory.php | 9 +-------- database/seeds/DatabaseSeeder.php | 1 + tests/Api/ShowTestHelper.php | 2 ++ .../Commands/Account/ShowCommandTest.php | 7 +++++-- .../Commands/Contact/EditCommandTest.php | 20 +++++++++++-------- .../Commands/Contact/ShowCommandTest.php | 7 +++++-- .../Commands/Domain/ListCommandTest.php | 6 +++++- .../Commands/Netblock/ListCommandTest.php | 19 ++++++++++++++---- .../Console/Commands/User/ListCommandTest.php | 19 ++++++++++++------ 10 files changed, 61 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 478cd182..2b7c0a69 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ AbuseIO.iml _ide_helper.php composer.lock htpasswd + +.DS_Store diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 294c750d..09ece843 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -33,15 +33,8 @@ }); $factory->define(AbuseIO\Models\Contact::class, function (Faker\Generator $faker) { - global $contact_reference_counter; - if (!$contact_reference_counter) { - $contact_reference_counter = 1; - } else { - $contact_reference_counter++; - } - return [ - 'reference' => sprintf('reference_%d', $contact_reference_counter), + 'reference' => sprintf('reference_%s',uniqid()), 'name' => $faker->name, 'email' => $faker->email, 'api_host' => $faker->url, //'api_host', diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index d454ca5e..4edac674 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -20,6 +20,7 @@ public function run() factory(AbuseIO\Models\Domain::class, 10)->create(); factory(AbuseIO\Models\Account::class, 4)->create(); factory(AbuseIO\Models\User::class, 4)->create(); + factory(AbuseIO\Models\Ticket::class, 10)->create(); factory(AbuseIO\Models\Role::class)->create( [ diff --git a/tests/Api/ShowTestHelper.php b/tests/Api/ShowTestHelper.php index ff4f18d8..59a5de3e 100644 --- a/tests/Api/ShowTestHelper.php +++ b/tests/Api/ShowTestHelper.php @@ -17,8 +17,10 @@ trait ShowTestHelper public function testStatusCodeValidRequest() { $this->initWithValidResponse(); + $this->assertEquals(200, $this->statusCode); + $obj = json_decode($this->content); $this->assertTrue($obj->message->success); } diff --git a/tests/Console/Commands/Account/ShowCommandTest.php b/tests/Console/Commands/Account/ShowCommandTest.php index 8f6bf3cc..b7cf5acc 100644 --- a/tests/Console/Commands/Account/ShowCommandTest.php +++ b/tests/Console/Commands/Account/ShowCommandTest.php @@ -2,6 +2,7 @@ namespace tests\Console\Commands\Account; +use AbuseIO\Models\Account; use Illuminate\Support\Facades\Artisan; use tests\TestCase; @@ -27,14 +28,16 @@ public function testWithValidIdFilter() public function testWithValidNameFilter() { + $account = Account::all()->random(); + $exitCode = Artisan::call( 'account:show', [ - 'account' => 'Customer Internet', + 'account' => $account->name, ] ); $this->assertEquals($exitCode, 0); - $this->assertContains('Customer Internet', Artisan::output()); + $this->assertContains($account->name, Artisan::output()); } public function testWithInvalidFilter() diff --git a/tests/Console/Commands/Contact/EditCommandTest.php b/tests/Console/Commands/Contact/EditCommandTest.php index e40ae379..9f15fb9f 100644 --- a/tests/Console/Commands/Contact/EditCommandTest.php +++ b/tests/Console/Commands/Contact/EditCommandTest.php @@ -34,41 +34,45 @@ public function testWithInvalidId() public function testName() { - $this->assertEquals('John Doe', Contact::find(1)->name); + $contact = Contact::all()->random(); + $oldname = $contact->name; $exitCode = Artisan::call( 'contact:edit', [ - 'id' => '1', + 'id' => $contact->id, '--name' => 'New name', ] ); $this->assertEquals($exitCode, 0); $this->assertContains('The contact has been updated', Artisan::output()); - $contact = Contact::find(1); + // update contact + $contact = Contact::find($contact->id); $this->assertEquals('New name', $contact->name); - $contact->name = 'John Doe'; + $contact->name = $oldname; $contact->save(); } public function testCompanyName() { - $this->assertEquals('JOHND', Contact::find(1)->reference); + $contact = Contact::all()->random(); + $oldref = $contact->reference; $exitCode = Artisan::call( 'contact:edit', [ - 'id' => '1', + 'id' => $contact->id, '--reference' => 'New reference', ] ); $this->assertEquals($exitCode, 0); $this->assertContains('The contact has been updated', Artisan::output()); - $contact = Contact::find(1); + // update contact + $contact = Contact::find($contact->id); $this->assertEquals('New reference', $contact->reference); - $contact->reference = 'JOHND'; + $contact->reference = $oldref; $contact->save(); } } diff --git a/tests/Console/Commands/Contact/ShowCommandTest.php b/tests/Console/Commands/Contact/ShowCommandTest.php index 562acaaf..cde13ad3 100644 --- a/tests/Console/Commands/Contact/ShowCommandTest.php +++ b/tests/Console/Commands/Contact/ShowCommandTest.php @@ -2,6 +2,7 @@ namespace tests\Console\Commands\Contact; +use AbuseIO\Models\Contact; use Illuminate\Support\Facades\Artisan; use tests\TestCase; @@ -27,14 +28,16 @@ public function testWithValidIdFilter() public function testWithValidNameFilter() { + $contact = Contact::all()->random(); + $exitCode = Artisan::call( 'contact:show', [ - 'contact' => 'John Doe', + 'contact' => $contact->name, ] ); $this->assertEquals($exitCode, 0); - $this->assertContains('j.doe@customers.isp.local', Artisan::output()); + $this->assertContains($contact->email, Artisan::output()); } public function testWithInvalidFilter() diff --git a/tests/Console/Commands/Domain/ListCommandTest.php b/tests/Console/Commands/Domain/ListCommandTest.php index 82f6fa45..37ae7310 100644 --- a/tests/Console/Commands/Domain/ListCommandTest.php +++ b/tests/Console/Commands/Domain/ListCommandTest.php @@ -2,6 +2,7 @@ namespace tests\Console\Commands\Domain; +use AbuseIO\Models\Domain; use Illuminate\Support\Facades\Artisan; use tests\TestCase; @@ -25,10 +26,13 @@ public function testHeaders() public function testAll() { + $domain = Domain::all()->random(); + $contact = $domain->contact; + $exitCode = Artisan::call('domain:list', []); $this->assertEquals($exitCode, 0); - $this->assertContains('Customer 1', Artisan::output()); + $this->assertContains($contact->name, Artisan::output()); } public function testFilter() diff --git a/tests/Console/Commands/Netblock/ListCommandTest.php b/tests/Console/Commands/Netblock/ListCommandTest.php index 09390cc9..2a80515c 100644 --- a/tests/Console/Commands/Netblock/ListCommandTest.php +++ b/tests/Console/Commands/Netblock/ListCommandTest.php @@ -2,6 +2,8 @@ namespace tests\Console\Commands\Netblock; +use AbuseIO\Models\Contact; +use AbuseIO\Models\Netblock; use Illuminate\Support\Facades\Artisan; use tests\TestCase; @@ -12,23 +14,32 @@ class ListCommandTest extends TestCase { public function testNetBlockListCommand() { + $netblock = Netblock::all()->random(); + $contact = $netblock->contact; + $exitCode = Artisan::call('netblock:list', []); $this->assertEquals($exitCode, 0); - $this->assertContains('John Doe', Artisan::output()); + $this->assertContains($contact->name, Artisan::output()); } public function testNetBlockListCommandWithValidFilter() { + $netblock = Netblock::all()->random(); + $ip = $netblock->first_ip; + $netblock_contact = $netblock->contact; + $other_contact = Contact::where('id', '!=', $netblock_contact->id) + ->get()->random(); + $exitCode = Artisan::call( 'netblock:list', [ - '--filter' => '192.168.1.0', + '--filter' => $ip, ] ); $this->assertEquals($exitCode, 0); - $this->assertContains('ISP Business Internet', Artisan::output()); - $this->assertNotContains('Customer 1', Artisan::output()); + $this->assertContains($netblock_contact->name, Artisan::output()); + $this->assertNotContains($other_contact->name, Artisan::output()); } } diff --git a/tests/Console/Commands/User/ListCommandTest.php b/tests/Console/Commands/User/ListCommandTest.php index ae4fa0e7..364b80d0 100644 --- a/tests/Console/Commands/User/ListCommandTest.php +++ b/tests/Console/Commands/User/ListCommandTest.php @@ -2,6 +2,7 @@ namespace tests\Console\Commands\User; +use AbuseIO\Models\User; use Illuminate\Support\Facades\Artisan; use tests\TestCase; @@ -10,8 +11,11 @@ */ class ListCommandTest extends TestCase { - public function testNetBlockListCommand() + public function testUserListCommand() { + + $user = User::all()->random(); + $exitCode = Artisan::call( 'user:list', [ @@ -20,22 +24,25 @@ public function testNetBlockListCommand() ); $this->assertEquals($exitCode, 0); - $this->assertContains('admin@isp.local', Artisan::output()); + $this->assertContains($user->email, Artisan::output()); } - public function testNetBlockListCommandWithValidFilter() + public function testUserListCommandWithValidFilter() { + $user = User::all()->random(); + $other_user = User::where('id','!=', $user->id)->get()->random(); + $exitCode = Artisan::call( 'user:list', [ - '--filter' => 'user', + '--filter' => $user->email, ] ); $this->assertEquals($exitCode, 0); $output = Artisan::output(); - $this->assertContains('user@isp.local', $output); - $this->assertNotContains('admin@account2.local', $output); + $this->assertContains($user->email, $output); + $this->assertNotContains($other_user->email, $output); } }