Skip to content

Commit

Permalink
Update unit tests to pass without pre-created settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemhall committed Sep 26, 2023
1 parent 8d59b73 commit c6f436c
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions t/controller_API-Client-v1_0.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,34 @@ my $c = Libki->new();

my $base = '/api/client/v1_0';
my $base_params
= '?node=testClient&location=testLocation&type=testType&ipaddress=123.123.123&macaddress=00-B0-D0-63-C2-26&hostname=testHostnamev';
= '?node=testClient&location=testLocation&type=testType&ipaddress=123.123.123&macaddress=00-B0-D0-63-C2-26&hostname=testHostname&version=1.0.0';

for my $s (
'DefaultTimeAllowance', 'DefaultSessionTimeAllowance',
'DefaultGuestTimeAllowance', 'DefaultGuestSessionTimeAllowance'
)
{
$c->model('DB::Setting')
->update_or_create( { instance => $c->instance, name => $s, value => 60 } );
}

$c->model('DB::Setting')->update_or_create( { instance => $c->instance, name => 'ClientBehavior', value => "FCFS+RES" } );

subtest 'Client registration' => sub {

# Test response for no action FIXME: Maybe we should return a 4xx here?
my $res = request($base);
ok( $res->is_success, 'Request should succeed' );
is( $res->status_line, '200 OK', 'Status line is 200 OK' );
is( $res->decoded_content, '{}', 'Content is empty JSON' );
my $res = request($base);
ok( $res->is_success, 'Request should succeed' );
is( $res->status_line, '200 OK', 'Status line is 200 OK' );
is( $res->decoded_content, '{}', 'Content is empty JSON' );

# Test basic client registration
$res = request( $base . $base_params . '&node_name=TestClient&action=register_node' );
ok( $res->is_success, 'Client registration succeeded' );
my $json = decode_json( $res->decoded_content );
is( $json->{status}, 'online', 'Registered client status is "online"' );
$res = request( $base . $base_params . '&node_name=TestClient&action=register_node' );
ok( $res->is_success, 'Client registration succeeded' );
my $json = decode_json( $res->decoded_content );
is( $json->{status}, 'online', 'Registered client status is "online"' );

};

my $user1
= Libki::Utils::User::create_or_update_user( $c, { username => "test1", password => "test1" } );
Expand All @@ -38,12 +53,12 @@ subtest 'Client login' => sub {

# Test bad logins
## Bad username
$res
my $res
= request( $base
. $base_params
. '&username=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&password=badpassword&action=login' );
ok( $res->is_success, 'Client login response succeeded' );
$json = decode_json( $res->decoded_content );
my $json = decode_json( $res->decoded_content );
is( $json->{error}, 'BAD_LOGIN', 'User was not authenticated' );

### No username
Expand All @@ -67,6 +82,7 @@ subtest 'Client login' => sub {
## Test good credentials
$res = request( $base . $base_params . '&username=test1&password=test1&action=login' );
ok( $res->is_success, 'Client login response succeeded' );
print STDERR "STUFF: " . $res->decoded_content;
$json = decode_json( $res->decoded_content );
is( $json->{authenticated}, '1', 'User was authenticated' );

Expand Down Expand Up @@ -101,19 +117,20 @@ subtest 'Client login' => sub {
};

subtest 'Client guest login' => sub {
my $setting = $c->model( 'DB::Setting' )->find( { instance => $c->instance, name => 'EnableGuestSelfRegistration' } );
my $setting = $c->model('DB::Setting')
->find_or_create( { instance => $c->instance, name => 'EnableGuestSelfRegistration', value => 1 } );
my $original_value = $setting->value;
$setting->update({ value => 1 });
$setting->update( { value => 1 } );

# Do guest login
$res = request( $base . $base_params . '&createGuest=1&action=login' );
my $res = request( $base . $base_params . '&createGuest=1&action=login' );
ok( $res->is_success, 'Client login response succeeded' );
$json = decode_json( $res->decoded_content );
my $json = decode_json( $res->decoded_content );
is( $json->{authenticated}, '1', 'User was authenticated' );
ok( $json->{username}, 'Got guest username' );
ok( $json->{password}, 'Got guest password' );
my $username1 = $json->{username};

# Do second guest login
$res = request( $base . $base_params . '&createGuest=1&action=login' );
ok( $res->is_success, 'Client login response succeeded' );
Expand All @@ -125,13 +142,13 @@ subtest 'Client guest login' => sub {

isnt( $username1, $username2, "Got different guest users for each login" );

$setting->update({ value => 0 });
$setting->update( { value => 0 } );
$res = request( $base . $base_params . '&createGuest=1&action=login' );
ok( !$res->is_success, 'Client login response failed' );
$json = decode_json( $res->decoded_content );
is( $json->{authenticated}, '0', 'User was not authenticated' );
is( $json->{error}, 'GUEST_SELF_REG_NOT_ENABLED', "Error indicates guest self registration is not enabled" );
my $username1 = $json->{username};
is( $json->{error}, 'GUEST_SELF_REG_NOT_ENABLED',
"Error indicates guest self registration is not enabled" );
};

done_testing()

0 comments on commit c6f436c

Please sign in to comment.