Skip to content

Commit

Permalink
Fix job group update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mitiao committed Dec 11, 2017
1 parent f8654aa commit 5e12c1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/OpenQA/WebAPI/Controller/API/V1/JobGroup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ sub list {

# check existing job group on top level to prevent create/update duplicate
sub check_top_level_group {
my ($self) = @_;
my ($self, $group_id) = @_;

return 0 if $self->is_parent;
my $properties = $self->load_properties;
return $self->resultset->search({name => $properties->{name}, parent_id => undef});
my $conditions = {name => $properties->{name}, parent_id => undef};
$conditions->{id} = {'!=', $group_id} if $group_id;
return $self->resultset->search($conditions);
}

sub create {
Expand Down Expand Up @@ -129,7 +131,7 @@ sub update {
my $group = $self->find_group;
return unless $group;

my $check = $self->check_top_level_group;
my $check = $self->check_top_level_group($group->id);
if ($check != 0) {
return $self->render(
json => {error => 'Unable to update group due to not allow duplicated job group on top level'},
Expand Down
32 changes: 32 additions & 0 deletions t/api/10-jobgroups.t
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,50 @@ subtest 'prevent deleting non-empty job group' => sub() {
};

subtest 'prevent create/update duplicate job group on top level' => sub() {
# Try to create a group has same name "Cool group" with existing one
$t->post_ok(
'/api/v1/job_groups',
form => {
name => 'Cool group',
parent_id => undef,
})->status_is(500);
# Update "opensuse" group to have name "Cool group"
$t->put_ok(
'/api/v1/job_groups/1001',
form => {
name => 'Cool group',
parent_id => undef,
})->status_is(500);
# Update the existing "Cool group"
$t->put_ok(
'/api/v1/job_groups/1003',
form => {
size_limit_gb => 300,
description => 'Updated group without parent',
keep_important_logs_in_days => 100
})->status_is(200);
my $get = $t->get_ok('/api/v1/job_groups/1003')->status_is(200);
is_deeply(
$get->tx->res->json,
[
{
name => 'Cool group',
parent_id => undef,
sort_order => undef,
keep_logs_in_days => 30,
keep_important_logs_in_days => 100,
default_priority => 50,
description => 'Updated group without parent',
keep_results_in_days => 365,
keep_important_results_in_days => 0,
size_limit_gb => 300,
build_version_sort => 1,
id => 1003,
exclusively_kept_asset_size => undef,
},
],
'Update Cool group without parent'
);
};

done_testing();

0 comments on commit 5e12c1d

Please sign in to comment.