Catalyst-Controller-DBIC-API

 view release on metacpan or  search on metacpan

t/rpc/update.t  view on Meta::CPAN

use strict;
use warnings;

use lib 't/lib';

my $base = 'http://localhost';
my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];

use RestTest;
use DBICTest;
use Test::More;
use Test::WWW::Mechanize::Catalyst 'RestTest';
use HTTP::Request::Common;
use JSON::MaybeXS;

my $json = JSON::MaybeXS->new(utf8 => 1);

my $mech = Test::WWW::Mechanize::Catalyst->new;
ok( my $schema = DBICTest->init_schema(), 'got schema' );

my $track         = $schema->resultset('Track')->first;
my %original_cols = $track->get_columns;

my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
my $any_track_update_url =
    "$base/api/rpc/any/track/id/" . $track->id . "/update";
my $tracks_update_url = "$base/api/rpc/track/update";

# test invalid track id caught
{
    diag 'DBIx::Class warns about a non-numeric id which is ok because we test for that too';
    foreach my $wrong_id ( 'sdsdsdsd', 3434234 ) {
        my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
        my $req = POST( $incorrect_url, { title => 'value' } );

        $mech->request( $req, $content_type );
        cmp_ok( $mech->status, '==', 400,
            'Attempt with invalid track id caught' );

        my $response = $json->decode( $mech->content );
        like(
            $response->{messages}->[0],
            qr/No object found for id/,
            'correct message returned'
        );

        $track->discard_changes;
        is_deeply(
            { $track->get_columns },
            \%original_cols,
            'no update occurred'
        );
    }
}

# validation when no params sent
{
    my $req = POST( $track_update_url, { wrong_param => 'value' } );
    $mech->request( $req, $content_type );
    cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );

    my $response = $json->decode( $mech->content );
    is_deeply( $response->{messages}, ['No valid keys passed'],
        'correct message returned' );

    $track->discard_changes;
    is_deeply(
        { $track->get_columns },
        \%original_cols,
        'no update occurred'
    );
}



( run in 1.769 second using v1.01-cache-2.11-cpan-e1769b4cff6 )