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