Skip to content

Commit

Permalink
fix: Unable to create a new user with bio included (#53)
Browse files Browse the repository at this point in the history
* 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
imorland and StyleCIBot committed Oct 3, 2023
1 parent 717651d commit 168d36b
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 11 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/backend.yml
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: .
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Javascript
name: FoF User Bio JS

on: [workflow_dispatch, push, pull_request]

Expand All @@ -8,11 +8,12 @@ jobs:
with:
enable_bundlewatch: false
enable_prettier: true
enable_typescript: false
enable_typescript: true

frontend_directory: ./js
backend_directory: .
js_package_manager: npm
main_git_branch: master

secrets:
bundlewatch_github_token: ${{ secrets.BUNDLEWATCH_GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ js/node_modules
vendor/
composer.lock
js/dist
.phpunit.result.cache
44 changes: 37 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"issues": "https://github.com/FriendsOfFlarum/user-bio/issues",
"source": "https://github.com/FriendsOfFlarum/user-bio",
"forum": "https://discuss.flarum.org/d/17775"
},
"homepage": "https://friendsofflarum.org",
"funding": [
},
"homepage": "https://friendsofflarum.org",
"funding": [
{
"type": "website",
"url": "https://opencollective.com/fof/donate"
"type": "website",
"url": "https://opencollective.com/fof/donate"
}
],
],
"require": {
"flarum/core": "^1.2.0"
},
Expand All @@ -31,7 +31,7 @@
},
{
"name": "IanM",
"email": "ian@blomstra.net",
"email": "ian@flarum.org",
"role": "Developer"
}
],
Expand All @@ -55,6 +55,36 @@
},
"flagrow": {
"discuss": "https://discuss.flarum.org/d/17775"
},
"flarum-cli": {
"modules": {
"backendTesting": true,
"githubActions": true
}
}
},
"require-dev": {
"flarum/bbcode": "*",
"flarum/testing": "^1.0.0"
},
"autoload-dev": {
"psr-4": {
"FoF\\UserBio\\Tests\\": "tests/"
}
},
"scripts": {
"test": [
"@test:unit",
"@test:integration"
],
"test:unit": "phpunit -c tests/phpunit.unit.xml",
"test:integration": "phpunit -c tests/phpunit.integration.xml",
"test:setup": "@php tests/integration/setup.php"
},
"scripts-descriptions": {
"test": "Runs all tests.",
"test:unit": "Runs all unit tests.",
"test:integration": "Runs all integration tests.",
"test:setup": "Sets up a database for use with integration tests. Execute this only once."
}
}
2 changes: 0 additions & 2 deletions src/Listeners/SaveUserBio.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public function handle(Saving $event)
if ($user->bio != $user->getOriginal('bio')) {
$user->raise(new BioChanged($user));
}

$user->save();
}
}
}
Empty file added tests/fixtures/.gitkeep
Empty file.
210 changes: 210 additions & 0 deletions tests/integration/api/BioTest.php
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']);
}
}
18 changes: 18 additions & 0 deletions tests/integration/setup.php
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();
25 changes: 25 additions & 0 deletions tests/phpunit.integration.xml
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>
27 changes: 27 additions & 0 deletions tests/phpunit.unit.xml
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 added tests/unit/.gitkeep
Empty file.

0 comments on commit 168d36b

Please sign in to comment.