uxweb's avatar
Level 20

How to test social login in laravel?

Hi :)

I was able to build an app that uses Laravel/Socialite to integrate Twitter authentication.

The only thing is that i want to have at least one test for the twitter auth,

Is there a way to build a test for this?

My test looks like this right now:

public function testUserCanRegisterUsingTwitter()
{
    $this->visit('/auth/login')
               ->seePageIs('/auth/login')
               ->click('Twitter')
               ->seePageIs('/');

But it fails with :

A request to [https://api.twitter.com/oauth/authenticate?oauth_token=UTOS_wAAAAAAjMu5AAABUacuqXw] failed. Received status code [404]

If i manually test that in the browser it's ok, no error.

am i missing something?

Thanks for your answers!

0 likes
8 replies
bobbybouwmann's avatar

I would say take a look at how others do it, but they don't seem to be running tests as well!

You can find other providers at this repository: https://github.com/SocialiteProviders But like I said they don't have tests as well..]

I guess you can try to mock everything but that will result in mocking everything and not really testing anything. I guess you need to test it with your bare hands ;)

toniperic's avatar

@uxweb so if you visit your /auth/login page, and click the Twitter button, you expect to be redirected to your own home page, and not to Twitter's authorization page?

uxweb's avatar
Level 20

@bobbybouwmann Thanks man, looks like this is a little complex to test :s.

@toniperic That's where i'm stucked :), When clicking the Twitter link, it will go to /auth/twitter, then it redirects to the provider auth url, then after the user has logged in, the auth provider page redirects to /auth/twitter/callback, in that route i get the user and then is logged in and redirected to '/'.

My question is, how would be possible to interact with the auth provider page to log in and then be redirected :s.

I just ... don't know.

toniperic's avatar

@uxweb you don't want to actually use a valid Twitter account for testing purposes, nor do you want to visit Twitter's authorization page.

To accomplish that, you'd want to mock Twitter's response, and not actually hit their servers with a real account. Then, you should test the callback URL, and assert that the user is being redirected to / with mocked data you provided.

Currently, your tests are checking that clicking the Twitter button redirects to your homepage, which is obviously not what you want (and which is obviously not happening when you test everything manually using a browser).

Hope this makes sense.

uxweb's avatar
Level 20

@toniperic Yes, then mocking Twitter is how it can be done?, i have no experience with mocking, but will read bout it and see if i can make a test :).

Thank you guys!

NoahSami's avatar

hi you can use selenium and run real acceptance tests with browser .

connecteev's avatar

@uxweb did you figure this out? Perhaps you could share your solution with the community

Please or to participate in this conversation.