From e886eb2ce6903b3aa54615b2721cd2911df38919 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 25 Aug 2017 14:44:36 +0100 Subject: [PATCH 1/5] Remove explicit undef returns --- lib/Pear/LocalLoop.pm | 2 +- lib/Pear/LocalLoop/Controller/Api/Auth.pm | 2 +- lib/Pear/LocalLoop/Controller/Api/User.pm | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index c41371d..3576c48 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -50,7 +50,7 @@ sub startup { return $user->id; } } - return undef; + return; }, }); diff --git a/lib/Pear/LocalLoop/Controller/Api/Auth.pm b/lib/Pear/LocalLoop/Controller/Api/Auth.pm index d64bf09..41e3b1e 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Auth.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Auth.pm @@ -86,7 +86,7 @@ sub post_login { } elsif ( defined $user_result->organisation_id ) { $display_name = $user_result->organisation->name; } else { - return undef; + return; } return $c->render( json => { diff --git a/lib/Pear/LocalLoop/Controller/Api/User.pm b/lib/Pear/LocalLoop/Controller/Api/User.pm index 12ef04b..65f380c 100644 --- a/lib/Pear/LocalLoop/Controller/Api/User.pm +++ b/lib/Pear/LocalLoop/Controller/Api/User.pm @@ -72,7 +72,7 @@ sub post_account { } elsif ( defined $user_result->organisation_id ) { $display_name = $user_result->organisation->name; } else { - return undef; + return; } return $c->render( json => { From 0a9ca576892e12cd9f672ad16c3b64dd5115b36d Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 25 Aug 2017 14:59:01 +0100 Subject: [PATCH 2/5] Added user type attribute --- lib/Pear/LocalLoop/Schema/Result/User.pm | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop/Schema/Result/User.pm b/lib/Pear/LocalLoop/Schema/Result/User.pm index 151075d..75a1dde 100644 --- a/lib/Pear/LocalLoop/Schema/Result/User.pm +++ b/lib/Pear/LocalLoop/Schema/Result/User.pm @@ -149,8 +149,17 @@ sub name { } elsif ( defined $self->organisation_id ) { return $self->organisation->name; } else { - return undef; + return; } } +sub type { + my $self = shift; + + if ( defined $self->customer_id ) { + return "customer"; + } + return "organisation"; +} + 1; From 4a4c568fb78ba5fe1295f33536258e6509f8860b Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 25 Aug 2017 14:59:15 +0100 Subject: [PATCH 3/5] Return user type on login --- lib/Pear/LocalLoop/Controller/Api/Auth.pm | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Auth.pm b/lib/Pear/LocalLoop/Controller/Api/Auth.pm index 41e3b1e..82cfb60 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Auth.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Auth.pm @@ -79,20 +79,12 @@ sub post_login { if ( defined $user_result ) { if ( $user_result->check_password($password) ) { my $session_key = $user_result->generate_session; - my $display_name; - - if ( defined $user_result->customer_id ) { - $display_name = $user_result->customer->display_name; - } elsif ( defined $user_result->organisation_id ) { - $display_name = $user_result->organisation->name; - } else { - return; - } return $c->render( json => { success => Mojo::JSON->true, session_key => $session_key, - display_name => $display_name, + display_name => $user_result->name, + user_type => $user_result->type, }); } } From 768cc38410dad92a4fdfd1835daaaa13d6b5783e Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 25 Aug 2017 15:14:43 +0100 Subject: [PATCH 4/5] Refactor api login test --- t/api/login.t | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/t/api/login.t b/t/api/login.t index 1521378..e2fd731 100644 --- a/t/api/login.t +++ b/t/api/login.t @@ -8,28 +8,22 @@ my $framework = Test::Pear::LocalLoop->new; my $t = $framework->framework; my $schema = $t->app->schema; -my $account_token = 'a'; -my $email = 'rufus@shinra.energy'; -my $password = 'MakoGold'; - -$schema->resultset('AccountToken')->create({ - name => $account_token -}); - -my $test_json = { - 'usertype' => 'customer', - 'token' => $account_token, - 'display_name' => 'RufusShinra', - 'full_name' => 'RufusShinra', - 'email' => $email, - 'postcode' => 'LA1 1AA', - 'password' => $password, - year_of_birth => 2006 +my $user = { + token => 'a', + usertype => 'customer', + display_name => 'Display Guy', + full_name => 'Real Name', + email => 'test@example.com', + postcode => 'LA1 1AA', + password => 'testerising', + year_of_birth => 2006, }; -$t->post_ok('/api/register' => json => $test_json) + +$schema->resultset('AccountToken')->create({ name => $user->{token} }); + +$t->post_ok('/api/register' => json => $user) ->status_is(200) ->json_is('/success', Mojo::JSON->true); -use Data::Dumper; is $schema->resultset('User')->count, 1, 'found a user'; @@ -48,8 +42,8 @@ $t->post_ok('/api/login' => json => { ->json_is('/success', Mojo::JSON->false); $t->post_ok('/api/login' => json => { - email => $email, - password => $password, + email => $user->{email}, + password => $user->{password}, }) ->status_is(200) ->json_is('/success', Mojo::JSON->true) From deba59dc00ed42bd918c263cc0958318e33e87f5 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 25 Aug 2017 15:17:12 +0100 Subject: [PATCH 5/5] Add test for showing display name and user type correctly --- t/api/login.t | 2 ++ 1 file changed, 2 insertions(+) diff --git a/t/api/login.t b/t/api/login.t index e2fd731..08cf959 100644 --- a/t/api/login.t +++ b/t/api/login.t @@ -47,6 +47,8 @@ $t->post_ok('/api/login' => json => { }) ->status_is(200) ->json_is('/success', Mojo::JSON->true) + ->json_is('/display_name', 'Display Guy') + ->json_is('/user_type', 'customer') ->json_has('/session_key'); my $session_key = $t->tx->res->json->{session_key};