Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

action #2996 - ensure well formed json for needles #164

Merged
merged 3 commits into from Feb 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/OpenQA/Controller/Step.pm
Expand Up @@ -377,6 +377,36 @@ sub _timestamp {
}
}

sub _json_validation($) {

my $self = shift;
my $json = shift;
my $djson = eval {decode_json($json)};
if (!$djson ) {
my $err=$@;
$err=~s@at /usr/.*$@@; #do not print perl module reference
return "syntax error: $err";
}

if (!exists $djson->{'area'} || !exists $djson->{'area'}[0]) {
return "no area defined";
}
if (!exists $djson->{'tags'} || !exists $djson->{'tags'}[0]) {
return "no tag defined";
}
my $areas=$djson->{'area'};
foreach my $area (@$areas) {
return "area without xpos" unless exists $area->{'xpos'};
return "area without ypos" unless exists $area->{'ypos'};
return "area without type" unless exists $area->{'type'};
return "area without height" unless exists $area->{'height'};
return "area without width" unless exists $area->{'width'};
}

return undef;

}

sub save_needle {
my ($self) = @_;
return 0 unless $self->init();
Expand Down Expand Up @@ -410,8 +440,16 @@ sub save_needle {
my $needlename = $validation->param('needlename');
my $overwrite = $validation->param('overwrite');
my $needledir = needledir($job->{'settings'}->{DISTRI}, $job->{'settings'}->{VERSION});
my $success = 1;

my $error=$self->_json_validation($json);
if ($error) {
my $message='Error validating needle: '.$error;
$self->app->log->error($message);
$self->stash(error => "$message\n");
return $self->edit;
}

my $success = 1;
my $imagepath;
if ($imagedistri) {
$imagepath = join('/', needledir($imagedistri, $imageversion), $imagename);
Expand Down
79 changes: 75 additions & 4 deletions t/ui/12-needle-edit.t
Expand Up @@ -63,12 +63,16 @@ $req = $t->post_ok('/tests/99937/modules/isosize/steps/1',{ 'X-CSRF-Token' => $t
$test_case->login($t, 'percival');

# post needle based on screenshot
$req = $t->post_ok('/tests/99937/modules/isosize/steps/1',{ 'X-CSRF-Token' => $token },form => { json => 'blah', imagename => 'isosize-1.png', needlename => "isosize-blah", overwrite => 'no' })->status_is(200);
my $json='{
"area" : [ { "width" : 1024,"xpos" : 0,"type" : "match","ypos" : 0, "height" : 768 } ],
"tags" : [ "blah" ]
}';
$req = $t->post_ok('/tests/99937/modules/isosize/steps/1',{ 'X-CSRF-Token' => $token },form => { json => $json, imagename => 'isosize-1.png', needlename => "isosize-blah", overwrite => 'no' })->status_is(200);
$req->element_exists_not('.ui-state-error');
ok(-f "$dir/isosize-blah.png", "isosize-blah.png created");
ok(-f "$dir/isosize-blah.json", "isosize-blah.json created");
# post needle again and diallow to overwrite
$req = $t->post_ok('/tests/99937/modules/isosize/steps/1',{ 'X-CSRF-Token' => $token },form => { json => 'blah', imagename => 'isosize-1.png', needlename => "isosize-blah", overwrite => 'yes' })->status_is(200);
$req = $t->post_ok('/tests/99937/modules/isosize/steps/1',{ 'X-CSRF-Token' => $token },form => { json => $json, imagename => 'isosize-1.png', needlename => "isosize-blah", overwrite => 'yes' })->status_is(200);
$req->text_like('.ui-state-highlight' => qr/Needle isosize-blah created/);

ok(open(GIT, '-|', @git, 'show'), "git show");
Expand All @@ -85,7 +89,7 @@ $req = $t->post_ok(
'/tests/99937/modules/isosize/steps/1',
{ 'X-CSRF-Token' => $token },
form => {
json => 'blub',
json => $json,
imagename => 'isosize-blah.png',
imagedistri => 'opensuse',
needlename => "isosize-blub",
Expand All @@ -99,7 +103,7 @@ $req = $t->post_ok(
'/tests/99937/modules/isosize/steps/1',
{ 'X-CSRF-Token' => $token },
form => {
json => 'blub',
json => $json,
imagename => '../isosize-blah.png',
imagedistri => 'ope/nsuse',
needlename => ".isosize-blub",
Expand All @@ -108,6 +112,73 @@ $req = $t->post_ok(
)->status_is(200);
$req->text_is('.ui-state-error' => 'Error creating/updating needle: wrong parameters imagename imagedistri imageversion needlename overwrite');

# post invalid json
$req = $t->post_ok(
'/tests/99937/modules/isosize/steps/1',
{ 'X-CSRF-Token' => $token },
form => {
json => 'blub',
imagename => 'isosize-blah.png',
imagedistri => 'opensuse',
needlename => "isosize-blub",
overwrite => "yes"
}
)->status_is(200);
$req->text_like('.ui-state-error' => '/Error validating needle: syntax error: malformed JSON string/');

# post incomplete json i)
my $json1='{
"area" : [ { "width" : 1024,"xpos" : 0,"type" : "match","ypos" : 0, "height" : 768 } ],
"tags" : [ ]
}';
$req = $t->post_ok(
'/tests/99937/modules/isosize/steps/1',
{ 'X-CSRF-Token' => $token },
form => {
json => $json1,
imagename => 'isosize-blah.png',
imagedistri => 'opensuse',
needlename => "isosize-blub",
overwrite => "yes"
}
)->status_is(200);
$req->text_is('.ui-state-error' => 'Error validating needle: no tag defined');

# post incomplete json ii)
my $json2='{
"tags" : [ "blah" ]
}';
$req = $t->post_ok(
'/tests/99937/modules/isosize/steps/1',
{ 'X-CSRF-Token' => $token },
form => {
json => $json2,
imagename => 'isosize-blah.png',
imagedistri => 'opensuse',
needlename => "isosize-blub",
overwrite => "yes"
}
)->status_is(200);
$req->text_is('.ui-state-error' => 'Error validating needle: no area defined');

# post incomplete json iii)
my $json3='{
"area" : [ { "xpos" : 0,"type" : "match","ypos" : 0, "height" : 768 } ],
"tags" : [ "blah" ]
}';
$req = $t->post_ok(
'/tests/99937/modules/isosize/steps/1',
{ 'X-CSRF-Token' => $token },
form => {
json => $json3,
imagename => 'isosize-blah.png',
imagedistri => 'opensuse',
needlename => "isosize-blub",
overwrite => "yes"
}
)->status_is(200);
$req->text_is('.ui-state-error' => 'Error validating needle: area without width');

#open (F, '|-', 'w3m -T text/html');
#open (F, '|-', 'w3m -dump');
#print F $req->tx->res->body;
Expand Down