]> BookStack Code Mirror - bookstack/blobdiff - tests/User/UserApiTokenTest.php
CM6: Further fixes/improvements after testing
[bookstack] / tests / User / UserApiTokenTest.php
index df686dd77df953423a103d8caa57313539dd832e..93070b712188e325f0ddc94a3653d600394702c5 100644 (file)
@@ -1,4 +1,6 @@
-<?php namespace Tests\User;
+<?php
+
+namespace Tests\User;
 
 use BookStack\Actions\ActivityType;
 use BookStack\Api\ApiToken;
@@ -7,20 +9,19 @@ use Tests\TestCase;
 
 class UserApiTokenTest extends TestCase
 {
-
     protected $testTokenData = [
-        'name' => 'My test API token',
+        'name'       => 'My test API token',
         'expires_at' => '2050-04-01',
     ];
 
     public function test_tokens_section_not_visible_without_access_api_permission()
     {
-        $user = $this->getViewer();
+        $user = $this->users->viewer();
 
         $resp = $this->actingAs($user)->get($user->getEditUrl());
         $resp->assertDontSeeText('API Tokens');
 
-        $this->giveUserPermissions($user, ['access-api']);
+        $this->permissions->grantUserRolePermissions($user, ['access-api']);
 
         $resp = $this->actingAs($user)->get($user->getEditUrl());
         $resp->assertSeeText('API Tokens');
@@ -29,9 +30,9 @@ class UserApiTokenTest extends TestCase
 
     public function test_those_with_manage_users_can_view_other_user_tokens_but_not_create()
     {
-        $viewer = $this->getViewer();
-        $editor = $this->getEditor();
-        $this->giveUserPermissions($viewer, ['users-manage']);
+        $viewer = $this->users->viewer();
+        $editor = $this->users->editor();
+        $this->permissions->grantUserRolePermissions($viewer, ['users-manage']);
 
         $resp = $this->actingAs($viewer)->get($editor->getEditUrl());
         $resp->assertSeeText('API Tokens');
@@ -40,7 +41,7 @@ class UserApiTokenTest extends TestCase
 
     public function test_create_api_token()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
 
         $resp = $this->asAdmin()->get($editor->getEditUrl('/create-api-token'));
         $resp->assertStatus(200);
@@ -51,8 +52,8 @@ class UserApiTokenTest extends TestCase
         $token = ApiToken::query()->latest()->first();
         $resp->assertRedirect($editor->getEditUrl('/api-tokens/' . $token->id));
         $this->assertDatabaseHas('api_tokens', [
-            'user_id' => $editor->id,
-            'name' => $this->testTokenData['name'],
+            'user_id'    => $editor->id,
+            'name'       => $this->testTokenData['name'],
             'expires_at' => $this->testTokenData['expires_at'],
         ]);
 
@@ -73,7 +74,7 @@ class UserApiTokenTest extends TestCase
 
     public function test_create_with_no_expiry_sets_expiry_hundred_years_away()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
         $this->asAdmin()->post($editor->getEditUrl('/create-api-token'), ['name' => 'No expiry token', 'expires_at' => '']);
         $token = ApiToken::query()->latest()->first();
 
@@ -81,26 +82,26 @@ class UserApiTokenTest extends TestCase
         $under = Carbon::now()->addYears(99);
         $this->assertTrue(
             ($token->expires_at < $over && $token->expires_at > $under),
-            "Token expiry set at 100 years in future"
+            'Token expiry set at 100 years in future'
         );
     }
 
     public function test_created_token_displays_on_profile_page()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
         $this->asAdmin()->post($editor->getEditUrl('/create-api-token'), $this->testTokenData);
         $token = ApiToken::query()->latest()->first();
 
         $resp = $this->get($editor->getEditUrl());
-        $resp->assertElementExists('#api_tokens');
-        $resp->assertElementContains('#api_tokens', $token->name);
-        $resp->assertElementContains('#api_tokens', $token->token_id);
-        $resp->assertElementContains('#api_tokens', $token->expires_at->format('Y-m-d'));
+        $this->withHtml($resp)->assertElementExists('#api_tokens');
+        $this->withHtml($resp)->assertElementContains('#api_tokens', $token->name);
+        $this->withHtml($resp)->assertElementContains('#api_tokens', $token->token_id);
+        $this->withHtml($resp)->assertElementContains('#api_tokens', $token->expires_at->format('Y-m-d'));
     }
 
     public function test_secret_shown_once_after_creation()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
         $resp = $this->asAdmin()->followingRedirects()->post($editor->getEditUrl('/create-api-token'), $this->testTokenData);
         $resp->assertSeeText('Token Secret');
 
@@ -113,11 +114,11 @@ class UserApiTokenTest extends TestCase
 
     public function test_token_update()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
         $this->asAdmin()->post($editor->getEditUrl('/create-api-token'), $this->testTokenData);
         $token = ApiToken::query()->latest()->first();
         $updateData = [
-            'name' => 'My updated token',
+            'name'       => 'My updated token',
             'expires_at' => '2011-01-01',
         ];
 
@@ -131,12 +132,12 @@ class UserApiTokenTest extends TestCase
 
     public function test_token_update_with_blank_expiry_sets_to_hundred_years_away()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
         $this->asAdmin()->post($editor->getEditUrl('/create-api-token'), $this->testTokenData);
         $token = ApiToken::query()->latest()->first();
 
         $resp = $this->put($editor->getEditUrl('/api-tokens/' . $token->id), [
-            'name' => 'My updated token',
+            'name'       => 'My updated token',
             'expires_at' => '',
         ]);
         $token->refresh();
@@ -145,13 +146,13 @@ class UserApiTokenTest extends TestCase
         $under = Carbon::now()->addYears(99);
         $this->assertTrue(
             ($token->expires_at < $over && $token->expires_at > $under),
-            "Token expiry set at 100 years in future"
+            'Token expiry set at 100 years in future'
         );
     }
 
     public function test_token_delete()
     {
-        $editor = $this->getEditor();
+        $editor = $this->users->editor();
         $this->asAdmin()->post($editor->getEditUrl('/create-api-token'), $this->testTokenData);
         $token = ApiToken::query()->latest()->first();
 
@@ -160,7 +161,7 @@ class UserApiTokenTest extends TestCase
         $resp = $this->get($tokenUrl . '/delete');
         $resp->assertSeeText('Delete Token');
         $resp->assertSeeText($token->name);
-        $resp->assertElementExists('form[action="'.$tokenUrl.'"]');
+        $this->withHtml($resp)->assertElementExists('form[action="' . $tokenUrl . '"]');
 
         $resp = $this->delete($tokenUrl);
         $resp->assertRedirect($editor->getEditUrl('#api_tokens'));
@@ -170,9 +171,9 @@ class UserApiTokenTest extends TestCase
 
     public function test_user_manage_can_delete_token_without_api_permission_themselves()
     {
-        $viewer = $this->getViewer();
-        $editor = $this->getEditor();
-        $this->giveUserPermissions($editor, ['users-manage']);
+        $viewer = $this->users->viewer();
+        $editor = $this->users->editor();
+        $this->permissions->grantUserRolePermissions($editor, ['users-manage']);
 
         $this->asAdmin()->post($viewer->getEditUrl('/create-api-token'), $this->testTokenData);
         $token = ApiToken::query()->latest()->first();
@@ -185,5 +186,4 @@ class UserApiTokenTest extends TestCase
         $resp->assertRedirect($viewer->getEditUrl('#api_tokens'));
         $this->assertDatabaseMissing('api_tokens', ['id' => $token->id]);
     }
-
-}
\ No newline at end of file
+}