Skip to content

Instantly share code, notes, and snippets.

@MayMeow
Created April 19, 2019 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MayMeow/a60b2847580bdaf9392252264c92ea8b to your computer and use it in GitHub Desktop.
Save MayMeow/a60b2847580bdaf9392252264c92ea8b to your computer and use it in GitHub Desktop.
Notes Controller Test
<?php
namespace App\Test\TestCase\Controller;
use App\Controller\NotesController;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use PHPUnit\Exception;
/**
* App\Controller\NotesController Test Case
*/
class NotesControllerTest extends TestCase
{
use IntegrationTestTrait;
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'app.Notes',
'app.Profiles',
'app.Users'
];
public function setUp()
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->users = TableRegistry::getTableLocator()->get('Identity.Users');
$this->profiles = TableRegistry::getTableLocator()->get('Profiles');
$this->notes = TableRegistry::getTableLocator()->get('Notes');
$this->enableCsrfToken();
$this->enableSecurityToken();
$data = [
'email' => 'test@local.sk',
'password' => 'pa$$wd'
];
try {
$this->post('/register', $data);
} catch (Exception $e) {
}
}
/**
* @throws \PHPUnit\Exception
*/
public function testIndex()
{
$user = $this->users->find()->where(['email' => 'test@local.sk'])->first();
$this->session([
'Auth' => [
'User' => [
'id' => $user->id,
'email' => $user->email,
// other keys.
]
]
]);
$this->get('/notes');
$this->assertResponseOk();
}
/**
* @throws Exception
*/
public function testView()
{
$user = $this->users->find()->where(['email' => 'test@local.sk'])->first();
$this->session([
'Auth' => [
'User' => [
'id' => $user->id,
'email' => $user->email,
// other keys.
]
]
]);
$data = [
'body' => 'test body'
];
$this->post('/notes/add', $data);
$note = $this->notes->find()->where(['body' => $data['body']])->first();
// view new note
$this->get('/notes/view/' . $note->id);
$this->assertResponseOk();
$this->assertResponseContains($data['body']);
}
/**
* @throws Exception
*/
public function testAdd()
{
$user = $this->users->find()->where(['email' => 'test@local.sk'])->first();
$this->session([
'Auth' => [
'User' => [
'id' => $user->id,
'email' => $user->email,
// other keys.
]
]
]);
$data = [
'body' => 'test body'
];
$this->post('/notes/add', $data);
$this->assertRedirect('/notes');
// test if new note is added
$notes = $this->notes->find()->where(['body' => $data['body']]);
$this->assertEquals(1, $notes->count());
}
/**
* Test edit method
*
* @return void
*/
public function testEdit()
{
$this->markTestIncomplete('Not implemented yet.');
}
/**
* @throws Exception
*/
public function testDelete()
{
$user = $this->users->find()->where(['email' => 'test@local.sk'])->first();
$this->session([
'Auth' => [
'User' => [
'id' => $user->id,
'email' => $user->email,
// other keys.
]
]
]);
$data = [
'body' => 'test body'
];
$this->post('/notes/add', $data);
// find nnote id and delete it
$note = $this->notes->find()->where(['body' => $data['body']])->first();
$this->delete('/notes/delete/' . $note->id);
$this->assertRedirect('/notes');
// check if id exists or it is deleted
$deleted = $this->notes->find()->where(['id' => $note->id]);
$this->assertEquals(0, $deleted->count());
}
/**
* @throws Exception
*/
public function testVerificationOfBody()
{
$user = $this->users->find()->where(['email' => 'test@local.sk'])->first();
$this->session([
'Auth' => [
'User' => [
'id' => $user->id,
'email' => $user->email,
// other keys.
]
]
]);
$data = [
'body' => 'test body for verification'
];
$this->post('/notes/add', $data);
$note = $this->notes->find()->where(['body' => $data['body']])->first();
$profile = $this->profiles->find()->where(['user_id' => $user->id])->first();
//check signature
$verification = openssl_verify($note->body, base64_decode($note->signature), openssl_pkey_get_public($profile->public_key), OPENSSL_ALGO_SHA512);
$this->assertTrue((boolean)$verification);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment