koel/tests/UserTest.php

69 lines
1.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
use App\Models\User;
2015-12-14 13:22:39 +00:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2015-12-13 04:42:28 +00:00
class UserTest extends TestCase
{
2016-09-26 06:30:00 +00:00
use DatabaseTransactions;
2015-12-13 04:42:28 +00:00
public function testCreateUser()
{
// Non-admins can't do shit
2016-09-26 06:30:00 +00:00
$this->postAsUser('api/user', [
2015-12-13 04:42:28 +00:00
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'qux',
])
->seeStatusCode(403);
2015-12-13 04:42:28 +00:00
// But admins can
2016-09-26 06:30:00 +00:00
$this->postAsUser('api/user', [
2015-12-13 04:42:28 +00:00
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'qux',
2016-09-26 06:30:00 +00:00
], factory(User::class, 'admin')->create());
2015-12-13 04:42:28 +00:00
$this->seeInDatabase('users', ['name' => 'Foo']);
}
public function testUpdateUser()
{
$user = factory(User::class)->create();
2016-09-26 06:30:00 +00:00
$this->putAsUser("api/user/{$user->id}", [
2015-12-13 04:42:28 +00:00
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'qux',
2016-09-26 06:30:00 +00:00
], factory(User::class, 'admin')->create());
2015-12-13 04:42:28 +00:00
$this->seeInDatabase('users', ['name' => 'Foo', 'email' => 'bar@baz.com']);
}
public function testDeleteUser()
{
$user = factory(User::class)->create();
2015-12-15 10:32:41 +00:00
$admin = factory(User::class, 'admin')->create();
2016-09-26 06:30:00 +00:00
$this->deleteAsUser("api/user/{$user->id}", [], $admin)
2015-12-15 10:32:41 +00:00
->notSeeInDatabase('users', ['id' => $user->id]);
2015-12-13 04:42:28 +00:00
2015-12-15 10:32:41 +00:00
// A user can't delete himself
2016-09-26 06:30:00 +00:00
$this->deleteAsUser("api/user/{$admin->id}", [], $admin)
2015-12-15 10:32:41 +00:00
->seeStatusCode(403)
->seeInDatabase('users', ['id' => $admin->id]);
2015-12-13 04:42:28 +00:00
}
2015-12-20 12:17:35 +00:00
public function testUserPreferences()
{
$user = factory(User::class)->create();
$this->assertNull($user->getPreference('foo'));
$user->setPreference('foo', 'bar');
$this->assertEquals('bar', $user->getPreference('foo'));
$user->deletePreference('foo');
$this->assertNull($user->getPreference('foo'));
}
2015-12-13 04:42:28 +00:00
}