11<?php
22
3+ use BookStack \EmailConfirmation ;
4+
35class AuthTest extends TestCase
46{
57
@@ -12,10 +14,9 @@ public function testAuthWorking()
1214 public function testLogin ()
1315 {
1416 $ this ->visit ('/ ' )
15- ->seePageIs ('/login ' )
16- ->type ('admin@admin.com ' , '#email ' )
17- ->type ('password ' , '#password ' )
18- ->press ('Sign In ' )
17+ ->seePageIs ('/login ' );
18+
19+ $ this ->login ('admin@admin.com ' , 'password ' )
1920 ->seePageIs ('/ ' )
2021 ->see ('BookStack ' );
2122 }
@@ -41,25 +42,64 @@ public function testRegistrationShowing()
4142
4243 public function testNormalRegistration ()
4344 {
45+ // Set settings and get user instance
4446 $ this ->setSettings (['registration-enabled ' => 'true ' ]);
4547 $ user = factory (\BookStack \User::class)->make ();
4648
49+ // Test form and ensure user is created
4750 $ this ->visit ('/register ' )
4851 ->see ('Sign Up ' )
4952 ->type ($ user ->name , '#name ' )
5053 ->type ($ user ->email , '#email ' )
5154 ->type ($ user ->password , '#password ' )
5255 ->press ('Create Account ' )
5356 ->seePageIs ('/ ' )
54- ->see ($ user ->name );
57+ ->see ($ user ->name )
58+ ->seeInDatabase ('users ' , ['name ' => $ user ->name , 'email ' => $ user ->email ]);
5559 }
5660
57- private function setSettings ( $ settingsArray )
61+ public function testConfirmedRegistration ( )
5862 {
59- $ settings = app ('BookStack\Services\SettingService ' );
60- foreach ($ settingsArray as $ key => $ value ) {
61- $ settings ->put ($ key , $ value );
62- }
63+ // Set settings and get user instance
64+ $ this ->setSettings (['registration-enabled ' => 'true ' , 'registration-confirmation ' => 'true ' ]);
65+ $ user = factory (\BookStack \User::class)->make ();
66+
67+ // Mock Mailer to ensure mail is being sent
68+ $ mockMailer = Mockery::mock ('Illuminate\Contracts\Mail\Mailer ' );
69+ $ mockMailer ->shouldReceive ('send ' )->with ('emails/email-confirmation ' , Mockery::type ('array ' ), Mockery::type ('callable ' ))->twice ();
70+ $ this ->app ->instance ('mailer ' , $ mockMailer );
71+
72+ // Go through registration process
73+ $ this ->visit ('/register ' )
74+ ->see ('Sign Up ' )
75+ ->type ($ user ->name , '#name ' )
76+ ->type ($ user ->email , '#email ' )
77+ ->type ($ user ->password , '#password ' )
78+ ->press ('Create Account ' )
79+ ->seePageIs ('/register/confirm ' )
80+ ->seeInDatabase ('users ' , ['name ' => $ user ->name , 'email ' => $ user ->email , 'email_confirmed ' => false ]);
81+
82+ // Test access and resend confirmation email
83+ $ this ->login ($ user ->email , $ user ->password )
84+ ->seePageIs ('/register/confirm/awaiting ' )
85+ ->see ('Resend ' )
86+ ->visit ('/books ' )
87+ ->seePageIs ('/register/confirm/awaiting ' )
88+ ->press ('Resend Confirmation Email ' );
89+
90+ // Get confirmation
91+ $ user = $ user ->where ('email ' , '= ' , $ user ->email )->first ();
92+ $ emailConfirmation = EmailConfirmation::where ('user_id ' , '= ' , $ user ->id )->first ();
93+
94+
95+ // Check confirmation email button and confirmation activation.
96+ $ this ->visit ('/register/confirm/ ' . $ emailConfirmation ->token . '/email ' )
97+ ->see ('Email Confirmation ' )
98+ ->click ('Confirm Email ' )
99+ ->seePageIs ('/ ' )
100+ ->see ($ user ->name )
101+ ->notSeeInDatabase ('email_confirmations ' , ['token ' => $ emailConfirmation ->token ])
102+ ->seeInDatabase ('users ' , ['name ' => $ user ->name , 'email ' => $ user ->email , 'email_confirmed ' => true ]);
63103 }
64104
65105 public function testLogout ()
@@ -71,4 +111,30 @@ public function testLogout()
71111 ->visit ('/ ' )
72112 ->seePageIs ('/login ' );
73113 }
114+
115+ /**
116+ * Quickly sets an array of settings.
117+ * @param $settingsArray
118+ */
119+ private function setSettings ($ settingsArray )
120+ {
121+ $ settings = app ('BookStack\Services\SettingService ' );
122+ foreach ($ settingsArray as $ key => $ value ) {
123+ $ settings ->put ($ key , $ value );
124+ }
125+ }
126+
127+ /**
128+ * Perform a login
129+ * @param string $email
130+ * @param string $password
131+ * @return $this
132+ */
133+ private function login ($ email , $ password )
134+ {
135+ return $ this ->visit ('/login ' )
136+ ->type ($ email , '#email ' )
137+ ->type ($ password , '#password ' )
138+ ->press ('Sign In ' );
139+ }
74140}
0 commit comments