Akron | 3340ae7 | 2022-11-22 12:20:13 +0100 | [diff] [blame] | 1 | #!usr/bin/env perl |
| 2 | use Mojolicious::Lite; |
| 3 | use Mojolicious::Commands; |
| 4 | use Data::Dumper; |
| 5 | use Test::Output qw/:stdout :stderr :combined :functions/; |
| 6 | use Test::More; |
| 7 | use Test::Mojo; |
| 8 | use Mojo::Util qw/decode encode/; |
| 9 | use Mojo::JSON qw/decode_json/; |
| 10 | use Mojo::File 'path'; |
| 11 | use File::Temp 'tempdir'; |
| 12 | |
| 13 | my $t = Test::Mojo->new('Kalamar'); |
| 14 | |
| 15 | my $dir = tempdir CLEANUP => 1; |
| 16 | chdir $dir; |
| 17 | |
| 18 | my $dir_ext = tempdir CLEANUP => 1; |
| 19 | |
| 20 | my $cmds = $t->app->commands; |
| 21 | |
| 22 | like( |
| 23 | join(' ', @{$cmds->namespaces}), |
| 24 | qr!Mojolicious::Command!, |
| 25 | 'Namespace is set' |
| 26 | ); |
| 27 | |
| 28 | like( |
| 29 | join(' ', @{$cmds->namespaces}), |
| 30 | qr!Kalamar::Command!, |
| 31 | 'Namespace is set' |
| 32 | ); |
| 33 | |
| 34 | stdout_like( |
| 35 | sub { |
| 36 | local $ENV{HARNESS_ACTIVE} = 0; |
| 37 | $cmds->run('super-client-info'); |
| 38 | }, |
| 39 | qr/\[write\].*super_client_info/, |
| 40 | 'Write' |
| 41 | ); |
| 42 | |
| 43 | stdout_unlike( |
| 44 | sub { |
| 45 | local $ENV{HARNESS_ACTIVE} = 0; |
| 46 | $cmds->run('super-client-info'); |
| 47 | }, |
| 48 | qr/\[write\].*super_client_info/, |
| 49 | 'Already exists' |
| 50 | ); |
| 51 | |
| 52 | my $file = path($dir, 'super_client_info'); |
| 53 | |
| 54 | my $out = decode_json($file->slurp); |
| 55 | |
| 56 | like($out->{client_id},qr!^.{20}$!); |
| 57 | like($out->{client_secret},qr!^.{20}$!); |
| 58 | |
| 59 | unlink $file->to_string; |
| 60 | |
| 61 | stdout_like( |
| 62 | sub { |
| 63 | local $ENV{HARNESS_ACTIVE} = 0; |
| 64 | $cmds->run('super-client-info', 'my-client'); |
| 65 | }, |
| 66 | qr/\[write\].*super_client_info/, |
| 67 | 'Write with client id' |
| 68 | ); |
| 69 | |
| 70 | $out = decode_json($file->slurp); |
| 71 | |
| 72 | like($out->{client_id}, qr!my-client$!); |
| 73 | like($out->{client_secret}, qr!^.{20}$!); |
| 74 | |
| 75 | my $file_ext = path($dir_ext, 'super_client_fun'); |
| 76 | |
| 77 | stdout_like( |
| 78 | sub { |
| 79 | local $ENV{HARNESS_ACTIVE} = 0; |
| 80 | $cmds->run('super-client-info', 'my-client-2', $file_ext); |
| 81 | }, |
| 82 | qr/\[write\].*super_client_fun/, |
| 83 | 'Error' |
| 84 | ); |
| 85 | |
| 86 | $out = decode_json($file->slurp); |
| 87 | |
| 88 | like($out->{client_id}, qr!my-client$!); |
| 89 | like($out->{client_secret}, qr!^.{20}$!); |
| 90 | |
| 91 | my $secret_1 = $out->{client_secret}; |
| 92 | |
| 93 | $out = decode_json($file_ext->slurp); |
| 94 | |
| 95 | like($out->{client_id}, qr!my-client-2$!); |
| 96 | like($out->{client_secret}, qr!^.{20}$!); |
| 97 | |
| 98 | my $secret_2 = $out->{client_secret}; |
| 99 | |
| 100 | isnt($secret_1, $secret_2, 'Generated secrets differ'); |
| 101 | |
| 102 | |
| 103 | done_testing; |
| 104 | __END__ |