From 99ef319c3b44b286f935d459d0e39b3d8de08263 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 21 Apr 2017 12:42:31 +0100 Subject: [PATCH] Started refactoring pending orgs table --- .../Schema/Result/PendingOrganisation.pm | 124 ++++++------------ .../Schema/Result/PendingTransaction.pm | 2 +- lib/Pear/LocalLoop/Schema/Result/User.pm | 2 +- t/admin-approve.t | 59 +++------ 4 files changed, 61 insertions(+), 126 deletions(-) diff --git a/lib/Pear/LocalLoop/Schema/Result/PendingOrganisation.pm b/lib/Pear/LocalLoop/Schema/Result/PendingOrganisation.pm index e9790fc..2d9e54d 100644 --- a/lib/Pear/LocalLoop/Schema/Result/PendingOrganisation.pm +++ b/lib/Pear/LocalLoop/Schema/Result/PendingOrganisation.pm @@ -1,100 +1,52 @@ use utf8; package Pear::LocalLoop::Schema::Result::PendingOrganisation; -# Created by DBIx::Class::Schema::Loader -# DO NOT MODIFY THE FIRST PART OF THIS FILE - -=head1 NAME - -Pear::LocalLoop::Schema::Result::PendingOrganisation - -=cut - use strict; use warnings; use base 'DBIx::Class::Core'; -=head1 COMPONENTS LOADED - -=over 4 - -=item * L - -=back - -=cut - __PACKAGE__->load_components("InflateColumn::DateTime"); -=head1 TABLE: C - -=cut - -__PACKAGE__->table("PendingOrganisations"); - -=head1 ACCESSORS - -=head2 pendingorganisationid - - data_type: 'integer' - is_auto_increment: 1 - is_nullable: 0 - -=head2 usersubmitted_fk - - data_type: 'integer' - is_foreign_key: 1 - is_nullable: 0 - -=head2 timedatesubmitted - - data_type: 'integer' - is_nullable: 0 - -=head2 name - - data_type: 'text' - is_nullable: 0 - -=head2 fulladdress - - data_type: 'text' - is_nullable: 1 - -=head2 postcode - - data_type: 'text' - is_nullable: 1 - -=cut +__PACKAGE__->table("pending_organisations"); __PACKAGE__->add_columns( - "pendingorganisationid", - { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, - "usersubmitted_fk", - { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, - "timedatesubmitted", - { data_type => "datetime", is_nullable => 0 }, - "name", - { data_type => "text", is_nullable => 0 }, - "fulladdress", - { data_type => "text", is_nullable => 1 }, - "postcode", - { data_type => "text", is_nullable => 1 }, + id => { + data_type => 'integer', + is_auto_increment => 1, + is_nullable => 0, + }, + name => { + data_type => 'varchar', + size => 255, + is_nullable => 0, + }, + street_address => { + data_type => 'text', + is_nullable => 1, + }, + town => { + data_type => 'varchar', + size => 255, + is_nullable => 0, + }, + postcode => { + data_type => 'varchar', + size => 16, + is_nullable => 1, + }, + submitted_by_id => { + data_type => "integer", + is_foreign_key => 1, + is_nullable => 0, + }, + submitted_at => { + data_type => "datetime", + is_nullable => 0, + }, ); -=head1 PRIMARY KEY - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->set_primary_key("pendingorganisationid"); +__PACKAGE__->set_primary_key('id'); =head1 RELATIONS @@ -110,7 +62,7 @@ __PACKAGE__->has_many( "pending_transactions", "Pear::LocalLoop::Schema::Result::PendingTransaction", { - "foreign.pendingsellerorganisationid_fk" => "self.pendingorganisationid", + "foreign.pendingsellerorganisationid_fk" => "self.id", }, { cascade_copy => 0, cascade_delete => 0 }, ); @@ -124,9 +76,9 @@ Related object: L =cut __PACKAGE__->belongs_to( - "usersubmitted_fk", + "submitted_by", "Pear::LocalLoop::Schema::Result::User", - { userid => "usersubmitted_fk" }, + { userid => "submitted_by_id" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); diff --git a/lib/Pear/LocalLoop/Schema/Result/PendingTransaction.pm b/lib/Pear/LocalLoop/Schema/Result/PendingTransaction.pm index 07ea25c..d90f700 100644 --- a/lib/Pear/LocalLoop/Schema/Result/PendingTransaction.pm +++ b/lib/Pear/LocalLoop/Schema/Result/PendingTransaction.pm @@ -139,7 +139,7 @@ Related object: L __PACKAGE__->belongs_to( "pendingsellerorganisationid_fk", "Pear::LocalLoop::Schema::Result::PendingOrganisation", - { pendingorganisationid => "pendingsellerorganisationid_fk" }, + { id => "pendingsellerorganisationid_fk" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); diff --git a/lib/Pear/LocalLoop/Schema/Result/User.pm b/lib/Pear/LocalLoop/Schema/Result/User.pm index 4f3439a..cf3883a 100644 --- a/lib/Pear/LocalLoop/Schema/Result/User.pm +++ b/lib/Pear/LocalLoop/Schema/Result/User.pm @@ -214,7 +214,7 @@ Related object: L __PACKAGE__->has_many( "pending_organisations", "Pear::LocalLoop::Schema::Result::PendingOrganisation", - { "foreign.usersubmitted_fk" => "self.userid" }, + { "foreign.submitted_by_id" => "self.userid" }, { cascade_copy => 0, cascade_delete => 0 }, ); diff --git a/t/admin-approve.t b/t/admin-approve.t index db3edd2..769a67c 100644 --- a/t/admin-approve.t +++ b/t/admin-approve.t @@ -1,36 +1,20 @@ use Mojo::Base -strict; + use Test::More; -use Test::Mojo; use Mojo::JSON; +use Test::Pear::LocalLoop; -use FindBin; +my $framework = Test::Pear::LocalLoop->new; +my $t = $framework->framework; +my $schema = $t->app->schema; +my $dump_error = sub { diag $t->tx->res->dom->at('pre[id="error"]')->text }; -BEGIN { - $ENV{MOJO_MODE} = 'testing'; - $ENV{MOJO_LOG_LEVEL} = 'debug'; -} - -my $t = Test::Mojo->new("Pear::LocalLoop"); - -my $dbh = $t->app->db; - -#Dump all pf the test tables and start again. -my $sqlDeployment = Mojo::File->new("$FindBin::Bin/../dropschema.sql")->slurp; -for (split ';', $sqlDeployment){ - $dbh->do($_) or die $dbh->errstr; -} - -$sqlDeployment = Mojo::File->new("$FindBin::Bin/../schema.sql")->slurp; -for (split ';', $sqlDeployment){ - $dbh->do($_) or die $dbh->errstr; -} - -my @accountTokens = ('a', 'b', 'c'); -my $tokenStatement = $dbh->prepare('INSERT INTO AccountTokens (AccountTokenName) VALUES (?)'); -foreach (@accountTokens){ - my $rowsAdded = $tokenStatement->execute($_); -} +my @account_tokens = ('a', 'b', 'c'); +$schema->resultset('AccountToken')->populate([ + [ 'accounttokenname' ], + map { [ $_ ] } @account_tokens, +]); #This depends on "register.t", "login.t" and "upload.t" working. @@ -40,7 +24,7 @@ my $emailReno = 'reno@shinra.energy'; my $passwordReno = 'turks'; my $testJson = { 'usertype' => 'customer', - 'token' => shift(@accountTokens), + 'token' => shift(@account_tokens), 'username' => 'Reno', 'email' => $emailReno, 'postcode' => 'E1 MP01', @@ -56,24 +40,24 @@ my $emailBilly = 'choco.billy@chocofarm.org'; my $passwordBilly = 'Choco'; $testJson = { 'usertype' => 'organisation', - 'token' => shift(@accountTokens), + 'token' => shift(@account_tokens), 'username' => 'ChocoBillysGreens', 'email' => $emailBilly, 'postcode' => 'E4 C12', 'password' => $passwordBilly, - 'fulladdress' => 'Chocobo Farm, Eastern Continent, Gaia' + 'street_address' => 'Chocobo Farm, Eastern Continent', + 'town' => 'Gaia', }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) ->json_is('/success', Mojo::JSON->true); - print "test 3 - Create admin account\n"; my $emailAdmin = 'admin@foodloop.net'; my $passwordAdmin = 'ethics'; $testJson = { 'usertype' => 'customer', - 'token' => shift(@accountTokens), + 'token' => shift(@account_tokens), 'username' => 'admin', 'email' => $emailAdmin, 'postcode' => 'NW1 W01', @@ -85,11 +69,10 @@ $t->post_ok('/api/register' => json => $testJson) ->json_is('/success', Mojo::JSON->true); print "test 4 - Making 'admin' an Admin\n"; -my $rufusUserId = $t->app->db->selectrow_array("SELECT UserId FROM Users WHERE Email = ?", undef, ($emailAdmin)); -is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Administrators")}[0],0,"No admins"; -$t->app->db->prepare("INSERT INTO Administrators (UserId) VALUES (?)")->execute($rufusUserId); -is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Administrators")}[0],1,"1 admin"; - +my $rufus_user = $schema->resultset('User')->find({ email => $emailAdmin }); +is $schema->resultset('Administrator')->count, 0, "No admins"; +$rufus_user->find_or_create_related('administrator', {}); +is $schema->resultset('Administrator')->count, 1, "1 admin"; ###################################################### @@ -350,7 +333,7 @@ $json = { session_key => $session_key, }; $t->post_ok('/api/admin-approve' => json => $json) - ->status_is(200) + ->status_is(200)->or($dump_error) ->json_is('/success', Mojo::JSON->true); is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingOrganisations", undef, ())}[0],2,"2 unverified organisation." ; is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM PendingTransactions", undef, ())}[0],4,"4 unverified transaction." ;