05-rest-api.t 2.49 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2
# Test REST API with valid and invalid requests.
# Check also metadatas request (root of a conf)
3 4

use Test::More;
5
use JSON;
6 7
use strict;

Xavier Guimard's avatar
Xavier Guimard committed
8
require 't/test-lib.pm';
9
my $struct = 'site/htdocs/static/struct.json';
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

my @bad = qw(
  /confs/1567
  /confs/abc
  /confs/1/virtualHosts/unknown/locationRules
  /confs/1/virtualHosts/test1.example.com
  /confs/1/virtualHosts/test1.example.com/vhostPorts
  /confs/1/samlIDPMetaDataNodes/unknown
  /confs/1/samlSPMetaDataNodes/unknown
  /confs/1/samlSPMetaDataNodes/unknown/key
  /confs/1/applicationList/unknown
  /confs/1/portal/unknown
);
my @good = qw(
  /confs/latest
  /confs/1
  /confs/1/portal
  /confs/1/virtualHosts
  /confs/1/virtualHosts/test1.example.com/locationRules
  /confs/1/virtualHosts/new__new.example.com/locationRules
  /confs/1/virtualHosts/test1.example.com/vhostPort
  /confs/1/samlIDPMetaDataNodes
  /confs/1/samlSPMetaDataNodes
  /confs/1/applicationList
);

foreach my $query (@good) {
37
    my $href = &client->jsonResponse($query);
38 39 40
}

foreach my $query (@bad) {
41
    my $res = &client->_get( $query, '' );
42 43 44 45
    ok( $res->[0] == 400, "Request reject for $query" )
      or print STDERR "# Receive a $res->[0] code";
    my $href;

46
    ok( $href = from_json( $res->[2]->[0] ), 'Response is JSON' );
47 48 49 50 51 52 53 54 55 56 57 58 59
    ok( $href->{error}, "Receive an explanation message ($href->{error})" );
    count(3);
}

open F, $struct or die "Unable to open $struct";
my @hkeys;
my $hstruct = '';
while (<F>) {
    push @hkeys, ( $_ =~ /cnodes":"([^"]+)/g );
    $hstruct .= $_;
}
close F;

60
ok( $hstruct = from_json($hstruct), 'struct.json is JSON' );
61 62
ok( ref $hstruct eq 'ARRAY', 'struct.json is an array' )
  or print STDERR "Expected: ARRAY, got: " . ( ref $hstruct ) . "\n";
63 64 65
count(2);

foreach my $query (@hkeys) {
66
    my $href = &client->jsonResponse( "/confs/1/$query", '' );
Xavier Guimard's avatar
Xavier Guimard committed
67
    ok( (
Xavier Guimard's avatar
Xavier Guimard committed
68 69 70 71
            ( ref $href eq 'ARRAY' )
              or (  ( ref $href eq 'HASH' )
                and ( $href->{error} =~ /setDefault$/ ) )
        ),
72 73
        'Response is an array'
    );
74
    count(1);
75
    next if ( ( ref $href eq 'HASH' ) and ( $href->{error} =~ /setDefault$/ ) );
76 77 78 79 80 81 82 83 84
    foreach my $k (@$href) {
        ok( defined $k->{title}, 'Title defined' );
        ok( defined $k->{id},    'Id defined' );
        count(2);
    }
}

# Metadatas
{
85
    my $href = &client->jsonResponse( 'confs/1', '' );
86 87 88 89 90 91 92
    foreach (qw(cfgNum cfgAuthor cfgAuthorIP cfgDate)) {
        ok( exists( $href->{$_} ), "Key $_ exists" );
    }
    ok( $href->{cfgNum} == 1, 'cfgNum is set to 1' );
    count(5);
}

Clément OUDOT's avatar
Clément OUDOT committed
93
done_testing( count() );
94