2015-01-19 18:16:14 +08:00
|
|
|
<?php
|
|
|
|
namespace Codeception\Module;
|
|
|
|
|
2015-01-22 12:14:33 +08:00
|
|
|
use Laracasts\TestDummy\Factory;
|
2015-01-19 18:16:14 +08:00
|
|
|
|
|
|
|
class ApiHelper extends \Codeception\Module
|
|
|
|
{
|
2015-01-22 12:14:33 +08:00
|
|
|
public function haveAnAccount($data = [])
|
|
|
|
{
|
2015-02-24 18:03:18 +08:00
|
|
|
$user = Factory::create('Flarum\Core\Models\User', $data);
|
|
|
|
$user->activate();
|
|
|
|
|
|
|
|
return $user;
|
2015-01-22 12:14:33 +08:00
|
|
|
}
|
2015-01-19 18:16:14 +08:00
|
|
|
|
2015-01-23 12:54:38 +08:00
|
|
|
public function login($identification, $password)
|
2015-01-22 12:14:33 +08:00
|
|
|
{
|
2015-02-24 18:03:18 +08:00
|
|
|
$this->getModule('REST')->sendPOST('/api/token', [
|
|
|
|
'identification' => $identification,
|
|
|
|
'password' => $password
|
|
|
|
]);
|
2015-01-22 12:14:33 +08:00
|
|
|
|
|
|
|
$response = json_decode($this->getModule('REST')->grabResponse(), true);
|
|
|
|
if ($response && is_array($response) && isset($response['token'])) {
|
|
|
|
return $response['token'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function amAuthenticated()
|
|
|
|
{
|
|
|
|
$user = $this->haveAnAccount();
|
2015-02-24 18:03:18 +08:00
|
|
|
$token = $this->login($user->email, 'password');
|
|
|
|
$this->getModule('REST')->haveHttpHeader('Authorization', 'Token '.$token);
|
2015-01-22 12:14:33 +08:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
2015-01-19 18:16:14 +08:00
|
|
|
}
|