From 246038524e3ce6008688934bc5841c21550d1a80 Mon Sep 17 00:00:00 2001 From: ArtOfCode- <hello@artofcode.co.uk> Date: Sun, 13 Dec 2020 13:55:25 +0000 Subject: [PATCH] Show tests --- app/assets/javascripts/application.js | 17 +++++----- app/views/posts/_expanded.html.erb | 10 +++--- config/routes.rb | 4 +++ test/controllers/posts_controller_test.rb | 40 +++++++++++++++++++++++ 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 627f7c955..5b5dc6f7d 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -72,30 +72,31 @@ $(document).on('ready', function() { }); }); - $("a.close-dialog-link").on("click", (ev) => { + $('.close-dialog-link').on('click', (ev) => { ev.preventDefault(); const self = $(ev.target); console.log(self.parents(".post--body").find(".js-close-box").toggleClass("is-active")); }); - $("button.close-question").on("click", (ev) => { + + $('.js-close-question').on('click', (ev) => { ev.preventDefault(); const self = $(ev.target); - active_radio = self.parents(".js-close-box").find("input[type='radio'][name='close-reason']:checked"); + const active_radio = self.parents('.js-close-box').find("input[type='radio'][name='close-reason']:checked"); const data = { 'reason_id': active_radio.val(), - 'other_post': active_radio.parents(".widget--body").find(".js-close-other-post").val() + 'other_post': active_radio.parents('.widget--body').find('.js-close-other-post').val() // option will be silently discarded if no input element }; - if (data["other_post"]) { - if (data["other_post"].match(/\/[0-9]+$/)) { - data["other_post"] = data["other_post"].replace(/.*\/([0-9]+)$/, "$1"); + if (data['other_post']) { + if (data['other_post'].match(/\/[0-9]+$/)) { + data['other_post'] = data['other_post'].replace(/.*\/([0-9]+)$/, "$1"); } } $.ajax({ 'type': 'POST', - 'url': '/questions/' + self.data("post-id") + '/close', + 'url': '/posts/' + self.data('post-id') + '/close', 'data': data, 'target': self }) diff --git a/app/views/posts/_expanded.html.erb b/app/views/posts/_expanded.html.erb index 3353a2741..736ea0ba6 100644 --- a/app/views/posts/_expanded.html.erb +++ b/app/views/posts/_expanded.html.erb @@ -226,14 +226,14 @@ <% if check_your_post_privilege(post, 'flag_curate') %> <% unless post.locked? %> <% if !post.deleted %> - <%= link_to url_for(controller: post.post_type.name.pluralize.downcase.to_sym, action: :destroy, id: post.id), - method: :delete, data: { confirm: 'Are you sure you want to delete this post?' }, class: "tools--item is-danger" do %> + <%= link_to delete_post_path(post), method: :post, + data: { confirm: 'Are you sure you want to delete this post?' }, class: "tools--item is-danger" do %> <i class="fa fa-trash"></i> Delete <% end %> <% else %> - <%= link_to url_for(controller: post.post_type.name.pluralize.downcase.to_sym, action: :undelete, id: post.id), - method: :post, data: { confirm: 'Restore this post, making it visible to regular users?' }, class: "tools--item is-danger is-filled" do %> + <%= link_to restore_post_path(post), method: :post, + data: { confirm: 'Restore this post, making it visible to regular users?' }, class: "tools--item is-danger is-filled" do %> <i class="fa fa-undo"></i> Restore <% end %> @@ -342,7 +342,7 @@ </div> <% end %> <div class="widget--footer"> - <button class="close-question button is-filled is-muted" data-post-type="<%= is_question ? 'Question' : 'Answer' %>" data-post-id="<%= post.id %>"> + <button class="js-close-question button is-filled is-muted" data-post-id="<%= post.id %>"> Close this post </button> </div> diff --git a/config/routes.rb b/config/routes.rb index 6513870ed..7cfa5fa15 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -86,6 +86,10 @@ Rails.application.routes.draw do get ':id/edit', to: 'posts#edit', as: :edit_post patch ':id/edit', to: 'posts#update', as: :update_post + post ':id/close', to: 'posts#close', as: :close_post + post ':id/reopen', to: 'posts#reopen', as: :reopen_post + post ':id/delete', to: 'posts#delete', as: :delete_post + post ':id/restore', to: 'posts#restore', as: :restore_post post ':id/category', to: 'posts#change_category', as: :change_category post ':id/toggle_comments', to: 'posts#toggle_comments', as: :post_comments_allowance_toggle diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb index 55150e19f..c02898391 100644 --- a/test/controllers/posts_controller_test.rb +++ b/test/controllers/posts_controller_test.rb @@ -180,4 +180,44 @@ class PostsControllerTest < ActionController::TestCase after = CommunityUser.where(user: user, community: communities(:sample)).count assert_equal before + 1, after, 'No CommunityUser record was created' end + + test 'anonymous user can get show' do + get :show, params: { id: posts(:question_one).id } + assert_response 200 + assert_not_nil assigns(:post) + assert_not_nil assigns(:children) + assert_not assigns(:children).any? { |c| c.deleted }, 'Anonymous user can see deleted answers' + end + + test 'standard user can get show' do + sign_in users(:standard_user) + get :show, params: { id: posts(:question_one).id } + assert_response 200 + assert_not_nil assigns(:post) + assert_not_nil assigns(:children) + assert_not assigns(:children).any? { |c| c.deleted }, 'Anonymous user can see deleted answers' + end + + test 'privileged user can see deleted post' do + sign_in users(:deleter) + get :show, params: { id: posts(:deleted).id } + assert_response 200 + assert_not_nil assigns(:post) + assert_not_nil assigns(:children) + end + + test 'privileged user can see deleted answers' do + sign_in users(:deleter) + get :show, params: { id: posts(:question_one).id } + assert_response 200 + assert_not_nil assigns(:post) + assert_not_nil assigns(:children) + assert assigns(:children).any? { |c| c.deleted }, 'Privileged user cannot see deleted answers' + end + + test 'show redirects parented to parent post' do + get :show, params: { id: posts(:answer_one).id } + assert_response 302 + assert_redirected_to post_path(posts(:answer_one).parent_id) + end end -- GitLab