Преглед на файлове

Refactore Auth feature tests

Bubka преди 5 години
родител
ревизия
25ee8f58fe
променени са 4 файла, в които са добавени 240 реда и са изтрити 161 реда
  1. 90 0
      tests/Feature/Auth/ForgotPasswordTest.php
  2. 146 0
      tests/Feature/Auth/LoginTest.php
  3. 2 84
      tests/Feature/Auth/RegisterTest.php
  4. 2 77
      tests/Feature/Auth/ResetPasswordTest.php

+ 90 - 0
tests/Feature/Auth/ForgotPasswordTest.php

@@ -0,0 +1,90 @@
+<?php
+
+namespace Tests\Feature\Auth;
+
+use App\User;
+use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Facades\Password;
+use Illuminate\Auth\Notifications\ResetPassword;
+use Illuminate\Support\Facades\Notification;
+use Tests\TestCase;
+
+class ForgotPasswordTest extends TestCase
+{
+    /** @var \App\User */
+    protected $user;
+
+    /**
+     * Testing submitting the email password request without
+     * email address.
+     */
+    public function testSubmitEmailPasswordRequestWithoutEmail()
+    {
+        $response = $this->json('POST', '/api/password/email', [
+            'email' => ''
+        ]);
+
+        $response->assertStatus(422)
+                 ->assertJsonValidationErrors(['email']);
+    }
+
+    /**
+     * Testing submitting the email password request with an invalid
+     * email address.
+     */
+    public function testSubmitEmailPasswordRequestWithInvalidEmail()
+    {
+        $response = $this->json('POST', '/api/password/email', [
+            'email' => 'nametest.com'
+        ]);
+
+        $response->assertStatus(422)
+                 ->assertJsonValidationErrors(['email']);
+    }
+
+    /**
+     * Testing submitting the email password request with an unknown
+     * email address.
+     */
+    public function testSubmitEmailPasswordRequestWithUnknownEmail()
+    {
+        $response = $this->json('POST', '/api/password/email', [
+            'email' => 'name@test.com'
+        ]);
+
+        $response->assertStatus(422)
+                 ->assertJsonValidationErrors(['email']);
+    }
+
+    /**
+     * Testing submitting the email password request with a valid email address.
+     */
+    public function testSubmitEmailPasswordRequest()
+    {
+        Notification::fake();
+
+        $this->user = factory(User::class)->create([
+            'name' => 'user',
+            'email' => 'user@example.org',
+            'password' => bcrypt('password'),
+            'email_verified_at' => now(),
+            'remember_token' => \Illuminate\Support\Str::random(10),
+        ]);
+
+        //$this->expectsNotification($this->user, ResetPassword::class);
+
+        $response = $this->json('POST', '/api/password/email', [
+            'email' => $this->user->email
+        ]);
+
+        $response->assertStatus(200);
+
+        $token = \Illuminate\Support\Facades\DB::table('password_resets')->first();
+        $this->assertNotNull($token);
+
+        // Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
+        //     return Hash::check($notification->token, $token->token) === true;
+        // });
+    }
+
+}

+ 146 - 0
tests/Feature/Auth/LoginTest.php

@@ -0,0 +1,146 @@
+<?php
+
+namespace Tests\Feature\Auth;
+
+use App\User;
+use Tests\TestCase;
+use Illuminate\Auth\Authenticatable;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Hash;
+use Illuminate\Auth\RequestGuard;
+
+class LoginTest extends TestCase
+{
+    /** @var \App\User */
+    protected $user;
+
+
+    /**
+     * @test
+     */
+    public function setUp(): void
+    {
+        parent::setUp();
+
+        $this->user = factory(User::class)->create();
+    }
+
+
+    /**
+     * test User login via API
+     *
+     * @test
+     */
+    public function testUserLogin()
+    {
+
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'password'
+        ]);
+
+        $response->assertStatus(200)
+            ->assertJsonStructure([
+                'message' => ['token']
+            ]);
+    }
+
+
+    /**
+     * test User login with missing values via API
+     *
+     * @test
+     */
+    public function testUserLoginWithMissingValues()
+    {
+        $response = $this->json('POST', '/api/login', [
+            'email' => '',
+            'password' => ''
+        ]);
+
+        $response->assertStatus(422);
+    }
+
+
+    /**
+     * test User login with invalid credentials via API
+     *
+     * @test
+     */
+    public function testUserLoginWithInvalidCredential()
+    {
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'badPassword'
+        ]);
+
+        $response->assertStatus(401)
+            ->assertJson([
+                'message' => 'unauthorised'
+            ]);
+    }
+
+
+    /**
+     * test User login with invalid credentials via API
+     *
+     * @test
+     */
+    public function testTooManyAttempsWithInvalidCredential()
+    {
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'badPassword'
+        ]);
+
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'badPassword'
+        ]);
+
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'badPassword'
+        ]);
+
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'badPassword'
+        ]);
+
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'badPassword'
+        ]);
+
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'badPassword'
+        ]);
+
+        $response->assertStatus(429);
+    }
+
+
+    /**
+     * test User logout via API
+     *
+     * @test
+     */
+    public function testUserLogout()
+    {
+        $response = $this->json('POST', '/api/login', [
+            'email' => $this->user->email,
+            'password' => 'password'
+        ]);
+
+        $headers = ['Authorization' => "Bearer " . $response->original['message']['token']];
+
+        $response = $this->json('POST', '/api/logout', [], $headers)
+            ->assertStatus(200)
+            ->assertJson([
+                'message' => 'signed out',
+            ]);
+    }
+
+}

+ 2 - 84
tests/Unit/UserTest.php → tests/Feature/Auth/RegisterTest.php

@@ -1,14 +1,11 @@
 <?php
 
-namespace Tests\Unit;
+namespace Tests\Unit\Auth;
 
 use App\User;
 use Tests\TestCase;
-use Illuminate\Auth\Authenticatable;
-use Illuminate\Support\Facades\Auth;
-use Illuminate\Support\Facades\Hash;
 
-class UserTest extends TestCase
+class RegisterTest extends TestCase
 {
     /** @var \App\User */
     protected $user;
@@ -100,85 +97,6 @@ class UserTest extends TestCase
     }
 
 
-    /**
-     * test User login via API
-     *
-     * @test
-     */
-    public function testUserLogin()
-    {
-
-        $response = $this->json('POST', '/api/login', [
-            'email' => $this->user->email,
-            'password' => 'password'
-        ]);
-
-        $response->assertStatus(200)
-            ->assertJsonStructure([
-                'message' => ['token']
-            ]);
-    }
-
-
-    /**
-     * test User login with missing values via API
-     *
-     * @test
-     */
-    public function testUserLoginWithMissingValues()
-    {
-        $response = $this->json('POST', '/api/login', [
-            'email' => '',
-            'password' => ''
-        ]);
-
-        $response->assertStatus(422);
-    }
-
-
-    /**
-     * test User login with invalid credentials via API
-     *
-     * @test
-     */
-    public function testUserLoginWithInvalidCredential()
-    {
-        $response = $this->json('POST', '/api/login', [
-            'email' => $this->user->email,
-            'password' => 'badPassword'
-        ]);
-
-        $response->assertStatus(401)
-            ->assertJson([
-                'message' => 'unauthorised'
-            ]);
-    }
-
-
-    /**
-     * test User logout via API
-     *
-     * @test
-     */
-    public function testUserLogout()
-    {
-        $credentials = [
-            'email' => $this->user->email,
-            'password' => 'password'
-        ];
-
-        Auth::attempt($credentials);
-        $token = Auth::user()->createToken('testToken')->accessToken;
-        $headers = ['Authorization' => "Bearer $token"];
-
-        $response = $this->json('POST', '/api/logout', [], $headers)
-            ->assertStatus(200)
-            ->assertJson([
-                'message' => 'signed out',
-            ]);
-    }
-
-
     /**
      * test User creation via API
      *

+ 2 - 77
tests/Feature/AuthTest.php → tests/Feature/Auth/ResetPasswordTest.php

@@ -1,93 +1,18 @@
 <?php
 
-namespace Tests\Feature;
+namespace Tests\Feature\Auth;
 
 use App\User;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Password;
-use Illuminate\Auth\Notifications\ResetPassword;
 use Illuminate\Support\Facades\Notification;
 use Tests\TestCase;
 
-class PasswordResetTest extends TestCase
+class ResetPasswordTest extends TestCase
 {
     /** @var \App\User */
     protected $user;
 
-    /**
-     * Testing submitting the email password request without
-     * email address.
-     */
-    public function testSubmitEmailPasswordRequestWithoutEmail()
-    {
-        $response = $this->json('POST', '/api/password/email', [
-            'email' => ''
-        ]);
-
-        $response->assertStatus(422)
-                 ->assertJsonValidationErrors(['email']);
-    }
-
-    /**
-     * Testing submitting the email password request with an invalid
-     * email address.
-     */
-    public function testSubmitEmailPasswordRequestWithInvalidEmail()
-    {
-        $response = $this->json('POST', '/api/password/email', [
-            'email' => 'nametest.com'
-        ]);
-
-        $response->assertStatus(422)
-                 ->assertJsonValidationErrors(['email']);
-    }
-
-    /**
-     * Testing submitting the email password request with an unknown
-     * email address.
-     */
-    public function testSubmitEmailPasswordRequestWithUnknownEmail()
-    {
-        $response = $this->json('POST', '/api/password/email', [
-            'email' => 'name@test.com'
-        ]);
-
-        $response->assertStatus(422)
-                 ->assertJsonValidationErrors(['email']);
-    }
-
-    /**
-     * Testing submitting the email password request with a valid email address.
-     */
-    public function testSubmitEmailPasswordRequest()
-    {
-        Notification::fake();
-
-        $this->user = factory(User::class)->create([
-            'name' => 'user',
-            'email' => 'user@example.org',
-            'password' => bcrypt('password'),
-            'email_verified_at' => now(),
-            'remember_token' => \Illuminate\Support\Str::random(10),
-        ]);
-
-        //$this->expectsNotification($this->user, ResetPassword::class);
-
-        $response = $this->json('POST', '/api/password/email', [
-            'email' => $this->user->email
-        ]);
-
-        $response->assertStatus(200);
-
-        $token = \Illuminate\Support\Facades\DB::table('password_resets')->first();
-        $this->assertNotNull($token);
-
-        // Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
-        //     return Hash::check($notification->token, $token->token) === true;
-        // });
-    }
-
-
 
     /**
      * Testing submitting the reset password  without