Debian Codegraph @

lemonldap-ng-manager/t/05-rest-api.t

raw
1 # Test REST API with valid and invalid requests.
2 # Check also metadatas request (root of a conf)
3
4 use Test::More;
5 use JSON;
6 use strict;
7 use warnings;
8
9 require 't/test-lib.pm';
10 my $struct = 'site/htdocs/static/struct.json';
11
12 my @bad = qw(
13 /confs/1567
14 /confs/abc
15 /confs/1/virtualHosts/unknown/locationRules
16 /confs/1/virtualHosts/test1.example.com
17 /confs/1/virtualHosts/test1.example.com/vhostPorts
18 /confs/1/samlIDPMetaDataNodes/unknown
19 /confs/1/samlSPMetaDataNodes/unknown
20 /confs/1/samlSPMetaDataNodes/unknown/key
21 /confs/1/applicationList/unknown
22 /confs/1/portal/unknown
23 );
24 my @good = qw(
25 /confs/latest
26 /confs/1
27 /confs/1/portal
28 /confs/1/virtualHosts
29 /confs/1/virtualHosts/test1.example.com/locationRules
30 /confs/1/virtualHosts/new__new.example.com/locationRules
31 /confs/1/virtualHosts/test1.example.com/vhostPort
32 /confs/1/samlIDPMetaDataNodes
33 /confs/1/samlSPMetaDataNodes
34 /confs/1/applicationList
35 );
36
37 foreach my $query (@good) {
38 my $href = &client->jsonResponse($query);
39 }
40
41 foreach my $query (@bad) {
42 my $res = &client->_get( $query, '' );
43 ok( $res->[0] == 400, "Request reject for $query" )
44 or print STDERR "# Receive a $res->[0] code";
45 my $href;
46
47 ok( $href = from_json( $res->[2]->[0] ), 'Response is JSON' );
48 ok( $href->{error}, "Receive an explanation message ($href->{error})" );
49 count(3);
50 }
51
52 open F, $struct or die "Unable to open $struct";
53 my @hkeys;
54 my $hstruct = '';
55 while (<F>) {
56 push @hkeys, ( $_ =~ /cnodes":"([^"]+)/g );
57 $hstruct .= $_;
58 }
59 close F;
60
61 ok( $hstruct = from_json($hstruct), 'struct.json is JSON' );
62 ok( ref $hstruct eq 'ARRAY', 'struct.json is an array' )
63 or print STDERR "Expected: ARRAY, got: " . ( ref $hstruct ) . "\n";
64 count(2);
65
66 foreach my $query (@hkeys) {
67 my $href = &client->jsonResponse( "/confs/1/$query", '' );
68 ok( (
69 ( ref $href eq 'ARRAY' )
70 or ( ( ref $href eq 'HASH' )
71 and ( $href->{error} =~ /setDefault$/ ) )
72 ),
73 'Response is an array'
74 );
75 count(1);
76 next if ( ( ref $href eq 'HASH' ) and ( $href->{error} =~ /setDefault$/ ) );
77 foreach my $k (@$href) {
78 ok( defined $k->{title}, 'Title defined' );
79 ok( defined $k->{id}, 'Id defined' );
80 count(2);
81 }
82 }
83
84 # Metadatas
85 {
86 my $href = &client->jsonResponse( 'confs/1', '' );
87 foreach (qw(cfgNum cfgAuthor cfgAuthorIP cfgDate)) {
88 ok( exists( $href->{$_} ), "Key $_ exists" );
89 }
90 ok( $href->{cfgNum} == 1, 'cfgNum is set to 1' );
91 count(5);
92 }
93
94 done_testing( count() );
95