2
2
3
3
namespace Tests \Commands ;
4
4
5
+ use BookStack \Users \Models \Role ;
5
6
use BookStack \Users \Models \User ;
7
+ use Illuminate \Support \Facades \Artisan ;
6
8
use Illuminate \Support \Facades \Auth ;
9
+ use Illuminate \Support \Facades \Hash ;
7
10
use Tests \TestCase ;
8
11
9
12
class CreateAdminCommandTest extends TestCase
10
13
{
11
14
public function test_standard_command_usage ()
12
15
{
13
16
$ this ->artisan ('bookstack:create-admin ' , [
14
-
15
- '--name ' => 'Admin Test ' ,
17
+
18
+ '--name ' => 'Admin Test ' ,
16
19
'--password ' => 'testing-4 ' ,
17
20
])->assertExitCode (0 );
18
21
19
22
$ this ->assertDatabaseHas ('users ' , [
20
23
21
- 'name ' => 'Admin Test ' ,
24
+ 'name ' => 'Admin Test ' ,
22
25
]);
23
26
24
27
/** @var User $user */
@@ -30,14 +33,14 @@ public function test_standard_command_usage()
30
33
public function test_providing_external_auth_id ()
31
34
{
32
35
$ this ->artisan ('bookstack:create-admin ' , [
33
-
34
- '--name ' => 'Admin Test ' ,
36
+
37
+ '--name ' => 'Admin Test ' ,
35
38
'--external-auth-id ' => 'xX_admin_Xx ' ,
36
39
])->assertExitCode (0 );
37
40
38
41
$ this ->assertDatabaseHas ('users ' , [
39
-
40
- 'name ' => 'Admin Test ' ,
42
+
43
+ 'name ' => 'Admin Test ' ,
41
44
'external_auth_id ' => 'xX_admin_Xx ' ,
42
45
]);
43
46
@@ -50,14 +53,178 @@ public function test_password_required_if_external_auth_id_not_given()
50
53
{
51
54
$ this ->artisan ('bookstack:create-admin ' , [
52
55
53
- '--name ' => 'Admin Test ' ,
56
+ '--name ' => 'Admin Test ' ,
54
57
])->expectsQuestion ('Please specify a password for the new admin user (8 characters min) ' , 'hunter2000 ' )
55
58
->assertExitCode (0 );
56
59
57
60
$ this ->assertDatabaseHas ('users ' , [
58
61
59
- 'name ' => 'Admin Test ' ,
62
+ 'name ' => 'Admin Test ' ,
60
63
]);
61
64
$ this ->
assertTrue (Auth::
attempt ([
'email ' =>
'[email protected] ' ,
'password ' =>
'hunter2000 ' ]));
62
65
}
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
+ }
63
230
}
0 commit comments