Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement tests? 馃敡 #51

Closed
gregorip02 opened this issue Aug 11, 2020 · 4 comments
Closed

How to implement tests? 馃敡 #51

gregorip02 opened this issue Aug 11, 2020 · 4 comments

Comments

@gregorip02
Copy link
Contributor

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
Copy link
Member

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 kreait/firebase-php#456

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

@gregorip02
Copy link
Contributor Author

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
Copy link
Contributor

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
Copy link

@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....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants