Commit 4f67274d authored by Francesc Guasch's avatar Francesc Guasch
Browse files

wip: improved list machines and updated tests

parent 90afd5cf
......@@ -128,12 +128,11 @@ Returns: listref of machines
=cut
sub _list_machines_user($self, $user, $access_data, $typeof) {
my $base = $typeof ? 1 : 0;
sub list_machines_user($self, $user, $access_data={}) {
my $sth = $CONNECTOR->dbh->prepare(
"SELECT id,name,is_public, description, screenshot, id_owner"
." FROM domains "
." WHERE is_base=$base"
." WHERE is_base=1"
." ORDER BY name "
);
my ($id, $name, $is_public, $description, $screenshot, $id_owner);
......@@ -155,6 +154,7 @@ sub _list_machines_user($self, $user, $access_data, $typeof) {
id_owner =>$user->id
,id_base => $id
);
next unless $clone || $user->is_admin || ($is_public && $user->allowed_access($id));
my %base = ( id => $id, name => $name
, is_public => ($is_public or 0)
, screenshot => ($screenshot or '')
......@@ -196,14 +196,6 @@ sub _list_machines_user($self, $user, $access_data, $typeof) {
return \@list;
}
sub list_machines_user($self, $user, $access_data={}) {
return _list_machines_user($self, $user, $access_data, 1);
}
sub list_private_machines_user($self, $user, $access_data={}) {
return _list_machines_user($self, $user, $access_data, 0);
}
sub _access_allowed($self, $id_base, $id_clone, $access_data) {
if ($id_clone) {
my $clone = Ravada::Front::Domain->open($id_clone);
......
......@@ -191,9 +191,6 @@ sub _list_machines_user_including_privates($rvd, $args) {
my $client = $args->{client};
my $ret = $rvd->list_machines_user($user, {client => $client});
my $priv_machines = $rvd->list_private_machines_user($user, {client => $client});
push(@$ret, $_) foreach (@$priv_machines);
return $ret;
}
......
use warnings;
use strict;
use Carp qw(confess);
use Data::Dumper;
use Hash::Util qw(lock_hash);
use Test::More;
......@@ -103,7 +104,7 @@ sub test_access_by_attribute_deny($vm, $do_clones=0) {
is($data->{teacher}->{user}->allowed_access( $base->id ), 1);
is($data->{other}->{user}->allowed_access( $base->id ), 1);
_do_clones($data, $base, $do_clones);
my ($clone_student, $clone_teacher) = _do_clones($data, $base, $do_clones);
$base->deny_ldap_access( givenName => $data->{student}->{user}->{name});
_refresh_users($data);
......@@ -111,6 +112,7 @@ sub test_access_by_attribute_deny($vm, $do_clones=0) {
is($data->{teacher}->{user}->allowed_access( $base->id ), 1);
is($data->{other}->{user}->allowed_access( $base->id ), 1);
$clone_student->remove(user_admin) if $clone_student;
my $list_bases = rvd_front->list_machines_user($data->{student}->{user});
is(scalar (@$list_bases), 0);
......@@ -310,7 +312,7 @@ sub test_access_by_attribute($vm, $do_clones=0) {
$base->prepare_base(user_admin);
$base->is_public(1);
_do_clones($data, $base, $do_clones);
my ($clone_student, $clone_teacher) = _do_clones($data, $base, $do_clones);
my $list_bases = rvd_front->list_machines_user($data->{student}->{user});
is(scalar (@$list_bases), 1);
......@@ -338,8 +340,10 @@ sub test_access_by_attribute($vm, $do_clones=0) {
$list_bases = rvd_front->list_machines_user($data->{student}->{user});
is(scalar (@$list_bases), 1);
$clone_teacher->remove(user_admin) if $clone_teacher;
$list_bases = rvd_front->list_machines_user($data->{teacher}->{user});
is(scalar (@$list_bases), 0);
is(scalar (@$list_bases), 0) or confess Dumper($list_bases,$base->id);
# other has no external_auth, access denied
$list_bases = rvd_front->list_machines_user($data->{other}->{user});
......@@ -409,7 +413,7 @@ sub test_access_by_attribute_2bases($vm, $do_clones=0) {
my @bases = _create_bases($vm,2);
_do_clones($data, $bases[0], $do_clones);
my($clone_student, $clone_teacher) = _do_clones($data, $bases[0], $do_clones);
_do_clones($data, $bases[1], $do_clones);
my $list_bases = rvd_front->list_machines_user($data->{student}->{user});
......@@ -444,8 +448,14 @@ sub test_access_by_attribute_2bases($vm, $do_clones=0) {
$list_bases = rvd_front->list_machines_user($data->{student}->{user});
is(scalar (@$list_bases), 2);
if ($do_clones) {
$list_bases = rvd_front->list_machines_user($data->{teacher}->{user});
is(scalar (@$list_bases), 2) or die Dumper($do_clones,$list_bases);
$clone_teacher->remove(user_admin) if $clone_teacher;
}
$list_bases = rvd_front->list_machines_user($data->{teacher}->{user});
is(scalar (@$list_bases), 1);
is(scalar (@$list_bases), 1) or die Dumper($list_bases, $bases[0]->id);
$list_bases = rvd_front->list_machines_user($data->{other}->{user});
is(scalar (@$list_bases), 1);
......
......@@ -24,7 +24,7 @@ for my $vm_name ( 'KVM' ) {
next if $vm_name eq 'KVM' && $>;
_set_no_password();
my $domain = import_domain($vm_name);
my $domain = import_domain($vm_name, $base);
is($domain->is_base,1,$domain." should be a base") or next;
my $clone = $domain->clone(
user => user_admin
......
......@@ -1178,18 +1178,11 @@ sub test_private_base {
my $clone_name = new_domain_name();
my $clone;
eval { $clone = $domain->clone(user => $USER, name => $clone_name); };
like($@,qr(private)) or exit;
my $clone2 = $vm->search_domain($clone_name);
ok(!$clone2,"Expecting no clone");
$clone2->remove(user_admin) if $clone2;
# admin can clone
eval { $clone = $domain->clone(user => user_admin, name => $clone_name); };
is($@,'');
$clone2 = $vm->search_domain($clone_name);
my $clone2 = $vm->search_domain($clone_name);
ok($clone2,"Expecting a clone");
$clone->remove(user_admin) if $clone;
......@@ -1202,13 +1195,6 @@ sub test_private_base {
ok($clone2,"Expecting a clone");
$clone->remove(user_admin) if $clone;
# hide it again
$domain->is_public(0);
eval { $clone = $domain->clone(user => $USER, name => $clone_name); };
like($@,qr(.));
$clone2 = $vm->search_domain($clone_name);
ok(!$clone2,"Expecting no clone");
}
sub test_domain_limit_admin {
my $vm_name = shift;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment