]> BookStack Code Mirror - bookstack/commitdiff
Updated and added tests for new default user system
authorDan Brown <redacted>
Thu, 29 Sep 2016 16:07:58 +0000 (17:07 +0100)
committerDan Brown <redacted>
Thu, 29 Sep 2016 16:07:58 +0000 (17:07 +0100)
Closes #138

app/Http/Controllers/PageController.php
app/Http/Controllers/UserController.php
app/Role.php
resources/views/users/edit.blade.php
tests/Auth/AuthTest.php
tests/Auth/LdapTest.php
tests/Permissions/RolesTest.php
tests/PublicActionTest.php [new file with mode: 0644]
tests/PublicViewTest.php [deleted file]
tests/TestCase.php
tests/UserProfileTest.php

index f4cbd33ead660ac430e2df8d2be9f453708f5e58..c2d8e257cb33297e28b49c448075017de8a0ad70 100644 (file)
@@ -106,7 +106,13 @@ class PageController extends Controller
         $this->checkOwnablePermission('page-create', $book);
         $this->setPageTitle('Edit Page Draft');
 
-        return view('pages/edit', ['page' => $draft, 'book' => $book, 'isDraft' => true]);
+        $draftsEnabled = $this->signedIn;
+        return view('pages/edit', [
+            'page' => $draft,
+            'book' => $book,
+            'isDraft' => true,
+            'draftsEnabled' => $draftsEnabled
+        ]);
     }
 
     /**
index 51c5d25bb5ed2de8a2591cf727e75001d0be23da..18ef1a671844fc4943e7df9673f62de42bcc5cea 100644 (file)
@@ -187,7 +187,7 @@ class UserController extends Controller
 
     /**
      * Show the user delete page.
-     * @param $id
+     * @param int $id
      * @return \Illuminate\View\View
      */
     public function delete($id)
@@ -220,6 +220,11 @@ class UserController extends Controller
             return redirect($user->getEditUrl());
         }
 
+        if ($user->system_name === 'public') {
+            session()->flash('error', 'You cannot delete the guest user');
+            return redirect($user->getEditUrl());
+        }
+
         $this->userRepo->destroy($user);
         session()->flash('success', 'User successfully removed');
 
index 8d0a79e753316e8070255da2181c072eaafdf6bc..bf9685ee25d597f35edb3403f82a31d33c220651 100644 (file)
@@ -66,7 +66,7 @@ class Role extends Model
     /**
      * Get the role object for the specified role.
      * @param $roleName
-     * @return mixed
+     * @return Role
      */
     public static function getRole($roleName)
     {
@@ -76,7 +76,7 @@ class Role extends Model
     /**
      * Get the role object for the specified system role.
      * @param $roleName
-     * @return mixed
+     * @return Role
      */
     public static function getSystemRole($roleName)
     {
index d06ec09bc3a3fcde92179766b0eed4b647185120..6cbbdb7f7ec879a776e3ec0087f13618c8a836c0 100644 (file)
@@ -15,7 +15,9 @@
                 </div>
                 <div class="col-sm-4">
                     <p></p>
-                    <a href="{{ baseUrl("/settings/users/{$user->id}/delete") }}" class="neg button float right">Delete User</a>
+                    @if($authMethod !== 'system')
+                        <a href="{{ baseUrl("/settings/users/{$user->id}/delete") }}" class="neg button float right">Delete User</a>
+                    @endif
                 </div>
             </div>
             <div class="row">
index 0affff799919c1e894f0c15dc9fe8254b7b1676c..08d5ef8adb296eaea113da265d7afe2339a4d716 100644 (file)
@@ -146,7 +146,7 @@ class AuthTest extends TestCase
 
     public function test_user_updating()
     {
-        $user = \BookStack\User::all()->last();
+        $user = $this->getNormalUser();
         $password = $user->password;
         $this->asAdmin()
             ->visit('/settings/users')
@@ -162,7 +162,7 @@ class AuthTest extends TestCase
 
     public function test_user_password_update()
     {
-        $user = \BookStack\User::all()->last();
+        $user = $this->getNormalUser();
         $userProfilePage = '/settings/users/' . $user->id;
         $this->asAdmin()
             ->visit($userProfilePage)
index 76fbc662ab89fab61bc50b6e8c79aa8e8d421b68..9573321fba98e5c3bb6fbf06630ddafbfb5a0221 100644 (file)
@@ -108,7 +108,7 @@ class LdapTest extends \TestCase
 
     public function test_user_edit_form()
     {
-        $editUser = User::all()->last();
+        $editUser = $this->getNormalUser();
         $this->asAdmin()->visit('/settings/users/' . $editUser->id)
             ->see('Edit User')
             ->dontSee('Password')
@@ -126,7 +126,7 @@ class LdapTest extends \TestCase
 
     public function test_non_admins_cannot_change_auth_id()
     {
-        $testUser = User::all()->last();
+        $testUser = $this->getNormalUser();
         $this->actingAs($testUser)->visit('/settings/users/' . $testUser->id)
             ->dontSee('External Authentication');
     }
index b64f40dc6af325bc5ebfdbeacf8e24b60cc8c9e6..7a0515fd939795ea5732326cfc016a770a37d009 100644 (file)
@@ -544,27 +544,38 @@ class RolesTest extends TestCase
             ->dontSeeInElement('.book-content', $otherPage->name);
     }
 
-    public function test_public_role_not_visible_in_user_edit_screen()
+    public function test_public_role_visible_in_user_edit_screen()
     {
         $user = \BookStack\User::first();
         $this->asAdmin()->visit('/settings/users/' . $user->id)
             ->seeElement('#roles-admin')
-            ->dontSeeElement('#roles-public');
+            ->seeElement('#roles-public');
     }
 
-    public function test_public_role_not_visible_in_role_listing()
+    public function test_public_role_visible_in_role_listing()
     {
         $this->asAdmin()->visit('/settings/roles')
             ->see('Admin')
-            ->dontSee('Public');
+            ->see('Public');
     }
 
-    public function test_public_role_not_visible_in_default_role_setting()
+    public function test_public_role_visible_in_default_role_setting()
     {
         $this->asAdmin()->visit('/settings')
             ->seeElement('[data-role-name="admin"]')
-            ->dontSeeElement('[data-role-name="public"]');
+            ->seeElement('[data-role-name="public"]');
 
     }
 
+    public function test_public_role_not_deleteable()
+    {
+        $this->asAdmin()->visit('/settings/roles')
+            ->click('Public')
+            ->see('Edit Role')
+            ->click('Delete Role')
+            ->press('Confirm')
+            ->see('Delete Role')
+            ->see('Cannot be deleted');
+    }
+
 }
diff --git a/tests/PublicActionTest.php b/tests/PublicActionTest.php
new file mode 100644 (file)
index 0000000..6851464
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+
+class PublicActionTest extends TestCase
+{
+
+    public function test_app_not_public()
+    {
+        $this->setSettings(['app-public' => 'false']);
+        $book = \BookStack\Book::orderBy('name', 'asc')->first();
+        $this->visit('/books')->seePageIs('/login');
+        $this->visit($book->getUrl())->seePageIs('/login');
+
+        $page = \BookStack\Page::first();
+        $this->visit($page->getUrl())->seePageIs('/login');
+    }
+
+    public function test_books_viewable()
+    {
+        $this->setSettings(['app-public' => 'true']);
+        $books = \BookStack\Book::orderBy('name', 'asc')->take(10)->get();
+        $bookToVisit = $books[1];
+
+        // Check books index page is showing
+        $this->visit('/books')
+            ->seeStatusCode(200)
+            ->see($books[0]->name)
+            // Check individual book page is showing and it's child contents are visible.
+            ->click($bookToVisit->name)
+            ->seePageIs($bookToVisit->getUrl())
+            ->see($bookToVisit->name)
+            ->see($bookToVisit->chapters()->first()->name);
+    }
+
+    public function test_chapters_viewable()
+    {
+        $this->setSettings(['app-public' => 'true']);
+        $chapterToVisit = \BookStack\Chapter::first();
+        $pageToVisit = $chapterToVisit->pages()->first();
+
+        // Check chapters index page is showing
+        $this->visit($chapterToVisit->getUrl())
+            ->seeStatusCode(200)
+            ->see($chapterToVisit->name)
+            // Check individual chapter page is showing and it's child contents are visible.
+            ->see($pageToVisit->name)
+            ->click($pageToVisit->name)
+            ->see($chapterToVisit->book->name)
+            ->see($chapterToVisit->name)
+            ->seePageIs($pageToVisit->getUrl());
+    }
+
+    public function test_public_page_creation()
+    {
+        $this->setSettings(['app-public' => 'true']);
+        $publicRole = \BookStack\Role::getSystemRole('public');
+        // Grant all permissions to public
+        $publicRole->permissions()->detach();
+        foreach (\BookStack\RolePermission::all() as $perm) {
+            $publicRole->attachPermission($perm);
+        }
+        $this->app[\BookStack\Services\PermissionService::class]->buildJointPermissionForRole($publicRole);
+
+        $chapter = \BookStack\Chapter::first();
+        $this->visit($chapter->book->getUrl());
+        $this->visit($chapter->getUrl())
+            ->click('New Page')
+            ->see('Create Page')
+            ->seePageIs($chapter->getUrl('/create-page'));
+
+        $this->submitForm('Continue', [
+            'name' => 'My guest page'
+        ])->seePageIs($chapter->book->getUrl('/page/my-guest-page/edit'));
+
+        $user = \BookStack\User::getDefault();
+        $this->seeInDatabase('pages', [
+            'name' => 'My guest page',
+            'chapter_id' => $chapter->id,
+            'created_by' => $user->id,
+            'updated_by' => $user->id
+        ]);
+    }
+
+}
\ No newline at end of file
diff --git a/tests/PublicViewTest.php b/tests/PublicViewTest.php
deleted file mode 100644 (file)
index 58e39df..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-
-class PublicViewTest extends TestCase
-{
-
-    public function test_books_viewable()
-    {
-        $this->setSettings(['app-public' => 'true']);
-        $books = \BookStack\Book::orderBy('name', 'asc')->take(10)->get();
-        $bookToVisit = $books[1];
-
-        // Check books index page is showing
-        $this->visit('/books')
-            ->seeStatusCode(200)
-            ->see($books[0]->name)
-            // Check individual book page is showing and it's child contents are visible.
-            ->click($bookToVisit->name)
-            ->seePageIs($bookToVisit->getUrl())
-            ->see($bookToVisit->name)
-            ->see($bookToVisit->chapters()->first()->name);
-    }
-
-    public function test_chapters_viewable()
-    {
-        $this->setSettings(['app-public' => 'true']);
-        $chapterToVisit = \BookStack\Chapter::first();
-        $pageToVisit = $chapterToVisit->pages()->first();
-
-        // Check chapters index page is showing
-        $this->visit($chapterToVisit->getUrl())
-            ->seeStatusCode(200)
-            ->see($chapterToVisit->name)
-            // Check individual chapter page is showing and it's child contents are visible.
-            ->see($pageToVisit->name)
-            ->click($pageToVisit->name)
-            ->see($chapterToVisit->book->name)
-            ->see($chapterToVisit->name)
-            ->seePageIs($pageToVisit->getUrl());
-    }
-
-}
\ No newline at end of file
index 6a8c2d732b65dc123de08569c6f4c2da7c2ac3bc..d3620eae0b9d1d985d8baaa988ca69c49c52a5f8 100644 (file)
@@ -66,6 +66,14 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
         return $this->actingAs($this->editor);
     }
 
+    /**
+     * Get a user that's not a system user such as the guest user.
+     */
+    public function getNormalUser()
+    {
+        return \BookStack\User::where('system_name', '=', null)->get()->last();
+    }
+
     /**
      * Quickly sets an array of settings.
      * @param $settingsArray
index 40ae004e981681b66e6fbea20b891c8a8e5fb248..9543adc1d3bc3f6752bffa1eadd27eb26387f064 100644 (file)
@@ -76,5 +76,23 @@ class UserProfileTest extends TestCase
             ->seePageIs('/user/' . $newUser->id)
             ->see($newUser->name);
     }
+
+    public function test_guest_profile_shows_limited_form()
+    {
+        $this->asAdmin()
+            ->visit('/settings/users')
+            ->click('Guest')
+            ->dontSeeElement('#password');
+    }
+
+    public function test_guest_profile_cannot_be_deleted()
+    {
+        $guestUser = \BookStack\User::getDefault();
+        $this->asAdmin()->visit('/settings/users/' . $guestUser->id . '/delete')
+            ->see('Delete User')->see('Guest')
+            ->press('Confirm')
+            ->seePageIs('/settings/users/' . $guestUser->id)
+            ->see('cannot delete the guest user');
+    }
     
 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.