22
33namespace CodeZero \StageFront \Tests \Feature ;
44
5+ use CodeZero \StageFront \Middleware \RedirectIfStageFrontIsEnabled ;
56use CodeZero \StageFront \Tests \Stubs \User ;
67use CodeZero \StageFront \Tests \TestCase ;
8+ use Illuminate \Contracts \Http \Kernel ;
79use Route ;
810
911class StageFrontTest extends TestCase
@@ -12,9 +14,9 @@ public function setUp()
1214 {
1315 parent ::setUp ();
1416
15- // Note that changing the URL or middleware config in the tests has no effect.
16- // These settings are used in the routes file, which has already been
17- // loaded before the tests run .
17+ // Note that changing the URL or middleware config in the tests
18+ // has no effect until you call $this->enableStageFront().
19+ // It is disabled by default so nothing is loaded by default .
1820 $ this ->url = config ('stagefront.url ' );
1921
2022 Route::get ('/page ' , function () {
@@ -45,11 +47,10 @@ public function the_login_route_does_not_exist_when_stagefront_is_disabled()
4547 /** @test */
4648 public function it_redirects_to_the_intended_url_when_you_provide_valid_credentials ()
4749 {
48- $ this ->enableStageFront ();
49-
5050 config ()->set ('stagefront.login ' , 'tester ' );
5151 config ()->set ('stagefront.password ' , 'p4ssw0rd ' );
5252
53+ $ this ->enableStageFront ();
5354 $ this ->setIntendedUrl ('/page ' );
5455
5556 $ response = $ this ->submitForm ([
@@ -63,11 +64,10 @@ public function it_redirects_to_the_intended_url_when_you_provide_valid_credenti
6364 /** @test */
6465 public function it_does_not_allow_access_when_you_provide_invalid_credentials ()
6566 {
66- $ this ->enableStageFront ();
67-
6867 config ()->set ('stagefront.login ' , 'tester ' );
6968 config ()->set ('stagefront.password ' , 'p4ssw0rd ' );
7069
70+ $ this ->enableStageFront ();
7171 $ this ->setIntendedUrl ('/page ' );
7272
7373 $ response = $ this ->submitForm ([
@@ -82,12 +82,11 @@ public function it_does_not_allow_access_when_you_provide_invalid_credentials()
8282 /** @test */
8383 public function the_password_may_be_stored_encrypted ()
8484 {
85- $ this ->enableStageFront ();
86-
8785 config ()->set ('stagefront.login ' , 'tester ' );
8886 config ()->set ('stagefront.password ' , bcrypt ('p4ssw0rd ' ));
8987 config ()->set ('stagefront.encrypted ' , true );
9088
89+ $ this ->enableStageFront ();
9190 $ this ->setIntendedUrl ('/page ' );
9291
9392 $ response = $ this ->submitForm ([
@@ -109,13 +108,12 @@ public function the_users_in_the_database_can_be_used_for_logging_in()
109108 'password ' => bcrypt ('str0ng p4ssw0rd ' ),
110109 ]);
111110
112- $ this ->enableStageFront ();
113-
114111 config ()->set ('stagefront.database ' , true );
115112 config ()->set ('stagefront.database_table ' , 'users ' );
116113 config ()->set ('stagefront.database_login_field ' , 'email ' );
117114 config ()->set ('stagefront.database_password_field ' , 'password ' );
118115
116+ $ this ->enableStageFront ();
119117 $ this ->setIntendedUrl ('/page ' );
120118
121119 $ response = $ this ->submitForm ([
@@ -147,14 +145,14 @@ public function you_can_limit_which_database_users_have_access()
147145 'password ' => bcrypt ('str0ng p4ssw0rd ' ),
148146 ]);
149147
150- $ this ->enableStageFront ();
151-
152148 config ()->set ('stagefront.database ' , true );
153149 config ()->set ('stagefront.database_whitelist ' , 'john@doe.io,jane@doe.io ' );
154150 config ()->set ('stagefront.database_table ' , 'users ' );
155151 config ()->set ('stagefront.database_login_field ' , 'email ' );
156152 config ()->set ('stagefront.database_password_field ' , 'password ' );
157153
154+ $ this ->enableStageFront ();
155+
158156 $ this ->setIntendedUrl ('/page ' )->submitForm ([
159157 'login ' => 'john@doe.io ' ,
160158 'password ' => 'str0ng p4ssw0rd ' ,
@@ -208,7 +206,7 @@ protected function submitForm(array $credentials)
208206
209207 /**
210208 * Enable StageFront.
211- * Routes haven't been loaded, so we should do this now.
209+ * Routes and middleware haven't been loaded, so we should do this now.
212210 *
213211 * @return void
214212 */
@@ -217,5 +215,9 @@ protected function enableStageFront()
217215 config ()->set ('stagefront.enabled ' , true );
218216
219217 include __DIR__ .'/../../routes/routes.php ' ;
218+
219+ app (Kernel::class)->prependMiddleware (
220+ RedirectIfStageFrontIsEnabled::class
221+ );
220222 }
221223}
0 commit comments