Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 353 lines (292 sloc) 9.514 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
use Mojo::Base -strict;

use Galileo::DB::Deploy;

use Test::More;
use Test::Mojo;

my $t = Galileo::DB::Deploy->create_test_object({test => 1});
$t->ua->max_redirects(2);

subtest 'Anonymous User' => sub {

  # landing page
  $t->get_ok('/')
    ->status_is(200)
    ->text_is( h1 => 'Galileo CMS' )
    ->text_is( h2 => 'Welcome to your Galileo CMS site!' )
    ->content_like( qr/very modern/ )
    ->element_exists( 'form#login' )
    ->element_exists_not( '#user-menu' );

  # attempt to get non-existant page
  $t->get_ok('/page/doesntexist')
    ->status_is(404);

  # attempt to edit page
  $t->get_ok('/edit/home')
    ->status_is(200)
    ->content_like( qr/Not Authorized/ );

  # attempt to menu admin page
  $t->get_ok('/admin/menu')
    ->status_is(200)
    ->content_like( qr/Not Authorized/ );

  # attempt to user admin page
  $t->get_ok('/admin/users')
    ->status_is(200)
    ->content_like( qr/Not Authorized/ );

};

subtest 'Do Login' => sub {

  # fail username
  $t->post_ok( '/login' => form => {from => '/page/home', username => 'wronguser', password => 'pass' } )
    ->status_is(200)
    ->content_like( qr/Sorry try again/ )
    ->element_exists( 'form#login' )
    ->element_exists_not( '#user-menu' );

  # fail password
  $t->post_ok( '/login' => form => {from => '/page/home', username => 'admin', password => 'wrongpass' } )
    ->status_is(200)
    ->content_like( qr/Sorry try again/ )
    ->element_exists( 'form#login' )
    ->element_exists_not( '#user-menu' );

  # successfully login
  $t->post_ok( '/login' => form => {from => '/page/home', username => 'admin', password => 'pass' } )
    ->status_is(200)
    ->content_like( qr/Welcome Back/ )
    ->element_exists_not( 'form#login' )
    ->text_like( '#user-menu li' => qr/Hello admin/ )
    ->element_exists( '#page-modal #new-page-link' )
    ->element_exists( '#user-modal #new-username' );

};

subtest 'Edit Page' => sub {

  # page editor
  $t->get_ok('/edit/home')
    ->status_is(200)
    ->text_like( '#wmd-input' => qr/Welcome to your Galileo CMS site!/ )
    ->element_exists( '#wmd-preview' );

  # save page
  my $text = 'I changed this text ☃';
  my $data = {
    name => 'home',
    title => 'New Home',
    html => "<p>$text</p>",
    md => $text,
  };
  $t->websocket_ok( '/store/page' )
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 1, message => 'Changes saved' } )
    ->finish_ok;

  # see that the changes are reflected
  $t->get_ok('/page/home')
    ->status_is(200)
    ->text_is( h1 => 'New Home' )
    ->text_like( '#content p' => qr/$text/ );

  # save page without title (error)
  my $data_notitle = {
    name => 'notitle',
    title => '',
    html => '<p>Hmmm no title</p>',
    md => 'Hmmm no title',
  };
  $t->websocket_ok( '/store/page' )
    ->send_ok({ json => $data_notitle })
    ->message_ok
    ->json_message_is( { success => 0, message => 'Not saved! A title is required!' } )
    ->finish_ok;

};

subtest 'New Page' => sub {

  # author request non-existant page => create new page
  $t->get_ok('/page/doesntexist')
    ->status_is(200)
    ->text_like( '#wmd-input' => qr/Hello World/ )
    ->element_exists( '#wmd-preview' );

  # save page
  my $text = 'Today it snowed so ☃ gets a new home';
  my $data = {
    name => 'snow❄flake',
    title => 'New Home for ☃',
    html => "<p>$text</p>",
    md => $text,
  };
  $t->websocket_ok( '/store/page' )
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 1, message => 'Changes saved' } )
    ->finish_ok;

  # see that the changes are reflected
  $t->get_ok('/page/snow❄flake')
    ->status_is(200)
    ->text_is( h1 => 'New Home for ☃' )
    ->text_like( '#content p' => qr/$text/ );

};

subtest 'Edit Main Navigation Menu' => sub {
  my $title = 'About Galileo';

  # check about page is in nav
  $t->get_ok('/admin/menu')
    ->status_is(200)
    ->text_is( 'ul#main > li:nth-of-type(3) > a' => $title )
    ->text_is( '#list-active-pages > #pages-2 > span' => $title );

  # remove about page from list
  my $data = {
    name => 'main',
    list => [],
  };
  $t->websocket_ok('/store/menu')
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 1, message => 'Changes saved' } )
    ->finish_ok;

  # check that item is removed
  $t->get_ok('/admin/menu')
    ->status_is(200)
    ->element_exists_not( 'ul#main > li:nth-of-type(3) > a' )
    ->text_is( '#list-inactive-pages > #pages-2 > span' => $title );

  # put about page back
  $data = {
    name => 'main',
    list => ['pages-2'],
  };
  $t->websocket_ok('/store/menu')
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 1, message => 'Changes saved' } )
    ->finish_ok;

  # check about page is back in nav (same as first test block)
  $t->get_ok('/admin/menu')
    ->status_is(200)
    ->text_is( 'ul#main > li:nth-of-type(3) > a' => $title )
    ->text_is( '#list-active-pages > #pages-2 > span' => $title );

};

subtest 'Administrative Overview: All Users' => sub {

  # test the admin pages
  $t->get_ok('/admin/users')
    ->status_is(200)
    ->text_is( h1 => 'Administration: Users' )
    ->text_is( 'tr > td:nth-of-type(2)' => 'admin' )
    ->text_is( 'tr > td:nth-of-type(3)' => 'Joe Admin' );

};

subtest 'Administrative Overview: All Pages' => sub {

  $t->get_ok('/admin/pages')
    ->status_is(200)
    ->text_is( h1 => 'Administration: Pages' )
    ->text_is( 'tr > td:nth-of-type(2)' => 'home' );

  # attempt to remove home page
  $t->websocket_ok('/remove/page')
    ->send_ok({ json => {id => 1} })
    ->message_ok
    ->json_message_is( { success => 0, message => 'Cannot remove home page' } )
    ->finish_ok;

  # attempt to remove invalid page
  $t->websocket_ok('/remove/page')
    ->send_ok({ json => {id => 5} })
    ->message_ok
    ->json_message_is( { success => 0, message => 'Could not access page (id 5)' } )
    ->finish_ok;

  # remove page
  $t->websocket_ok('/remove/page')
    ->send_ok({ json => {id => 2} })
    ->message_ok
    ->json_message_is( { success => 1, message => 'Page removed' } )
    ->finish_ok;

};

subtest 'Administer Users' => sub {

  $t->get_ok('/admin/user/admin')
    ->status_is(200)
    ->element_exists( 'input#name[placeholder=admin]' )
    ->element_exists( 'input#full[value="Joe Admin"]' )
    ->element_exists( 'input#is_author[checked=1]' )
    ->element_exists( 'input#is_admin[checked=1]' );

  # change name
  my $data = {
    name => "admin",
    full => "New Name",
    is_author => 1,
    is_admin => 1,
  };
  $t->websocket_ok('/store/user')
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 1, message => 'Changes saved' } )
    ->finish_ok;

  # check that the name change is reflected
  $t->get_ok('/admin/user/admin')
    ->status_is(200)
    ->element_exists( 'input#name[placeholder=admin]' )
    ->element_exists( 'input#full[value="New Name"]' );

  # attempt to change password, incorrectly
  $data = {
    name => "admin",
    full => "New Name",
    pass1 => 'newpass',
    pass2 => 'wrongpass',
    is_author => 1,
    is_admin => 1,
  };
  $t->websocket_ok('/store/user')
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 0, message => 'Not saved! Passwords do not match' } )
    ->finish_ok;

  ok( $t->app->get_user('admin')->check_password('pass'), 'Password not changed on non-matching passwords');

  # change password, correctly
  $data = {
    name => "admin",
    full => "New Name",
    pass1 => 'newpass',
    pass2 => 'newpass',
    is_author => 1,
    is_admin => 1,
  };
  $t->websocket_ok('/store/user')
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 1, message => 'Changes saved' } )
    ->finish_ok;

  ok( $t->app->get_user('admin')->check_password('newpass'), 'New password checks out');

};

subtest 'Create New User' => sub {

  # attempt to create a user without providing a password (fails)
  my $data = {
    name => "someone",
    full => "Jane ☃ Dow",
    is_author => 1,
    is_admin => 0,
  };
  $t->websocket_ok('/store/user')
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 0, message => 'Cannot create user without a password' } )
    ->finish_ok;

  # create a user
  $data = {
    name => "someone",
    full => "Jane ☃ Doe",
    pass1 => 'mypass',
    pass2 => 'mypass',
    is_author => 1,
    is_admin => 0,
  };
  $t->websocket_ok('/store/user')
    ->send_ok({ json => $data })
    ->message_ok
    ->json_message_is( { success => 1, message => 'Changes saved' } )
    ->finish_ok;

  # check the new user
  $t->get_ok('/admin/user/someone')
    ->status_is(200)
    ->element_exists( 'input#name[placeholder=someone]' )
    ->element_exists( 'input#full[value="Jane ☃ Doe"]' )
    ->element_exists( 'input#is_author:checked' )
    ->element_exists( 'input#is_admin:not(:checked)' );

};

subtest 'Extra CSS/JS' => sub {
  my $app = $t->app;
  local $app->config->{extra_css} = ['mytest.css'];
  local $app->config->{extra_js} = ['mytest.js' ];
  $t->get_ok('/')
    ->status_is(200)
    ->element_exists( 'link[href=mytest.css]' )
    ->element_exists( 'script[src=mytest.js]' );
};

subtest 'Logging Out' => sub {
  # This is essentially a repeat of the first test
  $t->get_ok('/logout')
    ->status_is(200)
    ->text_is( h1 => 'New Home' )
    ->element_exists( 'form' );
};

done_testing();

Something went wrong with that request. Please try again.