Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. require 'rails_helper'
  2. include SessionsHelper
  3.  
  4. RSpec.describe CommentsController, type: :controller do
  5. let(:my_user) { User.create!(name: 'Bloccit User', email: 'user@bloccit.com', password: 'helloworld') }
  6. let(:other_user) { User.create!(name: RandomData.random_name, email: RandomData.random_email, password: 'helloworld', role: 'member') }
  7. let(:my_topic) { Topic.create!(name: RandomData.random_sentence, description: RandomData.random_paragraph) }
  8. let(:my_post) { my_topic.posts.create!(title: RandomData.random_sentence, body: RandomData.random_paragraph, user: my_user) }
  9. let(:my_comment) { Comment.create!(body: 'Comment Body', post: my_post, topic: my_topic, user: my_user) }
  10.  
  11. context "guest" do
  12. describe "POST create" do
  13. it "redirects the user to the sign in view" do
  14. post :create, post_id: my_post.id, comment: {body: RandomData.random_paragraph}
  15.  
  16. expect(response).to redirect_to(new_session_path)
  17. end
  18. end
  19.  
  20. describe "DELETE destroy" do
  21. it "redirects the user to the sign in view" do
  22. delete :destroy, post_id: my_post.id, id: my_comment.id
  23.  
  24. expect(response).to redirect_to(new_session_path)
  25. end
  26. end
  27. end
  28.  
  29. context "member doing CRUD on a comment they don't own" do
  30. before do
  31. create_session(other_user)
  32. end
  33.  
  34. describe "POST create comment on post" do
  35. it "increases the number of comments by 1" do
  36. expect{ post :create, post_id: my_post.id, comment: {body: RandomData.random_sentence} }.to change(Comment, :count).by(1)
  37. end
  38.  
  39. it "redirects to the post show view" do
  40. post :create, post_id: my_post.id, comment: {body: RandomData.random_sentence}
  41.  
  42. expect(response).to redirect_to [my_topic, my_post]
  43. end
  44. end
  45.  
  46. describe "POST create comment on topic" do
  47. it "increases the number of comments by 1" do
  48. expect{ post :create, topic_id: my_topic.id, comment: {body: RandomData.random_sentence} }.to change(Comment, :count).by(1)
  49. end
  50.  
  51. it "redirects to the topic show view" do
  52. post :create, topic_id: my_topic.id, comment: {body: RandomData.random_sentence}
  53.  
  54. expect(response).to redirect_to my_topic
  55. end
  56. end
  57.  
  58. describe "DELETE destroy comment on post" do
  59. it "redirects the user to the posts show view" do
  60. delete :destroy, post_id: my_post.id, id: my_comment.id
  61.  
  62. expect(response).to redirect_to [my_topic, my_post]
  63. end
  64. end
  65.  
  66. describe "DELETE destroy comment on topic" do
  67. it "redirects the user to the topic show view" do
  68. delete :destroy, topic_id: my_topic.id, id: my_comment.id
  69.  
  70. expect(response).to redirect_to my_topic
  71. end
  72. end
  73. end
  74.  
  75. context "member doing CRUD on their own comment" do
  76. before do
  77. create_session(my_user)
  78. end
  79.  
  80. describe "POST create comment on post" do
  81. it "increases the number of comments by 1" do
  82. expect{ post :create, post_id: my_post.id, comment: {body: RandomData.random_sentence} }.to change(Comment, :count).by(1)
  83. end
  84.  
  85. it "redirects to the post show view" do
  86. post :create, post_id: my_post.id, comment: {body: RandomData.random_sentence}
  87.  
  88. expect(response).to redirect_to [my_topic, my_post]
  89. end
  90. end
  91.  
  92. describe "POST create comment on topic" do
  93. it "increases the number of comments by 1" do
  94. expect{ post :create, topic_id: my_topic.id, comment: {body: RandomData.random_sentence} }.to change(Comment, :count).by(1)
  95. end
  96.  
  97. it "redirects to the topic show view" do
  98. post :create, topic_id: my_topic.id, comment: {body: RandomData.random_sentence}
  99.  
  100. expect(response).to redirect_to my_topic
  101. end
  102. end
  103.  
  104. describe "DELETE destroy comment on post" do
  105. it "deletes the comment" do
  106. delete :destroy, post_id: my_post.id, id: my_comment.id
  107. count = Comment.where({id: my_comment.id}).count
  108. expect(count).to eq 0
  109. end
  110.  
  111. it "redirects the user to the posts show view" do
  112. delete :destroy, post_id: my_post.id, id: my_comment.id
  113.  
  114. expect(response).to redirect_to [my_topic, my_post]
  115. end
  116. end
  117.  
  118. describe "DELETE destroy comment on topic" do
  119. it "deletes the comment" do
  120. delete :destroy, topic_id: my_topic.id, id: my_comment.id
  121. count = Comment.where({id: my_comment.id}).count
  122. expect(count).to eq 0
  123. end
  124.  
  125. it "redirects the user to the topic show view" do
  126. delete :destroy, topic_id: my_topic.id, id: my_comment.id
  127.  
  128. expect(response).to redirect_to my_topic
  129. end
  130. end
  131. end
  132.  
  133. context "admin user doing CRUD on a comment they don't own" do
  134. before do
  135. other_user.admin!
  136. create_session(other_user)
  137. end
  138.  
  139. describe "POST create comment on post" do
  140. it "increases the number of comments by 1" do
  141. expect{ post :create, post_id: my_post.id, comment: {body: RandomData.random_sentence} }.to change(Comment, :count).by(1)
  142. end
  143.  
  144. it "redirects to the post show view" do
  145. post :create, post_id: my_post.id, comment: {body: RandomData.random_sentence}
  146.  
  147. expect(response).to redirect_to [my_topic, my_post]
  148. end
  149. end
  150.  
  151. describe "POST create comment on topic" do
  152. it "increases the number of comments by 1" do
  153. expect{ post :create, topic_id: my_topic.id, comment: {body: RandomData.random_sentence} }.to change(Comment, :count).by(1)
  154. end
  155.  
  156. it "redirects to the topic show view" do
  157. post :create, topic_id: my_topic.id, comment: {body: RandomData.random_sentence}
  158.  
  159. expect(response).to redirect_to my_topic
  160. end
  161. end
  162.  
  163. describe "DELETE destroy comment on post" do
  164. it "deletes the comment" do
  165. delete :destroy, post_id: my_post.id, id: my_comment.id
  166. count = Comment.where({id: my_comment.id}).count
  167. expect(count).to eq 0
  168. end
  169.  
  170. it "redirects the user to the posts show view" do
  171. delete :destroy, post_id: my_post.id, id: my_comment.id
  172.  
  173. expect(response).to redirect_to [my_topic, my_post]
  174. end
  175. end
  176.  
  177. describe "DELETE destroy comment on topic" do
  178. it "deletes the comment" do
  179. delete :destroy, topic_id: my_topic.id, id: my_comment.id
  180. count = Comment.where({id: my_comment.id}).count
  181. expect(count).to eq 0
  182. end
  183.  
  184. it "redirects the user to the topic show view" do
  185. delete :destroy, topic_id: my_topic.id, id: my_comment.id
  186.  
  187. expect(response).to redirect_to my_topic
  188. end
  189. end
  190. end
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement