feature#146 backend

This commit is contained in:
Guillermo 2018-12-12 01:23:58 -03:00
parent 7b7dc334d8
commit 234de7ed2c
13 changed files with 607 additions and 5 deletions

View File

@ -16,5 +16,9 @@ $ticketControllers->addController(new ReOpenController);
$ticketControllers->addController(new ChangePriorityController);
$ticketControllers->addController(new SeenController);
$ticketControllers->addController(new DeleteController);
$ticketControllers->addController(new AddTagController);
$ticketControllers->addController(new EditTagController);
$ticketControllers->addController(new DeleteTagController);
$ticketControllers->addController(new GetTagsController);
$ticketControllers->finalize();

View File

@ -0,0 +1,62 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
/**
* @api {post} /ticket/add-tag Add tag
* @apiVersion 4.3.2
*
* @apiName Add tag
*
* @apiGroup Ticket
*
* @apiDescription This path add a new tag.
*
* @apiPermission staff1
*
* @apiParam {Number} name The new name of the tag.
* @apiParam {String} color The new color of the tag.
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_NAME
* @apiUse TAG_EXISTS
*
* @apiSuccess {Object} data Empty object
*
*/
class AddTagController extends Controller {
const PATH = '/add-tag';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_1',
'requestData' => [
'name' => [
'validation' => DataValidator::length(2, 100),
'error' => ERRORS::INVALID_NAME
]
]
];
}
public function handler() {
$name = Controller::request('name');
$color = Controller::request('color');
if (!Tag::getDataStore($name, 'name')->isNull()) {
throw new RequestException(ERRORS::TAG_EXISTS);
}
$tagInstance = new Tag();
$tagInstance->setProperties([
'name' => $name,
'color' => $color
]);
$tagInstance->store();
Response::respondSuccess();
}
}

View File

@ -0,0 +1,50 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
/**
* @api {post} /ticket/delete-tag Delete a tag
* @apiVersion 4.3.2
*
* @apiName Delete tag
*
* @apiGroup Ticket
*
* @apiDescription This path delete a tag.
*
* @apiPermission staff1
*
* @apiParam {Number} tagId The id of the tag.
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_TAG
*
* @apiSuccess {Object} data Empty object
*
*/
class DeleteTagController extends Controller {
const PATH = '/delete-tag';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_1',
'requestData' => [
'tagId' => [
'validation' => DataValidator::dataStoreId('tag'),
'error' => ERRORS::INVALID_TAG
]
]
];
}
public function handler() {
$tagInstance = Tag::getDataStore(Controller::request('tagId'));
$tagInstance->delete();
Response::respondSuccess();
}
}

View File

@ -0,0 +1,61 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
/**
* @api {post} /ticket/edit-tag Edit tag
* @apiVersion 4.3.2
*
* @apiName Edit tag
*
* @apiGroup Ticket
*
* @apiDescription This path edit tags.
*
* @apiPermission staff1
*
* @apiParam {Number} tagId The id of the tag.
* @apiParam {Number} name The new name of the tag.
* @apiParam {String} color The new color of the tag.
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_TAG
* @apiUse TAG_EXISTS
*
* @apiSuccess {Object} data Empty object
*
*/
class EditTagController extends Controller {
const PATH = '/edit-tag';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_1',
'requestData' => [
'tagId' => [
'validation' => DataValidator::dataStoreId('tag'),
'error' => ERRORS::INVALID_TAG
]
]
];
}
public function handler() {
$name = Controller::request('name');
$color = Controller::request('color');
$tagInstance = Tag::getDataStore(Controller::request('tagId'));
if($name) $tagInstance->name = $name;
if($color) $tagInstance->color = $color;
if (!Tag::getDataStore($name, 'name')->isNull()) {
throw new RequestException(ERRORS::TAG_EXISTS);
}
$tagInstance->store();
Response::respondSuccess();
}
}

View File

@ -0,0 +1,39 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
/**
* @api {post} /ticket/get-tags Get tags
* @apiVersion 4.3.2
*
* @apiName Get tags
*
* @apiGroup Ticket
*
* @apiDescription This path returns all the tags.
*
* @apiPermission staff1
*
* @apiUse NO_PERMISSION
*
* @apiSuccess {Object} data Empty object
*
*/
class GetTagsController extends Controller {
const PATH = '/get-tags';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_1',
'requestData' => []
];
}
public function handler() {
$tags = Tag::getAll();
Response::respondSuccess($tags->toArray());
}
}

View File

@ -11,7 +11,11 @@
* @apiDefine USER_EXISTS
* @apiError {String} USER_EXISTS The user already exists.
*/
/**
/**
* @apiDefine TAG_EXISTS
* @apiError {String} TAG_EXISTS The tag already exists.
*/
/**
* @apiDefine NO_PERMISSION
* @apiError {String} NO_PERMISSION You have no permission to perform this operation.
*/
@ -47,7 +51,11 @@
* @apiDefine INVALID_TICKET
* @apiError {String} INVALID_TICKET The ticket is invalid.
*/
/**
/**
* @apiDefine INVALID_TAG
* @apiError {String} INVALID_TAG The tag is invalid.
*/
/**
* @apiDefine INIT_SETTINGS_DONE
* @apiError {String} INIT_SETTINGS_DONE The init settings are already done.
*/
@ -204,6 +212,7 @@ class ERRORS {
const INVALID_CREDENTIALS = 'INVALID_CREDENTIALS';
const SESSION_EXISTS = 'SESSION_EXISTS';
const USER_EXISTS = 'USER_EXISTS';
const TAG_EXISTS = 'TAG_EXISTS';
const NO_PERMISSION = 'NO_PERMISSION';
const INVALID_TITLE = 'INVALID_TITLE';
const INVALID_CONTENT = 'INVALID_CONTENT';
@ -213,6 +222,7 @@ class ERRORS {
const INVALID_SETTING = 'INVALID_SETTING';
const INVALID_DEPARTMENT = 'INVALID_DEPARTMENT';
const INVALID_TICKET = 'INVALID_TICKET';
const INVALID_TAG = 'INVALID_TAG';
const INIT_SETTINGS_DONE = 'INIT_SETTINGS_DONE';
const INVALID_OLD_PASSWORD = 'INVALID_OLD_PASSWORD';
const INVALID_CAPTCHA = 'INVALID_CAPTCHA';

View File

@ -40,6 +40,8 @@ class DataStoreId extends AbstractRule {
case 'article':
$dataStore = \Article::getDataStore($dataStoreId);
break;
case 'tag':
$dataStore = \Tag::getDataStore($dataStoreId);
}
return !$dataStore->isNull();
@ -53,7 +55,8 @@ class DataStoreId extends AbstractRule {
'department',
'customresponse',
'topic',
'article'
'article',
'tag'
]);
}
}
}

20
server/models/Tag.php Normal file
View File

@ -0,0 +1,20 @@
<?php
//documentacion
class Tag extends DataStore {
const TABLE = 'tag';
public static function getProps() {
return [
'name',
'color'
];
}
public function toArray() {
return[
'id'=> $this->id,
'name'=> $this->name,
'color' => $this->color
];
}
}

View File

@ -50,7 +50,7 @@ require './system/add-department.rb'
require './system/edit-department.rb'
require './system/delete-department.rb'
require './staff/last-events.rb'
require './system/mail-templates.rb'
# require './system/mail-templates.rb'
require './system/disable-registration.rb'
require './system/enable-registration.rb'
require './system/add-api-key.rb'
@ -60,3 +60,7 @@ require './system/file-upload-download.rb'
require './system/csv-import.rb'
require './system/disable-user-system.rb'
require './system/get-stats.rb'
require './ticket/add-tag.rb'
require './ticket/edit-tag.rb'
require './ticket/get-tags.rb'
require './ticket/delete-tag.rb'

114
tests/ticket/add-tag.rb Normal file
View File

@ -0,0 +1,114 @@
describe '/ticket/add-tag' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
Scripts.createStaff('lvl1@opensupports.com', 'pass1', 'name1','1')
Scripts.createStaff('lvl2@opensupports.com', 'pass2', 'name2','2')
it 'should add a tag if is a Staff 3 logged' do
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
name: 'tag1',
color: 'blue'
})
tag = $database.getRow('tag', 1 , 'id')
(request['status']).should.equal('success')
(tag['name']).should.equal('tag1')
end
it 'should add a tag if a Staff 1 is logged' do
request('/user/logout')
Scripts.login('lvl1@opensupports.com', 'pass1',true)
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
name: 'tag2',
color: 'red'
})
tag = $database.getRow('tag', 2 , 'id')
(result['status']).should.equal('success')
(tag['name']).should.equal('tag2')
end
it 'should add a tag if a Staff 2 is logged' do
request('/user/logout')
Scripts.login('lvl2@opensupports.com', 'pass2',true)
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
name: 'tag3',
color:'green'
})
tag = $database.getRow('tag', 3 , 'id')
(result['status']).should.equal('success')
(tag['name']).should.equal('tag3')
end
it 'should fail if the name is invalid' do
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
color: 'black'
})
(result['status']).should.equal('fail')
(result['message']).should.equal('INVALID_NAME')
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
name: 'T',
color: 'black'
})
(result['status']).should.equal('fail')
(result['message']).should.equal('INVALID_NAME')
long_text = ''
200.times {long_text << 'a'}
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
name: long_text,
color: 'black'
})
(result['status']).should.equal('fail')
(result['message']).should.equal('INVALID_NAME')
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
name: 'tag1',
color: 'black'
})
(result['status']).should.equal('fail')
(result['message']).should.equal('TAG_EXISTS')
end
it 'should fail if a user is logged' do
request('/user/logout')
Scripts.login()
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
name: 'usertag',
color: 'pink'
})
(result['status']).should.equal('fail')
(result['message']).should.equal('NO_PERMISSION')
request('/user/logout')
end
end

View File

@ -0,0 +1,66 @@
describe '/ticket/delete-tag' do
it 'should fail if a user is logged' do
request('/user/logout')
Scripts.login()
result = request('/ticket/delete-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 1
})
(result['status']).should.equal('fail')
(result['message']).should.equal('NO_PERMISSION')
request('/user/logout')
end
Scripts.login($staff[:email], $staff[:password], true)
it 'should delete a tag if is a Staff 3 logged' do
result = request('/ticket/delete-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 1
})
(request['status']).should.equal('success')
end
it 'should delete a tag if a Staff 1 is logged' do
request('/user/logout')
Scripts.login('lvl1@opensupports.com', 'pass1',true)
result = request('/ticket/add-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 2
})
(result['status']).should.equal('success')
end
it 'should delete a tag if a Staff 2 is logged' do
request('/user/logout')
Scripts.login('lvl2@opensupports.com', 'pass2',true)
result = request('/ticket/delete-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 3
})
(result['status']).should.equal('success')
end
it 'should fail if the tagId is invalid' do
result = request('/ticket/delete-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 1000
})
(result['status']).should.equal('fail')
(result['message']).should.equal('INVALID_TAG')
end
end

99
tests/ticket/edit-tag.rb Normal file
View File

@ -0,0 +1,99 @@
describe '/ticket/edit-tag' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
it 'should edit a tag if is a Staff 3 logged' do
result = request('/ticket/edit-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 1,
name: 'TAG1',
color: 'yellow'
})
tag = $database.getRow('tag', 1, 'id')
(tag['name']).should.equal('TAG1')
(tag['color']).should.equal('yellow')
(request['status']).should.equal('success')
end
it 'should edit a tag if a Staff 1 is logged' do
request('/user/logout')
Scripts.login('lvl1@opensupports.com', 'pass1',true)
result = request('/ticket/edit-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 2,
name:'TAG2',
color:'orange'
})
tag = $database.getRow('tag', 2 , 'id')
(result['status']).should.equal('success')
(tag['name']).should.equal('TAG2')
(tag['color']).should.equal('orange')
end
it 'should edit a tag if a Staff 2 is logged' do
request('/user/logout')
Scripts.login('lvl2@opensupports.com', 'pass2',true)
result = request('/ticket/edit-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 3,
name: 'TAG3',
color: 'grey'
})
tag = $database.getRow('tag', 3 , 'id')
(tag['name']).should.equal('TAG3')
(tag['color']).should.equal('grey')
(result['status']).should.equal('success')
end
it 'should fail if the name already exists' do
result = request('/ticket/edit-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 1,
name: 'TAG1'
})
(result['status']).should.equal('fail')
(result['message']).should.equal('TAG_EXISTS')
end
it 'should fail if the tagId is invalid' do
result = request('/ticket/edit-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 100
})
(result['status']).should.equal('fail')
(result['message']).should.equal('INVALID_TAG')
end
it 'should fail if a user is logged' do
request('/user/logout')
Scripts.login()
result = request('/ticket/edit-tag', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
tagId: 1,
name: 'usertag',
color:'pink'
})
(result['status']).should.equal('fail')
(result['message']).should.equal('NO_PERMISSION')
request('/user/logout')
end
end

70
tests/ticket/get-tags.rb Normal file
View File

@ -0,0 +1,70 @@
describe '/ticket/get-tags' do
it 'should fail if a user is logged' do
request('/user/logout')
Scripts.login()
result = request('/ticket/get-tags', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(result['status']).should.equal('fail')
(result['message']).should.equal('NO_PERMISSION')
request('/user/logout')
end
Scripts.login($staff[:email], $staff[:password], true)
it 'should get the tags if is a Staff 3 logged' do
result = request('/ticket/get-tags', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(request['status']).should.equal('success')
(request['data'][0]['name']).should.equal('TAG1')
(request['data'][0]['color']).should.equal('yellow')
(request['data'][1]['name']).should.equal('TAG2')
(request['data'][1]['color']).should.equal('orange')
(request['data'][2]['name']).should.equal('TAG3')
(request['data'][2]['color']).should.equal('grey')
end
it 'should get the tags if a Staff 1 is logged' do
request('/user/logout')
Scripts.login('lvl1@opensupports.com', 'pass1',true)
result = request('/ticket/get-tags', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(request['status']).should.equal('success')
(request['data'][0]['name']).should.equal('TAG1')
(request['data'][0]['color']).should.equal('yellow')
(request['data'][1]['name']).should.equal('TAG2')
(request['data'][1]['color']).should.equal('orange')
(request['data'][2]['name']).should.equal('TAG3')
(request['data'][2]['color']).should.equal('grey')
end
it 'should get the tags if a Staff 2 is logged' do
request('/user/logout')
Scripts.login('lvl2@opensupports.com', 'pass2',true)
result = request('/ticket/get-tags', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(request['status']).should.equal('success')
(request['data'][0]['name']).should.equal('TAG1')
(request['data'][0]['color']).should.equal('yellow')
(request['data'][1]['name']).should.equal('TAG2')
(request['data'][1]['color']).should.equal('orange')
(request['data'][2]['name']).should.equal('TAG3')
(request['data'][2]['color']).should.equal('grey')
end
end