-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Unable to create a new user with bio included (#53)
* chore: add bbcode as a dev dep * fix: unable to provide a bio at the time of user creation * chore: basic function tests, more still needed * Apply fixes from StyleCI * test annotation --------- Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
717651d
commit 168d36b
Showing
11 changed files
with
332 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: FoF User Bio PHP | ||
|
||
on: [workflow_dispatch, push, pull_request] | ||
|
||
jobs: | ||
run: | ||
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main | ||
with: | ||
enable_backend_testing: true | ||
|
||
backend_directory: . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ js/node_modules | |
vendor/ | ||
composer.lock | ||
js/dist | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/user-bio. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\UserBio\tests\integration\api; | ||
|
||
use Carbon\Carbon; | ||
use Flarum\Testing\integration\RetrievesAuthorizedUsers; | ||
use Flarum\Testing\integration\TestCase; | ||
use Flarum\User\User; | ||
|
||
class BioTest extends TestCase | ||
{ | ||
use RetrievesAuthorizedUsers; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setting('mail_driver', 'log'); | ||
|
||
$this->extension('fof-user-bio'); | ||
|
||
$this->prepareDatabase([ | ||
'users' => [ | ||
$this->normalUser(), | ||
[ | ||
'id' => 3, | ||
'username' => 'normal2', | ||
'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" | ||
'email' => '[email protected]', | ||
'is_email_confirmed' => 1, | ||
'last_seen_at' => Carbon::now()->subSecond(), | ||
'bio' => 'This is a test bio for normal2.', | ||
], | ||
[ | ||
'id' => 4, | ||
'username' => 'normal3', | ||
'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" | ||
'email' => '[email protected]', | ||
'is_email_confirmed' => 1, | ||
'last_seen_at' => Carbon::now()->subHour(), | ||
], | ||
], | ||
]); | ||
} | ||
|
||
protected function giveNormalUsersEditOwnPerms() | ||
{ | ||
$this->prepareDatabase([ | ||
'group_permission' => [ | ||
['permission' => 'fof-user-bio.editOwn', 'group_id' => 3], | ||
], | ||
]); | ||
} | ||
|
||
protected function giveNormalUserViewBioPerms() | ||
{ | ||
$this->prepareDatabase([ | ||
'group_permission' => [ | ||
['permission' => 'fof-user-bio.view', 'group_id' => 3], | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function admin_can_create_user_with_bio() | ||
{ | ||
$response = $this->send( | ||
$this->request( | ||
'POST', | ||
'/api/users', | ||
[ | ||
'authenticatedAs' => 1, | ||
'json' => [ | ||
'data' => [ | ||
'attributes' => [ | ||
'username' => 'test', | ||
'password' => 'too-obscure', | ||
'email' => '[email protected]', | ||
'bio' => 'This is a test bio.', | ||
], | ||
], | ||
], | ||
] | ||
) | ||
); | ||
|
||
$this->assertEquals(201, $response->getStatusCode()); | ||
|
||
/** @var User $user */ | ||
$user = User::where('username', 'test')->firstOrFail(); | ||
|
||
$this->assertEquals(0, $user->is_email_confirmed); | ||
$this->assertEquals('test', $user->username); | ||
$this->assertEquals('[email protected]', $user->email); | ||
$this->assertEquals('This is a test bio.', $user->bio); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function cannot_update_own_bio_when_permission_not_granted() | ||
{ | ||
$response = $this->send( | ||
$this->request( | ||
'PATCH', | ||
'/api/users/2', | ||
[ | ||
'authenticatedAs' => 2, | ||
'json' => [ | ||
'data' => [ | ||
'attributes' => [ | ||
'bio' => 'This is a test bio.', | ||
], | ||
], | ||
], | ||
] | ||
) | ||
); | ||
|
||
$this->assertEquals(403, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_update_own_user_with_added_bio_when_permission_granted() | ||
{ | ||
$this->giveNormalUsersEditOwnPerms(); | ||
|
||
$response = $this->send( | ||
$this->request( | ||
'PATCH', | ||
'/api/users/2', | ||
[ | ||
'authenticatedAs' => 2, | ||
'json' => [ | ||
'data' => [ | ||
'attributes' => [ | ||
'bio' => 'This is a test bio.', | ||
], | ||
], | ||
], | ||
] | ||
) | ||
); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
|
||
/** @var User $user */ | ||
$user = User::where('id', 2)->firstOrFail(); | ||
|
||
$this->assertEquals('This is a test bio.', $user->bio); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function normal_user_cannot_see_bio_of_other_user_without_permission() | ||
{ | ||
$response = $this->send( | ||
$this->request( | ||
'GET', | ||
'/api/users/3', | ||
[ | ||
'authenticatedAs' => 2, | ||
] | ||
) | ||
); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
|
||
$this->assertArrayNotHasKey('bio', json_decode($response->getBody()->getContents(), true)['data']['attributes']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function normal_user_can_see_others_bio_with_permission() | ||
{ | ||
$this->giveNormalUserViewBioPerms(); | ||
|
||
$response = $this->send( | ||
$this->request( | ||
'GET', | ||
'/api/users/3', | ||
[ | ||
'authenticatedAs' => 2, | ||
] | ||
) | ||
); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
|
||
$data = json_decode($response->getBody()->getContents(), true); | ||
|
||
$this->assertArrayHasKey('bio', $data['data']['attributes']); | ||
$this->assertEquals('This is a test bio for normal2.', $data['data']['attributes']['bio']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/user-bio. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Flarum\Testing\integration\Setup\SetupScript; | ||
|
||
require __DIR__.'/../../vendor/autoload.php'; | ||
|
||
$setup = new SetupScript(); | ||
|
||
$setup->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="true" | ||
stopOnFailure="false" | ||
> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">../src/</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Flarum Integration Tests"> | ||
<directory suffix="Test.php">./integration</directory> | ||
<exclude>./integration/tmp</exclude> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">../src/</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Flarum Unit Tests"> | ||
<directory suffix="Test.php">./unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
<listeners> | ||
<listener class="\Mockery\Adapter\Phpunit\TestListener" /> | ||
</listeners> | ||
</phpunit> |
Empty file.