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

Fixes #6065 - Update TopbarSweeper to clear cache for other users than User.current #1495

Closed
wants to merge 1 commit into from

Conversation

GregSutcliffe
Copy link
Member

I'm unclear how to test this, does anyone have any ideas? I don't see any existing tests for TopbarSweeper...

@ares
Copy link
Member

ares commented Jun 5, 2014

@GregSutcliffe you'd need to stub TopbarSweeper#expire_fragment and use expect to check that right record is passed. It would be nice to have either separate sweeper for every affected class or at least separate sweep method since this sweeper is now coupled to all those classes interfaces. Hence to test this sweeper you have to setup a lot of unrelated objects. If you want to keep it this way, factory girl could help you to setup all objects.

@dLobatog
Copy link
Member

Ticking/unticking the admin flag works well, however I realized removing an user from an user group does not sweep the top bar as expected.

@dLobatog
Copy link
Member

dLobatog commented Jul 5, 2014

@GregSutcliffe ping?

@GregSutcliffe
Copy link
Member Author

@elobato I havent forgotten, been busy :)

@domcleal
Copy link
Contributor

I agree with @ares about breaking this down, it'd be nice to keep the implementation a bit closer to (or inside) the classes it interacts with, especially if we can move the common code to a reusable base. Plugins might want to extend the functionality and this will make it possible. (We have a similar issue with our audit helper.)

If you can look at sorting this PR next week, I'd much appreciate it as I'd like it in 1.5.2 for the following week.

@dLobatog
Copy link
Member

👍 to the functionality, fixes what the gif shows and works well. @domcleal is this close enough to the classes it interacts with? It looks good to me, just misses some tests, I expect that will be tricky to write unfortunately.

@GregSutcliffe
Copy link
Member Author

@elobato @domcleal I was looking at tests on Friday. Sadly we set caching to false in environments/test.rb so you can't actually test if the cache is set/cleared.

I could try to do something with expects and check that the method is called at least once in the appropriate place - do you think it's worth it?

@dLobatog
Copy link
Member

I think that's good enough testing

@@ -373,6 +373,10 @@ def self.random_password(size = 16)
size.times.collect {|i| set[rand(set.size)] }.join
end

def expire_topbar_cache
ActionController::Base.new.expire_fragment(TopbarSweeper.fragment_name(id))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should topbar sweeper simply be passed through these expire_topbar_cache methods so you can call expire_fragment on it, rather than instantiating a new controller here?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can do that if you think it's better - I don't have a preference, but as I understand it, I think it all ends up calling the same method anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I think it's cleaner as we already have the sweeper object.

@domcleal
Copy link
Contributor

I think expecting the right calls to expire_fragment when you modify these objects is the right way to test it, there's no need to test the caching itself.

Generally with the placement of expire_topbar_cache in the respective classes, it looks a lot better.

@GregSutcliffe
Copy link
Member Author

@domcleal updated - passing "sweeper" around seems to work ok. I've not tested the Org/Loc sweeper explicitly, but it should be ok. Tests will prove it, hopefully shortly :)

@GregSutcliffe
Copy link
Member Author

Ok, tests done, although I couldn;t think of a way to test the UserRole observer since it has no controller of it's own. Given the observers all pass through the same way, it should be fine.

@domcleal
Copy link
Contributor

If you updated a user's roles through the UsersController, it would modify UserRole objects, right?

@GregSutcliffe
Copy link
Member Author

@domcleal good point - my confusion was forgetting I could just expect on UserRole instead of User for that test. Updated.

@dLobatog
Copy link
Member

[test]

@@ -195,4 +195,14 @@ class LocationsControllerTest < ActionController::TestCase
assert_equal location.id, assigns(:taxonomy).parent_id
end

test "changes should expire topbar cache" do
User.current = users :admin
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use as_admin around the minimum block required, ditto in the other tests

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated.

@domcleal
Copy link
Contributor

Reviews fine for me, thanks for the update.

test "changes should expire topbar cache" do
user1 = FactoryGirl.create(:user, :with_mail)
user2 = FactoryGirl.create(:user, :with_mail)
usergroup = FactoryGirl.create(:usergroup, :users = [user1, user2])
Copy link
Member

Choose a reason for hiding this comment

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

Here :users = is causing the test suite to fail, it should be `:users =>``

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, thanks @elobato. Updated.

@dLobatog
Copy link
Member

Successfully tested this, merged as 70acceb for 1.5.2, thanks @GregSutcliffe !

@dLobatog dLobatog closed this Jul 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants