Skip to content

Device story comments - #743

Merged
robouden merged 25 commits into
Safecast:masterfrom
Nakushi64:device-story-comments
Jan 8, 2021
Merged

robouden merged 25 commits into
Safecast:masterfrom
Nakushi64:device-story-comments

Conversation

@Nakushi64

Copy link
Copy Markdown
Contributor

Refer to #606

  • Can leave comments on device story pages if signed in
  • Can delete own comments
  • Moderators can delete everyone's comments

Comment thread app/models/device_story.rb Outdated
validates :device_urn, uniqueness: true
validates :device_urn, :custodian_name, presence: true
has_many :device_story_comments
accepts_nested_attributes_for :device_story_comments

@matschaffer matschaffer Dec 4, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like the right move to me. I'd probably opt to have the device story comment be its own controller (a nested route could be good so you always have the device story id) and write directly. Rather than what you have here which traverses DeviceStoriesController and DeviceStory to write a comment.

Could be that's my old-school rails background talking though. Maybe @sasharevzin or @eitoball would be able to cross check me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will work on separating them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @matschaffer. We should use Nested resources.

@matschaffer

Copy link
Copy Markdown
Contributor

We should also plan to get https://akismet.com/ in here, otherwise we'll likely start getting spam submissions at some point.

The authenticated guard helps a bit, but some bots do work out registration in order to sneak in spam links.

@matschaffer

matschaffer commented Dec 4, 2020 •

Copy link
Copy Markdown
Contributor

We actually had a case not too long ago where a spam bot used the registration process itself since we display the name on pages like https://api.safecast.org/en-US/users/1154 - leading to #631 as a workaround.

@matschaffer

matschaffer commented Dec 4, 2020 •

Copy link
Copy Markdown
Contributor

@Nakushi64

Copy link
Copy Markdown
Contributor Author

Got it! Will try to implement your suggestions.

@matschaffer

matschaffer commented Dec 4, 2020 •

Copy link
Copy Markdown
Contributor

ah, here's the gem I found last time I was googling #606 (comment)

Looks like it should support a basic string check against the free akismet API. Register your own email for starters, if it works I can handle registering a key for the the deployed sites.

create_table :device_story_comments do |t|
t.references :device_story, foreign_key: true
t.references :user, foreign_key: true
t.text :text

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to have a validation to check size fo text in DeviceStoryComment because PostgresSQL accepts up to 1 GB (https://wiki.postgresql.org/wiki/FAQ#What_is_the_maximum_size_for_a_row.2C_a_table.2C_and_a_database.3F). I don't think that we would want comments in such size.

@Nakushi64

Copy link
Copy Markdown
Contributor Author

The comments now have their own controller and I used this to implement Akismet. Also I limited the size of each comment in the model to 1000 characters. Let me know if there is still anything I should do differently.

end

def device_story_comment_params
params.inspect

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guessing this line isn't supposed to be here

if @device_story_comment.save && !@device_story_comment.spam?
format.html { redirect_to device_story_path(@device_story), notice: 'Comment successfully submitted!' }
else
flash[:error] = 'Could not submit your comment. Please, try again!'

@matschaffer matschaffer Dec 22, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would trying again definitely help? Might be good to capture the error that surfaced and show something to the user based on that. Of course if it's spam being obscure makes some sense.

Comment thread config/initializers/rakismet.rb Resolved
Comment thread config/initializers/rakismet.rb Resolved
end

def destroy
@device_story_comment.destroy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure at the moment this allows any user to delete (or update) any comment, we should set it so only moderator or comment author can do that.

<b><%= link_to comment.user.name , comment.user %></b> <div style="display: inline-block; color: dimgrey; font-size: small">(<%= time_ago_in_words(comment.created_at) %> ago)</div> <br>
<%= comment.content %>
<%- if user_signed_in? -%>
<%- if (comment.user_id == current_user.id) || moderator?(current_user) %>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you got the right idea here, but we should protect the route as well. People could build their own requests to delete the user if the API allows it.

Comment thread app/views/device_stories/show.html.erb Outdated
<%= form_with model: DeviceStoryComment, url: device_story_device_story_comments_path(@device_story), local: true, method: 'post' do |form| %>
<%= form.label :content, current_user.name %>
<%= form.text_area :content, :rows => 3, style: 'width:97%;' %>
<%= form.hidden_field :user_id, :value => current_user.id %>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have this here since anyone could override it. Move it to DeviceStoryCommentsController#create

let!(:user) { Fabricate(:user) }
let!(:device_story_comment) { Fabricate(:device_story_comment) }
describe 'POST #create' do
WebMock.allow_net_connect!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd take this out and use the calls in https://github.com/joshfrench/rakismet#testing to ensure rakismet doesn't actually try to check spam during testing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use WebMock to check if it sends request to Akismet.

For example:

let(:addressable) { Addressable::Template.new('https://{key}.rest.akismet.com/1.1/comment-check') }
...
before { stub_request(:post, addressable) }
...
  expect(WebMock).to have_requested(:post, addressable)

Comment thread app/models/device_story_comment.rb Resolved
@eitoball

Copy link
Copy Markdown
Member

In spec, there are some lines that specify IDs or such like here. It might be better not to specify IDs.

The diff below is a sample that tries not to specify IDs with modifications that I commented. I thought that showing code might be easier to understand.

diff --git a/spec/fabricators/device_story_comment_fabricator.rb b/spec/fabricators/device_story_comment_fabricator.rb
index db89db59..25588d86 100644
--- a/spec/fabricators/device_story_comment_fabricator.rb
+++ b/spec/fabricators/device_story_comment_fabricator.rb
@@ -2,6 +2,4 @@
 
 Fabricator(:device_story_comment) do
   content 'This is a test!'
-  user_id 1
-  device_story_id 1
 end
diff --git a/spec/requests/device_story_comments_controller_spec.rb b/spec/requests/device_story_comments_controller_spec.rb
index 2db5dd01..f6f27626 100644
--- a/spec/requests/device_story_comments_controller_spec.rb
+++ b/spec/requests/device_story_comments_controller_spec.rb
@@ -3,47 +3,35 @@
 require 'spec_helper'
 
 RSpec.describe DeviceStoryCommentsController, type: :request do
-  let!(:device_story) { Fabricate(:device_story, last_location_name: 'Israel', device_urn: 'safecast:114699387') }
-  let!(:user) { Fabricate(:user) }
-  let!(:device_story_comment) { Fabricate(:device_story_comment) }
+  let(:user) { Fabricate(:user) }
+  let(:device_story) { Fabricate(:device_story, last_location_name: 'Israel', device_urn: 'safecast:114699387') }
+
   describe 'POST #create' do
-    WebMock.allow_net_connect!
+    let(:addressable) { Addressable::Template.new('https://{key}.rest.akismet.com/1.1/comment-check') }
+
+    before { stub_request(:post, addressable).to_return(body: 'false') }
+
     it 'creates valid comment' do
-      comment_params = {
-        content: 'This is a test!',
-        device_story_id: DeviceStory.first.id,
-        user_id: User.first.id
-      }
-      expect { post '/device_stories/1/device_story_comments', params: { device_story_comment: comment_params } }.to change { DeviceStoryComment.count }.by(1)
+      comment_params = { content: 'This is a test!', user_id: user.id }
+      expect { post device_story_device_story_comments_path(device_story_id: device_story.id), params: { device_story_comment: comment_params } }.to change { DeviceStoryComment.count }.by(1)
       expect(response.status).to eq 302
+      expect(WebMock).to have_requested(:post, addressable)
     end
 
     it 'creates invalid comment' do
-      comment_params = {
-        content: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
-        nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
-        At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
-        takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
-        sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
-        erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
-        kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet,
-        consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
-        aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
-        clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor
-        sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore
-        magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
-        Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
-        device_story_id: DeviceStory.first.id,
-        user_id: User.first.id
-      }
-      expect { post '/device_stories/1/device_story_comments', params: { device_story_comment: comment_params } }.to change { DeviceStoryComment.count }.by(0)
+      comment_params = { content: 'a' * 1_001, user_id: user.id }
+      expect { post device_story_device_story_comments_path(device_story_id: device_story.id), params: { device_story_comment: comment_params } }.not_to change { DeviceStoryComment.count }
       expect(response.status).to eq 302
+      expect(WebMock).not_to have_requested(:post, addressable)
     end
   end
 
   describe 'DELETE #destroy' do
+    let(:device_story_comment) { Fabricate(:device_story_comment, device_story: device_story, user: user) }
+
     it 'deletes comment' do
-      expect { delete '/device_stories/1/device_story_comments/1' }.to change { DeviceStoryComment.count }.by(-1)
+      delete device_story_device_story_comment_path(device_story_id: device_story.id, id: device_story_comment.id)
+      expect { device_story_comment.reload }.to raise_error(ActiveRecord::RecordNotFound)
       expect(response.status).to eq 302
     end
   end

@Nakushi64

Copy link
Copy Markdown
Contributor Author

@eitoball , thank you for helping me out. Any idea why I get the NoMethodError in rspec when I use sign_in user? I looked at the other spec files and they seem to utilise it without any issues.


require 'spec_helper'

RSpec.describe DeviceStoryCommentsController, type: :request do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea why I get the NoMethodError in rspec when I use sign_in user?

Suggested change
RSpec.describe DeviceStoryCommentsController, type: :request do
RSpec.describe DeviceStoryCommentsController, type: :controller do

@Nakushi64 If changed like above, it should solve error.

Devise::Test::ControllerHelpers that defines sigin_user method will be included. It is defined in spec/spec_helper.rb (config.include Devise::Test::ControllerHelpers, type: :controller)

@Nakushi64

Copy link
Copy Markdown
Contributor Author

@matschaffer @eitoball Alright, I just finished applying all the suggestions and all tests are successful when I use my Akismet key. If there is still anything to fix let me know. Otherwise I can think we can test it on the dev server.

@matschaffer

Copy link
Copy Markdown
Contributor

We should be able to get the tests green with something like @eitoball suggested in #743 (comment)

describe 'POST #create' do
let(:addressable) { Addressable::Template.new('https://{key}.rest.akismet.com/1.1/comment-check') }

before { stub_request(:post, addressable).to_return(body: 'false') }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have it already here.

@Nakushi64

Copy link
Copy Markdown
Contributor Author

@matschaffer I am out of ideas. When I use my key it works but using these environment variables
Safecast::Application.config.rakismet.key = ENV['AKISMET_API_KEY']
Safecast::Application.config.rakismet.url = "https://#{ENV['DEFAULT_HOSTNAME']}/"
it gives the Rakismet.key is not defined error...

@matschaffer

Copy link
Copy Markdown
Contributor

I suspect it’s the library throwing from a guard expression. There’s likely some option to suppress it for testing.

@robouden

robouden commented Jan 7, 2021

Copy link
Copy Markdown
Contributor

Wish I could help out Emre, but unfortunately I am not so familiar with the issue.

@eitoball

eitoball commented Jan 7, 2021

Copy link
Copy Markdown
Member

@Nakushi64 You could mock Rakismet.key call like allow(Rakismet).to receive(:key).and_return('12345678'). This should be placed in before clause, I think.

@Nakushi64

Copy link
Copy Markdown
Contributor Author

@eitoball It worked! Thank you so much!

@robouden
robouden merged commit 993ece4 into Safecast:master Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants