diff --git a/server/controllers/ticket.php b/server/controllers/ticket.php index 3648e604..1dc5b23c 100755 --- a/server/controllers/ticket.php +++ b/server/controllers/ticket.php @@ -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(); diff --git a/server/controllers/ticket/add-tag.php b/server/controllers/ticket/add-tag.php new file mode 100644 index 00000000..052d8578 --- /dev/null +++ b/server/controllers/ticket/add-tag.php @@ -0,0 +1,62 @@ + '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(); + } +} diff --git a/server/controllers/ticket/delete-tag.php b/server/controllers/ticket/delete-tag.php new file mode 100644 index 00000000..76d7f855 --- /dev/null +++ b/server/controllers/ticket/delete-tag.php @@ -0,0 +1,50 @@ + '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(); + } +} diff --git a/server/controllers/ticket/edit-tag.php b/server/controllers/ticket/edit-tag.php new file mode 100644 index 00000000..5ce4ba4d --- /dev/null +++ b/server/controllers/ticket/edit-tag.php @@ -0,0 +1,61 @@ + '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(); + } +} diff --git a/server/controllers/ticket/get-tags.php b/server/controllers/ticket/get-tags.php new file mode 100644 index 00000000..3052f70c --- /dev/null +++ b/server/controllers/ticket/get-tags.php @@ -0,0 +1,39 @@ + 'staff_1', + 'requestData' => [] + ]; + } + + public function handler() { + $tags = Tag::getAll(); + + Response::respondSuccess($tags->toArray()); + } +} diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index add61421..227a715c 100755 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -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'; diff --git a/server/libs/validations/dataStoreId.php b/server/libs/validations/dataStoreId.php index 029ee33c..7bf02427 100755 --- a/server/libs/validations/dataStoreId.php +++ b/server/libs/validations/dataStoreId.php @@ -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' ]); } -} \ No newline at end of file +} diff --git a/server/models/Tag.php b/server/models/Tag.php new file mode 100644 index 00000000..7d1ea08c --- /dev/null +++ b/server/models/Tag.php @@ -0,0 +1,20 @@ + $this->id, + 'name'=> $this->name, + 'color' => $this->color + ]; + } +} diff --git a/tests/init.rb b/tests/init.rb index cd719892..7f369bd8 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -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' diff --git a/tests/ticket/add-tag.rb b/tests/ticket/add-tag.rb new file mode 100644 index 00000000..a40b20fe --- /dev/null +++ b/tests/ticket/add-tag.rb @@ -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 diff --git a/tests/ticket/delete-tag.rb b/tests/ticket/delete-tag.rb new file mode 100644 index 00000000..a5274523 --- /dev/null +++ b/tests/ticket/delete-tag.rb @@ -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 diff --git a/tests/ticket/edit-tag.rb b/tests/ticket/edit-tag.rb new file mode 100644 index 00000000..ea0623e0 --- /dev/null +++ b/tests/ticket/edit-tag.rb @@ -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 diff --git a/tests/ticket/get-tags.rb b/tests/ticket/get-tags.rb new file mode 100644 index 00000000..30d2bd13 --- /dev/null +++ b/tests/ticket/get-tags.rb @@ -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