Repository navigation
Device story comments - #743
Conversation
| validates :device_urn, uniqueness: true | ||
| validates :device_urn, :custodian_name, presence: true | ||
| has_many :device_story_comments | ||
| accepts_nested_attributes_for :device_story_comments |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ok, will work on separating them.
There was a problem hiding this comment.
I agree with @matschaffer. We should use Nested resources.
|
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. |
|
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. |
|
Finally this is big enough that I think we should add a basic spec. We have examples in https://github.com/Safecast/safecastapi/tree/master/spec/acceptance if you want to go that route. There's also https://github.com/Safecast/safecastapi/blob/master/spec/controllers/device_stories_controller_spec.rb and https://github.com/Safecast/safecastapi/blob/master/spec/safecast/tasks/device_stories/update_metadata_spec.rb which should help provide some background material for a commenting spec. |
|
Got it! Will try to implement your suggestions. |
|
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 |
There was a problem hiding this comment.
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.
|
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 |
There was a problem hiding this comment.
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!' |
There was a problem hiding this comment.
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.
| end | ||
|
|
||
| def destroy | ||
| @device_story_comment.destroy |
There was a problem hiding this comment.
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) %> |
There was a problem hiding this comment.
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.
| <%= 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 %> |
There was a problem hiding this comment.
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! |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)|
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 |
Co-authored-by: Eito Katagiri <[email protected]>
Co-authored-by: Eito Katagiri <[email protected]>
Co-authored-by: Mat Schaffer <[email protected]>
Co-authored-by: Mat Schaffer <[email protected]>
|
@eitoball , thank you for helping me out. Any idea why I get the |
|
|
||
| require 'spec_helper' | ||
|
|
||
| RSpec.describe DeviceStoryCommentsController, type: :request do |
There was a problem hiding this comment.
Any idea why I get the NoMethodError in rspec when I use sign_in user?
| 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)
|
@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. |
|
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') } |
There was a problem hiding this comment.
I have it already here.
|
@matschaffer I am out of ideas. When I use my key it works but using these environment variables |
|
I suspect it’s the library throwing from a guard expression. There’s likely some option to suppress it for testing. |
|
Wish I could help out Emre, but unfortunately I am not so familiar with the issue. |
|
@Nakushi64 You could mock |
|
@eitoball It worked! Thank you so much! |
Refer to #606