Laravel Dusk logs in as wrong user












0















I have a fairly simple browser test (laravel dusk). The test fails, not because of the actual asserts but because of missing permissions.



The user with which I login (loginAs) has those permissions. It turned out that in the end, although I log in as testUser the actual authenticated user is another user.



It seems that the authenticated user switches between the loginAs() and accessing the controller function. But as I see it, these two steps should come one after another.



I gave my best to explain the situation if something is unclear please ask for clarification.



Test



/**
* @throws Throwable
*/
public function test_something()
{
$this->browse(function (Browser $browser) {
$browser->loginAs($this->testUser) //made a breakpoint here and it showed the correct user
->visit(new IndexPage())
//some asserts
});
}


Corresponding Controller



public function index()
{
dd(Auth::user()->username); //shows not the name of testUser
if (! Auth::user()->can('index'))
return abort(403, "Operation not permitted"); //fails because of wrong user

//gather data

return view('index', compact('data'));
}









share|improve this question





























    0















    I have a fairly simple browser test (laravel dusk). The test fails, not because of the actual asserts but because of missing permissions.



    The user with which I login (loginAs) has those permissions. It turned out that in the end, although I log in as testUser the actual authenticated user is another user.



    It seems that the authenticated user switches between the loginAs() and accessing the controller function. But as I see it, these two steps should come one after another.



    I gave my best to explain the situation if something is unclear please ask for clarification.



    Test



    /**
    * @throws Throwable
    */
    public function test_something()
    {
    $this->browse(function (Browser $browser) {
    $browser->loginAs($this->testUser) //made a breakpoint here and it showed the correct user
    ->visit(new IndexPage())
    //some asserts
    });
    }


    Corresponding Controller



    public function index()
    {
    dd(Auth::user()->username); //shows not the name of testUser
    if (! Auth::user()->can('index'))
    return abort(403, "Operation not permitted"); //fails because of wrong user

    //gather data

    return view('index', compact('data'));
    }









    share|improve this question



























      0












      0








      0


      0






      I have a fairly simple browser test (laravel dusk). The test fails, not because of the actual asserts but because of missing permissions.



      The user with which I login (loginAs) has those permissions. It turned out that in the end, although I log in as testUser the actual authenticated user is another user.



      It seems that the authenticated user switches between the loginAs() and accessing the controller function. But as I see it, these two steps should come one after another.



      I gave my best to explain the situation if something is unclear please ask for clarification.



      Test



      /**
      * @throws Throwable
      */
      public function test_something()
      {
      $this->browse(function (Browser $browser) {
      $browser->loginAs($this->testUser) //made a breakpoint here and it showed the correct user
      ->visit(new IndexPage())
      //some asserts
      });
      }


      Corresponding Controller



      public function index()
      {
      dd(Auth::user()->username); //shows not the name of testUser
      if (! Auth::user()->can('index'))
      return abort(403, "Operation not permitted"); //fails because of wrong user

      //gather data

      return view('index', compact('data'));
      }









      share|improve this question
















      I have a fairly simple browser test (laravel dusk). The test fails, not because of the actual asserts but because of missing permissions.



      The user with which I login (loginAs) has those permissions. It turned out that in the end, although I log in as testUser the actual authenticated user is another user.



      It seems that the authenticated user switches between the loginAs() and accessing the controller function. But as I see it, these two steps should come one after another.



      I gave my best to explain the situation if something is unclear please ask for clarification.



      Test



      /**
      * @throws Throwable
      */
      public function test_something()
      {
      $this->browse(function (Browser $browser) {
      $browser->loginAs($this->testUser) //made a breakpoint here and it showed the correct user
      ->visit(new IndexPage())
      //some asserts
      });
      }


      Corresponding Controller



      public function index()
      {
      dd(Auth::user()->username); //shows not the name of testUser
      if (! Auth::user()->can('index'))
      return abort(403, "Operation not permitted"); //fails because of wrong user

      //gather data

      return view('index', compact('data'));
      }






      laravel-5.5 laravel-dusk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 23:24









      Joel

      1,5686719




      1,5686719










      asked Nov 14 '18 at 23:12









      TimSchTimSch

      309115




      309115
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Usually I would simply delete this question as it turned out as some kind of beginner's mistake. But as someone "starred" this question I'd rather answer it.

          I can't tell exactly where the issue came from but I cleared everything I could clear (caches) and now the test goes green.






          share|improve this answer































            0














            I think this issue has something to do with the way Dusk logs itself in. I noticed that my observers would always get null when trying to access the current user despite having called $browser->loginAs($user). My solution was to also call Auth::login($user) which solves the problem.



            It's important to remember that the PHP process running dusk tests is completely isolated from the application code which can lead to a lot of confusion.



            Long term solution is probably to abandon Dusk and use WebDriver directly as the zero-config nature of Dusk leaves developers very confused when something going wrong and that's particularly bad with something as flaky as Laravel Dusk.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310155%2flaravel-dusk-logs-in-as-wrong-user%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Usually I would simply delete this question as it turned out as some kind of beginner's mistake. But as someone "starred" this question I'd rather answer it.

              I can't tell exactly where the issue came from but I cleared everything I could clear (caches) and now the test goes green.






              share|improve this answer




























                0














                Usually I would simply delete this question as it turned out as some kind of beginner's mistake. But as someone "starred" this question I'd rather answer it.

                I can't tell exactly where the issue came from but I cleared everything I could clear (caches) and now the test goes green.






                share|improve this answer


























                  0












                  0








                  0







                  Usually I would simply delete this question as it turned out as some kind of beginner's mistake. But as someone "starred" this question I'd rather answer it.

                  I can't tell exactly where the issue came from but I cleared everything I could clear (caches) and now the test goes green.






                  share|improve this answer













                  Usually I would simply delete this question as it turned out as some kind of beginner's mistake. But as someone "starred" this question I'd rather answer it.

                  I can't tell exactly where the issue came from but I cleared everything I could clear (caches) and now the test goes green.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 18:59









                  TimSchTimSch

                  309115




                  309115

























                      0














                      I think this issue has something to do with the way Dusk logs itself in. I noticed that my observers would always get null when trying to access the current user despite having called $browser->loginAs($user). My solution was to also call Auth::login($user) which solves the problem.



                      It's important to remember that the PHP process running dusk tests is completely isolated from the application code which can lead to a lot of confusion.



                      Long term solution is probably to abandon Dusk and use WebDriver directly as the zero-config nature of Dusk leaves developers very confused when something going wrong and that's particularly bad with something as flaky as Laravel Dusk.






                      share|improve this answer




























                        0














                        I think this issue has something to do with the way Dusk logs itself in. I noticed that my observers would always get null when trying to access the current user despite having called $browser->loginAs($user). My solution was to also call Auth::login($user) which solves the problem.



                        It's important to remember that the PHP process running dusk tests is completely isolated from the application code which can lead to a lot of confusion.



                        Long term solution is probably to abandon Dusk and use WebDriver directly as the zero-config nature of Dusk leaves developers very confused when something going wrong and that's particularly bad with something as flaky as Laravel Dusk.






                        share|improve this answer


























                          0












                          0








                          0







                          I think this issue has something to do with the way Dusk logs itself in. I noticed that my observers would always get null when trying to access the current user despite having called $browser->loginAs($user). My solution was to also call Auth::login($user) which solves the problem.



                          It's important to remember that the PHP process running dusk tests is completely isolated from the application code which can lead to a lot of confusion.



                          Long term solution is probably to abandon Dusk and use WebDriver directly as the zero-config nature of Dusk leaves developers very confused when something going wrong and that's particularly bad with something as flaky as Laravel Dusk.






                          share|improve this answer













                          I think this issue has something to do with the way Dusk logs itself in. I noticed that my observers would always get null when trying to access the current user despite having called $browser->loginAs($user). My solution was to also call Auth::login($user) which solves the problem.



                          It's important to remember that the PHP process running dusk tests is completely isolated from the application code which can lead to a lot of confusion.



                          Long term solution is probably to abandon Dusk and use WebDriver directly as the zero-config nature of Dusk leaves developers very confused when something going wrong and that's particularly bad with something as flaky as Laravel Dusk.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 3 at 18:26









                          CoboltCobolt

                          4211515




                          4211515






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310155%2flaravel-dusk-logs-in-as-wrong-user%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python