API: return 409 on resource conflicts (#2034)

Moo
Maxime Besson 5 years ago
parent ceac3e7d6f
commit 4a610f036d
  1. 8
      lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Api/Providers/OidcRp.pm
  2. 8
      lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Api/Providers/SamlSp.pm
  3. 28
      lemonldap-ng-manager/t/04-providers-api.t

@ -123,13 +123,13 @@ sub addOidcRp {
return $self->sendError(
$req,
"Invalid input: An OIDC RP with confKey $add->{confKey} already exists",
400
409
) if ( defined $self->_getOidcRpByConfKey( $conf, $add->{confKey} ) );
return $self->sendError(
$req,
"Invalid input: An OIDC RP with clientId $add->{clientId} already exists",
400
409
) if ( defined $self->_getOidcRpByClientId( $conf, $add->{clientId} ) );
$add->{options} = {} unless ( defined $add->{options} );
@ -172,7 +172,7 @@ sub updateOidcRp {
# check if new clientID exists already
my $res = $self->_isNewOidcRpClientIdUnique( $conf, $confKey, $update );
return $self->sendError( $req, $res->{msg}, 400 )
return $self->sendError( $req, $res->{msg}, 409 )
unless ( $res->{res} eq 'ok' );
$res = $self->_pushOidcRp( $conf, $confKey, $update, 0 );
@ -230,7 +230,7 @@ sub replaceOidcRp {
# check if new clientID exists already
my $res = $self->_isNewOidcRpClientIdUnique( $conf, $confKey, $replace );
return $self->sendError( $req, $res->{msg}, 400 )
return $self->sendError( $req, $res->{msg}, 409 )
unless ( $res->{res} eq 'ok' );
$replace->{options} = {} unless ( defined $replace->{options} );

@ -107,11 +107,11 @@ sub addSamlSp {
return $self->sendError(
$req,
"Invalid input: A SAML SP with confKey $add->{confKey} already exists",
400
409
) if ( defined $self->_getSamlSpByConfKey( $conf, $add->{confKey} ) );
return $self->sendError( $req,
"Invalid input: A SAML SP with entityID $entityId already exists", 400 )
"Invalid input: A SAML SP with entityID $entityId already exists", 409 )
if ( defined $self->_getSamlSpByEntityId( $conf, $entityId ) );
my $res = $self->_pushSamlSp( $conf, $add->{confKey}, $add, 1 );
@ -152,7 +152,7 @@ sub replaceSamlSp {
# check if new entityId exists already
my $res = $self->_isNewSamlSpEntityIdUnique( $conf, $confKey, $replace );
return $self->sendError( $req, $res->{msg}, 400 )
return $self->sendError( $req, $res->{msg}, 409 )
unless ( $res->{res} eq 'ok' );
$res = $self->_pushSamlSp( $conf, $confKey, $replace, 1 );
@ -192,7 +192,7 @@ sub updateSamlSp {
# check if new entityId exists already
$res = $self->_isNewSamlSpEntityIdUnique( $conf, $confKey, $update );
return $self->sendError( $req, $res->{msg}, 400 )
return $self->sendError( $req, $res->{msg}, 409 )
unless ( $res->{res} eq 'ok' );
}

@ -19,6 +19,16 @@ sub check200 {
}
sub check409 {
my ( $test, $res ) = splice @_;
#diag Dumper($res);
is( $res->[0], "409", "$test: Result code is 409" )
or diag explain $res->[2];
count(1);
checkJson( $test, $res );
}
sub check404 {
my ( $test, $res ) = splice @_;
@ -72,7 +82,7 @@ sub checkAdd {
sub checkAddFailsIfExists {
my ( $test, $type, $add ) = splice @_;
check400( $test, add( $test, $type, $add ) );
check409( $test, add( $test, $type, $add ) );
}
sub checkAddWithUnknownAttributes {
@ -145,7 +155,7 @@ sub checkUpdateNotFound {
sub checkUpdateFailsIfExists {
my ( $test, $type, $confKey, $update ) = splice @_;
check400( $test, update( $test, $type, $confKey, $update ) );
check409( $test, update( $test, $type, $confKey, $update ) );
}
sub checkUpdateWithUnknownAttributes {
@ -323,7 +333,7 @@ checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/clientSecret', 'secret' );
$test = "OidcRp - Check attribute default value was set after add";
checkGet( $test, 'oidc/rp', 'myOidcRp1', 'options/IDTokenSignAlg', 'HS512' );
$test = "OidcRp - Add Should fail on duplicate confKey";
$test = "OidcRp - Add should fail on duplicate confKey";
checkAddFailsIfExists( $test, 'oidc/rp', $oidcRp );
$test = "OidcRp - Update should succeed and keep existing values";
@ -354,12 +364,12 @@ $oidcRp->{options}->{playingPossum} = 'elephant';
checkUpdateWithUnknownAttributes( $test, 'oidc/rp', 'myOidcRp1', $oidcRp );
delete $oidcRp->{options}->{playingPossum};
$test = "OidcRp - Add Should fail on duplicate clientId";
$test = "OidcRp - Add should fail on duplicate clientId";
$oidcRp->{clientId} = "myOidcClient1";
$oidcRp->{confKey} = 'myOidcRp2';
$oidcRp->{confKey} = 'myOidcRp2';
checkAddFailsIfExists( $test, 'oidc/rp', $oidcRp );
$test = "OidcRp - Add Should fail on non existing options";
$test = "OidcRp - Add should fail on non existing options";
$oidcRp->{confKey} = 'myOidcRp2';
$oidcRp->{clientId} = 'myOidcClient2';
$oidcRp->{options}->{playingPossum} = 'ElephantInTheRoom';
@ -482,7 +492,7 @@ checkGet( $test, 'saml/sp', 'mySamlSp1',
$test = "SamlSp - Check attribute default value was set after add";
checkGet( $test, 'saml/sp', 'mySamlSp1', 'options/notOnOrAfterTimeout', 72000 );
$test = "SamlSp - Add Should fail on duplicate confKey";
$test = "SamlSp - Add should fail on duplicate confKey";
checkAddFailsIfExists( $test, 'saml/sp', $samlSp );
$test = "SamlSp - Update should succeed and keep existing values";
@ -516,11 +526,11 @@ $samlSp->{options}->{playingPossum} = 'elephant';
checkUpdateWithUnknownAttributes( $test, 'saml/sp', 'mySamlSp1', $samlSp );
delete $samlSp->{options}->{playingPossum};
$test = "SamlSp - Add Should fail on duplicate entityId";
$test = "SamlSp - Add should fail on duplicate entityId";
$samlSp->{confKey} = 'mySamlSp2';
checkAddFailsIfExists( $test, 'saml/sp', $samlSp );
$test = "SamlSp - Add Should fail on non existing options";
$test = "SamlSp - Add should fail on non existing options";
$samlSp->{confKey} = 'mySamlSp2';
$samlSp->{metadata} = $metadata2;
$samlSp->{options}->{playingPossum} = 'ElephantInTheRoom';

Loading…
Cancel
Save