Trog-DataSource-Virt.t 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. use 5.040;
  2. use re '/aasx';
  3. use warnings FATAL => 'all';
  4. use Test::More;
  5. use Test::MockModule qw{strict};
  6. use Test::Fatal qw{exception};
  7. use Path::Tiny();
  8. use JSON::MaybeXS();
  9. use FindBin;
  10. use FindBin::libs;
  11. # Nothing in this file may ever reach a real hypervisor. Every libvirt object
  12. # here is a fake that records what was asked of it, which is the only safe way
  13. # to test the destructive half: the machine this suite runs on is itself a
  14. # guest of the hypervisor the feature talks to.
  15. require_ok('Trog::DataSource::Virt') or BAIL_OUT("Can't find SUT");
  16. my $logmock = Test::MockModule->new('Trog::DataSource::Virt');
  17. $logmock->redefine( WARN => sub { note(shift) } );
  18. $logmock->redefine( INFO => sub { note(shift) } );
  19. our @CALLS;
  20. {
  21. package FakeDomain;
  22. sub new { my ( $c, %a ) = @_; return bless {%a}, $c }
  23. sub get_name { return $_[0]{name} }
  24. sub get_uuid_string { return $_[0]{uuid} }
  25. sub is_active { return $_[0]{active} }
  26. sub get_info { return { state => $_[0]{state}, memory => 1024, nrVirtCpu => 2 } }
  27. # The operations we must never actually perform: record and return.
  28. sub create { push @main::CALLS, [ 'create', $_[0]{name} ]; return 1 }
  29. sub shutdown { push @main::CALLS, [ 'shutdown', $_[0]{name} ]; return 1 }
  30. sub destroy { push @main::CALLS, [ 'destroy', $_[0]{name} ]; return 1 }
  31. sub create_snapshot { push @main::CALLS, [ 'create_snapshot', $_[0]{name} ]; return 1 }
  32. # Reading a console is not a mutation, so this one really runs.
  33. sub screenshot { return 'image/png' }
  34. }
  35. {
  36. package FakeStream;
  37. sub new { return bless {}, shift }
  38. sub recv_all { my ( $self, $cb ) = @_; $cb->( $self, 'fake-png-bytes' ); return 1 }
  39. sub finish { return 1 }
  40. }
  41. {
  42. package FakeConn;
  43. sub new { return bless {}, shift }
  44. sub new_stream { return FakeStream->new() }
  45. sub list_all_domains {
  46. return (
  47. FakeDomain->new( name => 'alpha', uuid => 'uuid-alpha', active => 1, state => 1 ),
  48. FakeDomain->new( name => 'beta', uuid => 'uuid-beta', active => 0, state => 5 ),
  49. );
  50. }
  51. }
  52. my $connect_fails = 0;
  53. my $virtmock = Test::MockModule->new('Sys::Virt');
  54. $virtmock->redefine(
  55. new => sub {
  56. die "libvirt error: nope\n" if $connect_fails;
  57. return FakeConn->new();
  58. }
  59. );
  60. # Every subtest below that does not say otherwise runs as a machine which is
  61. # not one of the fake guests -- otherwise the suite's answers would depend on
  62. # what the box running it happens to be called.
  63. $Trog::DataSource::Virt::hostname = 'spec-host-that-is-no-guest';
  64. my $series = {
  65. child_form => 'guests.tx',
  66. tags => ['guests'],
  67. visibility => 'private',
  68. user => 'specadmin',
  69. hypervisors => [ { id => 'hv-1', title => 'spec-hv', conn_uri => 'test:///default' } ],
  70. };
  71. subtest 'guests become posts' => sub {
  72. @CALLS = ();
  73. my @posts = Trog::DataSource::Virt::posts( $series, { user_acls => ['admin'] } );
  74. is( scalar @posts, 2, 'one post per guest' );
  75. is( $posts[0]{title}, 'alpha', 'named after the guest' );
  76. is( $posts[0]{id}, 'uuid-alpha', "and identified by the guest's uuid, not ours" );
  77. is( $posts[0]{form}, 'guests.tx', "wearing the series' child type" );
  78. is( $posts[0]{state}, 'running', 'with a legible state' );
  79. is( $posts[1]{state}, 'shut off', '...for each of libvirt\'s numeric ones' );
  80. is( $posts[0]{is_active}, 1, 'active guests are marked so' );
  81. is( $posts[1]{is_active}, 0, 'and inactive ones are not' );
  82. # The listing must not take screenshots: that is a per-guest round trip,
  83. # and it is the browser's job via the preview route.
  84. is( $posts[0]{preview}, '/guest/screenshot/hv-1/alpha', 'the preview is a route, not an inline capture' );
  85. is_deeply( \@CALLS, [], 'and nothing was done to any guest merely by listing' );
  86. is_deeply( $posts[0]{tags}, ['guests'], "children inherit the series' tags" );
  87. is( $posts[0]{visibility}, 'private', 'and its visibility' );
  88. };
  89. subtest 'the guest this server runs on is marked as such' => sub {
  90. local $Trog::DataSource::Virt::hostname = 'alpha';
  91. my @posts = Trog::DataSource::Virt::posts( $series, { user_acls => ['admin'] } );
  92. is( $posts[0]{is_self}, 1, 'the guest named after this machine is flagged' );
  93. is( $posts[1]{is_self}, 0, 'and the one that merely lives beside it is not' );
  94. };
  95. subtest 'a guest is this server when it is named like it' => sub {
  96. my @same = (
  97. [ 'foo', 'foo' => 'the same name' ],
  98. [ 'foo', 'FOO' => 'the same name in another case' ],
  99. [ 'foo.troglodyne.net', 'foo.troglodyne.net' => 'both fully qualified' ],
  100. );
  101. foreach my $case (@same) {
  102. my ( $guest, $host, $why ) = @$case;
  103. local $Trog::DataSource::Virt::hostname = $host;
  104. ok( Trog::DataSource::Virt::is_self($guest), "$why is this server" );
  105. }
  106. my @different = (
  107. [ 'beta', 'foo' => 'a different name' ],
  108. [ 'fooling', 'foo' => 'a name this one is only a prefix of' ],
  109. [ 'foo.test.test', 'foo.troglodyne.net' => 'the same label in someone else\'s domain' ],
  110. [ '', 'foo' => 'a guest with no name at all' ],
  111. [ 'foo', '' => 'any guest, when we cannot tell what we are called' ],
  112. );
  113. foreach my $case (@different) {
  114. my ( $guest, $host, $why ) = @$case;
  115. local $Trog::DataSource::Virt::hostname = $host;
  116. ok( !Trog::DataSource::Virt::is_self($guest), "$why is not" );
  117. }
  118. };
  119. subtest 'an unreachable hypervisor says so rather than vanishing' => sub {
  120. $connect_fails = 1;
  121. my @posts = Trog::DataSource::Virt::posts( $series, { user_acls => ['admin'] } );
  122. $connect_fails = 0;
  123. is( scalar @posts, 1, 'we get a post back' );
  124. ok( $posts[0]{unreachable}, 'flagged as unreachable' );
  125. like( $posts[0]{state}, qr/unreachable/, 'saying as much' );
  126. is( $posts[0]{title}, 'spec-hv', 'named for the hypervisor we could not reach' );
  127. };
  128. subtest 'act() drives the right libvirt call, and only on request' => sub {
  129. my %expected = (
  130. poweron => 'create',
  131. poweroff => 'shutdown',
  132. snapshot => 'create_snapshot',
  133. destroy => 'destroy',
  134. );
  135. foreach my $action ( sort keys %expected ) {
  136. @CALLS = ();
  137. my ( $ok, $message ) = Trog::DataSource::Virt::act( $action, qw{test:///default alpha specadmin} );
  138. ok( $ok, "$action succeeded" ) or diag($message);
  139. is_deeply( \@CALLS, [ [ $expected{$action}, 'alpha' ] ], "$action called $expected{$action} on the named guest, once" );
  140. }
  141. };
  142. subtest 'act() refuses what it should' => sub {
  143. @CALLS = ();
  144. my ( $ok, $message ) = Trog::DataSource::Virt::act(qw{rm-rf test:///default alpha specadmin});
  145. ok( !$ok, 'an action we do not define is refused' );
  146. like( $message, qr/not[ ]something[ ]we[ ]do/, 'and says so' );
  147. ( $ok, $message ) = Trog::DataSource::Virt::act(qw{destroy test:///default ../../etc/passwd specadmin});
  148. ok( !$ok, 'a guest name that is a path is refused' );
  149. like( $message, qr/bad[ ]guest[ ]name/, 'before any connection is made' );
  150. ( $ok, $message ) = Trog::DataSource::Virt::act(qw{destroy test:///default nosuchguest specadmin});
  151. ok( !$ok, 'a guest that is not there is refused' );
  152. like( $message, qr/no[ ]guest[ ]called/, 'rather than acting on some other one' );
  153. is_deeply( \@CALLS, [], 'and none of those touched a guest' );
  154. $connect_fails = 1;
  155. ( $ok, $message ) = Trog::DataSource::Virt::act(qw{poweroff test:///default alpha specadmin});
  156. $connect_fails = 0;
  157. ok( !$ok, 'an unreachable hypervisor is refused' );
  158. like( $message, qr/could[ ]not[ ]reach/, 'and reported' );
  159. is_deeply( \@CALLS, [], 'having done nothing' );
  160. };
  161. subtest 'act() will not take the server down with it' => sub {
  162. # Hiding the buttons is not the guard: /guest/act is a POST anybody with
  163. # the admin acl can craft, and if this went through there would be nothing
  164. # left running to say what happened.
  165. local $Trog::DataSource::Virt::hostname = 'alpha';
  166. foreach my $action (qw{poweroff destroy}) {
  167. @CALLS = ();
  168. my ( $ok, $message ) = Trog::DataSource::Virt::act( $action, qw{test:///default alpha specadmin} );
  169. ok( !$ok, "$action on the guest we are running in is refused" );
  170. like( $message, qr/is[ ]this[ ]server/, 'saying why' );
  171. is_deeply( \@CALLS, [], "and the $action never reached libvirt" );
  172. }
  173. # The rest are not destructive, and a guest that cannot be snapshotted is
  174. # less useful than one that can.
  175. foreach my $action (qw{snapshot poweron}) {
  176. @CALLS = ();
  177. my ( $ok, $message ) = Trog::DataSource::Virt::act( $action, qw{test:///default alpha specadmin} );
  178. ok( $ok, "$action is still allowed on it" ) or diag($message);
  179. is( scalar @CALLS, 1, 'and reached libvirt' );
  180. }
  181. # Its neighbours are ordinary guests.
  182. @CALLS = ();
  183. my ($ok) = Trog::DataSource::Virt::act(qw{destroy test:///default beta specadmin});
  184. ok( $ok, 'another guest on the same hypervisor is unaffected' );
  185. is_deeply( \@CALLS, [ [ 'destroy', 'beta' ] ], 'and is destroyed as asked' );
  186. };
  187. subtest 'screenshot() validates its inputs before anything else' => sub {
  188. my $hv = { id => 'hv-1', conn_uri => 'test:///default' };
  189. my ( $path, $why ) = Trog::DataSource::Virt::screenshot( $hv, '../../../etc/passwd' );
  190. is( $path, undef, 'a path-shaped guest name gets no screenshot' );
  191. like( $why, qr/bad[ ]guest[ ]name/, 'and is named as the reason' );
  192. ( $path, $why ) = Trog::DataSource::Virt::screenshot( $hv, 'alpha/../../beta' );
  193. is( $path, undef, 'nor does one with traversal in the middle' );
  194. # The cache path is built from the hypervisor id too, so it has to be
  195. # checked as well -- otherwise it is a directory traversal of its own.
  196. ( $path, $why ) = Trog::DataSource::Virt::screenshot( { id => '../../..', conn_uri => 'test:///default' }, 'alpha' );
  197. is( $path, undef, 'nor does a path-shaped hypervisor id' );
  198. like( $why, qr/bad[ ]hypervisor[ ]id/, 'which is reported separately' );
  199. };
  200. subtest 'the guest cache is scoped per hypervisor' => sub {
  201. # Two hypervisors, each with a guest called 'alpha' -- which is the normal
  202. # state of affairs, not a corner case.
  203. my $root = Path::Tiny->tempdir();
  204. no warnings 'once';
  205. local $Trog::DataSource::Virt::screenshot_dir = "$root";
  206. my @paths;
  207. foreach my $id (qw{hv-1 hv-2}) {
  208. my ( $path, $why ) = Trog::DataSource::Virt::screenshot( { id => $id, conn_uri => 'test:///default' }, 'alpha' );
  209. ok( $path, "screenshot taken for $id" ) or diag($why);
  210. push( @paths, $path );
  211. }
  212. isnt( $paths[0], $paths[1], "two hypervisors' guests do not share a cache entry" );
  213. like( $paths[0], qr{/hv-1/alpha\.png$}, 'the path names the hypervisor and the guest' );
  214. };
  215. subtest 'the datasource hook only loads what it should' => sub {
  216. require Trog::Routes::HTML;
  217. my $htmlmock = Test::MockModule->new('Trog::Routes::HTML');
  218. $htmlmock->redefine( WARN => sub { note(shift) } );
  219. my $declared;
  220. my $dmmock = Test::MockModule->new('Trog::DataModule');
  221. $dmmock->redefine( type_meta_for => sub { return { 'x-tcms-datasource' => $declared } } );
  222. my @stored = ( { title => 'a stored post' } );
  223. my $query = { primary_post => { child_form => 'guests.tx' }, user_acls => ['admin'] };
  224. # A type that declares nothing keeps its own posts -- this hook must be
  225. # invisible to the overwhelming majority of pages.
  226. $declared = undef;
  227. is_deeply( [ Trog::Routes::HTML::datasource_posts( $query, \@stored ) ], \@stored, 'no datasource declared, posts untouched' );
  228. # The namespace restriction is the point: a sidecar is a file on disk, and
  229. # it must not be able to make us require an arbitrary module.
  230. foreach my $bad (qw{Trog::Routes::HTML File::Path ../../etc/passwd Trog::DataSource::Virt::act}) {
  231. $declared = $bad;
  232. is_deeply( [ Trog::Routes::HTML::datasource_posts( $query, \@stored ) ], \@stored, "'$bad' is refused as a datasource" );
  233. }
  234. # One that does not exist is a warning, not a dead page.
  235. $declared = 'Trog::DataSource::Nonexistent';
  236. is_deeply( [ Trog::Routes::HTML::datasource_posts( $query, \@stored ) ], \@stored, 'a datasource that will not load falls back' );
  237. # And a real one is actually used.
  238. $declared = 'Trog::DataSource::Virt';
  239. $query->{primary_post}{hypervisors} = [ { id => 'hv-1', title => 'spec-hv', conn_uri => 'test:///default' } ];
  240. my @got = Trog::Routes::HTML::datasource_posts( $query, \@stored );
  241. is( scalar @got, 2, 'a declared datasource replaces the stored posts' );
  242. is( $got[0]{title}, 'alpha', 'with what it built' );
  243. };
  244. subtest 'the guest routes avoid the router\'s own query keys' => sub {
  245. require Trog::Routes::HTML;
  246. # TPSGI applies a route's captures in extract_query, and *then* overwrites
  247. # $query->{domain} with the request's own host. A capture or form field by
  248. # that name therefore never reaches the callback -- which silently aimed
  249. # the screenshot and action routes at a guest named after the web host.
  250. my @reserved = qw{domain route method scheme port user acls to};
  251. no warnings 'once';
  252. my $captures = $Trog::Routes::HTML::routes{'/guest/screenshot/(.*)/(.*)'}{captures};
  253. foreach my $capture (@$captures) {
  254. ok( !( grep { $_ eq $capture } @reserved ), "the screenshot route's '$capture' capture is not a router key" );
  255. }
  256. # The action form posts the same field names the handler reads, so check
  257. # the template rather than trusting that they still agree.
  258. my $form = Path::Tiny->new("$FindBin::Bin/../www/templates/html/components/forms/guests.tx")->slurp_utf8;
  259. foreach my $reserved (@reserved) {
  260. unlike( $form, qr/name="\Q$reserved\E"/, "the guest action form does not post a field called '$reserved'" )
  261. unless $reserved eq 'to'; # 'to' is the redirect target, and is meant to be the router's
  262. }
  263. like( $form, qr/name="guest"/, 'it posts the guest under a name that survives routing' );
  264. };
  265. subtest 'guests show nothing an unprivileged viewer cannot use' => sub {
  266. require Text::Xslate;
  267. # Rendered rather than grepped: what matters is what a given viewer ends up
  268. # looking at, and the template has enough branches that reading it is not
  269. # the same as knowing.
  270. my $tx = Text::Xslate->new(
  271. path => ["$FindBin::Bin/../www/templates/html/components"],
  272. function => { render_it => sub { $_[0] } },
  273. );
  274. my %post = (
  275. form => 'guests.tx',
  276. id => 'uuid-alpha', title => 'alpha', state => 'running', is_active => 1,
  277. preview => '/guest/screenshot/hv-1/alpha', domain => 'alpha',
  278. hypervisor => 'hv-1', hypervisor_title => 'spec-hv', vcpus => 2,
  279. addpost => 0, unreachable => 0,
  280. );
  281. my %views = (
  282. 'tiled, logged out' => { tiled => 1, can_edit => 0 },
  283. 'untiled, logged out' => { tiled => 0, can_edit => 0 },
  284. 'untiled, admin' => { tiled => 0, can_edit => 1 },
  285. );
  286. require Trog::Routes::HTML;
  287. foreach my $view ( sort keys %views ) {
  288. my $admin = $views{$view}{can_edit};
  289. # Redact the way posts() does before rendering, so this covers the
  290. # thing that actually protects the capture -- the template only decides
  291. # whether to draw an img for a value it was given.
  292. my %shown = %post;
  293. Trog::Routes::HTML::redact_private( \%shown ) unless $admin;
  294. my $out = $tx->render( 'forms/guests.tx', { post => \%shown, style => '', route => '/vm', %{ $views{$view} } } );
  295. # The guest is always named -- that is the point of the page.
  296. like( $out, qr/alpha/, "$view: the guest is listed" );
  297. # The screenshot route requires the admin acl, so showing the img to
  298. # anyone else is a broken image and nothing else.
  299. is( scalar( () = $out =~ m/<img[ ]/g ), $admin ? 1 : 0, "$view: console capture shown only to an admin" );
  300. is( scalar( () = $out =~ m{href="/guest/screenshot}g ), $admin ? 1 : 0, "$view: and so is the link to it" );
  301. # Powering off, snapshotting and destroying likewise.
  302. is( scalar( () = $out =~ m{action="/guest/act"}g ), $admin ? 1 : 0, "$view: controls shown only to an admin" );
  303. foreach my $action (qw{poweroff snapshot destroy}) {
  304. is( scalar( () = $out =~ m/value="\Q$action\E"/g ), $admin ? 1 : 0, "$view: no $action button" ) unless $admin;
  305. }
  306. }
  307. };
  308. subtest 'the server itself is drawn without the controls that would kill it' => sub {
  309. require Text::Xslate;
  310. my $tx = Text::Xslate->new(
  311. path => ["$FindBin::Bin/../www/templates/html/components"],
  312. function => { render_it => sub { $_[0] } },
  313. );
  314. my %post = (
  315. form => 'guests.tx',
  316. id => 'uuid-alpha', title => 'alpha', state => 'running', is_active => 1,
  317. preview => '/guest/screenshot/hv-1/alpha', domain => 'alpha',
  318. hypervisor => 'hv-1', hypervisor_title => 'spec-hv', vcpus => 2,
  319. addpost => 0, unreachable => 0,
  320. );
  321. foreach my $tiled ( 0, 1 ) {
  322. my $view = $tiled ? 'tiled' : 'untiled';
  323. my $me = $tx->render(
  324. 'forms/guests.tx',
  325. { post => { %post, is_self => 1 }, style => '', route => '/vm', tiled => $tiled, can_edit => 1 }
  326. );
  327. my $them = $tx->render(
  328. 'forms/guests.tx',
  329. { post => { %post, is_self => 0 }, style => '', route => '/vm', tiled => $tiled, can_edit => 1 }
  330. );
  331. # Said out loud, so an admin knows why this one is different.
  332. like( $me, qr/dom0/, "$view: the guest we are running in says so" );
  333. unlike( $them, qr/dom0/, "$view: and an ordinary guest does not" );
  334. }
  335. # Only the untiled view draws controls at all.
  336. my $me = $tx->render(
  337. 'forms/guests.tx',
  338. { post => { %post, is_self => 1 }, style => '', route => '/vm', tiled => 0, can_edit => 1 }
  339. );
  340. foreach my $action (qw{poweroff destroy}) {
  341. is( scalar( () = $me =~ m/value="\Q$action\E"/g ), 0, "no $action button for the server itself" );
  342. }
  343. is( scalar( () = $me =~ m/value="snapshot"/g ), 1, 'but it can still be snapshotted' );
  344. like( $me, qr{action="/guest/act"}, 'and the form is still there to do it with' );
  345. # A guest that is merely switched off must keep its Power On button -- the
  346. # is_self guard wraps that branch too.
  347. my $off = $tx->render(
  348. 'forms/guests.tx',
  349. { post => { %post, is_self => 0, is_active => 0, state => 'shut off' }, style => '', route => '/vm', tiled => 0, can_edit => 1 }
  350. );
  351. is( scalar( () = $off =~ m/value="poweron"/g ), 1, 'an ordinary stopped guest can still be started' );
  352. };
  353. subtest 'a datasource says whether its posts can be edited' => sub {
  354. require Trog::Routes::HTML;
  355. my $sources = Trog::Routes::HTML::get_datasources();
  356. ok( ( grep { $_ eq 'Trog::DataSource::Virt' } @$sources ), 'the wizard can find this datasource' );
  357. ok( !( grep { !m/^Trog::DataSource::\w+$/ } @$sources ), 'and finds nothing that is not one' );
  358. is( Trog::DataSource::Virt->EDITABLE, 0, 'a guest cannot be edited' );
  359. is( Trog::Routes::HTML::datasource_editable('Trog::DataSource::Virt'), 0, 'so no editor is appropriate' );
  360. # The datastore is the default, and posts in it are written by people.
  361. is( Trog::Routes::HTML::datasource_editable(''), 1, 'the datastore is editable' );
  362. # A datasource that says nothing is assumed editable, since that is what a
  363. # post type is unless it has a reason not to be.
  364. {
  365. no warnings qw{once};
  366. $INC{'Trog/DataSource/SpecMute.pm'} = 1;
  367. @Trog::DataSource::SpecMute::ISA = ();
  368. }
  369. is( Trog::Routes::HTML::datasource_editable('Trog::DataSource::SpecMute'), 1, 'one that declares nothing is assumed editable' );
  370. };
  371. subtest 'the guests type is built by the wizard, from a datasource' => sub {
  372. my $sidecar = JSON::MaybeXS::decode_json( Path::Tiny->new("$FindBin::Bin/../www/templates/html/components/forms/guests.json")->slurp_utf8 );
  373. is( $sidecar->{'x-tcms-datasource'}, 'Trog::DataSource::Virt', 'it names its datasource' );
  374. ok( $sidecar->{'x-tcms-post-type'}{generated}, 'and is wizard generated rather than hand written' );
  375. # The relation is what tells the datasource which hypervisors to ask.
  376. is_deeply(
  377. $sidecar->{'x-tcms-relations'}, { hypervisors => { form => 'hypervisor.tx' } },
  378. 'and keeps the relation the datasource reads'
  379. );
  380. # No editor: a guest is built on every view and has nothing to save.
  381. my $form = Path::Tiny->new("$FindBin::Bin/../www/templates/html/components/forms/guests.tx")->slurp_utf8;
  382. unlike( $form, qr{action="/post/save"}, 'the template offers no editor' );
  383. };
  384. subtest 'the guests type declares its console capture private' => sub {
  385. require Trog::DataModule;
  386. is_deeply(
  387. Trog::DataModule::private_fields_for('guests.tx'), ['preview'],
  388. 'so the url to it is withheld rather than guarded in the template'
  389. );
  390. };
  391. subtest 'private fields are dropped before a non-editor sees them' => sub {
  392. require Trog::Routes::HTML;
  393. require Trog::DataModule;
  394. my $dmmock = Test::MockModule->new('Trog::DataModule');
  395. $dmmock->redefine( private_fields_for => sub { return $_[0] eq 'secretive.tx' ? ['hidden'] : [] } );
  396. my $post = {
  397. form => 'secretive.tx',
  398. title => 'a post',
  399. hidden => 'the secret',
  400. shown => 'not a secret',
  401. # A relation is a post in its own right, with its own private fields.
  402. related => { form => 'secretive.tx', title => 'related', hidden => 'another secret' },
  403. many => [ { form => 'secretive.tx', hidden => 'a third secret' } ],
  404. other => { form => 'ordinary.tx', hidden => 'not declared private here' },
  405. };
  406. Trog::Routes::HTML::redact_private($post);
  407. ok( !exists $post->{hidden}, 'the private field is gone' );
  408. is( $post->{shown}, 'not a secret', 'and the public ones are not' );
  409. is( $post->{title}, 'a post', '...including the ones every post has' );
  410. ok( !exists $post->{related}{hidden}, 'a related post is redacted too' );
  411. ok( !exists $post->{many}[0]{hidden}, 'and so is one reached through a list' );
  412. is( $post->{other}{hidden}, 'not declared private here', 'a type that declares nothing keeps everything' );
  413. };
  414. subtest 'a post that refers to itself does not hang the redactor' => sub {
  415. require Trog::Routes::HTML;
  416. require Trog::DataModule;
  417. my $dmmock = Test::MockModule->new('Trog::DataModule');
  418. $dmmock->redefine( private_fields_for => sub { return ['hidden'] } );
  419. my $post = { form => 'loop.tx', hidden => 'secret' };
  420. $post->{itself} = $post;
  421. # A relation can point back at whatever pulled it in.
  422. is( exception { Trog::Routes::HTML::redact_private($post) }, undef, 'it terminates' );
  423. ok( !exists $post->{hidden}, 'having done the job' );
  424. };
  425. subtest 'a hypervisor does not show its connection uri to everyone' => sub {
  426. require Trog::DataModule;
  427. # The uri carries a host and a username, and these pages are public. It is
  428. # declared private rather than guarded in the template, so the value is
  429. # dropped before rendering and the template needs no guard of its own.
  430. my $private = Trog::DataModule::private_fields_for('hypervisor.tx');
  431. is_deeply( $private, ['conn_uri'], 'the connection uri is declared private' );
  432. my $sidecar = JSON::MaybeXS::decode_json( Path::Tiny->new("$FindBin::Bin/../www/templates/html/components/forms/hypervisor.json")->slurp_utf8 );
  433. ok( $sidecar->{properties}{conn_uri}{'x-tcms-private'}, 'and says so in its sidecar' );
  434. };
  435. done_testing();