Here is the test case:
Controller:
package Test::TestController;
use Dancer ':syntax';
use strict;
use Test::Model::Test;
our $VERSION = '0.1';
prefix '/test';
route();
sub route {
hook 'before' => sub {
if (! session('user') && request->path_info =~ /^\/test\// &&
+request->path_info !~ m{^/login}) {
var requested_path => request->path_info;
request->path_info('/test/login');
}
};
get '/login' => sub {
template 'login_test.tt', {
};
};
##log user in. Validate authentication then redirect to user base
+route
post '/login' => sub {
session user => {id => 1, role =>{ id => 1} };
redirect '/test/website/get/1';
};
get '/website/get/:id' => sub {
##check we're not being passed non id stuff
unless (params->{id} =~ /^[\d]+$/) { redirect '/test/login'; r
+eturn }
my $website = Test::Model::Test::get_website(params->{id});
+
##only for admin for all websites
##check that the website is owned by this user otherwise
unless (session('user')->{role}->{id} eq Test::Model::Test::RO
+LE_ADMIN ||
$website->{created_by} eq session('user')->{id}) {
redirect '/login';
}
template 'website_test.tt', {
'values' => $website,
'form_url' => '/test/website/edit/'.params->{id},
};
};
post '/website/edit/:id' => sub {
##check we're not being passed non id stuff
unless (params->{id} =~ /^[\d]+$/) { redirect '/test/login'; r
+eturn }
my $website = Test::Model::Test::get_website(params->{id});
+
##only for admin for all websites
##check that the website is owned by this user otherwise
unless (session('user')->{role}->{id} eq Test::Model::Test::RO
+LE_ADMIN ||
$website->{created_by} eq session('user')->{id}) {
redirect '/login';
}
my $param_ref = params;
Test::Model::Test::edit_website(session('user'), $param_ref);
##Redirect to add a new website with a flash message
#flash message => 'Website successfully edited!';
redirect '/test/website/get/'.params->{id};
};
}
true;
Model
package Test::Model::Test;
use Dancer::Plugin::Database;
use Dancer::Logger;
use constant ROLE_ADMIN => 1;
##Edits a new website
sub edit_website($$) {
my ($user, $website) = @_;
database->quick_update('test_website', {id => $website->{id}},
{ name => $website
+->{name},
url => $website-
+>{url},
});
database->commit();
}
##Return sthe website object .
sub get_website($) {
my ($id) = @_;
my $website = database->quick_select('test_website', { id => $id }
+);
return $website;
}
true;
Template
<form action="<% form_url %>" method="post" id="f-submit-form" enctype
+="multipart/form-data">
<fieldset>
<h2>Edit Website</h2>
<ul>
<li id="f-container-website-name">
<label for="f-website-name">Name</label>
<input type="text" name="name" id="f-website-name" <%IF va
+lues.name %>value="<% values.name %>"<% END %> />
</li>
<li id="f-container-website-url">
<label for="f-website-url">Url</label>
<input type="text" name="url" id="f-website-url" <%IF valu
+es.url %>value="<% values.url %>"<% END %> />
</li>
<li id="f-container-submit">
<button value="Submit" class="maia-button" id="sub
+mit" type="submit">Submit</button>
</li>
</ul>
</form>
Login template
<div class="login-container">
<div class="login-header">
Login
</div>
<% IF err %>
<div class="login-error-container-enabled">Incorrect username
+/ password</div>
<% END %>
<div class="login-form-container">
<form id="f-form-login" action="<% request.uri_base %>/test/lo
+gin" method="post" enctype="multipart/form-data">
<fieldset>
<ul>
<li id="f-container-username">
<label for="f-username">Username</label>
<input type="text" name="username" id="f-usern
+ame"/>
</li>
<li id="f-container-password">
<label for="f-password">Password</label>
<input type="password" name="password" id="f-p
+assword"/>
</li>
<li id="f-container-submit">
<button value="Submit" class="button" id="subm
+it" type="submit">LOGIN</button>
</li>
</ul>
</fieldset>
</form>
</div>
</div>
DB
create table if not exists `testdb`.test_website (
id integer primary key auto_increment,
name varchar(255) not null,
url varchar(255) not null
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `testdb`.test_website(name, url) values ('test1', 'mytest.
+com');
Steps:
- Login (any user/pwd will do for the test)
- In /website/get/1 , change the name
- Click Submit
- See the name is not refreshed
- F5, name is refreshed
Above steps work fine when running perl bin/app.pl
Test fails when running
sudo -u nginx /usr/local/bin/plackup -E production -s Starman --workers=2 -l /tmp/plack.sock -a bin/app.pl &
Hopefully somebody can point to what I'm doing wrong in my code. Thanks for taking the time.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.