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

Add specs for NotificationsController [#311] #319

Merged

Conversation

tcdowney
Copy link
Collaborator

@tcdowney tcdowney commented Oct 9, 2016

Issue: #311

This includes some minor refactoring of the NotificationsController.

# Use callbacks to share common setup or constraints between actions.
def set_notification
begin
@notification = Notification.find(params[:id])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This used to let users destroy notifications belonging to other users provided that they knew (or guessed) a valid Notification ID. I got rid of this before_action since it was only used in one place and was duplicating logic and used a more specific query in in #destroy.

@@ -26,33 +31,14 @@ def fetch_notifications
end

def signed_in
if !user_signed_in?
signed_in = -1
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This code wasn't reachable due to the if_not_signed_in before action. If the intent was for this endpoint to actually return -1 to un-authenticated users, I can restore this code and prevent the before action from acting on this action.

Copy link
Member

Choose a reason for hiding this comment

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

So this code is being accessed in notifications.js:197. This was created to figure out whether the user is signed in from notifications.js.

Copy link
Member

Choose a reason for hiding this comment

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

Your refactor should be fine 👍

respond_to do |format|
format.html { redirect_to :back }
format.html { redirect_to :back }
format.json { head :no_content }
end
end

def clear
Copy link
Collaborator Author

@tcdowney tcdowney Oct 9, 2016

Choose a reason for hiding this comment

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

                          clear_notifications GET      /notifications/clear(.:format)               notifications#clear
    fetch_notifications_notifications GET      /notifications/fetch_notifications(.:format) notifications#fetch_notifications
                   signed_in_notifications GET      /notifications/signed_in(.:format)           notifications#signed_in
                                    notifications GET      /notifications(.:format)                     notifications#index
                                      notification DELETE   /notifications/:id(.:format)                 notifications#destroy

I don't think the route for clear should be using HTTP GET since destroying all notifications for a user is a very stateful request. It's also susceptible to CSRF attacks since a malicious user could give someone a relatively benign looking link like http://if-me.org/notifications/clear and the act of clicking on it (provided they were logged in) would delete all of their notifications whether they liked it or not.

http://programmers.stackexchange.com/questions/188860/why-shouldnt-a-get-request-change-data-on-the-server

Mind if I create an issue for switching this over?

Edit: logged #320

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for pointing this out! Definitely fix this 💯

@julianguyen
Copy link
Member

Looks like @esBeee also made a PR related to the notifications controller (#316). Can you two sync on this? Also @esBeee, please email us at join.ifme@gmail.com to be added to our Slack.

@@ -1,20 +1,25 @@
class NotificationsController < ApplicationController
before_filter :if_not_signed_in
before_action :set_notification, only: [:destroy]
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Cool, glad someone else noticed it earlier! I'll sync up and work my changes around theirs. Thanks for the heads up.

describe '#fetch_notifications' do
let(:user) { FactoryGirl.create(:user1) }
let(:other_user) { FactoryGirl.create(:user2) }
let!(:other_user_notification) do
Copy link
Member

Choose a reason for hiding this comment

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

Is adding ! necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's necessary for this one because I wanted to make sure that we were testing that fetch_notifications only returned the current user's notifications. Since this let is never explicitly invoked anywhere in the test, this second notification won't be created without the !.

@julianguyen
Copy link
Member

What's the status on this @tcdowney?

@julianguyen
Copy link
Member

@tcdowney Can you rebase and let me know when it's ready to merge? Thanks :)

@tcdowney tcdowney force-pushed the pr/311-notifications_controller_spec branch from 94c0ccb to 3538e8e Compare November 13, 2016 07:20
- includes some minor refactoring of the notifications controller
@tcdowney tcdowney force-pushed the pr/311-notifications_controller_spec branch from 3538e8e to 7e2057a Compare November 13, 2016 07:31
@tcdowney
Copy link
Collaborator Author

@julianguyen

I rebased this branch with what's on master and all tests are passing locally. Tests are failing on CI, though, but the reason why doesn't seem related to the code that I've changed here -- have you see those failures before? 😢

@julianguyen
Copy link
Member

Not sure, I'm going to rebuild your PR on CI.

@julianguyen
Copy link
Member

julianguyen commented Nov 13, 2016

@tcdowney

Devise.secret_key was not set. Please add the following to your Devise initializer:

EDIT: Hmm I'm wondering if anything needs to be set up with CircleCI from your end...

@julianguyen
Copy link
Member

julianguyen commented Nov 13, 2016

@tcdowney Hmm maybe you need to add yourself to the if me CircleCI org?
Thoughts @brunoocasali?

@julianguyen julianguyen merged commit 89b01d3 into ifmeorg:master Nov 13, 2016
@brunoocasali
Copy link
Member

@julianguyen, @tcdowney I don't know what this happens andela-oesho for example, have made a "passed" build... Having an account or not, should not matters to build pass or not, I will rebuild to see.

@julianguyen
Copy link
Member

@brunoocasali Are things working now?

@brunoocasali
Copy link
Member

I can't understand what happened on this PR, but it already merged, we need to merge the #339 to get the new features of continuous integration (by the way there are the changes about C.I.) I suppose when he goes to be merged we have no more problems with it :D

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.

3 participants