From 673a92c1964b4f8837a94f0f7282e8491d316341 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Sun, 10 Feb 2019 20:52:42 -0300 Subject: [PATCH 1/2] Remove file uploading from email polling --- server/controllers/system/email-polling.php | 19 ++++++++++++------- server/libs/Controller.php | 1 - 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/server/controllers/system/email-polling.php b/server/controllers/system/email-polling.php index c409d192..e9508da1 100755 --- a/server/controllers/system/email-polling.php +++ b/server/controllers/system/email-polling.php @@ -41,7 +41,7 @@ class EmailPollingController extends Controller { $errors = []; $emails = $this->getLastEmails(); -/* + $session = Session::getInstance(); $oldSession = [ 'userId' => $session->getUserId(), @@ -70,9 +70,9 @@ class EmailPollingController extends Controller { return null; }); - - if($email->getAttachement()) { - $attachment = $email->getAttachement(); + /* + if($email->getAttachment()) { + $attachment = $email->getAttachment(); $_FILES['file'] = [ 'name' => $attachment->name, 'type' => mime_content_type($attachment->filePath), @@ -81,6 +81,7 @@ class EmailPollingController extends Controller { 'size' => filesize($attachment->filePath), ]; } + */ try { if($email->isReply()) { @@ -113,7 +114,7 @@ class EmailPollingController extends Controller { Response::respondError(ERRORS::EMAIL_POLLING, null, $errors); } else { Response::respondSuccess(); - }*/ + } } public function getLastEmails() { @@ -124,15 +125,19 @@ class EmailPollingController extends Controller { foreach($mailsIds as $mailId) { $mail = $this->mailbox->getMail($mailId); $mailHeader = $this->mailbox->getMailHeader($mailId); - $mailAttachment = count($mail->getAttachments()) ? $mail->getAttachments()[0] : null; + // $mailAttachment = count($mail->getAttachments()) ? current($mail->getAttachments()) : null; $emails[] = new Email([ 'fromAddress' => $mailHeader->fromAddress, 'fromName' => $mailHeader->fromName, 'subject' => $mailHeader->subject, 'content' => $mail->textPlain, - 'file' => $mailAttachment, + 'file' => null, ]); + + foreach($mail->getAttachments() as $attachment) { + unlink($attachment->filePath); + } } return $emails; diff --git a/server/libs/Controller.php b/server/libs/Controller.php index de16fff4..e8c61ff2 100755 --- a/server/libs/Controller.php +++ b/server/libs/Controller.php @@ -134,7 +134,6 @@ abstract class Controller { $fileUploader->setMaxSize($maxSize); if($fileUploader->upload('file')) { - return $fileUploader; } else { throw new RequestException(ERRORS::INVALID_FILE); From f6f8262880b2ab85fb220a74d5b865512408486f Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Tue, 12 Feb 2019 23:37:27 -0300 Subject: [PATCH 2/2] Add custom fields test --- .../system/delete-custom-field.php | 8 +- server/libs/validations/dataStoreId.php | 6 +- tests/init.rb | 1 + tests/libs.rb | 2 +- tests/scripts.rb | 10 + tests/system/custom-fields.rb | 251 ++++++++++++++++++ 6 files changed, 274 insertions(+), 4 deletions(-) create mode 100644 tests/system/custom-fields.rb diff --git a/server/controllers/system/delete-custom-field.php b/server/controllers/system/delete-custom-field.php index 9049d44e..f1bb1274 100644 --- a/server/controllers/system/delete-custom-field.php +++ b/server/controllers/system/delete-custom-field.php @@ -41,16 +41,20 @@ class DeleteCustomFieldController extends Controller { public function handler() { $customField = Customfield::getDataStore(Controller::request('id')); - foreach(Users::getAll() as $user) { - $customFieldValueList = $user->xownCustomfieldvalueList || []; + foreach(User::getAll() as $user) { + $customFieldValueList = $user->xownCustomfieldvalueList ? $user->xownCustomfieldvalueList : []; foreach($customFieldValueList as $customFieldValue) { if($customFieldValue->customfield->id == $customField->id) { $user->xownCustomfieldvalueList->remove($customFieldValue); } } + + $user->store(); } $customField->delete(); + + Response::respondSuccess(); } } diff --git a/server/libs/validations/dataStoreId.php b/server/libs/validations/dataStoreId.php index 029ee33c..d38a6098 100755 --- a/server/libs/validations/dataStoreId.php +++ b/server/libs/validations/dataStoreId.php @@ -40,6 +40,9 @@ class DataStoreId extends AbstractRule { case 'article': $dataStore = \Article::getDataStore($dataStoreId); break; + case 'customfield': + $dataStore = \Customfield::getDataStore($dataStoreId); + break; } return !$dataStore->isNull(); @@ -53,7 +56,8 @@ class DataStoreId extends AbstractRule { 'department', 'customresponse', 'topic', - 'article' + 'article', + 'customfield' ]); } } \ No newline at end of file diff --git a/tests/init.rb b/tests/init.rb index 208ba0bf..3feb19cc 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -62,5 +62,6 @@ require './system/delete-api-key.rb' require './system/get-api-keys.rb' require './system/file-upload-download.rb' require './system/csv-import.rb' +require './system/custom-fields.rb' require './system/disable-user-system.rb' require './system/get-stats.rb' diff --git a/tests/libs.rb b/tests/libs.rb index 279a5bf3..178ed3e6 100644 --- a/tests/libs.rb +++ b/tests/libs.rb @@ -141,7 +141,7 @@ end $database = Database.new # $mail_server.clear_mails -# $mail_server.send_mail('suppport message 1', 'texttexttext tex') +# $mail_server.send_mail('support message 1', 'texttexttext tex') # $mail_server.check $staff = { diff --git a/tests/scripts.rb b/tests/scripts.rb index 64872afc..84b7d5a4 100644 --- a/tests/scripts.rb +++ b/tests/scripts.rb @@ -97,4 +97,14 @@ class Scripts name: name }) end + + def self.createTextCustomField(name,description) + request('/system/add-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: name, + type: 'text', + description: description + }) + end end diff --git a/tests/system/custom-fields.rb b/tests/system/custom-fields.rb new file mode 100644 index 00000000..9a09decf --- /dev/null +++ b/tests/system/custom-fields.rb @@ -0,0 +1,251 @@ +describe 'Custom fields' do + request('/user/logout') + Scripts.login($staff[:email], $staff[:password], true) + + describe '/system/add-custom-field' do + + it 'should fail if the name is to short ' do + result = request('/system/add-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: 'A', + type: 'text', + description: 'custom field description', + options: nil + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('INVALID_NAME') + + end + + it 'should fail if the name is to long' do + long_text = '' + 101.times {long_text << 'A'} + + result = request('/system/add-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: long_text, + type: 'text', + description: 'custom field description', + options: nil + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('INVALID_NAME') + end + + it 'should fail if the type is not one of text or select'do + result = request('/system/add-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: 'name of custom field', + type: 'tex', + description: 'custom field description' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('INVALID_CUSTOM_FIELD_TYPE') + + result = request('/system/add-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: 'name of custom field', + type: 'selec', + description: 'custom field description' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('INVALID_CUSTOM_FIELD_TYPE') + end + + it 'should fail if the option is invalid' do + result = request('/system/add-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: 'name of custom field', + type: 'select', + description: 'custom field description', + options: 'json' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('INVALID_CUSTOM_FIELD_OPTIONS') + end + + it 'should create name of select customfield' do + result = request('/system/add-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: 'mockselectfield', + type: 'select', + description: 'custom field description', + options: '["option1","option2","option3"]' + }) + + (result['status']).should.equal('success') + + custom_field_row = $database.getRow('customfield', 'mockselectfield', 'name') + custom_field_row_id = custom_field_row['id'] + + (custom_field_row['name']).should.equal('mockselectfield') + (custom_field_row['type']).should.equal('select') + (custom_field_row['description']).should.equal('custom field description') + + ($database.getRow('customfieldoption', 'option1', 'name')['customfield_id']).should.equal(custom_field_row_id) + ($database.getRow('customfieldoption', 'option2', 'name')['customfield_id']).should.equal(custom_field_row_id) + ($database.getRow('customfieldoption', 'option3', 'name')['customfield_id']).should.equal(custom_field_row_id) + + quantity_of_options = $database.query("SELECT COUNT(*) as qt FROM customfieldoption WHERE customfield_id='#{custom_field_row_id}'").fetch_hash['qt'] + (quantity_of_options).should.equal("3") + end + + it 'should fail if field name already exists' do + result = request('/system/add-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: 'mockselectfield', + type: 'select', + description: 'custom field description', + options: '["option1","option2","option3"]' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('CUSTOM_FIELD_ALREADY_EXISTS') + end + end + + describe '/system/get-custom-fields' do + + it 'should success and shows all custom fields' do + Scripts.createTextCustomField('mocktextfield1','description number 1') + Scripts.createTextCustomField('mocktextfield2','description number 2') + Scripts.createTextCustomField('mocktextfield3',nil) + + result = request('/system/get-custom-fields', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (result['status']).should.equal('success') + (result['data'].size).should.equal(4) + + result['data'][0]['name'].should.equal('mockselectfield') + result['data'][0]['type'].should.equal('select') + result['data'][0]['description'].should.equal('custom field description') + result['data'][0]['options'].should.equal([ + {'id' => '1', 'name'=> 'option1'}, + {'id' => '2', 'name'=> 'option2'}, + {'id' => '3', 'name'=> 'option3'}, + ]) + result['data'][1]['name'].should.equal('mocktextfield1') + result['data'][1]['type'].should.equal('text') + result['data'][1]['description'].should.equal('description number 1') + result['data'][2]['name'].should.equal('mocktextfield2') + result['data'][2]['type'].should.equal('text') + result['data'][2]['description'].should.equal('description number 2') + result['data'][3]['name'].should.equal('mocktextfield3') + result['data'][3]['type'].should.equal('text') + result['data'][3]['description'].should.equal('') + end + end + + + describe '/user/edit-custom-fields' do + + it 'should add custom field values to user' do + last_user_id = $database.getLastRow('user')['id'] + result = request('/user/edit-custom-fields', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + userId: last_user_id, + customfield_mockselectfield: 'option2', + customfield_mocktextfield3: 'mockvalue', + }) + (result['status']).should.equal('success') + + result = request('/user/get-user', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + userId: last_user_id, + }) + + (result['status']).should.equal('success') + (result['data']['customfields'][0]['customfield']).should.equal('mockselectfield') + (result['data']['customfields'][0]['value']).should.equal('option2') + (result['data']['customfields'][0]['customfieldoption']['id']).should.equal('2') + (result['data']['customfields'][0]['customfieldoption']['name']).should.equal('option2') + + (result['data']['customfields'][1]['customfield']).should.equal('mocktextfield3') + (result['data']['customfields'][1]['value']).should.equal('mockvalue') + end + + it 'should allow changes' do + last_user_id = $database.getLastRow('user')['id'] + result = request('/user/edit-custom-fields', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + userId: last_user_id, + customfield_mockselectfield: 'option3', + customfield_mocktextfield3: 'mockvalue2', + }) + (result['status']).should.equal('success') + + result = request('/user/get-user', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + userId: last_user_id, + }) + + (result['status']).should.equal('success') + (result['data']['customfields'].size).should.equal(2) + (result['data']['customfields'][0]['customfield']).should.equal('mockselectfield') + (result['data']['customfields'][0]['value']).should.equal('option3') + (result['data']['customfields'][0]['customfieldoption']['id']).should.equal('3') + (result['data']['customfields'][0]['customfieldoption']['name']).should.equal('option3') + + (result['data']['customfields'][1]['customfield']).should.equal('mocktextfield3') + (result['data']['customfields'][1]['value']).should.equal('mockvalue2') + end + end + + describe '/system/delete-custom-field' do + + it 'should fail if is an invalid custom field ' do + result = request('/system/delete-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + id: 100 + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('INVALID_CUSTOM_FIELD') + + end + it 'should success if everything is ok' do + result = request('/system/delete-custom-field', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + id: 1 + }) + + (result['status']).should.equal('success') + + custom_field_row = $database.getRow('customfield', 1, 'id') + (custom_field_row).should.equal(nil) + + last_user_id = $database.getLastRow('user')['id'] + result = request('/user/get-user', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + userId: last_user_id, + }) + + (result['status']).should.equal('success') + (result['data']['customfields'].size).should.equal(1) + (result['data']['customfields'][0]['customfield']).should.equal('mocktextfield3') + (result['data']['customfields'][0]['value']).should.equal('mockvalue2') + end + end +end