 
 create or replace function test_users_function_login_changes_session_owner()
 returns setof text as $test$
-	begin
-		perform users.validate_user('web-session-1', users.add_user(
-			'web-session-1', 'test-user1', 'password', 
-			'test-user1@testers.com'));
-		perform web.set_session_data('users-session-1', '{}',
-			now() + interval '1 day');
-		perform users.login('users-session-1', 'test-user1',
+	declare
+		activeusercur		cursor for select users.test_user.name
+							from users.test_user
+							left outer join users.user on 
+								(lower(users.test_user.name) = 
+								users.user.name)
+							where users.user.active = true;
+		activeuser		text;
+	begin 
+		open activeusercur;
+		fetch from activeusercur into activeuser;
+		perform users.login('web-session-2', activeuser,
 			'password');
 		return next results_eq(
 			$$select user_id from web.session 
-				where sess_id = 'users-session-1'$$,
+				where sess_id = 'web-session-2'$$,
 			$$select id from users.user 
-				where name = lower('test-user1')$$,
+				where name = lower('$$ || activeuser || $$')$$,
 			'Login should assign the user to the session.');
 	end;
 $test$ language plpgsql;
 
 create or replace function test_users_function_login_username_case_insensitive()
 returns setof text as $test$
-	begin
-		perform users.validate_user('web-session-1', users.add_user(
-			'web-session-1', 'test-user1', 'password', 
-			'test-user1@testers.com'));
-		perform web.set_session_data('users-session-1', '{}',
-			now() + interval '1 day');
-		perform users.login('users-session-1', 'TEST-USER1',
+	declare
+		activeusercur		cursor for select users.test_user.name
+							from users.test_user
+							left outer join users.user on 
+								(lower(users.test_user.name) = 
+								users.user.name)
+							where users.user.active = true;
+		activeuser		text;
+	begin 
+		open activeusercur;
+		fetch from activeusercur into activeuser;
+		perform users.login('web-session-2', upper(activeuser),
 			'password');
 		return next results_eq(
 			$$select user_id from web.session 
-				where sess_id = 'users-session-1'$$,
+				where sess_id = 'web-session-2'$$,
 			$$select id from users.user 
-				where name = lower('TEST-USER1')$$,
+				where name = lower('$$ || activeuser || $$')$$,
 			'Login should assign the user to the session.');
 	end;
 $test$ language plpgsql;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 
 create or replace function test_users_function_login_fails_for_incorrect_values()
 returns setof text as $test$


create or replace function test_users_function_deleteuser_removes_data()
returns setof text as $test$
	begin 
		perform users.add_user('web-session-1', 'test-user',
			'password', 'tester@test.com');
		perform users.delete_user('web-session-1', 'test-user');
		return next is_empty(
			$$select * from users.user 
				where name = 'test-user'$$,
			'Delete user should remove the user.');
	end;
$test$ language plpgsql;

create or replace function test_users_function_deleteuser_not_case_sensitive()
returns setof text as $test$
	begin 
		perform users.add_user('web-session-1', 'test-user',
			'password', 'tester@test.com');
		perform users.delete_user('web-session-1', 'TEST-USER');
		return next is_empty(
			$$select * from users.user 
				where name = 'test-user'$$,
			'Delete user should remove the user.');
	end;
$test$ language plpgsql;

-- This setup needs to run after webs setup.
create or replace function startup_20_users()
returns setof text as $test$
	declare
		nameholder	text;
		userid		uuid;
		grpid		uuid;
	begin
		delete from users.test_user;
		loop
			select md5(random()::text) into nameholder;
			select id into userid
				from users.user 
				where name = lower(nameholder)
					or email = lower(nameholder);
			if found then
				continue;
			end if;
			insert into users.test_user (name) 
				values (nameholder);
			if (select count(*) > 5 from users.test_user) then
				insert into users.user (id, name, password, email)
					values (public.uuid_generate_v1(), nameholder,
					public.crypt('password', public.gen_salt('bf')),
					nameholder);
			end if;
			if (select count(*) > 10 from users.test_user) then
				update users.user set active = true 
					where name = lower(nameholder);
			end if;
			exit when (select count(*) > 14 from users.test_user);
		end loop;
		delete from users.test_group;
		loop 
			select md5(random()::text) into nameholder;
			select id into grpid
				from users.group
				where name = lower(nameholder);
			if found then
				continue;
			end if;
			insert into users.test_group (name)
				values (nameholder);
			if (select count(*) > 5 from users.test_group) then 
				insert into users.group (id, name) values
					(public.uuid_generate_v1(), nameholder);
			end if;
			exit when (select count(*) > 9 from users.test_group);
		end loop;
	exception
		when invalid_schema_name or  undefined_function or
			undefined_table or undefined_column then
			--do nothing
	end;
$test$ language plpgsql;

create or replace function setup_20_users()
returns setof text as $test$
	begin
		update web.session 
			set user_id = 
				(select 
					users.group_user_link.user_id 
				from users.group,
					users.group_user_link
				where 
					users.group.id = users.group_user_link.group_id
					and users.group.name = 'admin'
				limit 1)
			where web.session.sess_id = 'web-session-1';
	exception
		when invalid_schema_name or undefined_function or
			undefined_table or undefined_column then
			--do nothing
	end; 
$test$ language plpgsql;

create or replace function test_users_schema()
returns setof text as $$
	begin 
		return next has_schema('users', 'There should be a users schema.');
	end;
$$ language plpgsql;

create or replace function test_users_table_testusers_exists()
returns setof text as $test$
	begin
		return next has_table('users', 'test_user', 
			'There should be a table to hold test users.');
	end;
$test$ language plpgsql;

create or replace function test_users_table_testusers_column_name_exists()
returns setof text as $test$
	begin
		return next has_column('users', 'test_user', 'name',
			'Needs a user name column in test users.');
	end;
$test$ language plpgsql;  

create or replace function test_users_table_testuser_column_name_is_text()
returns setof text as $test$
	begin
		return next col_type_is('users', 'test_user', 'name', 'text',
			'The test user name needs to be text.');
	end;
$test$ language plpgsql;  

create or replace function test_users_table_testuser_column_name_is_pk()
returns setof text as $test$
	begin 
		return next col_is_pk('users', 'test_user', 'name',
			'Name should be primary key for test users.');
	end;
$test$ language plpgsql;

create or replace function test_users_table_testgroup_exists()
returns setof text as $test$
	begin
		return next has_table('users', 'test_group', 
			'There should be a table to hold test groups.');
	end;
$test$ language plpgsql;

create or replace function test_users_table_testgroup_column_name_exists()
returns setof text as $test$
	begin
		return next has_column('users', 'test_group', 'name',
			'Needs a user name column in test users.');
	end;
$test$ language plpgsql;  

create or replace function test_users_table_testgroup_column_name_is_text()
returns setof text as $test$
	begin
		return next col_type_is('users', 'test_group', 'name', 'text',
			'The test group name needs to be text.');
	end;
$test$ language plpgsql;  

create or replace function test_users_table_testgroup_column_name_is_pk()
returns setof text as $test$
	begin 
		return next col_is_pk('users', 'test_group', 'name',
			'Name should be primary key for test group.');
	end;
$test$ language plpgsql;

create or replace function test_users_user_exists()
returns setof text as $$
