-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Added test to block basic user edits, closes issue #397 #1114
Conversation
It failed the tests! |
All right -- that's good, but check why it failed (click "Details" on the test check box) -- was it that it didn't show the right message?
We should put a message there as outlined in #397 -- flash.now[:warning] = "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it." Then, try to implement the feature itself -- as described in #397 --
if @node.has_power_tag('locked') && (current_user.role != "admin" && current_user.role != "moderator")
flash.now[:warning] = "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it."
redirect_to @node.path
end Then the test should pass (if it's written properly! I might've made errors...) because it'll match the right message. You could also enclose it in a block to make sure that the total # of assert_difference 'DrupalNodeRevision.count', 0 do
# put your POST call inside here
end That's a nice type of test -- it'll ensure that within the block, the number of revisions cannot change, or the test will fail. |
Still failing tests! Different error now. Need your help with this @jywarren |
node(:organizers).add_tag('locked', rusers(:admin)) # lock the page with a tag | ||
# then try updating it | ||
assert_difference 'DrupalNodeRevision.count', 0 do | ||
post :update, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, note that your changes were to the :edit
action, but your test is for the :update
action. Both need to be changed and tested, but I think you're changing one but testing the other in this step.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! You are right! And the entire post block comes under assert_difference right?
assert_difference 'DrupalNodeRevision.count', 0 do | ||
post :edit, | ||
id: node(:organizers).id, | ||
uid: rusers(:bob).id, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two lines shouldn't be needed in the edit action.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I meant the UID and the next one. The id is still required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I removed uid and title. Error with the message now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I meant the UID and the next one. The id is still required.
Also I'm not sure if I left a comment but the id needs to be the slug in this context, so you can use the new node.slug_from_path method, or just the string "organizers"!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh OK -- I'll try to take a look later today!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you mentioned abt the ID. And Thanks in advance!
@@ -108,21 +108,21 @@ def teardown | |||
|
|||
test "viewing edit wiki page" do | |||
|
|||
get :edit, | |||
get :edit, | |||
id: 'organizers' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also look how the id for the edit action should be the page slug, not the numeric id! That should solve your last issue.
@ryzokuken Can you tell why I am getting this error and how to fix this? |
I'll check the Travis log. |
You failed the test you created yourself, as there were no elements in the page as |
@ryzokuken Okay so I will remove the '.alert' test. But how do I test this? |
But wait, don't we want the test to identify the alert you've written? Why
isn't it detecting the notice?
…On Dec 19, 2016 4:16 PM, "Swapnil Gupta" ***@***.***> wrote:
Okay so I will remove the '.alert' test. But how do I test this?
flash.now[:warning] = "This page is locked
<http:///wiki/power-tags#Locking>, and only moderators
<http:///wiki/moderators> can edit it."
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1114 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABfJ8ZSQR-OxluX_r_jdG982bSfQmSFks5rJvQqgaJpZM4LPeow>
.
|
I dont know why its showing an error! How do I detect a 'flash.now[:warning]'? |
@@ -72,6 +72,10 @@ def edit | |||
else | |||
@node = DrupalNode.find_wiki(params[:id]) | |||
end | |||
if @node.has_power_tag('locked') && (current_user.role != "admin" && current_user.role != "moderator") | |||
flash.now[:warning] = "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, i think the problem is here -- a flash.now
runs before the redirect, but a flash[]
runs after. If you remove the "now" it should work and pass the test!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
id: 'organizers' | ||
end | ||
assert_template "wiki/edit" | ||
assert_select ".alert", "flash.now[:warning] = 'This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it.'" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, remove flash.now[:warning] =
from this -- the select will test the contents of the element, so it should match the message itself. Good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done it!
The error is that there is no ".alert" message. We need to use a different method for detecting flash[:warning] any ideas? Can we use assert_equal? |
Hmm, odd. You can also test the flash like this: https://github.com/publiclab/plots2/pull/1114/files#diff-afcc4c4b1665a46e6c88e913be20b76dR132 But please add it above the existing flash test so we test both ways. Thanks! |
Okay so the statement would be like: assert_equal flash[:warning] , "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it." right? |
Yes, that's right! Sorry for the slow response, we're still in a holiday here but I'll be more responsive now. Thanks! |
Well, it's still failing. I think we have to figure out if all these conditions are actually passing in the test:
Is there any way they wouldn't be? Could we echo out each one, like:
in the controller code and see what we get? |
@@ -72,6 +72,10 @@ def edit | |||
else | |||
@node = DrupalNode.find_wiki(params[:id]) | |||
end | |||
if @node.has_power_tag('locked') && (current_user.role != "admin" && current_user.role != "moderator") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! Here's the problem! has_power_tag
looks for a tag with the given key, and any value, in the format key:value
-- but the locked
tag is not a key:value
format tag. Here we should instead just use @node.has_tag('locked')
. That should fix it!
@500swapnil -- the change i just proposed should work. But after this PR, you really should begin using named feature branches -- see this guide for how to do this. That way your PRs won't come from your master branch, but from a branch named, for example, |
I made the required change and yes, I will work using branches after this PR! |
Hmm, OK, can we assert template after a redirect? Maybe not -- I guess best remove that line. |
See, because in functional tests we can only access the most recent request. Tests with a sequence of multiple requests really belong in integration tests, which have their own abilities and features. Thanks!! |
And perhaps similarly the asserr_select can't be done without loading the redirect. |
Hmm, it's only passing now because you removed all the assertions -- but we need to preserve some -- was the Let's see if 961812c passes, and if not, why! |
OK -- got the test in 961812c to run -- https://travis-ci.org/publiclab/plots2/builds/187231729
I believe it's catching on this line: https://github.com/publiclab/plots2/pull/1114/files#diff-afcc4c4b1665a46e6c88e913be20b76dR154 This makes sense -- and I think it means you can go back to the state you were in at 961812c -- because I believe this is showing that it's not rendering a template at that point -- it's redirecting since the basic user shouldn't be able to post an update -- the "organizers" page is locked. Does that help? Thanks for your persistence on this -- the restarting-tests thing is annoying, but you're getting closer to completion here. |
id: 'organizers' | ||
end | ||
assert_equal flash[:warning] , "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it." | ||
assert_redirected_to node.path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah - should be the last thing here -- this should be node(:organizers).path
since we haven't made a variable called node
in this test. Fingers crossed that this is the last thing!
I made the required changes and yes it works now! Thank you. Can you explain in brief the syntax of this line? I am trying to understand the working of tests. |
Great! Tests stalled but I restarted them; we'll see. Yes,
So that whole line is adding a tag to the test fixture data, but in such a way that it doesn't require a change to the fixture files themselves - it's just happening within the test itself. We might add a comment there to explain that line if you like, but it's not required. OK -- waiting for those tests! |
Passed! Perfect. Merging! We actually also need similar tests and limits for the |
Yeah I will try fixing that too! Thanks! |
Make sure these boxes are checked before your pull request is ready to be reviewed and merged. Thanks!
rake test:all
schema.rb.example
has been updated if any database migrations were addedPlease be sure you've reviewed our contribution guidelines at https://publiclab.org/wiki/contributing-to-public-lab-software
We have a loose schedule of reviewing and pulling in changes every Tuesday and Friday, and publishing changes on Fridays. Please alert developers on plots-dev@googlegroups.com when your request is ready or if you need assistance.
Thanks!