Dan Brown

Updated and added tests for new default user system

Closes #138
...@@ -106,7 +106,13 @@ class PageController extends Controller ...@@ -106,7 +106,13 @@ class PageController extends Controller
106 $this->checkOwnablePermission('page-create', $book); 106 $this->checkOwnablePermission('page-create', $book);
107 $this->setPageTitle('Edit Page Draft'); 107 $this->setPageTitle('Edit Page Draft');
108 108
109 - return view('pages/edit', ['page' => $draft, 'book' => $book, 'isDraft' => true]); 109 + $draftsEnabled = $this->signedIn;
110 + return view('pages/edit', [
111 + 'page' => $draft,
112 + 'book' => $book,
113 + 'isDraft' => true,
114 + 'draftsEnabled' => $draftsEnabled
115 + ]);
110 } 116 }
111 117
112 /** 118 /**
......
...@@ -187,7 +187,7 @@ class UserController extends Controller ...@@ -187,7 +187,7 @@ class UserController extends Controller
187 187
188 /** 188 /**
189 * Show the user delete page. 189 * Show the user delete page.
190 - * @param $id 190 + * @param int $id
191 * @return \Illuminate\View\View 191 * @return \Illuminate\View\View
192 */ 192 */
193 public function delete($id) 193 public function delete($id)
...@@ -220,6 +220,11 @@ class UserController extends Controller ...@@ -220,6 +220,11 @@ class UserController extends Controller
220 return redirect($user->getEditUrl()); 220 return redirect($user->getEditUrl());
221 } 221 }
222 222
223 + if ($user->system_name === 'public') {
224 + session()->flash('error', 'You cannot delete the guest user');
225 + return redirect($user->getEditUrl());
226 + }
227 +
223 $this->userRepo->destroy($user); 228 $this->userRepo->destroy($user);
224 session()->flash('success', 'User successfully removed'); 229 session()->flash('success', 'User successfully removed');
225 230
......
...@@ -66,7 +66,7 @@ class Role extends Model ...@@ -66,7 +66,7 @@ class Role extends Model
66 /** 66 /**
67 * Get the role object for the specified role. 67 * Get the role object for the specified role.
68 * @param $roleName 68 * @param $roleName
69 - * @return mixed 69 + * @return Role
70 */ 70 */
71 public static function getRole($roleName) 71 public static function getRole($roleName)
72 { 72 {
...@@ -76,7 +76,7 @@ class Role extends Model ...@@ -76,7 +76,7 @@ class Role extends Model
76 /** 76 /**
77 * Get the role object for the specified system role. 77 * Get the role object for the specified system role.
78 * @param $roleName 78 * @param $roleName
79 - * @return mixed 79 + * @return Role
80 */ 80 */
81 public static function getSystemRole($roleName) 81 public static function getSystemRole($roleName)
82 { 82 {
......
...@@ -15,7 +15,9 @@ ...@@ -15,7 +15,9 @@
15 </div> 15 </div>
16 <div class="col-sm-4"> 16 <div class="col-sm-4">
17 <p></p> 17 <p></p>
18 + @if($authMethod !== 'system')
18 <a href="{{ baseUrl("/settings/users/{$user->id}/delete") }}" class="neg button float right">Delete User</a> 19 <a href="{{ baseUrl("/settings/users/{$user->id}/delete") }}" class="neg button float right">Delete User</a>
20 + @endif
19 </div> 21 </div>
20 </div> 22 </div>
21 <div class="row"> 23 <div class="row">
......
...@@ -146,7 +146,7 @@ class AuthTest extends TestCase ...@@ -146,7 +146,7 @@ class AuthTest extends TestCase
146 146
147 public function test_user_updating() 147 public function test_user_updating()
148 { 148 {
149 - $user = \BookStack\User::all()->last(); 149 + $user = $this->getNormalUser();
150 $password = $user->password; 150 $password = $user->password;
151 $this->asAdmin() 151 $this->asAdmin()
152 ->visit('/settings/users') 152 ->visit('/settings/users')
...@@ -162,7 +162,7 @@ class AuthTest extends TestCase ...@@ -162,7 +162,7 @@ class AuthTest extends TestCase
162 162
163 public function test_user_password_update() 163 public function test_user_password_update()
164 { 164 {
165 - $user = \BookStack\User::all()->last(); 165 + $user = $this->getNormalUser();
166 $userProfilePage = '/settings/users/' . $user->id; 166 $userProfilePage = '/settings/users/' . $user->id;
167 $this->asAdmin() 167 $this->asAdmin()
168 ->visit($userProfilePage) 168 ->visit($userProfilePage)
......
...@@ -108,7 +108,7 @@ class LdapTest extends \TestCase ...@@ -108,7 +108,7 @@ class LdapTest extends \TestCase
108 108
109 public function test_user_edit_form() 109 public function test_user_edit_form()
110 { 110 {
111 - $editUser = User::all()->last(); 111 + $editUser = $this->getNormalUser();
112 $this->asAdmin()->visit('/settings/users/' . $editUser->id) 112 $this->asAdmin()->visit('/settings/users/' . $editUser->id)
113 ->see('Edit User') 113 ->see('Edit User')
114 ->dontSee('Password') 114 ->dontSee('Password')
...@@ -126,7 +126,7 @@ class LdapTest extends \TestCase ...@@ -126,7 +126,7 @@ class LdapTest extends \TestCase
126 126
127 public function test_non_admins_cannot_change_auth_id() 127 public function test_non_admins_cannot_change_auth_id()
128 { 128 {
129 - $testUser = User::all()->last(); 129 + $testUser = $this->getNormalUser();
130 $this->actingAs($testUser)->visit('/settings/users/' . $testUser->id) 130 $this->actingAs($testUser)->visit('/settings/users/' . $testUser->id)
131 ->dontSee('External Authentication'); 131 ->dontSee('External Authentication');
132 } 132 }
......
...@@ -544,27 +544,38 @@ class RolesTest extends TestCase ...@@ -544,27 +544,38 @@ class RolesTest extends TestCase
544 ->dontSeeInElement('.book-content', $otherPage->name); 544 ->dontSeeInElement('.book-content', $otherPage->name);
545 } 545 }
546 546
547 - public function test_public_role_not_visible_in_user_edit_screen() 547 + public function test_public_role_visible_in_user_edit_screen()
548 { 548 {
549 $user = \BookStack\User::first(); 549 $user = \BookStack\User::first();
550 $this->asAdmin()->visit('/settings/users/' . $user->id) 550 $this->asAdmin()->visit('/settings/users/' . $user->id)
551 ->seeElement('#roles-admin') 551 ->seeElement('#roles-admin')
552 - ->dontSeeElement('#roles-public'); 552 + ->seeElement('#roles-public');
553 } 553 }
554 554
555 - public function test_public_role_not_visible_in_role_listing() 555 + public function test_public_role_visible_in_role_listing()
556 { 556 {
557 $this->asAdmin()->visit('/settings/roles') 557 $this->asAdmin()->visit('/settings/roles')
558 ->see('Admin') 558 ->see('Admin')
559 - ->dontSee('Public'); 559 + ->see('Public');
560 } 560 }
561 561
562 - public function test_public_role_not_visible_in_default_role_setting() 562 + public function test_public_role_visible_in_default_role_setting()
563 { 563 {
564 $this->asAdmin()->visit('/settings') 564 $this->asAdmin()->visit('/settings')
565 ->seeElement('[data-role-name="admin"]') 565 ->seeElement('[data-role-name="admin"]')
566 - ->dontSeeElement('[data-role-name="public"]'); 566 + ->seeElement('[data-role-name="public"]');
567 567
568 } 568 }
569 569
570 + public function test_public_role_not_deleteable()
571 + {
572 + $this->asAdmin()->visit('/settings/roles')
573 + ->click('Public')
574 + ->see('Edit Role')
575 + ->click('Delete Role')
576 + ->press('Confirm')
577 + ->see('Delete Role')
578 + ->see('Cannot be deleted');
579 + }
580 +
570 } 581 }
......
1 <?php 1 <?php
2 2
3 -class PublicViewTest extends TestCase 3 +class PublicActionTest extends TestCase
4 { 4 {
5 5
6 + public function test_app_not_public()
7 + {
8 + $this->setSettings(['app-public' => 'false']);
9 + $book = \BookStack\Book::orderBy('name', 'asc')->first();
10 + $this->visit('/books')->seePageIs('/login');
11 + $this->visit($book->getUrl())->seePageIs('/login');
12 +
13 + $page = \BookStack\Page::first();
14 + $this->visit($page->getUrl())->seePageIs('/login');
15 + }
16 +
6 public function test_books_viewable() 17 public function test_books_viewable()
7 { 18 {
8 $this->setSettings(['app-public' => 'true']); 19 $this->setSettings(['app-public' => 'true']);
...@@ -38,4 +49,35 @@ class PublicViewTest extends TestCase ...@@ -38,4 +49,35 @@ class PublicViewTest extends TestCase
38 ->seePageIs($pageToVisit->getUrl()); 49 ->seePageIs($pageToVisit->getUrl());
39 } 50 }
40 51
52 + public function test_public_page_creation()
53 + {
54 + $this->setSettings(['app-public' => 'true']);
55 + $publicRole = \BookStack\Role::getSystemRole('public');
56 + // Grant all permissions to public
57 + $publicRole->permissions()->detach();
58 + foreach (\BookStack\RolePermission::all() as $perm) {
59 + $publicRole->attachPermission($perm);
60 + }
61 + $this->app[\BookStack\Services\PermissionService::class]->buildJointPermissionForRole($publicRole);
62 +
63 + $chapter = \BookStack\Chapter::first();
64 + $this->visit($chapter->book->getUrl());
65 + $this->visit($chapter->getUrl())
66 + ->click('New Page')
67 + ->see('Create Page')
68 + ->seePageIs($chapter->getUrl('/create-page'));
69 +
70 + $this->submitForm('Continue', [
71 + 'name' => 'My guest page'
72 + ])->seePageIs($chapter->book->getUrl('/page/my-guest-page/edit'));
73 +
74 + $user = \BookStack\User::getDefault();
75 + $this->seeInDatabase('pages', [
76 + 'name' => 'My guest page',
77 + 'chapter_id' => $chapter->id,
78 + 'created_by' => $user->id,
79 + 'updated_by' => $user->id
80 + ]);
81 + }
82 +
41 } 83 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -67,6 +67,14 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase ...@@ -67,6 +67,14 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
67 } 67 }
68 68
69 /** 69 /**
70 + * Get a user that's not a system user such as the guest user.
71 + */
72 + public function getNormalUser()
73 + {
74 + return \BookStack\User::where('system_name', '=', null)->get()->last();
75 + }
76 +
77 + /**
70 * Quickly sets an array of settings. 78 * Quickly sets an array of settings.
71 * @param $settingsArray 79 * @param $settingsArray
72 */ 80 */
......
...@@ -77,4 +77,22 @@ class UserProfileTest extends TestCase ...@@ -77,4 +77,22 @@ class UserProfileTest extends TestCase
77 ->see($newUser->name); 77 ->see($newUser->name);
78 } 78 }
79 79
80 + public function test_guest_profile_shows_limited_form()
81 + {
82 + $this->asAdmin()
83 + ->visit('/settings/users')
84 + ->click('Guest')
85 + ->dontSeeElement('#password');
86 + }
87 +
88 + public function test_guest_profile_cannot_be_deleted()
89 + {
90 + $guestUser = \BookStack\User::getDefault();
91 + $this->asAdmin()->visit('/settings/users/' . $guestUser->id . '/delete')
92 + ->see('Delete User')->see('Guest')
93 + ->press('Confirm')
94 + ->seePageIs('/settings/users/' . $guestUser->id)
95 + ->see('cannot delete the guest user');
96 + }
97 +
80 } 98 }
......