[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[2926] trunk



Revision: 2926
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=2926&view=rev
Author:   einhverfr
Date:     2010-03-01 01:15:14 +0000 (Mon, 01 Mar 2010)

Log Message:
-----------
Database creation now works consistently from the make test process

Modified Paths:
--------------
    trunk/sql/modules/Payment.sql
    trunk/sql/modules/Roles.sql
    trunk/sql/modules/admin.sql
    trunk/t/40-dbsetup.t
    trunk/t/89-dropdb.t

Modified: trunk/sql/modules/Payment.sql
===================================================================
--- trunk/sql/modules/Payment.sql	2010-02-28 18:43:08 UTC (rev 2925)
+++ trunk/sql/modules/Payment.sql	2010-03-01 01:15:14 UTC (rev 2926)
@@ -1237,7 +1237,6 @@
 it is usefull for printing payments and build reports :) $$;
 
 -- We will use a view to handle all the overpayments
-DROP VIEW IF EXISTS overpayments CASCADE;
 CREATE VIEW overpayments AS
 SELECT p.id as payment_id, p.reference as payment_reference, p.payment_class, p.closed as payment_closed,
        p.payment_date, ac.chart_id, c.accno, c.description as chart_description,

Modified: trunk/sql/modules/Roles.sql
===================================================================
--- trunk/sql/modules/Roles.sql	2010-02-28 18:43:08 UTC (rev 2925)
+++ trunk/sql/modules/Roles.sql	2010-03-01 01:15:14 UTC (rev 2926)
@@ -60,7 +60,7 @@
 GRANT ALL ON entity_credit_account_id_seq TO "lsmb_<?lsmb dbname ?>__contact_create";
 GRANT INSERT ON company_to_contact TO "lsmb_<?lsmb dbname ?>__contact_create";
 GRANT INSERT ON company_to_entity TO "lsmb_<?lsmb dbname ?>__contact_create";
-GRANT ALL ON SEQUENCE note_id_seq TO "lsmb_<?lsmb dbname ?>__contact_create";
+GRANT ALL ON note_id_seq TO "lsmb_<?lsmb dbname ?>__contact_create";
 GRANT INSERT ON company_to_location TO "lsmb_<?lsmb dbname ?>__contact_create";
 GRANT INSERT ON customertax TO "lsmb_<?lsmb dbname ?>__contact_create";
 GRANT INSERT ON entity_bank_account TO "lsmb_<?lsmb dbname ?>__contact_create";
@@ -554,7 +554,7 @@
 GRANT DELETE ON cr_report_line
 TO "lsmb_<?lsmb dbname ?>__reconciliation_enter";
 GRANT SELECT ON acc_trans TO "lsmb_<?lsmb dbname ?>__reconciliation_enter";
- GRANT ALL ON cr_reports_id_seq TO "lsmb_<?lsmb dbname ?>__reconciliation_enter";
+ GRANT ALL ON cr_report_id_seq TO "lsmb_<?lsmb dbname ?>__reconciliation_enter";
 
 INSERT INTO menu_acl (node_id, acl_type, role_name)
 values (35, 'allow', 'lsmb_<?lsmb dbname ?>_reconciliation_enter');

Modified: trunk/sql/modules/admin.sql
===================================================================
--- trunk/sql/modules/admin.sql	2010-02-28 18:43:08 UTC (rev 2925)
+++ trunk/sql/modules/admin.sql	2010-03-01 01:15:14 UTC (rev 2926)
@@ -324,26 +324,34 @@
         l_id int;
         stmt text;
     BEGIN
-        -- WARNING TO PROGRAMMERS:  This function runs as the definer.
+        -- WARNING TO PROGRAMMERS:  This function runs as the definer and runs
+        -- utility statements via EXECUTE.
         -- PLEASE BE VERY CAREFUL ABOUT SQL-INJECTION INSIDE THIS FUNCTION.
     
-        select * into a_user from users lu where lu.id = in_id;
-        
-        IF NOT FOUND THEN 
+        if admin__is_user(in_username) then
+                
+                execute 'ALTER USER ' || quote_ident( in_username ) || 
+                     ' WITH ENCRYPTED PASSWORD ' || quote_literal (in_password)
+                     || $e$ valid until $e$ || quote_literal(now() + '1 day'::interval);
+        else
             if in_password IS NULL THEN
                 RAISE EXCEPTION 'Must create password when adding new users!';
             end if;
+           
+            -- create an actual user
+                execute 'CREATE USER ' || quote_ident( in_username ) || 
+                     ' WITH ENCRYPTED PASSWORD ' || quote_literal (in_password)
+                     || $e$ valid until $e$ || quote_literal(now() + '1 day'::interval);
+            end if;         
+        
+        select * into a_user from users lu where lu.id = in_id;
+        IF FOUND THEN 
+            return a_user.id;
+        ELSE
             -- Insert cycle
             
             --- The entity is expected to already BE created. See admin.pm.
             
-            if admin__is_user(in_username) then
-                
-                -- uhm, this is bad. -AS.  Not necessarily.  -CT 
-                RAISE EXCEPTION 'Fatal exception: Username already exists in Postgres; not
-                    a valid lsmb user.';
-            end if;         
-            -- create an actual user
             
             v_user_id := nextval('users_id_seq');
             insert into users (id, username, entity_id) VALUES (
@@ -356,24 +364,9 @@
 
             -- Finally, issue the create user statement
             
-            execute 'CREATE USER ' || quote_ident( in_username ) || 
-                     ' WITH ENCRYPTED PASSWORD ' || quote_literal (in_password)
-                     || $e$ valid until $e$ || quote_literal(now() + '1 day'::interval);
-            
             return v_user_id ;
 
-        ELSIF FOUND THEN
             
-            -- update cycle
-            IF in_password IS NOT NULL THEN
-            
-                execute ' alter user '|| quote_ident(in_username) || 
-                     ' with encrypted password ' 
-                             || quote_literal(in_password) || 
-                     $e$ valid until now()::timezone + '1 day'::interval $e$;
-            end if;
-                      
-            return a_user.id;
         
         END IF;
     
@@ -527,11 +520,7 @@
     BEGIN
     
         select * into pg_user from pg_roles where rolname = in_user;
-        
-        IF NOT FOUND THEN
-            return 'f'::bool;
-        END IF;
-        return 't'::bool;
+        RETURN found;     
     
     END;
     

Modified: trunk/t/40-dbsetup.t
===================================================================
--- trunk/t/40-dbsetup.t	2010-02-28 18:43:08 UTC (rev 2925)
+++ trunk/t/40-dbsetup.t	2010-03-01 01:15:14 UTC (rev 2926)
@@ -2,18 +2,19 @@
 
 use Test::More;
 use strict;
+use DBI;
 
 my $temp = $ENV{TEMP} || '/tmp/';
 my $run_tests = 1;
 for my $evar (qw(LSMB_NEW_DB LSMB_TEST_DB PG_CONTRIB_DIR)){
   if (!defined $ENV{$evar}){
       $run_tests = 0;
-      plan skipall => "$evar not set";
+      plan skip_all => "$evar not set";
   }
 }
 
 if ($run_tests){
-	plan tests => 25;
+	plan tests => 29;
 	$ENV{PGDATABASE} = $ENV{LSMB_NEW_DB};
 }
 
@@ -93,7 +94,47 @@
 
 print PSQL "BEGIN;\n";
 for my $roleline (<ROLES>){
-    $roleline =~ s/<?lsmb dbname ?>/$ENV{LSMB_NEW_DB}/;
+    $roleline =~ s/<\?lsmb dbname \?>/$ENV{LSMB_NEW_DB}/;
     print PSQL $roleline;
 }
+
 print PSQL "COMMIT;\n";
+close (PSQL);
+SKIP: {
+     skip 'No admin info', 4
+           if (!defined $ENV{LSMB_USERNAME} 
+                or !defined $ENV{LSMB_PASSWORD}
+                or !defined $ENV{LSMB_COUNTRY_CODE}
+                or !defined $ENV{LSMB_ADMIN_FNAME}
+                or !defined $ENV{LSMB_ADMIN_LNAME});
+     my $dbh = DBI->connect("dbi:Pg:dbname=$ENV{PGDATABASE}", 
+                                       undef, undef);
+     ok($dbh, 'Connected to new database');
+     $dbh->{autocommit} = 1;
+     my $sth = $dbh->prepare(
+              "SELECT admin__save_user(NULL, -- no user id yet, create new user
+                                       person__save(NULL, -- create new person
+                                                    NULL,
+                                                    ?, -- First Name
+                                                    NULL,
+                                                    ?, -- Last Name
+                                                    (select id from country
+                                                    where short_name = ?) 
+                                       ),
+                                       ?, -- Username desired
+                                       ? -- password
+                       )");
+      ok($sth->execute($ENV{LSMB_ADMIN_FNAME}, 
+              $ENV{LSMB_ADMIN_LNAME}, 
+              $ENV{LSMB_COUNTRY_CODE},
+              $ENV{LSMB_USERNAME},
+              $ENV{LSMB_PASSWORD}), 'Admin user creation query ran');
+      my ($var) = $sth->fetchrow_array();
+      cmp_ok($var, '>', 0, 'User id retrieved');
+      $sth->finish;
+      $sth = $dbh->prepare("SELECT admin__add_user_to_role(?, ?)");
+      my $rolename = "lsmb_" . $ENV{PGDATABASE} . "__users_manage";
+      ok($sth->execute($ENV{LSMB_USERNAME}, $rolename), 
+            'Admin user assigned rights');
+      $sth->finish;
+};

Modified: trunk/t/89-dropdb.t
===================================================================
--- trunk/t/89-dropdb.t	2010-02-28 18:43:08 UTC (rev 2925)
+++ trunk/t/89-dropdb.t	2010-03-01 01:15:14 UTC (rev 2926)
@@ -3,24 +3,28 @@
 
 my $temp = $ENV{TEMP} || '/tmp/';
 my $run_tests = 5;
-for my $evar (qw(LSMB_NEW_DB LSMB_TEST_DB PG_CONTRIB_DIR LSMB_INSTALL_DB)){
+for my $evar (qw(LSMB_NEW_DB LSMB_TEST_DB PG_CONTRIB_DIR)){
   if (!defined $ENV{$evar}){
       $run_tests = 0;
-      plan skipall => "$eval not set";
+      plan skip_all => "$evar not set";
   }
 }
+if ($ENV{LSMB_INSTALL_DB}){
+   plan skip_all => 'LSMB_INSTALL_DB SET';
+}
 
 if ($run_tests){
-	plan tests => 5;
+	plan tests => 6;
 	$ENV{PGDATABASE} = $ENV{LSMB_NEW_DB};
 }
 
 ok(open (DBLOCK, '<', "$temp/LSMB_TEST_DB"), 'Opened db lock file');
 my $db = <DBLOCK>;
 chomp($db);
+cmp_ok($db, 'eq', $ENV{LSMB_NEW_DB}, 'Got expected db name out');
 ok(close (DBLOCK), 'Closed db lock file');
-ok(open (DROPDB, '-|', "dropdb -d $db"), 'Opened drop db');
-my $dropvar = <DROPDB>
+ok(open (DROPDB, '-|', "dropdb $db"), 'Opened drop db');
+my $dropvar = <DROPDB>;
 chomp($dropvar);
-cmp_ok($dropdb, 'eq', 'DROP DATABASE', 'Database dropped');
-ok(unlink "$temp/LSMB_TEST_DB", 'Removed test db lockfile');
+cmp_ok($dropvar, 'eq', 'DROP DATABASE', 'Database dropped');
+ok(unlink ("$temp/LSMB_TEST_DB"), 'Removed test db lockfile');


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.