Completed register controller tests
This commit is contained in:
parent
50c013af8b
commit
0bd56d5e1b
2 changed files with 95 additions and 2 deletions
|
@ -27,8 +27,11 @@ class RegisterControllerTest extends TestCase
|
|||
public function it_give_404_if_registration_are_off()
|
||||
{
|
||||
$this->updateSetting('register_enabled', 'off');
|
||||
$response = $this->get(route('register'));
|
||||
|
||||
$response = $this->get(route('register'));
|
||||
$this->assertSame(404, $response->getStatusCode());
|
||||
|
||||
$response = $this->post(route('register'));
|
||||
$this->assertSame(404, $response->getStatusCode());
|
||||
}
|
||||
|
||||
|
@ -48,6 +51,14 @@ class RegisterControllerTest extends TestCase
|
|||
|
||||
$response = $this->get(route('register'));
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
|
||||
$response = $this->post(route('register'));
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
|
||||
$response = $this->get(route('activate', [
|
||||
'activateToken' => 'token',
|
||||
]));
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
@ -68,4 +79,48 @@ class RegisterControllerTest extends TestCase
|
|||
$result = $this->database()->query('SELECT * FROM users WHERE email = "mario@example.com"')->fetch();
|
||||
$this->assertIsObject($result);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_activate_a_new_user()
|
||||
{
|
||||
$activateToken = bin2hex(random_bytes(16));
|
||||
|
||||
$userId = $this->createUser([
|
||||
'active' => 0,
|
||||
'activate_token' => $activateToken,
|
||||
]);
|
||||
|
||||
$response = $this->get(route('activate', [
|
||||
'activateToken' => $activateToken,
|
||||
]));
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('login'), $response->getHeaderLine('Location'));
|
||||
|
||||
$result = $this->database()->query('SELECT * FROM users WHERE id = ?', $userId)->fetch();
|
||||
$this->assertSame('1', $result->active);
|
||||
$this->assertNull($result->activate_token);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_cant_activate_if_user_not_exists()
|
||||
{
|
||||
$response = $this->get(route('activate', [
|
||||
'activateToken' => 'token',
|
||||
]));
|
||||
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('login'), $response->getHeaderLine('Location'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_with_no_data()
|
||||
{
|
||||
$response = $this->get(route('register'));
|
||||
$form = $this->getCrawler($response)
|
||||
->selectButton('Register')
|
||||
->form([], 'POST');
|
||||
$response = $this->submitForm($form);
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('register'), $response->getHeaderLine('Location'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,10 @@ abstract class TestCase extends BaseTestCase
|
|||
$this->createApplication();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param null $value
|
||||
*/
|
||||
public function updateSetting($key, $value = null)
|
||||
{
|
||||
if (!$this->database()->query('SELECT `value` FROM `settings` WHERE `key` = '.$this->database()->getPdo()->quote($key))->fetch()) {
|
||||
|
@ -24,9 +28,43 @@ abstract class TestCase extends BaseTestCase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @return string
|
||||
*/
|
||||
public function createAdminUser($email = 'admin@example.com', $username = 'admin', $password = 'admin')
|
||||
{
|
||||
$this->database()->query("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES (?, ?, ?, 1, ?)", [$email, $username, password_hash($password, PASSWORD_DEFAULT), humanRandomString(5)]);
|
||||
return $this->createUser([
|
||||
'email' => $email,
|
||||
'username' => $username,
|
||||
'password' => password_hash($password, PASSWORD_DEFAULT),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return string
|
||||
*/
|
||||
public function createUser($attributes = [])
|
||||
{
|
||||
$attributes = array_replace_recursive([
|
||||
'email' => 'user@example.com',
|
||||
'username' => 'user',
|
||||
'password' => password_hash('user', PASSWORD_DEFAULT),
|
||||
'is_admin' => 1,
|
||||
'active' => 1,
|
||||
'user_code' => humanRandomString(5),
|
||||
'token' => 'token_'.md5(uniqid('', true)),
|
||||
'max_disk_quota' => -1,
|
||||
'activate_token' => null,
|
||||
'ldap' => 0,
|
||||
'hide_uploads' => 0,
|
||||
'copy_raw' => 0,
|
||||
], $attributes);
|
||||
|
||||
$this->database()->query('INSERT INTO `users`(`email`, `username`, `password`, `is_admin`, `active`, `user_code`, `token`, `max_disk_quota`, `activate_token`, `ldap`, `hide_uploads`, `copy_raw`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', array_values($attributes));
|
||||
return $this->database()->getPdo()->lastInsertId();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue