diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index f66b815c..6b10b00a 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -44,8 +44,7 @@ class CreateController extends Controller { $this->language = Controller::request('language'); } - private function storeTicket() - { + private function storeTicket() { $department = Department::getDataStore($this->departmentId); $author = Controller::getLoggedUser(); diff --git a/server/libs/DataStoreList.php b/server/libs/DataStoreList.php index 1a6d1b93..88babde2 100644 --- a/server/libs/DataStoreList.php +++ b/server/libs/DataStoreList.php @@ -28,6 +28,7 @@ class DataStoreList { $beanList = []; foreach($this->list as $item) { + $item->updateBeanProperties(); $beanList[] = $item->getBeanInstance(); } diff --git a/server/models/DataStore.php b/server/models/DataStore.php index 01e95404..2799c289 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -92,6 +92,7 @@ abstract class DataStore { } public function store() { + $this->updateBeanProperties(); return RedBean::store($this->getBeanInstance()); } @@ -102,8 +103,6 @@ abstract class DataStore { } public function getBeanInstance() { - $this->updateBeanProperties(); - return $this->_bean; } @@ -111,7 +110,7 @@ abstract class DataStore { return false; } - private function updateBeanProperties() { + public function updateBeanProperties() { foreach ($this->properties as $key => $prop) { $this->updateBeanProp($key, $prop); } diff --git a/tests/Gemfile b/tests/Gemfile index a5cabf73..5e7043c7 100644 --- a/tests/Gemfile +++ b/tests/Gemfile @@ -1,3 +1,4 @@ source "https://rubygems.org" gem 'mysql' gem 'bacon' +gem 'mechanize' diff --git a/tests/init.rb b/tests/init.rb index 5c5f792b..9c471f2e 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -4,6 +4,7 @@ require 'net/http' require 'uri' require 'mysql' require 'json' +require 'mechanize' require './libs.rb' require './scripts.rb' @@ -13,4 +14,5 @@ require './user/signup.rb' require './user/login.rb' require './user/send-recover-password.rb' require './user/recover-password.rb' -#require './ticket/create.rb' +require './ticket/create.rb' +require './ticket/comment.rb' diff --git a/tests/libs.rb b/tests/libs.rb index 1da036ee..4357cb07 100644 --- a/tests/libs.rb +++ b/tests/libs.rb @@ -1,7 +1,8 @@ +@@agent = Mechanize.new def request(path, data = {}) - uri = URI('http://localhost:8080' + path) - response = Net::HTTP.post_form(uri, data) + uri = 'http://localhost:8080' + path + response = @@agent.post(uri, data) return JSON.parse(response.body) end diff --git a/tests/ticket/comment.rb b/tests/ticket/comment.rb index 3b2b12b1..f6364be2 100644 --- a/tests/ticket/comment.rb +++ b/tests/ticket/comment.rb @@ -1,14 +1,23 @@ describe 'ticket/comment/' do - it 'should fail if not logged' do + #it 'should fail if not logged' do - end + #end - describe 'on successful request' do - it 'should add comment to current ticket' do + describe 'on successful request' do + it 'should add comment to current ticket' do + result = request('/ticket/comment', { + content: 'some commment content', + ticketId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (result['status']).should.equal('success') + end + + # it 'should link the comment to author' do + + # end end - it 'should link the comment to author' do - - end - end end \ No newline at end of file diff --git a/tests/ticket/create.rb b/tests/ticket/create.rb index 5d3cd1f3..54fd5039 100644 --- a/tests/ticket/create.rb +++ b/tests/ticket/create.rb @@ -1,56 +1,79 @@ describe '/ticket/create' do - it 'should fail if title is too short' do - result = request('/ticket/create', { - title: 'GG' - }) - - (result['status']).should.equal('fail') - (result['message']).should.equal('Invalid title') - - end - - it 'should fail if title is very long' do - result = request('/ticket/create',{ - title: 'I WISH I WAS THE MONSTER YOU THINK I AM. -Tyrion' - }) - - (result['status']).should.equal('fail') - (result['message']).should.equal('Invalid title') - - end - - it 'should fail if content is too short' do - result = request('/ticket/create',{ - title: 'Winter is coming', - content: 'Test' - }) - - (result['status']).should.equal('fail') - (result['message']).should.equal('Invalid content') - end - - it 'should fail if content is very long' do - long_text = '' - 600.times {long_text << 'a'} - - result = request('/ticket/create',{ - title: 'Winter is coming', - content: long_text + request('/user/logout') + result = request('/user/login', { + email: 'steve@jobs.com', + password: 'custom' }) - (result['status']).should.equal('fail') - (result['message']).should.equal('Invalid content') + $csrf_userid = result['data']['userId'] + $csrf_token = result['data']['token'] - end + it 'should fail if title is too short' do + result = request('/ticket/create', { + title: 'GG', + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) - it 'should create ticket if pass data is valid' do - result = request('/ticket/create',{ - title: 'Winter is coming', - content: 'The north remembers' - }) + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid title') + end - (result['status']).should.equal('success') - ticket = $database.getRow('ticket','Winter is coming','title') - (ticket['content']).should.equal('The north remembers') - end + it 'should fail if title is very long' do + result = request('/ticket/create',{ + title: 'I WISH I WAS THE MONSTER YOU THINK I AM. -Tyrion', + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid title') + end + + it 'should fail if content is too short' do + result = request('/ticket/create', { + title: 'Winter is coming', + content: 'Test', + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid content') + end + + it 'should fail if content is very long' do + long_text = '' + 600.times {long_text << 'a'} + + result = request('/ticket/create',{ + title: 'Winter is coming', + content: long_text, + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid content') + + end + + it 'should create ticket if pass data is valid' do + result = request('/ticket/create',{ + title: 'Winter is coming', + content: 'The north remembers', + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + puts result['message'] + (result['status']).should.equal('success') + ticket = $database.getRow('ticket','Winter is coming','title') + (ticket['content']).should.equal('The north remembers') + end end \ No newline at end of file