Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow only YouTube/Vimeo URLs on 'video_url' attribute #1854

Merged
merged 1 commit into from
Sep 13, 2017

Conversation

aitbw
Copy link
Collaborator

@aitbw aitbw commented Sep 12, 2017

Where

Fixes #1841

What

This PR solves issue #1841, by allowing only valid YouTube/Vimeo URLs if the video_url attribute is present.

How

A custom validator called valid_video_url? was defined on the Proposal model to check whether the submitted video_url is valid. The method is only executed when the aforementioned attribute is not blank. A constant titled VALID_DOMAINS was declared to include all the possible hosts an URL could match.

Screenshots

screenshot-2017-9-12 consul
Here's how the form looks when an invalid URL is submitted

Test

Test coverage was slightly increased in order to solve this issue.

Copy link
Collaborator

@bertocq bertocq left a comment

Choose a reason for hiding this comment

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

Nice start of your contributions to consul 👍 , here my comments, we can chat on slack if you prefer 😃

@@ -15,6 +15,7 @@ class Proposal < ActiveRecord::Base
include ActsAsParanoidAliases

RETIRE_OPTIONS = %w(duplicated started unfeasible done other)
VALID_DOMAINS = %w(www.youtube.com youtube.com youtu.be www.vimeo.com vimeo.com)
Copy link
Collaborator

Choose a reason for hiding this comment

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

VALID_VIDEO_DOMAINS would be more descriptive as a constant name, what do you think?

@@ -118,6 +121,14 @@ def votable_by?(user)
user && user.level_two_or_three_verified?
end

def valid_video_url?
return if video_url.blank?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the constant holding the valid video domains and the body of this function should be shared on a common helper for the near future (other models are for sure getting video urls this next week :) ). Like at https://github.com/consul/consul/blob/master/app/helpers/embed_videos_helper.rb. And this valid_video_url? could just be using it.

@@ -155,7 +155,7 @@
description 'Proposal description'
question 'Proposal question'
external_url 'http://external_documention.es'
video_url 'http://video_link.com'
video_url 'https://youtu.be/nhuNb0XtRhQ'
Copy link
Collaborator

Choose a reason for hiding this comment

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

😸 I was expecting "Gandalf Sax Guy 10 Hours HD" or good old RickRolling ^_^ But cool 👏

@@ -67,6 +67,13 @@
end
end

describe "#video_url" do
it "should not be valid when URL is not from Youtube or Vimeo" do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Really great to have test for it 👍 💯 . What do you think about testing valid scenarios? Maybe looping through the valid video domains array checking the helper valid_video_url? method validates them all?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just for clarification: You mean replacing this test with one where the valid_video_url? method tests each domain in the valid_video_domains array?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not replacing, but increasing. I mean a test should both check valid and invalid scenarios, right? And in this particular case, the valid scenarios are only a few so checking all of them may not be too slow I think (but I may be wrong and just checking one could be enough)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think one example should do. I created a .txt file under /fixtures/files with 5 valid URLs to test all the use cases, but according to Knapsack, this is too slow.

Creating an array on the test could work, but looks ugly since we have all these Youtube/Vimeo links.

What do you think?

Fixes consuldemocracy#1841

On branch aperez-valid-video-url
  Changes to be committed:
    modified:   app/models/proposal.rb
    modified:   spec/factories.rb
    modified:   spec/models/proposal_spec.rb

Allow only YouTube/Vimeo URLs on 'video_url' attribute

Fixes consuldemocracy#1841

On branch aperez-valid-video-url
  Changes to be committed:
    modified:   app/helpers/embed_videos_helper.rb
    modified:   app/models/proposal.rb
    modified:   spec/features/management/proposals_spec.rb
    modified:   spec/features/proposals_spec.rb
    modified:   spec/features/tags/proposals_spec.rb
    modified:   spec/models/proposal_spec.rb
Copy link
Collaborator

@bertocq bertocq left a comment

Choose a reason for hiding this comment

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

Nice an clean 🙂 👍

Copy link
Contributor

@MariaCheca MariaCheca left a comment

Choose a reason for hiding this comment

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

👍

@bertocq bertocq merged commit e38f290 into consuldemocracy:master Sep 13, 2017
@aitbw aitbw deleted the aperez-valid-video-url branch January 15, 2018 18:35
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.

None yet

3 participants