11<?php
22
3+ declare (strict_types=1 );
4+
35/**
46 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
57 * SPDX-License-Identifier: AGPL-3.0-or-later
68 */
79
8- declare (strict_types=1 );
9-
1010namespace Test \Authentication \Login ;
1111
1212use OC \Authentication \Login \CreateSessionTokenCommand ;
1313use OC \Authentication \Token \IToken ;
1414use OC \User \Session ;
15+ use OCP \AppFramework \Utility \ITimeFactory ;
1516use OCP \IConfig ;
17+ use OCP \IURLGenerator ;
1618use PHPUnit \Framework \MockObject \MockObject ;
1719
1820class CreateSessionTokenCommandTest extends ALoginTestCommand {
19- /** @var IConfig|MockObject */
20- private $ config ;
21-
22- /** @var Session|MockObject */
23- private $ userSession ;
21+ private IConfig &MockObject $ config ;
22+ private Session &MockObject $ userSession ;
23+ private IURLGenerator &MockObject $ urlGenerator ;
24+ private ITimeFactory &MockObject $ timeFactory ;
2425
2526 #[\Override]
2627 protected function setUp (): void {
2728 parent ::setUp ();
2829
2930 $ this ->config = $ this ->createMock (IConfig::class);
3031 $ this ->userSession = $ this ->createMock (Session::class);
32+ $ this ->urlGenerator = $ this ->createMock (IURLGenerator::class);
33+ $ this ->timeFactory = $ this ->createMock (ITimeFactory::class);
3134
3235 $ this ->cmd = new CreateSessionTokenCommand (
3336 $ this ->config ,
34- $ this ->userSession
37+ $ this ->userSession ,
38+ $ this ->urlGenerator ,
39+ $ this ->timeFactory ,
3540 );
3641 }
3742
3843 public function testProcess (): void {
44+ // Just return the route name as path to not return an empty string
45+ $ this ->urlGenerator ->expects (self ::once ())
46+ ->method ('linkToRoute ' )
47+ ->willReturnArgument (0 );
3948 $ data = $ this ->getLoggedInLoginData ();
4049 $ this ->config ->expects ($ this ->once ())
4150 ->method ('getSystemValueInt ' )
@@ -54,7 +63,8 @@ public function testProcess(): void {
5463 $ this ->username ,
5564 $ this ->username ,
5665 $ this ->password ,
57- IToken::REMEMBER
66+ IToken::REMEMBER ,
67+ null
5868 );
5969 $ this ->userSession ->expects ($ this ->once ())
6070 ->method ('updateTokens ' )
@@ -69,6 +79,10 @@ public function testProcess(): void {
6979 }
7080
7181 public function testProcessDoNotRemember (): void {
82+ // Just return the route name as path to not return an empty string
83+ $ this ->urlGenerator ->expects (self ::once ())
84+ ->method ('linkToRoute ' )
85+ ->willReturnArgument (0 );
7286 $ data = $ this ->getLoggedInLoginData ();
7387 $ this ->config ->expects ($ this ->once ())
7488 ->method ('getSystemValueInt ' )
@@ -87,7 +101,8 @@ public function testProcessDoNotRemember(): void {
87101 $ this ->username ,
88102 $ this ->username ,
89103 $ this ->password ,
90- IToken::DO_NOT_REMEMBER
104+ IToken::DO_NOT_REMEMBER ,
105+ null
91106 );
92107 $ this ->userSession ->expects ($ this ->once ())
93108 ->method ('updateTokens ' )
@@ -101,4 +116,46 @@ public function testProcessDoNotRemember(): void {
101116 $ this ->assertTrue ($ result ->isSuccess ());
102117 $ this ->assertFalse ($ data ->isRememberLogin ());
103118 }
119+
120+ public function testLoginFlowEphemeral (): void {
121+ $ this ->redirectUrl = 'EPHEMERAL_ROUTE ' ;
122+ $ this ->urlGenerator ->expects (self ::once ())
123+ ->method ('linkToRoute ' )
124+ ->willReturn ($ this ->redirectUrl );
125+ $ this ->timeFactory ->expects (self ::once ())
126+ ->method ('getTime ' )
127+ ->willReturn (1000 );
128+
129+ $ data = $ this ->getLoggedInLoginDataWithRedirectUrl ();
130+ $ this ->config ->expects ($ this ->once ())
131+ ->method ('getSystemValueInt ' )
132+ ->with (
133+ 'remember_login_cookie_lifetime ' ,
134+ 60 * 60 * 24 * 15
135+ )
136+ ->willReturn (100 );
137+ $ this ->user ->expects ($ this ->any ())
138+ ->method ('getUID ' )
139+ ->willReturn ($ this ->username );
140+ $ this ->userSession ->expects ($ this ->once ())
141+ ->method ('createSessionToken ' )
142+ ->with (
143+ $ this ->request ,
144+ $ this ->username ,
145+ $ this ->username ,
146+ $ this ->password ,
147+ IToken::REMEMBER ,
148+ 1000 + 5 * 60
149+ );
150+ $ this ->userSession ->expects ($ this ->once ())
151+ ->method ('updateTokens ' )
152+ ->with (
153+ $ this ->username ,
154+ $ this ->password
155+ );
156+
157+ $ result = $ this ->cmd ->process ($ data );
158+
159+ $ this ->assertTrue ($ result ->isSuccess ());
160+ }
104161}
0 commit comments