2018-04-13 13:52:39 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
|
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2019-01-02 05:17:09 +08:00
|
|
|
namespace Flarum\Tests\integration\api\Controller;
|
2018-04-13 13:52:39 +08:00
|
|
|
|
|
|
|
use Flarum\Api\Controller\CreateUserController;
|
|
|
|
use Flarum\Settings\SettingsRepositoryInterface;
|
|
|
|
use Flarum\User\User;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
2018-04-13 15:06:42 +08:00
|
|
|
class CreateUserControllerTest extends ApiControllerTestCase
|
2018-04-13 13:52:39 +08:00
|
|
|
{
|
|
|
|
protected $controller = CreateUserController::class;
|
|
|
|
|
|
|
|
protected $data = [
|
|
|
|
'username' => 'test',
|
|
|
|
'password' => 'too-obscure',
|
|
|
|
'email' => 'test@machine.local'
|
|
|
|
];
|
|
|
|
|
2019-01-31 04:15:27 +08:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->prepareDatabase([
|
|
|
|
'users' => [
|
|
|
|
$this->adminUser(),
|
|
|
|
],
|
|
|
|
'groups' => [
|
|
|
|
$this->adminGroup(),
|
|
|
|
],
|
|
|
|
'group_user' => [
|
|
|
|
['user_id' => 1, 'group_id' => 1],
|
|
|
|
],
|
Merge pull request from GHSA-3wjh-93gr-chh6
* Integration tests: Memoize request handler as well
This is useful to send HTTP requests (or their PSR-7 equivalents)
through the entire application's middleware stack (instead of
talking to specific controllers, which should be considered
implementation detail).
* Add tests for CSRF token check
* Integration tests: Configure vendor path
Now that this is possible, make the easy change...
* Implement middleware for CSRF token verification
This fixes a rather large oversight in Flarum's codebase, which was that
we had no explicit CSRF protection using the traditional token approach.
The JS frontend was actually sending these tokens, but the backend did
not require them.
* Accept CSRF token in request body as well
* Refactor tests to shorten HTTP requests
Multiple tests now provide JSON request bodies, and others copy cookies
from previous responses, so let's provide convenient helpers for these.
* Fixed issue with tmp/storage/views not existing, this caused tmpname to notice.
Fixed csrf test that assumed an access token allows application access, which is actually api token.
Improved return type hinting in the StartSession middleware
* Using a different setting key now, so that it won't break tests whenever you re-run them once smtp is set.
Fixed, badly, the test to create users etc caused by the prepareDatabase flushing all settings by default.
* added custom view, now needs translation
2019-06-24 15:14:39 +08:00
|
|
|
'settings' => [
|
|
|
|
['key' => 'mail_driver', 'value' => 'log']
|
|
|
|
]
|
2019-01-31 04:15:27 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-04-13 13:52:39 +08:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function cannot_create_user_without_data()
|
|
|
|
{
|
2019-08-10 05:57:33 +08:00
|
|
|
$response = $this->callWith();
|
2019-07-30 06:09:10 +08:00
|
|
|
|
2019-08-10 05:57:33 +08:00
|
|
|
$this->assertEquals(422, $response->getStatusCode());
|
2018-04-13 13:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function can_create_user()
|
|
|
|
{
|
|
|
|
$response = $this->callWith($this->data);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response->getStatusCode());
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::where('username', 'test')->firstOrFail();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $user->is_activated);
|
|
|
|
|
|
|
|
foreach (Arr::except($this->data, 'password') as $property => $value) {
|
|
|
|
$this->assertEquals($value, $user->{$property});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function admins_can_create_activated_users()
|
|
|
|
{
|
2019-01-31 04:15:27 +08:00
|
|
|
$this->actor = User::find(1);
|
2018-04-13 13:52:39 +08:00
|
|
|
|
|
|
|
$response = $this->callWith(array_merge($this->data, [
|
2018-08-24 20:05:04 +08:00
|
|
|
'isEmailConfirmed' => 1
|
2018-04-13 13:52:39 +08:00
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response->getStatusCode());
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::where('username', 'test')->firstOrFail();
|
|
|
|
|
2018-05-14 17:34:24 +08:00
|
|
|
$this->assertEquals(1, $user->is_email_confirmed);
|
2018-04-13 13:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function disabling_sign_up_prevents_user_creation()
|
|
|
|
{
|
|
|
|
/** @var SettingsRepositoryInterface $settings */
|
2018-08-15 06:47:00 +08:00
|
|
|
$settings = app(SettingsRepositoryInterface::class);
|
2018-04-13 13:52:39 +08:00
|
|
|
$settings->set('allow_sign_up', false);
|
|
|
|
|
2019-08-10 05:57:33 +08:00
|
|
|
$response = $this->callWith($this->data);
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
2019-07-30 06:09:10 +08:00
|
|
|
|
2019-08-10 05:57:33 +08:00
|
|
|
$settings->set('allow_sign_up', true);
|
2018-04-13 13:52:39 +08:00
|
|
|
}
|
|
|
|
}
|