kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
995 stars 161 forks source link

How to implement tests? 🔧 #51

Closed gregorip02 closed 3 years ago

gregorip02 commented 3 years ago

Hi, I am trying to test my endpoints protected by firebase authentication, does your package offer an example for use in testing environments? Sorry if I haven't done enough research.

jeromegamez commented 3 years ago

Unfortunately, it currently doesn't. What you could do is abstracting away the Firebase interactions in an application service.

It's planned for a future release to allow using the Firebase Emulator suite (which still would need spinning up a testing environment and which wouldn't really be unit tests anyway), but I can't give you an ETA 🙈

You can follow the progress (when it happens) at https://github.com/kreait/firebase-php/issues/456

If you have another idea besides the abstraction, please let me know 🤞

gregorip02 commented 3 years ago

Hi, I temporarily solved with a simple trait that creates a test user on my side, generates a uid using Str::random(28) and authenticates with signInWithCustomToken.

<?php

namespace Tests;

use App\Models\User\User;
use Kreait\Firebase\Auth\SignInResult;
use Kreait\Laravel\Firebase\Facades\FirebaseAuth;

trait InteractsWithFirebase
{
    /**
     * Crea un nuevo usuario de nuestro lado y lo intenta autenticar con firebase.
     *
     * @param  array  $attributes
     * @return \Kreait\Firebase\Auth\SignInResult
     */
    public function actingAsFirebaseUser(array $attributes = []): SignInResult
    {
        $user = factory(User::class)->create($attributes);

        $customToken = FirebaseAuth::createCustomToken(
            $user->getAttributeValue('firebase_uid')
        );

        $authenticated = FirebaseAuth::signInWithCustomToken($customToken);

        if ($authenticated instanceof SignInResult) {
            $this->withToken($authenticated->idToken());
        }

        return $authenticated;
    }
}

I don't know if it's the best way, but my tests behave as I expect.

<?php

class MySuperTesting {
    use InteractsWithFirebase;

    /**
     * @test Can update the authenticated user profile.
     */
    public function can_update_user_profile()
    {
        $this->actingAsFirebaseUser();
        $response = $this->putJson('/api/users', ['firstname' => 'Maria']);

        $response->assertOk();
        $response->assertJson([
            'data' => ['firstname' => 'Maria']
        ]);
    }
}
github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

eskayamadeus commented 2 years ago

@gregorip02 Yes, your tests may work as expected, but the drawback here is that you actually interact with the Firebase server when you need to run your test. An improved solution should make it possible for you to act as firebase user without actually hitting their endpoints to authenticate. There should be a way to sandbox their authentication...somehow....