22
33namespace Tests \Commands ;
44
5+ use BookStack \Users \Models \Role ;
56use BookStack \Users \Models \User ;
7+ use Illuminate \Support \Facades \Artisan ;
68use Illuminate \Support \Facades \Auth ;
9+ use Illuminate \Support \Facades \Hash ;
710use Tests \TestCase ;
811
912class CreateAdminCommandTest extends TestCase
1013{
1114 public function test_standard_command_usage ()
1215 {
1316 $ this ->artisan ('bookstack:create-admin ' , [
14- 15- '--name ' => 'Admin Test ' ,
17+ 18+ '--name ' => 'Admin Test ' ,
1619 '--password ' => 'testing-4 ' ,
1720 ])->assertExitCode (0 );
1821
1922 $ this ->assertDatabaseHas ('users ' , [
202321- 'name ' => 'Admin Test ' ,
24+ 'name ' => 'Admin Test ' ,
2225 ]);
2326
2427 /** @var User $user */
@@ -30,14 +33,14 @@ public function test_standard_command_usage()
3033 public function test_providing_external_auth_id ()
3134 {
3235 $ this ->artisan ('bookstack:create-admin ' , [
33- 34- '--name ' => 'Admin Test ' ,
36+ 37+ '--name ' => 'Admin Test ' ,
3538 '--external-auth-id ' => 'xX_admin_Xx ' ,
3639 ])->assertExitCode (0 );
3740
3841 $ this ->assertDatabaseHas ('users ' , [
39- 40- 'name ' => 'Admin Test ' ,
42+ 43+ 'name ' => 'Admin Test ' ,
4144 'external_auth_id ' => 'xX_admin_Xx ' ,
4245 ]);
4346
@@ -50,14 +53,178 @@ public function test_password_required_if_external_auth_id_not_given()
5053 {
5154 $ this ->artisan ('bookstack:create-admin ' , [
525553- '--name ' => 'Admin Test ' ,
56+ '--name ' => 'Admin Test ' ,
5457 ])->expectsQuestion ('Please specify a password for the new admin user (8 characters min) ' , 'hunter2000 ' )
5558 ->assertExitCode (0 );
5659
5760 $ this ->assertDatabaseHas ('users ' , [
586159- 'name ' => 'Admin Test ' ,
62+ 'name ' => 'Admin Test ' ,
6063 ]);
6164 $ this ->
assertTrue (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'hunter2000 ' ]));
6265 }
66+
67+ public function test_generate_password_option ()
68+ {
69+ $ this ->withoutMockingConsoleOutput ()
70+ ->artisan ('bookstack:create-admin ' , [
71+ 72+ '--name ' => 'Admin Test ' ,
73+ '--generate-password ' => true ,
74+ ]);
75+
76+ $ output = trim (Artisan::output ());
77+ $ this ->assertMatchesRegularExpression ('/^[a-zA-Z0-9]{32}$/ ' , $ output );
78+
79+ $ user = User::
query ()->
where (
'email ' ,
'= ' ,
'[email protected] ' )->
first ();
80+ $ this ->assertTrue (Hash::check ($ output , $ user ->password ));
81+ }
82+
83+ public function test_initial_option_updates_default_admin ()
84+ {
85+ $ defaultAdmin = User::
query ()->
where (
'email ' ,
'= ' ,
'[email protected] ' )->
first ();
86+
87+ $ this ->artisan ('bookstack:create-admin ' , [
88+ 89+ '--name ' => 'Admin Test ' ,
90+ '--password ' => 'testing-7 ' ,
91+ '--initial ' => true ,
92+ ])->expectsOutput ('The default admin user has been updated with the provided details! ' )
93+ ->assertExitCode (0 );
94+
95+ $ defaultAdmin ->refresh ();
96+
97+ $ this ->
assertEquals (
'[email protected] ' ,
$ defaultAdmin->
email );
98+ }
99+
100+ public function test_initial_option_does_not_update_if_only_non_default_admin_exists ()
101+ {
102+ $ defaultAdmin = User::
query ()->
where (
'email ' ,
'= ' ,
'[email protected] ' )->
first ();
103+ $ defaultAdmin->
email =
'[email protected] ' ;
104+ $ defaultAdmin ->save ();
105+
106+ $ this ->artisan ('bookstack:create-admin ' , [
107+ 108+ '--name ' => 'Admin Test ' ,
109+ '--password ' => 'testing-7 ' ,
110+ '--initial ' => true ,
111+ ])->expectsOutput ('Non-default admin user already exists. Skipping creation of new admin user. ' )
112+ ->assertExitCode (0 );
113+
114+ $ defaultAdmin ->refresh ();
115+
116+ $ this ->
assertEquals (
'[email protected] ' ,
$ defaultAdmin->
email );
117+ }
118+
119+ public function test_initial_option_updates_creates_new_admin_if_none_exists ()
120+ {
121+ $ adminRole = Role::getSystemRole ('admin ' );
122+ $ adminRole ->users ()->delete ();
123+ $ this ->assertEquals (0 , $ adminRole ->users ()->count ());
124+
125+ $ this ->artisan ('bookstack:create-admin ' , [
126+ 127+ '--name ' => 'My initial admin ' ,
128+ '--password ' => 'testing-7 ' ,
129+ '--initial ' => true ,
130+ ])->
expectsOutput (
"Admin account with email \"[email protected] \" successfully created! " )
131+ ->assertExitCode (0 );
132+
133+ $ this ->assertEquals (1 , $ adminRole ->users ()->count ());
134+ $ this ->assertDatabaseHas ('users ' , [
135+ 136+ 'name ' => 'My initial admin ' ,
137+ ]);
138+ }
139+
140+ public function test_initial_rerun_does_not_error_but_skips ()
141+ {
142+ $ adminRole = Role::getSystemRole ('admin ' );
143+ $ adminRole ->users ()->delete ();
144+
145+ $ this ->artisan ('bookstack:create-admin ' , [
146+ 147+ '--name ' => 'My initial admin ' ,
148+ '--password ' => 'testing-7 ' ,
149+ '--initial ' => true ,
150+ ])->
expectsOutput (
"Admin account with email \"[email protected] \" successfully created! " )
151+ ->assertExitCode (0 );
152+
153+ $ this ->artisan ('bookstack:create-admin ' , [
154+ 155+ '--name ' => 'My initial admin ' ,
156+ '--password ' => 'testing-7 ' ,
157+ '--initial ' => true ,
158+ ])->expectsOutput ("Non-default admin user already exists. Skipping creation of new admin user. " )
159+ ->assertExitCode (0 );
160+ }
161+
162+ public function test_initial_option_creation_errors_if_email_already_exists ()
163+ {
164+ $ adminRole = Role::getSystemRole ('admin ' );
165+ $ adminRole ->users ()->delete ();
166+ $ editor = $ this ->users ->editor ();
167+
168+ $ this ->artisan ('bookstack:create-admin ' , [
169+ '--email ' => $ editor ->email ,
170+ '--name ' => 'My initial admin ' ,
171+ '--password ' => 'testing-7 ' ,
172+ '--initial ' => true ,
173+ ])->expectsOutput ("Could not create admin account. " )
174+ ->expectsOutput ("An account with the email address \"{$ editor ->email }\" already exists. " )
175+ ->assertExitCode (1 );
176+ }
177+
178+ public function test_initial_option_updating_errors_if_email_already_exists ()
179+ {
180+ $ editor = $ this ->users ->editor ();
181+ $ defaultAdmin = User::
query ()->
where (
'email ' ,
'= ' ,
'[email protected] ' )->
first ();
182+ $ this ->assertNotNull ($ defaultAdmin );
183+
184+ $ this ->artisan ('bookstack:create-admin ' , [
185+ '--email ' => $ editor ->email ,
186+ '--name ' => 'My initial admin ' ,
187+ '--password ' => 'testing-7 ' ,
188+ '--initial ' => true ,
189+ ])->expectsOutput ("Could not create admin account. " )
190+ ->expectsOutput ("An account with the email address \"{$ editor ->email }\" already exists. " )
191+ ->assertExitCode (1 );
192+ }
193+
194+ public function test_initial_option_does_not_require_name_or_email_to_be_passed ()
195+ {
196+ $ adminRole = Role::getSystemRole ('admin ' );
197+ $ adminRole ->users ()->delete ();
198+ $ this ->assertEquals (0 , $ adminRole ->users ()->count ());
199+
200+ $ this ->artisan ('bookstack:create-admin ' , [
201+ '--generate-password ' => true ,
202+ '--initial ' => true ,
203+ ])->assertExitCode (0 );
204+
205+ $ this ->assertEquals (1 , $ adminRole ->users ()->count ());
206+ $ this ->assertDatabaseHas ('users ' , [
207+ 208+ 'name ' => 'Admin ' ,
209+ ]);
210+ }
211+
212+ public function test_initial_option_updating_existing_user_with_generate_password_only_outputs_password ()
213+ {
214+ $ defaultAdmin = User::
query ()->
where (
'email ' ,
'= ' ,
'[email protected] ' )->
first ();
215+
216+ $ this ->withoutMockingConsoleOutput ()
217+ ->artisan ('bookstack:create-admin ' , [
218+ 219+ '--name ' => 'Admin Test ' ,
220+ '--generate-password ' => true ,
221+ '--initial ' => true ,
222+ ]);
223+
224+ $ output = Artisan::output ();
225+ $ this ->assertMatchesRegularExpression ('/^[a-zA-Z0-9]{32}$/ ' , $ output );
226+
227+ $ defaultAdmin ->refresh ();
228+ $ this ->
assertEquals (
'[email protected] ' ,
$ defaultAdmin->
email );
229+ }
63230}
0 commit comments