Git Product home page Git Product logo

Comments (4)

gregorip02 avatar gregorip02 commented on June 24, 2024 2

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']
        ]);
    }
}

from laravel-firebase.

jeromegamez avatar jeromegamez commented on June 24, 2024

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 🤞

from laravel-firebase.

github-actions avatar github-actions commented on June 24, 2024

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.

from laravel-firebase.

eskayamadeus avatar eskayamadeus commented on June 24, 2024

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

from laravel-firebase.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.