Permalink
Browse files

Merge pull request #167 from tomykaira/admin_404

Fix #151
  • Loading branch information...
2 parents a963d60 + 5d4a746 commit e950797e8abca074a1fd9c197171583f535f53b4 @komagata committed Feb 29, 2012
View
37 lib/lokka/app/admin.rb
@@ -70,12 +70,12 @@ class App
end
get '/admin/comments/:id/edit' do |id|
- @comment = Comment.get(id)
+ @comment = Comment.get(id) or raise Sinatra::NotFound
render_any :'comments/edit'
end
put '/admin/comments/:id' do |id|
- @comment = Comment.get(id)
+ @comment = Comment.get(id) or raise Sinatra::NotFound
if @comment.update(params['comment'])
flash[:notice] = t('comment_was_successfully_updated')
redirect '/admin/comments'
@@ -91,7 +91,8 @@ class App
end
delete '/admin/comments/:id' do |id|
- Comment.get(id).destroy
+ comment = Comment.get(id) or raise Sinatra::NotFound
+ comment.destroy
flash[:notice] = t('comment_was_successfully_deleted')
redirect '/admin/comments'
end
@@ -120,12 +121,12 @@ class App
end
get '/admin/categories/:id/edit' do |id|
- @category = Category.get(id)
+ @category = Category.get(id) or raise Sinatra::NotFound
render_any :'categories/edit'
end
put '/admin/categories/:id' do |id|
- @category = Category.get(id)
+ @category = Category.get(id) or raise Sinatra::NotFound
if @category.update(params['category'])
flash[:notice] = t('category_was_successfully_updated')
redirect '/admin/categories'
@@ -135,7 +136,8 @@ class App
end
delete '/admin/categories/:id' do |id|
- Category.get(id).destroy
+ category = Category.get(id) or raise Sinatra::NotFound
+ category.destroy
flash[:notice] = t('category_was_successfully_deleted')
redirect '/admin/categories'
end
@@ -148,12 +150,12 @@ class App
end
get '/admin/tags/:id/edit' do |id|
- @tag = Tag.get(id)
+ @tag = Tag.get(id) or raise Sinatra::NotFound
render_any :'tags/edit'
end
put '/admin/tags/:id' do |id|
- @tag = Tag.get(id)
+ @tag = Tag.get(id) or raise Sinatra::NotFound
if @tag.update(params['tag'])
flash[:notice] = t('tag_was_successfully_updated')
redirect '/admin/tags'
@@ -163,7 +165,8 @@ class App
end
delete '/admin/tags/:id' do |id|
- Tag.get(id).destroy
+ tag = Tag.get(id) or raise Sinatra::NotFound
+ tag.destroy
flash[:notice] = t('tag_was_successfully_deleted')
redirect '/admin/tags'
end
@@ -191,12 +194,12 @@ class App
end
get '/admin/users/:id/edit' do |id|
- @user = User.get(id)
+ @user = User.get(id) or raise Sinatra::NotFound
render_any :'users/edit'
end
put '/admin/users/:id' do |id|
- @user = User.get(id)
+ @user = User.get(id) or raise Sinatra::NotFound
if @user.update(params['user'])
flash[:notice] = t('user_was_successfully_updated')
redirect '/admin/users'
@@ -206,7 +209,7 @@ class App
end
delete '/admin/users/:id' do |id|
- target_user = User.get(id)
+ target_user = User.get(id) or raise Sinatra::NotFound
if current_user == target_user
flash[:alert] = 'Can not delete your self.'
else
@@ -241,12 +244,12 @@ class App
end
get '/admin/snippets/:id/edit' do |id|
- @snippet = Snippet.get(id)
+ @snippet = Snippet.get(id) or raise Sinatra::NotFound
render_any :'snippets/edit'
end
put '/admin/snippets/:id' do |id|
- @snippet = Snippet.get(id)
+ @snippet = Snippet.get(id) or raise Sinatra::NotFound
if @snippet.update(params['snippet'])
flash[:notice] = t('snippet_was_successfully_updated')
redirect '/admin/snippets'
@@ -256,7 +259,8 @@ class App
end
delete '/admin/snippets/:id' do |id|
- Snippet.get(id).destroy
+ snippet = Snippet.get(id) or raise Sinatra::NotFound
+ snippet.destroy
flash[:notice] = t('snippet_was_successfully_deleted')
redirect '/admin/snippets'
end
@@ -389,7 +393,8 @@ class App
end
delete '/admin/field_names/:id' do |id|
- FieldName.get(id).destroy
+ field_name = FieldName.get(id) or raise Sinatra::NotFound
+ field_name.destroy
flash[:notice] = t('field_name_was_successfully_deleted')
redirect '/admin/field_names'
end
View
6 lib/lokka/helpers.rb
@@ -222,7 +222,7 @@ def get_admin_entry_new(entry_class)
def get_admin_entry_edit(entry_class, id)
@name = entry_class.name.downcase
- @entry = entry_class.get(id)
+ @entry = entry_class.get(id) or raise Sinatra::NotFound
@categories = Category.all.map {|c| [c.id, c.title] }.unshift([nil, t('not_select')])
@field_names = FieldName.all(:order => :name.asc)
render_any :'entries/edit'
@@ -248,7 +248,7 @@ def post_admin_entry(entry_class)
def put_admin_entry(entry_class, id)
@name = entry_class.name.downcase
- @entry = entry_class.get(id)
+ @entry = entry_class.get(id) or raise Sinatra::NotFound
if params['preview']
render_preview entry_class.new(params[@name])
else
@@ -265,7 +265,7 @@ def put_admin_entry(entry_class, id)
def delete_admin_entry(entry_class, id)
name = entry_class.name.downcase
- entry = entry_class.get(id)
+ entry = entry_class.get(id) or raise Sinatra::NotFound
entry.destroy
flash[:notice] = t("#{name}_was_successfully_deleted")
if entry.draft
View
19 spec/integration/admin/categories_spec.rb
@@ -54,4 +54,23 @@
Category(@category.id).should be_nil
end
end
+
+ context 'when the category does not exist' do
+ before { Category.destroy }
+
+ context 'GET' do
+ before { get '/admin/categories/9999/edit' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'PUT' do
+ before { put '/admin/categories/9999' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'DELETE' do
+ before { delete '/admin/categories/9999' }
+ it_behaves_like 'a not found page'
+ end
+ end
end
View
19 spec/integration/admin/comments_spec.rb
@@ -70,4 +70,23 @@
Comment.spam.size.should == 0
end
end
+
+ context 'when the comment does not exist' do
+ before { Comment.destroy }
+
+ context 'GET' do
+ before { get '/admin/comments/9999/edit' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'PUT' do
+ before { put '/admin/comments/9999' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'DELETE' do
+ before { delete '/admin/comments/9999' }
+ it_behaves_like 'a not found page'
+ end
+ end
end
View
9 spec/integration/admin/field_names_spec.rb
@@ -37,4 +37,13 @@
FieldName.get(@field_name.id).should be_nil
end
end
+
+ context 'when the field name does not exist' do
+ before { FieldName.destroy }
+
+ context 'DELETE' do
+ before { delete '/admin/field_names/9999' }
+ it_behaves_like 'a not found page'
+ end
+ end
end
View
19 spec/integration/admin/pages_spec.rb
@@ -65,4 +65,23 @@
Page(@page.id).should be_nil
end
end
+
+ context 'when the page does not exist' do
+ before { Page.destroy }
+
+ context 'GET' do
+ before { get '/admin/pages/9999/edit' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'PUT' do
+ before { put '/admin/pages/9999' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'DELETE' do
+ before { delete '/admin/pages/9999' }
+ it_behaves_like 'a not found page'
+ end
+ end
end
View
19 spec/integration/admin/posts_spec.rb
@@ -65,4 +65,23 @@
Post(@post.id).should be_nil
end
end
+
+ context 'when the post does not exist' do
+ before { Post.destroy }
+
+ context 'GET' do
+ before { get '/admin/posts/9999/edit' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'PUT' do
+ before { put '/admin/posts/9999' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'DELETE' do
+ before { delete '/admin/posts/9999' }
+ it_behaves_like 'a not found page'
+ end
+ end
end
View
19 spec/integration/admin/snippets_spec.rb
@@ -52,4 +52,23 @@
Snippet.get(@snippet.id).should be_nil
end
end
+
+ context 'when the snippet does not exist' do
+ before { Snippet.destroy }
+
+ context 'GET' do
+ before { get '/admin/snippets/9999/edit' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'PUT' do
+ before { put '/admin/snippets/9999' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'DELETE' do
+ before { delete '/admin/snippets/9999' }
+ it_behaves_like 'a not found page'
+ end
+ end
end
View
6 spec/integration/admin/spec_helper.rb
@@ -11,3 +11,9 @@
after { User.destroy }
end
+
+shared_examples_for 'a not found page' do
+ it 'should return 404' do
+ last_response.status.should == 404
+ end
+end
View
19 spec/integration/admin/tags_spec.rb
@@ -35,4 +35,23 @@
Tag.get(@tag.id).should be_nil
end
end
+
+ context 'when the tag does not exist' do
+ before { Tag.destroy }
+
+ context 'GET' do
+ before { get '/admin/tags/9999/edit' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'PUT' do
+ before { put '/admin/tags/9999' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'DELETE' do
+ before { delete '/admin/tags/9999' }
+ it_behaves_like 'a not found page'
+ end
+ end
end
View
19 spec/integration/admin/users_spec.rb
@@ -73,4 +73,23 @@
User.get(@user.id).should_not be_nil
end
end
+
+ context 'when the user does not exist' do
+ before { User.destroy }
+
+ context 'GET' do
+ before { get '/admin/users/9999/edit' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'PUT' do
+ before { put '/admin/users/9999' }
+ it_behaves_like 'a not found page'
+ end
+
+ context 'DELETE' do
+ before { delete '/admin/users/9999' }
+ it_behaves_like 'a not found page'
+ end
+ end
end

0 comments on commit e950797

Please sign in to comment.