Merged in ticket-get-path (pull request #45)
Path /ticket/get and /user/get + Fix de problemas de connectividad de FrontEnd<->API
This commit is contained in:
commit
6e155884a5
|
@ -17,7 +17,7 @@ gulp.task('server', function() {
|
|||
server.use(express.static(config.buildDir));
|
||||
|
||||
// Proxy php server api
|
||||
server.use('/api', proxy('http://localhost:8080/', {
|
||||
server.use('/api', proxy('http://localhost:8080', {
|
||||
forwardPath: function(req, res) {
|
||||
return require('url').parse(req.url).path;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,8 @@ describe('Session Actions,', function () {
|
|||
then: function (resolve) {
|
||||
resolve({
|
||||
data: {
|
||||
userId: 14
|
||||
userId: 14,
|
||||
token: 'SOME_TOKEN'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -35,7 +36,8 @@ describe('Session Actions,', function () {
|
|||
expect(APICallMock.call).to.have.been.calledWith({
|
||||
path: '/user/get',
|
||||
data: {
|
||||
userId: 14
|
||||
csrf_userid: 14,
|
||||
csrf_token: 'SOME_TOKEN'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -12,7 +12,7 @@ export default {
|
|||
path: '/user/login',
|
||||
data: loginData
|
||||
}).then((result) => {
|
||||
store.dispatch(this.getUserData(result.data.userId));
|
||||
store.dispatch(this.getUserData(result.data.userId, result.data.token));
|
||||
|
||||
return result;
|
||||
})
|
||||
|
@ -32,7 +32,7 @@ export default {
|
|||
isAutomatic: true
|
||||
}
|
||||
}).then((result) => {
|
||||
store.dispatch(this.getUserData(result.data.userId));
|
||||
store.dispatch(this.getUserData(result.data.userId, result.data.token));
|
||||
|
||||
return result;
|
||||
})
|
||||
|
@ -49,14 +49,21 @@ export default {
|
|||
};
|
||||
},
|
||||
|
||||
getUserData(userId) {
|
||||
getUserData(userId, token) {
|
||||
let data = {};
|
||||
|
||||
if (userId && token) {
|
||||
data = {
|
||||
csrf_userid: userId,
|
||||
csrf_token: token
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'USER_DATA',
|
||||
payload: API.call({
|
||||
path: '/user/get',
|
||||
data: {
|
||||
userId: userId
|
||||
}
|
||||
data: data
|
||||
})
|
||||
}
|
||||
},
|
||||
|
|
|
@ -14,13 +14,13 @@ class Captcha extends React.Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
return (this.props.sitekey) ? (
|
||||
<ReCAPTCHA sitekey={this.props.sitekey} ref="reCaptcha" onChange={(value) => {this.setState({value})}} tabIndex="0" />
|
||||
);
|
||||
) : <div></div>;
|
||||
}
|
||||
|
||||
getValue() {
|
||||
return this.state.value;
|
||||
return (this.props.sitekey) ? this.state.value : 'valid';
|
||||
}
|
||||
|
||||
focus() {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import React from 'react';
|
||||
import _ from 'lodash';
|
||||
import ReCAPTCHA from 'react-google-recaptcha';
|
||||
import { browserHistory } from 'react-router';
|
||||
|
||||
|
@ -90,7 +91,9 @@ class CreateTicketForm extends React.Component {
|
|||
|
||||
API.call({
|
||||
path: '/ticket/create',
|
||||
data: formState
|
||||
data: _.extend({}, formState, {
|
||||
departmentId: formState.departmentId + 1
|
||||
})
|
||||
}).then(this.onTicketSuccess.bind(this)).catch(this.onTicketFail.bind(this));
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ class TicketAction extends React.Component {
|
|||
<span className="ticket-action__comment-author-type">({i18n((config.author.staff) ? 'STAFF' : 'CUSTOMER')})</span>
|
||||
</div>
|
||||
<div className="ticket-action__comment-date">{config.date}</div>
|
||||
<div className="ticket-action__comment-content">{config.content}</div>
|
||||
<div className="ticket-action__comment-content" dangerouslySetInnerHTML={{__html: config.content}}></div>
|
||||
{this.renderFileRow(config.file)}
|
||||
</div>
|
||||
);
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
import React from 'react';
|
||||
import _ from 'lodash';
|
||||
|
||||
import i18n from 'lib-app/i18n';
|
||||
import API from 'lib-app/api-call';
|
||||
import store from 'app/store';
|
||||
import SessionActions from 'actions/session-actions';
|
||||
|
||||
import i18n from 'lib-app/i18n';
|
||||
import TicketAction from 'app/main/dashboard/dashboard-ticket/ticket-action';
|
||||
import Form from 'core-components/form';
|
||||
import FormField from 'core-components/form-field';
|
||||
|
@ -19,6 +24,15 @@ class TicketViewer extends React.Component {
|
|||
}
|
||||
};
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
loading: false
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div className="ticket-viewer">
|
||||
|
@ -45,7 +59,7 @@ class TicketViewer extends React.Component {
|
|||
<div className="ticket-viewer__response">
|
||||
<div className="ticket-viewer__response-title row">{i18n('RESPOND')}</div>
|
||||
<div className="ticket-viewer__response-field row">
|
||||
<Form>
|
||||
<Form onSubmit={this.onSubmit.bind(this)} loading={this.state.loading}>
|
||||
<FormField name="content" validation="TEXT_AREA" required field="textarea" />
|
||||
<SubmitButton>{i18n('RESPOND_TICKET')}</SubmitButton>
|
||||
</Form>
|
||||
|
@ -60,6 +74,33 @@ class TicketViewer extends React.Component {
|
|||
<TicketAction type="comment" config={comment} key={index} />
|
||||
);
|
||||
}
|
||||
|
||||
onSubmit(formState) {
|
||||
this.setState({
|
||||
loading: true
|
||||
});
|
||||
|
||||
API.call({
|
||||
path: '/ticket/comment',
|
||||
data: _.extend({
|
||||
ticketNumber: this.props.ticket.ticketNumber
|
||||
}, formState)
|
||||
}).then(this.onCommentSuccess.bind(this), this.onCommentFail.bind(this));
|
||||
}
|
||||
|
||||
onCommentSuccess() {
|
||||
this.setState({
|
||||
loading: false
|
||||
});
|
||||
|
||||
store.dispatch(SessionActions.getUserData());
|
||||
}
|
||||
|
||||
onCommentFail() {
|
||||
this.setState({
|
||||
loading: false
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default TicketViewer;
|
|
@ -55,7 +55,7 @@ class MainLayoutHeader extends React.Component {
|
|||
return {
|
||||
className: 'main-layout-header__languages',
|
||||
items: this.getLanguageList(),
|
||||
selectedIndex: Object.keys(codeLanguages).map((key) => codeLanguages[key]).indexOf(this.props.config.language),
|
||||
selectedIndex: Object.keys(codeLanguages).map((key) => codeLanguages[key]).indexOf(this.getPropLanguage()),
|
||||
onChange: this.changeLanguage.bind(this)
|
||||
};
|
||||
}
|
||||
|
@ -69,10 +69,24 @@ class MainLayoutHeader extends React.Component {
|
|||
});
|
||||
}
|
||||
|
||||
changeLanguage(event) {
|
||||
let language = Object.keys(codeLanguages)[event.index];
|
||||
getPropLanguage() {
|
||||
let language = this.props.config.language;
|
||||
|
||||
this.props.dispatch(ConfigActions.changeLanguage(codeLanguages[language]));
|
||||
if (language === 'en') {
|
||||
language = 'us';
|
||||
}
|
||||
|
||||
return language;
|
||||
}
|
||||
|
||||
changeLanguage(event) {
|
||||
let language = codeLanguages[Object.keys(codeLanguages)[event.index]];
|
||||
|
||||
if (language === 'us') {
|
||||
language = 'en';
|
||||
}
|
||||
|
||||
this.props.dispatch(ConfigActions.changeLanguage(language));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,8 +32,8 @@ class MainRecoverPasswordPage extends React.Component {
|
|||
<Widget title={i18n('RECOVER_PASSWORD')} className="col-md-4 col-md-offset-4">
|
||||
<Form className="recover-password__form" onSubmit={this.onRecoverPasswordSubmit.bind(this)} loading={this.state.loading}>
|
||||
<div className="recover-password__inputs">
|
||||
<FormField placeholder={i18n('NEW_PASSWORD')} name="password" className="recover-password__input" validation="PASSWORD" password required/>
|
||||
<FormField placeholder={i18n('REPEAT_NEW_PASSWORD')} name="password-repeat" className="recover-password__input" validation="REPEAT_PASSWORD" password required/>
|
||||
<FormField placeholder={i18n('NEW_PASSWORD')} name="password" className="recover-password__input" validation="PASSWORD" fieldProps={{password: true}} required/>
|
||||
<FormField placeholder={i18n('REPEAT_NEW_PASSWORD')} name="password-repeat" className="recover-password__input" validation="REPEAT_PASSWORD" fieldProps={{password: true}} required/>
|
||||
</div>
|
||||
<div className="recover-password__submit-button">
|
||||
<SubmitButton type="primary">{i18n('SUBMIT')}</SubmitButton>
|
||||
|
|
|
@ -4,6 +4,7 @@ import _ from 'lodash';
|
|||
|
||||
import i18n from 'lib-app/i18n';
|
||||
import API from 'lib-app/api-call';
|
||||
import SessionStore from 'lib-app/session-store';
|
||||
|
||||
import Captcha from 'app/main/captcha';
|
||||
import SubmitButton from 'core-components/submit-button';
|
||||
|
|
|
@ -6,7 +6,7 @@ module.exports = [
|
|||
return {
|
||||
status: 'success',
|
||||
data: {
|
||||
'language': 'us',
|
||||
'language': 'en',
|
||||
'reCaptchaKey': '6LfM5CYTAAAAAGLz6ctpf-hchX2_l0Ge-Bn-n8wS',
|
||||
'departments': [
|
||||
'Sales Support',
|
||||
|
|
|
@ -7,7 +7,7 @@ import turkishLanguage from 'data/languages/en';
|
|||
import indianLanguage from 'data/languages/en';
|
||||
|
||||
const languages = {
|
||||
'us': englishLanguage,
|
||||
'en': englishLanguage,
|
||||
'es': spanishLanguage,
|
||||
'de': germanLanguage,
|
||||
'fr': frenchLanguage,
|
||||
|
|
|
@ -5,7 +5,10 @@ const SessionStore = require('lib-app/session-store');
|
|||
const root = 'http://localhost:3000/api';
|
||||
|
||||
function processData (data) {
|
||||
return _.extend(SessionStore.getSessionData(), data);
|
||||
return _.extend({
|
||||
csrf_token: SessionStore.getSessionData().token,
|
||||
csrf_userid: SessionStore.getSessionData().userId
|
||||
}, data);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
@ -6,7 +6,7 @@ class SessionStore {
|
|||
this.storage = LocalStorage;
|
||||
|
||||
if (!this.getItem('language')) {
|
||||
this.setItem('language', 'us');
|
||||
this.setItem('language', 'en');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
<?php
|
||||
include 'ticket/create.php';
|
||||
include 'ticket/comment.php';
|
||||
include 'ticket/get.php';
|
||||
|
||||
$ticketControllers = new ControllerGroup();
|
||||
$ticketControllers->setGroupPath('/ticket');
|
||||
|
||||
$ticketControllers->addController(new CreateController);
|
||||
$ticketControllers->addController(new CommentController);
|
||||
$ticketControllers->addController(new TicketGetController);
|
||||
|
||||
$ticketControllers->finalize();
|
|
@ -16,8 +16,8 @@ class CommentController extends Controller {
|
|||
'validation' => DataValidator::length(20, 500),
|
||||
'error' => ERRORS::INVALID_CONTENT
|
||||
],
|
||||
'ticketId' => [
|
||||
'validation' => DataValidator::dataStoreId('ticket'),
|
||||
'ticketNumber' => [
|
||||
'validation' => DataValidator::validTicketNumber(),
|
||||
'error' => ERRORS::INVALID_TICKET
|
||||
]
|
||||
]
|
||||
|
@ -37,9 +37,9 @@ class CommentController extends Controller {
|
|||
}
|
||||
|
||||
private function requestData() {
|
||||
$ticketId = Controller::request('ticketId');
|
||||
$ticketNumber = Controller::request('ticketNumber');
|
||||
|
||||
$this->ticket = Ticket::getTicket($ticketId);
|
||||
$this->ticket = Ticket::getByTicketNumber($ticketNumber);
|
||||
$this->content = Controller::request('content');
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ class CreateController extends Controller {
|
|||
private $content;
|
||||
private $departmentId;
|
||||
private $language;
|
||||
private $ticketNumber;
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
|
@ -31,17 +32,16 @@ class CreateController extends Controller {
|
|||
}
|
||||
|
||||
public function handler() {
|
||||
$this->storeRequestData();
|
||||
$this->storeTicket();
|
||||
|
||||
Response::respondSuccess();
|
||||
}
|
||||
|
||||
private function storeRequestData() {
|
||||
$this->title = Controller::request('title');
|
||||
$this->content = Controller::request('content');
|
||||
$this->departmentId = Controller::request('departmentId');
|
||||
$this->language = Controller::request('language');
|
||||
|
||||
$this->storeTicket();
|
||||
|
||||
Response::respondSuccess([
|
||||
'ticketNumber' => $this->ticketNumber
|
||||
]);
|
||||
}
|
||||
|
||||
private function storeTicket() {
|
||||
|
@ -65,5 +65,7 @@ class CreateController extends Controller {
|
|||
|
||||
$author->store();
|
||||
$ticket->store();
|
||||
|
||||
$this->ticketNumber = $ticket->ticketNumber;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
DataValidator::with('CustomValidations', true);
|
||||
|
||||
class TicketGetController extends Controller {
|
||||
const PATH = '/get';
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
'permission' => 'user',
|
||||
'requestData' => [
|
||||
'ticketNumber' => [
|
||||
'validation' => DataValidator::validTicketNumber(),
|
||||
'error' => ERRORS::INVALID_TICKET
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function handler() {
|
||||
$ticketNumber = Controller::request('ticketNumber');
|
||||
|
||||
$ticket = Ticket::getByTicketNumber($ticketNumber);
|
||||
|
||||
if ($ticket->author->id != Controller::getLoggedUser()->id) {
|
||||
Response::respondError(ERRORS::INVALID_TICKET);
|
||||
} else {
|
||||
Response::respondSuccess($ticket->toArray());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,10 +2,12 @@
|
|||
include 'user/login.php';
|
||||
include 'user/signup.php';
|
||||
include 'user/logout.php';
|
||||
include 'user/check-session.php';
|
||||
include 'user/recover-password.php';
|
||||
include 'user/send-recover-password.php';
|
||||
include 'user/edit-password.php';
|
||||
include 'user/edit-email.php';
|
||||
include 'user/get.php';
|
||||
|
||||
$userControllers = new ControllerGroup();
|
||||
$userControllers->setGroupPath('/user');
|
||||
|
@ -13,9 +15,11 @@ $userControllers->setGroupPath('/user');
|
|||
$userControllers->addController(new LoginController);
|
||||
$userControllers->addController(new SignUpController);
|
||||
$userControllers->addController(new LogoutController);
|
||||
$userControllers->addController(new CheckSessionController);
|
||||
$userControllers->addController(new SendRecoverPasswordController);
|
||||
$userControllers->addController(new RecoverPasswordController);
|
||||
$userControllers->addController(new EditPassword);
|
||||
$userControllers->addController(new EditEmail);
|
||||
$userControllers->addController(new GetUserController);
|
||||
|
||||
$userControllers->finalize();
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
DataValidator::with('CustomValidations', true);
|
||||
|
||||
class GetUserController extends Controller {
|
||||
const PATH = '/get';
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
'permission' => 'user',
|
||||
'requestData' => []
|
||||
];
|
||||
}
|
||||
|
||||
public function handler() {
|
||||
$user = Controller::getLoggedUser();
|
||||
$parsedTicketList = [];
|
||||
$ticketList = $user->sharedTicketList;
|
||||
|
||||
foreach($ticketList as $ticket) {
|
||||
$parsedTicketList[] = $ticket->toArray();
|
||||
}
|
||||
|
||||
Response::respondSuccess([
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'tickets' => $parsedTicketList
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -37,18 +37,21 @@ class SignUpController extends Controller {
|
|||
public function handler() {
|
||||
$this->storeRequestData();
|
||||
|
||||
try {
|
||||
$userId = $this->createNewUserAndRetrieveId();
|
||||
$this->sendRegistrationMail();
|
||||
$existentUser = User::getUser($this->userEmail, 'email');
|
||||
|
||||
Response::respondSuccess([
|
||||
'userId' => $userId,
|
||||
'userEmail' => $this->userEmail
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
Response::respondError($e->getMessage());
|
||||
if (!$existentUser->isNull()) {
|
||||
Response::respondError(ERRORS::USER_EXISTS);
|
||||
return;
|
||||
}
|
||||
|
||||
$userId = $this->createNewUserAndRetrieveId();
|
||||
$this->sendRegistrationMail();
|
||||
|
||||
Response::respondSuccess([
|
||||
'userId' => $userId,
|
||||
'userEmail' => $this->userEmail
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function storeRequestData() {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
class ERRORS {
|
||||
const INVALID_CREDENTIALS = 'User or password is not defined';
|
||||
const SESSION_EXISTS = 'User is already logged in';
|
||||
const USER_EXISTS = 'Email already exists';
|
||||
const NO_PERMISSION = 'You have no permission to access';
|
||||
const INVALID_TITLE = 'Invalid title';
|
||||
const INVALID_CONTENT = 'Invalid content';
|
||||
|
|
|
@ -41,6 +41,7 @@ spl_autoload_register(function ($class) {
|
|||
include_once 'libs/validations/dataStoreId.php';
|
||||
include_once 'libs/validations/userEmail.php';
|
||||
include_once 'libs/validations/captcha.php';
|
||||
include_once 'libs/validations/validTicketNumber.php';
|
||||
|
||||
// LOAD CONTROLLERS
|
||||
foreach (glob('controllers/*.php') as $controller) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
require_once 'models/DataStore.php';
|
||||
|
||||
class DataStoreList {
|
||||
class DataStoreList implements IteratorAggregate{
|
||||
private $list = [];
|
||||
|
||||
public static function getList($type, $beanList) {
|
||||
|
@ -14,6 +14,10 @@ class DataStoreList {
|
|||
return $dataStoreList;
|
||||
}
|
||||
|
||||
public function getIterator() {
|
||||
return new ArrayIterator($this->list);
|
||||
}
|
||||
|
||||
public function add(DataStore $dataStore) {
|
||||
$this->list[] = $dataStore;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace CustomValidations;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
|
||||
class ValidTicketNumber extends AbstractRule {
|
||||
|
||||
public function validate($ticketNumber) {
|
||||
$ticket = \Ticket::getByTicketNumber($ticketNumber);
|
||||
|
||||
return !$ticket->isNull();
|
||||
}
|
||||
}
|
|
@ -29,7 +29,16 @@ abstract class DataStore {
|
|||
}
|
||||
}
|
||||
|
||||
return ($validProp) ? $propToValidate : 'id';
|
||||
return ($validProp) ? self::from_camel_case($propToValidate) : 'id';
|
||||
}
|
||||
|
||||
private static function from_camel_case($input) {
|
||||
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
|
||||
$ret = $matches[0];
|
||||
foreach ($ret as &$match) {
|
||||
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
|
||||
}
|
||||
return implode('_', $ret);
|
||||
}
|
||||
|
||||
public function __construct($beanInstance = null) {
|
||||
|
@ -62,6 +71,7 @@ abstract class DataStore {
|
|||
}
|
||||
|
||||
public function &__get($name) {
|
||||
'hello';
|
||||
if (!array_key_exists($name, $this->properties) || !$this->properties[$name]) {
|
||||
$this->properties[$name] = $this->parseBeanProp($name);
|
||||
}
|
||||
|
|
|
@ -4,8 +4,6 @@ use RedBeanPHP\Facade as RedBean;
|
|||
class Ticket extends DataStore {
|
||||
const TABLE = 'ticket';
|
||||
|
||||
private $author;
|
||||
|
||||
public static function getProps() {
|
||||
return array(
|
||||
'ticketNumber',
|
||||
|
@ -26,6 +24,10 @@ class Ticket extends DataStore {
|
|||
public static function getTicket($value, $property = 'id') {
|
||||
return parent::getDataStore($value, $property);
|
||||
}
|
||||
|
||||
public static function getByTicketNumber($value) {
|
||||
return Ticket::getTicket($value, 'ticketNumber');
|
||||
}
|
||||
|
||||
public function getDefaultProps() {
|
||||
return array(
|
||||
|
@ -36,10 +38,6 @@ class Ticket extends DataStore {
|
|||
|
||||
public function store() {
|
||||
parent::store();
|
||||
|
||||
if ($this->author instanceof User) {
|
||||
$this->author->store();
|
||||
}
|
||||
}
|
||||
public function generateUniqueTicketNumber() {
|
||||
$ticketQuantity = Ticket::count('ticket');
|
||||
|
@ -57,4 +55,74 @@ class Ticket extends DataStore {
|
|||
|
||||
return $ticketNumber;
|
||||
}
|
||||
|
||||
public function toArray() {
|
||||
$author = $this->author;
|
||||
|
||||
return [
|
||||
'ticketNumber' => $this->ticketNumber,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'department' => [
|
||||
'id' => $this->department->id,
|
||||
'name' => $this->department->name
|
||||
],
|
||||
'date' => $this->date,
|
||||
'file' => $this->file,
|
||||
'language' => $this->language,
|
||||
'unread' => !!$this->unread,
|
||||
'closed' => !!$this->closed,
|
||||
'author' => $this->authorToArray(),
|
||||
'owner' => $this->ownerToArray(),
|
||||
'comments' => $this->commentsToArray()
|
||||
];
|
||||
}
|
||||
|
||||
public function authorToArray() {
|
||||
$author = $this->author;
|
||||
|
||||
if ($author && !$author->isNull()) {
|
||||
return [
|
||||
'id' => $author->id,
|
||||
'name' => $author->name,
|
||||
'email' => $author->email
|
||||
];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function ownerToArray() {
|
||||
$owner = $this->owner;
|
||||
|
||||
if ($owner && !$owner->isNull()) {
|
||||
return [
|
||||
'id' => $owner->id,
|
||||
'name' => $owner->name,
|
||||
'email' => $owner->email
|
||||
];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function commentsToArray() {
|
||||
$comments = [];
|
||||
|
||||
foreach ($this->ownCommentList as $comment) {
|
||||
$comments[] = [
|
||||
'content'=> $comment->content,
|
||||
'author' => [
|
||||
'id'=> 15,
|
||||
'name' => $comment->author->name,
|
||||
'email' => $comment->author->email,
|
||||
'staff' => $comment->author->staff
|
||||
],
|
||||
'date'=> $comment->date,
|
||||
'file'=> $comment->file
|
||||
];
|
||||
}
|
||||
|
||||
return $comments;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,5 +17,7 @@ require './user/send-recover-password.rb'
|
|||
require './user/recover-password.rb'
|
||||
require './user/edit-password.rb'
|
||||
require './user/edit-email.rb'
|
||||
require './user/get.rb'
|
||||
require './ticket/create.rb'
|
||||
require './ticket/comment.rb'
|
||||
require './ticket/get.rb'
|
||||
|
|
|
@ -18,6 +18,23 @@ class Scripts
|
|||
:password => password
|
||||
})
|
||||
|
||||
if response['data'].any?
|
||||
$csrf_userid = response['data']['userId']
|
||||
$csrf_token = response['data']['token']
|
||||
end
|
||||
|
||||
response['data']
|
||||
end
|
||||
|
||||
def self.createTicket()
|
||||
result = request('/ticket/create', {
|
||||
title: 'Winter is coming',
|
||||
content: 'The north remembers',
|
||||
departmentId: 1,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
result['data']
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,15 @@
|
|||
describe '/ticket/comment/' do
|
||||
Scripts.createUser('commenter@os4.com', 'commenter', 'Commenter')
|
||||
Scripts.login('commenter@os4.com', 'commenter')
|
||||
|
||||
result = Scripts.createTicket
|
||||
|
||||
@ticketNumber = result['ticketNumber']
|
||||
|
||||
it 'should fail if invalid token is passed' do
|
||||
result = request('/ticket/comment', {
|
||||
content: 'some comment content',
|
||||
ticketId: 1,
|
||||
ticketId: @ticketNumber,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: 'INVALID_TOKEN'
|
||||
})
|
||||
|
@ -14,7 +21,7 @@ describe '/ticket/comment/' do
|
|||
it 'should fail if content is too short' do
|
||||
result = request('/ticket/comment', {
|
||||
content: 'Test',
|
||||
ticketId: 1,
|
||||
ticketNumber: @ticketNumber,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
@ -29,7 +36,7 @@ describe '/ticket/comment/' do
|
|||
|
||||
result = request('/ticket/comment', {
|
||||
content: long_text,
|
||||
ticketId: 1,
|
||||
ticketNumber: @ticketNumber,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
@ -41,7 +48,7 @@ describe '/ticket/comment/' do
|
|||
it 'should fail if ticket does not exist' do
|
||||
result = request('/ticket/comment', {
|
||||
content: 'some comment content',
|
||||
ticketId: 30,
|
||||
ticketNumber: 30,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
@ -53,28 +60,28 @@ describe '/ticket/comment/' do
|
|||
it 'should add comment to ticket' do
|
||||
result = request('/ticket/comment', {
|
||||
content: 'some comment content',
|
||||
ticketId: 1,
|
||||
ticketNumber: @ticketNumber,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
|
||||
comment = $database.getRow('comment', '1', 'id')
|
||||
ticket = $database.getRow('ticket', @ticketNumber, 'ticket_number')
|
||||
comment = $database.getRow('comment', ticket['id'], 'ticket_id')
|
||||
(comment['content']).should.equal('some comment content')
|
||||
(comment['ticket_id']).should.equal('1')
|
||||
(comment['author_id']).should.equal($csrf_userid)
|
||||
end
|
||||
|
||||
it 'should fail if user is not the author nor owner' do
|
||||
Scripts.createUser('commenter@comment.com', 'commenter', 'Commenter')
|
||||
data = Scripts.login('commenter@comment.com', 'commenter')
|
||||
Scripts.createUser('no_commenter@comment.com', 'no_commenter', 'No Commenter')
|
||||
Scripts.login('no_commenter@comment.com', 'no_commenter')
|
||||
|
||||
result = request('/ticket/comment', {
|
||||
content: 'some comment content',
|
||||
ticketId: 1,
|
||||
csrf_userid: data['userId'],
|
||||
csrf_token: data['token']
|
||||
ticketNumber: @ticketNumber,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
(result['status']).should.equal('fail')
|
||||
|
|
|
@ -1,13 +1,7 @@
|
|||
describe '/ticket/create' do
|
||||
request('/user/logout')
|
||||
Scripts.createUser('jonhsnow@os4.com','jonhpass','Jonh Snow')
|
||||
result = request('/user/login', {
|
||||
email: 'jonhsnow@os4.com',
|
||||
password: 'jonhpass'
|
||||
})
|
||||
|
||||
$csrf_userid = result['data']['userId']
|
||||
$csrf_token = result['data']['token']
|
||||
Scripts.createUser('creator@os4.com','creator','Creator')
|
||||
Scripts.login('creator@os4.com','creator')
|
||||
|
||||
it 'should fail if invalid token is passed' do
|
||||
result = request('/ticket/create', {
|
||||
|
@ -77,7 +71,7 @@ describe '/ticket/create' do
|
|||
end
|
||||
|
||||
it 'should fail if departmentId is invalid' do
|
||||
result = request('/ticket/create',{
|
||||
result = request('/ticket/create', {
|
||||
title: 'Winter is coming',
|
||||
content: 'The north remembers',
|
||||
departmentId: 30,
|
||||
|
@ -91,7 +85,7 @@ describe '/ticket/create' do
|
|||
end
|
||||
|
||||
it 'should create ticket if pass data is valid' do
|
||||
result = request('/ticket/create',{
|
||||
result = request('/ticket/create', {
|
||||
title: 'Winter is coming',
|
||||
content: 'The north remembers',
|
||||
departmentId: 1,
|
||||
|
@ -110,7 +104,7 @@ describe '/ticket/create' do
|
|||
(ticket['author_id']).should.equal($csrf_userid)
|
||||
(ticket['ticket_number'].size).should.equal(6)
|
||||
|
||||
ticket_user_relation = $database.getRow('ticket_user','1','ticket_id')
|
||||
ticket_user_relation = $database.getRow('ticket_user', ticket['id'],'ticket_id')
|
||||
(ticket_user_relation['user_id']).should.equal($csrf_userid)
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
describe '/ticket/get/' do
|
||||
request('/user/logout')
|
||||
Scripts.createUser('cersei@os4.com', 'cersei','Cersei Lannister')
|
||||
Scripts.createUser('not_ticket_getter@os4.com', 'not_ticket_getter','No Author')
|
||||
|
||||
before do
|
||||
result = Scripts.login('cersei@os4.com', 'cersei')
|
||||
$csrf_userid = result['userId']
|
||||
$csrf_token = result['token']
|
||||
result = request('/ticket/create', {
|
||||
title: 'Should we pay?',
|
||||
content: 'A Lannister always pays his debts.',
|
||||
departmentId: 1,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
@ticketNumber = result['data']['ticketNumber']
|
||||
end
|
||||
|
||||
it 'should fail if ticketNumber is invalid' do
|
||||
result = request('/ticket/get', {
|
||||
ticketNumber: (@ticketNumber.to_i + 1).to_s,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
(result['status']).should.equal('fail')
|
||||
end
|
||||
|
||||
it 'should fail if ticket does not belong to user' do
|
||||
request('/user/logout')
|
||||
result = Scripts.login('not_ticket_getter@os4.com', 'not_ticket_getter')
|
||||
|
||||
$csrf_userid = result['userId']
|
||||
$csrf_token = result['token']
|
||||
result = request('/ticket/get', {
|
||||
ticketNumber: @ticketNumber,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
(result['status']).should.equal('fail')
|
||||
end
|
||||
|
||||
it 'should successfully return the ticket information' do
|
||||
result = Scripts.login('cersei@os4.com', 'cersei')
|
||||
$csrf_userid = result['userId']
|
||||
$csrf_token = result['token']
|
||||
result = request('/ticket/get', {
|
||||
ticketNumber: @ticketNumber,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
ticket = $database.getRow('ticket', @ticketNumber, 'ticket_number')
|
||||
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
(result['data']['ticketNumber']).should.equal(ticket['ticket_number'])
|
||||
(result['data']['title']).should.equal(ticket['title'])
|
||||
(result['data']['content']).should.equal(ticket['content'])
|
||||
(result['data']['department']['id']).should.equal('1')
|
||||
(result['data']['department']['name']).should.equal($database.getRow('department', 1)['name'])
|
||||
(result['data']['date']).should.equal(ticket['date'])
|
||||
(result['data']['file']).should.equal(ticket['file'])
|
||||
(result['data']['language']).should.equal(ticket['language'])
|
||||
(result['data']['unread']).should.equal(false)
|
||||
(result['data']['author']['name']).should.equal('Cersei Lannister')
|
||||
(result['data']['author']['email']).should.equal('cersei@os4.com')
|
||||
(result['data']['owner']).should.equal([])
|
||||
(result['data']['comments']).should.equal([])
|
||||
end
|
||||
end
|
|
@ -0,0 +1,57 @@
|
|||
describe '/user/get' do
|
||||
request('/user/logout')
|
||||
Scripts.createUser('user_get@os4.com', 'user_get','User Get')
|
||||
|
||||
result = Scripts.login('user_get@os4.com', 'user_get')
|
||||
$csrf_userid = result['userId']
|
||||
$csrf_token = result['token']
|
||||
result = request('/ticket/create', {
|
||||
title: 'Should we pay?',
|
||||
content: 'A Lannister always pays his debts.',
|
||||
departmentId: 1,
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
@ticketNumber = result['data']['ticketNumber']
|
||||
|
||||
it 'should fail if not logged' do
|
||||
request('/user/logout')
|
||||
result = request('/user/get', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
(result['status']).should.equal('fail')
|
||||
end
|
||||
|
||||
it 'should successfully return the ticket information' do
|
||||
result = Scripts.login('user_get@os4.com', 'user_get')
|
||||
$csrf_userid = result['userId']
|
||||
$csrf_token = result['token']
|
||||
result = request('/user/get', {
|
||||
csrf_userid: $csrf_userid,
|
||||
csrf_token: $csrf_token
|
||||
})
|
||||
|
||||
ticket = $database.getRow('ticket', @ticketNumber, 'ticket_number')
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
(result['data']['name']).should.equal('User Get')
|
||||
(result['data']['email']).should.equal('user_get@os4.com')
|
||||
|
||||
ticketFromUser = result['data']['tickets'][0]
|
||||
(ticketFromUser['ticketNumber']).should.equal(ticket['ticket_number'])
|
||||
(ticketFromUser['title']).should.equal(ticket['title'])
|
||||
(ticketFromUser['content']).should.equal(ticket['content'])
|
||||
(ticketFromUser['department']['id']).should.equal('1')
|
||||
(ticketFromUser['department']['name']).should.equal($database.getRow('department', 1)['name'])
|
||||
(ticketFromUser['date']).should.equal(ticket['date'])
|
||||
(ticketFromUser['file']).should.equal(ticket['file'])
|
||||
(ticketFromUser['language']).should.equal(ticket['language'])
|
||||
(ticketFromUser['unread']).should.equal(false)
|
||||
(ticketFromUser['author']['name']).should.equal('User Get')
|
||||
(ticketFromUser['author']['email']).should.equal('user_get@os4.com')
|
||||
(ticketFromUser['owner']).should.equal([])
|
||||
(ticketFromUser['comments']).should.equal([])
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue