Skip to content

Commit

Permalink
Converts Response to Msg, removes react-toastify
Browse files Browse the repository at this point in the history
  • Loading branch information
cayb0rg committed Nov 15, 2023
1 parent 6635291 commit 19476a7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 68 deletions.
2 changes: 1 addition & 1 deletion fuel/app/classes/controller/api/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Controller_Api_Instance extends Controller_Rest
*/
public function get_history()
{
if ( ! $inst_id = Input::get('inst_id')) return $this->response(new \Materia\Msg('Requires an inst_id parameter!', \Materia\Msg::ERROR), 401);
if ( ! $inst_id = Input::get('inst_id')) return $this->response(\Materia\Msg::invalid_input('Requires an inst_id parameter!'), 401);
if ( ! \Materia\Util_Validator::is_valid_hash($inst_id) ) return $this->response(\Materia\Msg::invalid_input($inst_id), 401);
if ( ! ($inst = \Materia\Widget_Instance_Manager::get($inst_id))) return $this->response(new \Materia\Msg('Instance not found', \Materia\Msg::ERROR), 404);
if ( ! \Materia\Perm_Manager::user_has_any_perm_to(\Model_User::find_current_id(), $inst_id, \Materia\Perm::INSTANCE, [\Materia\Perm::FULL])) return $this->response(\Materia\Msg::no_login(), 401);
Expand Down
35 changes: 18 additions & 17 deletions fuel/app/classes/materia/msg.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,54 +35,55 @@ class Msg
public $title;
public $type;

public function __construct($msg, $title='', $type='error', $halt=false)
public function __construct($msg, $title='', $type='error', $halt=false, $status=403)
{
$this->type = $type;
$this->title = $title;
$this->msg = $msg;
$this->halt = $halt;
$this->msg = $msg;
$this->title = $title;
$this->type = $type;
$this->halt = $halt;
$this->status = $status;
}

static public function invalid_input($msg = '', $title = 'Validation Error')
{
$msg = new Msg($msg, $title, Msg::ERROR, true);
return new \Response(json_encode($msg), 403);
return $msg;
}

static public function no_login()
{
$msg = new Msg('You have been logged out, and must login again to continue', 'Invalid Login', Msg::ERROR, true);
\Session::set_flash('login_error', $msg->msg);
return new \Response(json_encode($msg), 403);
return $msg;
}

static public function no_perm($msg = 'You do not have permission to access the requested content', $title = 'Permission Denied')
{
$msg = new Msg($msg, $title, Msg::WARN);
return new \Response(json_encode($msg), 401);
$msg = new Msg($msg, $title, Msg::WARN, false, 401);
return $msg;
}

static public function student_collab()
{
$msg = new Msg('Students cannot be added as collaborators to widgets that have guest access disabled.', 'Share Not Allowed', Msg::ERROR);
return new \Response(json_encode($msg), 401);
$msg = new Msg('Students cannot be added as collaborators to widgets that have guest access disabled.', 'Share Not Allowed', Msg::ERROR, false, 401);
return $msg;
}

static public function student()
{
$msg = new Msg('Students are unable to receive notifications via Materia', 'No Notifications', Msg::NOTICE);
return new \Response(json_encode($msg), 403);
$msg = new Msg('Students are unable to receive notifications via Materia', 'No Notifications', Msg::NOTICE, false, 403);
return $msg;
}

static public function failure($msg = 'The requested action could not be completed', $title = 'Action Failed')
{
$msg = new Msg($msg, $title, Msg::ERROR);
return new \Response(json_encode($msg), 403);
$msg = new Msg($msg, $title, Msg::ERROR, false, 403);
return $msg;
}

static public function not_found($msg = 'The requested content could not be found', $title = 'Not Found')
{
$msg = new Msg($msg, $title, Msg::ERROR);
return new \Response(json_encode($msg), 404);
$msg = new Msg($msg, $title, Msg::ERROR, false, 404);
return $msg;
}
}
39 changes: 16 additions & 23 deletions fuel/app/tests/api/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,8 @@ public function test_widget_instance_new()
$qset = $this->create_new_qset($question, $answer);

$output = Api_V1::widget_instance_new($widget->id, 'test', $qset, false);
$this->assertInstanceOf('\Response', $output);
$body = json_decode($output->body);
$this->assertEquals('Widget type can not be published by students.', $body->msg);
$this->assertInstanceOf('\Materia\Msg', $output);
$this->assertEquals('Widget type can not be published by students.', $output->msg);
}

public function test_widget_instance_update()
Expand Down Expand Up @@ -622,9 +621,8 @@ public function test_widget_instance_edit_perms_verify(): void
// ======= AS NO ONE ========
\Auth::logout();
$output = Api_V1::widget_instance_edit_perms_verify($instance->id);
$this->assertInstanceOf('\Response', $output);
$body = json_decode($output->body);
$this->assertEquals('Invalid Login', $body->title);
$this->assertInstanceOf('\Materia\Msg', $output);
$this->assertEquals('Invalid Login', $output->title);

// ======= STUDENT ========
$this->_as_student();
Expand Down Expand Up @@ -663,9 +661,8 @@ public function test_widget_publish_perms_verify(): void
// ======= AS NO ONE ========
\Auth::logout();
$output = Api_V1::widget_publish_perms_verify($widget->id);
$this->assertInstanceOf('\Response', $output);
$body = json_decode($output->body);
$this->assertEquals('Invalid Login', $body->title);
$this->assertInstanceOf('\Materia\Msg', $output);
$this->assertEquals('Invalid Login', $output->title);

// ======= STUDENT ========
$this->_as_student();
Expand Down Expand Up @@ -768,9 +765,8 @@ public function test_session_play_create()

// this should fail - you cant play drafts
$output = Api_V1::session_play_create($saveOutput->id);
$this->assertInstanceOf('\Response', $output);
$body = json_decode($output->body);
$this->assertEquals('Drafts Not Playable', $body->title);
$this->assertInstanceOf('\Materia\Msg', $output);
$this->assertEquals('Drafts Not Playable', $output->title);

Api_V1::widget_instance_delete($saveOutput->id);

Expand Down Expand Up @@ -1439,7 +1435,7 @@ public function test_notification_delete(){
// ======= STUDENT ========
$this->_as_student();
$output = Api_V1::notification_delete(5, false);
$this->assertInstanceOf('\Response', $output);
$this->assertInstanceOf('\Materia\Msg', $output);

$author = $this->_as_author();
$notifications = Api_V1::notifications_get();
Expand All @@ -1466,7 +1462,7 @@ public function test_notification_delete(){
// try as someone author2
$this->_as_author_2();
$output = Api_V1::notification_delete($notifications[0]['id'], false);
$this->assertInstanceOf('\Response', $output);
$this->assertInstanceOf('\Materia\Msg', $output);

$this->_as_author();
$output = Api_V1::notification_delete($notifications[0]['id'], false);
Expand Down Expand Up @@ -1590,22 +1586,19 @@ protected function assert_not_message($result)

protected function assert_invalid_login_message($msg)
{
$this->assertInstanceOf('\Response', $msg);
$body = json_decode($msg->body);
$this->assertEquals('Invalid Login', $body->title);
$this->assertInstanceOf('\Materia\Msg', $msg);
$this->assertEquals('Invalid Login', $msg->title);
}

protected function assert_permission_denied_message($msg)
{
$this->assertInstanceOf('\Response', $msg);
$body = json_decode($msg->body);
$this->assertEquals('Permission Denied', $body->title);
$this->assertInstanceOf('\Materia\Msg', $msg);
$this->assertEquals('Permission Denied', $msg->title);
}

protected function assert_validation_error_message($msg)
{
$this->assertInstanceOf('\Response', $msg);
$body = json_decode($msg->body);
$this->assertEquals('Validation Error', $body->title);
$this->assertInstanceOf('\Materia\Msg', $msg);
$this->assertEquals('Validation Error', $msg->title);
}
}
29 changes: 15 additions & 14 deletions fuel/app/tests/controller/api/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function test_get_history()
->execute()
->response();

$output = json_decode($response->body);
$this->assertEquals($response->status, 401);
$body = json_decode($response->body);
$this->assertEquals($body->msg, 'Requires an inst_id parameter!');
$this->assertEquals($output->msg, 'Requires an inst_id parameter!');

// ======= NO INST ID FOUND ========
$response = Request::forge('/api/instance/history')
Expand All @@ -31,9 +31,10 @@ public function test_get_history()
->execute()
->response();

$output = json_decode($response->body);

$this->assertEquals($response->status, 404);
$body = json_decode($response->body);
$this->assertEquals($body->msg, 'Instance not found');
$this->assertEquals($output->msg, 'Instance not found');

// == Now we're an author
$this->_as_author();
Expand Down Expand Up @@ -70,9 +71,9 @@ public function test_post_request_access()
->execute()
->response();

$output = json_decode($response->body);
$this->assertEquals($response->status, 401);
$body = json_decode($response->body);
$this->assertEquals($body->msg, 'Requires an inst_id parameter');
$this->assertEquals($output->msg, 'Requires an inst_id parameter');

// ======= NO OWNER ID PROVIDED ========
$response = Request::forge('/api/instance/request_access')
Expand All @@ -81,9 +82,9 @@ public function test_post_request_access()
->execute()
->response();

$output = json_decode($response->body);
$this->assertEquals($response->status, 401);
$body = json_decode($response->body);
$this->assertEquals($body->msg, 'Requires an owner_id parameter');
$this->assertEquals($output->msg, 'Requires an owner_id parameter');

// == Now we're an author
$this->_as_author();
Expand All @@ -105,9 +106,9 @@ public function test_post_request_access()
->execute()
->response();

$body = json_decode($response->body);
$this->assertEquals($body->msg, 'Instance not found');
$output = json_decode($response->body);
$this->assertEquals($response->status, 404);
$this->assertEquals($output->msg, 'Instance not found');

// ======= NO OWNER ID FOUND ========
$response = Request::forge('/api/instance/request_access')
Expand All @@ -117,9 +118,9 @@ public function test_post_request_access()
->execute()
->response();

$output = json_decode($response->body);
$this->assertEquals($response->status, 404);
$body = json_decode($response->body);
$this->assertEquals($body->msg, 'Owner not found');
$this->assertEquals($output->msg, 'Owner not found');

// ======= OWNER DOES NOT OWN INSTANCE =========
// Switch users
Expand All @@ -132,9 +133,9 @@ public function test_post_request_access()
->execute()
->response();

$output = json_decode($response->body);
$this->assertEquals($response->status, 404);
$body = json_decode($response->body);
$this->assertEquals($body->msg, 'Owner does not own instance');
$this->assertEquals($output->msg, 'Owner does not own instance');

// ======= SUCCESSFUL REQUEST ========
$response = Request::forge('/api/instance/request_access')
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"react-datepicker": "^4.8.0",
"react-overlays": "^5.2.1",
"react-query": "^3.39.2",
"react-toastify": "^9.1.3",
"uuid": "^9.0.1"
},
"devDependencies": {
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2656,11 +2656,6 @@ clone-deep@^4.0.1:
kind-of "^6.0.2"
shallow-clone "^3.0.0"

clsx@^1.1.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.2.1.tgz#0ddc4a20a549b59c93a4116bb26f5294ca17dc12"
integrity sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg==

co@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
Expand Down Expand Up @@ -6665,13 +6660,6 @@ react-test-renderer@^17.0.2:
react-shallow-renderer "^16.13.1"
scheduler "^0.20.2"

react-toastify@^9.1.3:
version "9.1.3"
resolved "https://registry.yarnpkg.com/react-toastify/-/react-toastify-9.1.3.tgz#1e798d260d606f50e0fab5ee31daaae1d628c5ff"
integrity sha512-fPfb8ghtn/XMxw3LkxQBk3IyagNpF/LIKjOBflbexr2AWxAH1MJgvnESwEwBn9liLFXgTKWgBSdZpw9m4OTHTg==
dependencies:
clsx "^1.1.1"

react@^16.13.1:
version "16.14.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.14.0.tgz#94d776ddd0aaa37da3eda8fc5b6b18a4c9a3114d"
Expand Down

0 comments on commit 19476a7

Please sign in to comment.