Merged OS-39-create-user-ticket-comment-classes into master

This commit is contained in:
Ivan Diaz 2016-05-14 21:05:57 -03:00
commit acc29e9d89
19 changed files with 127 additions and 65 deletions

View File

@ -1,12 +0,0 @@
describe '/user/signup' do
it 'should create user in database' do
response = request('/user/signup', {
'email' => 'steve@jobs.com',
'password' => 'custom'
})
userRow = $database.getRow('users', response['data']['userId'])
(userRow['email']).should.equal('steve@jobs.com')
end
end

View File

@ -19,7 +19,7 @@ class SignUpController extends Controller {
$userInstance = new User(); $userInstance = new User();
$userInstance->setProperties(array( $userInstance->setProperties(array(
'email' => $email, 'email' => $email,
'password' => User::hashPassword($password) 'password' => Hashing::hashPassword($password)
)); ));
return $userInstance->store(); return $userInstance->store();

View File

@ -1,9 +0,0 @@
#!/bin/bash
# DELETE ALL TABLES
TABLES=$(mysql -u os_dev -pos_dev os_dev -e "SHOW TABLES IN os_dev;" | awk '{ print $1}' | grep -v '^Tables')
for t in $TABLES
do
mysql -u os_dev -pos_dev os_dev -e "DROP TABLE $t"
done

View File

@ -10,6 +10,11 @@ RedBean::setup('mysql:host='. $mysql_host .';dbname=' . $mysql_database, $mysql_
\Slim\Slim::registerAutoLoader(); \Slim\Slim::registerAutoLoader();
$app = new \Slim\Slim(); $app = new \Slim\Slim();
// LOAD LIBRARIES
include_once 'libs/Controller.php';
include_once 'libs/ControllerGroup.php';
include_once 'libs/Hashing.php';
// LOAD MODELS // LOAD MODELS
spl_autoload_register(function ($class) { spl_autoload_register(function ($class) {
$classPath = "models/{$class}.php"; $classPath = "models/{$class}.php";
@ -20,9 +25,6 @@ spl_autoload_register(function ($class) {
}); });
// LOAD CONTROLLERS // LOAD CONTROLLERS
include_once 'libs/Controller.php';
include_once 'libs/ControllerGroup.php';
foreach (glob('controllers/*.php') as $controller) { foreach (glob('controllers/*.php') as $controller) {
include $controller; include $controller;
} }

View File

@ -0,0 +1,18 @@
<?php
class FileUploader {
private static $instance = null;
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new FileUploader();
}
return self::$instance;
}
private function __construct() {}
public function upload() {
// TODO: Implement file upload features
}
}

10
server/libs/Hashing.php Normal file
View File

@ -0,0 +1,10 @@
<?php
class Hashing {
public static function hashPassword($password) {
return password_hash($password, PASSWORD_DEFAULT);
}
public static function verifyPassword($password, $hash) {
return password_verify($password, $hash);
}
}

19
server/models/Comment.php Normal file
View File

@ -0,0 +1,19 @@
<?php
class Comment extends DataStore {
const TABLE = 'comments';
public static function getProps() {
return array(
'content',
'file',
'ticket',
'author',
'date'
);
}
protected function getDefaultProps() {
return array();
}
}

View File

@ -4,7 +4,7 @@ use RedBeanPHP\Facade as RedBean;
abstract class DataStore { abstract class DataStore {
protected $_bean; protected $_bean;
abstract protected function getDefaultProperties(); abstract protected function getDefaultProps();
public static function getDataStore($value, $property = 'id') { public static function getDataStore($value, $property = 'id') {
$bean = RedBean::findOne(static::TABLE, static::validateProp($property) . ' =:value', array( $bean = RedBean::findOne(static::TABLE, static::validateProp($property) . ' =:value', array(
@ -21,7 +21,7 @@ abstract class DataStore {
} }
else { else {
$this->_bean = RedBean::dispense(static::TABLE); $this->_bean = RedBean::dispense(static::TABLE);
$defaultProperties = $this->getDefaultProperties(); $defaultProperties = $this->getDefaultProps();
foreach ($defaultProperties as $PROP => $VALUE) { foreach ($defaultProperties as $PROP => $VALUE) {
$this->_bean[$PROP] = $VALUE; $this->_bean[$PROP] = $VALUE;
@ -29,15 +29,9 @@ abstract class DataStore {
} }
} }
protected static function deleteDataStore($dataStore) { public function delete() {
if ($dataStore instanceof DataStore) { RedBean::trash($this->getBeanInstance());
RedBean::trash($dataStore->getBeanInstance()); unset($this);
unset($dataStore);
return true;
}
else {
return false;
}
} }
public function getBeanInstance() { public function getBeanInstance() {

26
server/models/Ticket.php Normal file
View File

@ -0,0 +1,26 @@
<?php
class Ticket extends DataStore {
const TABLE = 'tickets';
public static function getProps() {
return array(
'ticketId',
'title',
'content',
'language',
'department',
'file',
'date',
'unread',
'closed',
'author',
'owner',
'ownComments'
);
}
protected function getDefaultProps() {
return array();
}
}

View File

@ -3,40 +3,33 @@
class User extends DataStore { class User extends DataStore {
const TABLE = 'users'; const TABLE = 'users';
public static function hashPassword($password) {
return password_hash($password, PASSWORD_DEFAULT);
}
public static function verifyPassword($password, $hash) {
return password_verify($password, $hash);
}
public static function authenticate($userEmail, $userPassword) { public static function authenticate($userEmail, $userPassword) {
$user = static::getUser($userEmail, 'email'); $user = User::getUser($userEmail, 'email');
return ($user && static::verifyPassword($userPassword, $user->password)) ? $user : null; return ($user && Hashing::verifyPassword($userPassword, $user->password)) ? $user : null;
} }
public static function getProps() { public static function getProps() {
return array( return array(
'email', 'email',
'password' 'password',
'name',
'verificationToken',
'ownTickets'
); );
} }
public function getDefaultProperties() { public function getDefaultProps() {
return array(); return array(
'ownTickets' => []
);
}
public function addTicket($ticket) {
$this->ownTickets[] = $ticket;
} }
public static function getUser($value, $property = 'id') { public static function getUser($value, $property = 'id') {
return parent::getDataStore($value, $property); return parent::getDataStore($value, $property);
} }
public static function deleteUser($user) {
parent::deleteDataStore($user);
}
public function showUserDetails() {
return $this->_user;
}
} }

View File

@ -17,16 +17,12 @@ class DataStoreMock extends DataStore {
); );
} }
public function getDefaultProperties() { public function getDefaultProps() {
return array( return array(
'prop1' => 0, 'prop1' => 0,
'prop2' => 'hello' 'prop2' => 'hello'
); );
} }
public static function deleteDataStore($dataStore) {
return parent::deleteDataStore($dataStore);
}
} }
class DataStoreTest extends PHPUnit_Framework_TestCase { class DataStoreTest extends PHPUnit_Framework_TestCase {
@ -78,9 +74,8 @@ class DataStoreTest extends PHPUnit_Framework_TestCase {
} }
public function testDeleteDataStore() { public function testDeleteDataStore() {
$beanInstance = $this->instance->getBeanInstance(); $this->instance->delete();
DataStoreMock::deleteDataStore($this->instance);
$this->assertTrue(RedBean::get('trash')->hasBeenCalledWithArgs($beanInstance)); $this->assertTrue(RedBean::get('trash')->hasBeenCalledWithArgs($this->instance->getBeanInstance()));
} }
} }

9
tests/clean_db.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
# DELETE ALL TABLES
TABLES=$(mysql -u root development -e "SHOW TABLES IN os_dev;" | awk '{ print $1}' | grep -v '^Tables')
for t in $TABLES
do
mysql -u root development -e "DROP TABLE $t"
done

View File

@ -5,6 +5,7 @@ require 'uri'
require 'mysql' require 'mysql'
require 'json' require 'json'
require './libs.rb' require './libs.rb'
require './scripts.rb'
# TESTS # TESTS
require './user/signup.rb' require './user/signup.rb'

2
tests/run-tests.sh Executable file
View File

@ -0,0 +1,2 @@
./clean_db.sh
bacon init.rb

View File

@ -9,3 +9,4 @@ class Scripts
raise "Could not create user" raise "Could not create user"
end end
end end
end

View File

@ -22,3 +22,4 @@ describe '/user/login' do
it 'should fail if already logged in' do it 'should fail if already logged in' do
end end
end

12
tests/user/signup.rb Normal file
View File

@ -0,0 +1,12 @@
describe '/user/signup' do
it 'should create user in database' do
response = request('/user/signup', {
'email' => 'steve@jobs.com',
'password' => 'custom'
})
userRow = $database.getRow('users', response['data']['userId'])
(userRow['email']).should.equal('steve@jobs.com')
end
end