Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / t / unit_fixture / CXGN / BreedersToolbox / ProductProfile.t
blobbc7b6034878ab424338e8ec0f4acdc08c0125039
1 use strict;
2 use warnings;
3 use lib 't/lib';
4 use Test::More;
5 use Data::Dumper;
6 use SGN::Test::Fixture;
7 use Test::More;
8 use Test::WWW::Mechanize;
9 use LWP::UserAgent;
10 use CXGN::BreedersToolbox::Projects;
11 use Data::Dumper;
12 use JSON;
13 local $Data::Dumper::Indent = 0;
16 my $f = SGN::Test::Fixture->new();
17 my $schema = $f->bcs_schema();
19 for my $extension ("xls", "xlsx") {
21     my $mech = Test::WWW::Mechanize->new;
23     $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username" => "janedoe", "password" => "secretpw", "grant_type" => "password" ]);
24     my $response = decode_json $mech->content;
25     is($response->{'metadata'}->{'status'}->[2]->{'message'}, 'Login Successfull');
26     my $session_id = $response->{access_token};
28     my $program_rs = $schema->resultset('Project::Project')->find({ name => 'test' });
29     my $program_id = $program_rs->project_id();
31     my $profile_json_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'product_profile_json', 'project_property')->cvterm_id();
32     my $before_adding_profile_all_projectprop = $schema->resultset("Project::Projectprop")->search({})->count();
33     my $before_adding_profile_projectprop = $schema->resultset("Project::Projectprop")->search({ project_id => $program_id, type_id => $profile_json_type_id })->count();
35     my $file = $f->config->{basepath} . "/t/data/product_profile_test.$extension";
36     my $ua = LWP::UserAgent->new;
37     $response = $ua->post(
38         'http://localhost:3010/ajax/breeders/program/upload_profile',
39         Content_Type => 'form-data',
40         Content      => [
41             "profile_uploaded_file" => [
42                 $file,
43                 "product_profile_test.$extension",
44                 Content_Type => ($extension eq "xls") ? 'application/vnd.ms-excel' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
45             ],
46             "profile_program_id"    => $program_id,
47             "new_profile_name"      => 'product_profile_1',
48             "new_profile_scope"     => 'test_upload',
49             "sgn_session_id"        => $session_id
50         ]
51     );
53     ok($response->is_success);
54     my $message = $response->decoded_content;
55     my $message_hash = decode_json $message;
56     is_deeply($message_hash, { 'success' => 1 });
58     my $after_adding_profile_all_projectprop = $schema->resultset("Project::Projectprop")->search({})->count();
59     my $after_adding_profile_projectprop = $schema->resultset("Project::Projectprop")->search({ project_id => $program_id, type_id => $profile_json_type_id })->count();
60     is($after_adding_profile_all_projectprop, $before_adding_profile_all_projectprop + 1);
61     is($after_adding_profile_projectprop, $before_adding_profile_projectprop + 1);
62     $f->clean_up_db();
65 done_testing();