You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Richard Adams <ri...@researchspace.com> on 2014/01/29 20:12:40 UTC
Acquiring Shiro session for multiple users simultaneously in JUnit tests
Hello,
I'm writing acceptance Junit tests for a Spring-MVC web
application that uses Shiro for its security.
Within these tests, I can login/logout as different users, no
problem, so long as there is only ever 1 logged in user - this is
pretty straightforward as the tests run synchronously.
What I'm trying to test now is simulating the case where 2 users are
simultaneously logged in , and are trying to access a resource. We
have some complicated business rules
involving permissions on who has is authorized to access a
contested resource, that I'd like to test.
So the psuedo-code for my test is:
loginAsUser1;
accessResource(user1);
loginAsUser2;
assertUser2CanOrCannotAccessResource(user2);
Since this all exceutes in a single thread, I'm not at all sure this
faithfully represents what would be going on in a multithreaded web
application.
For example, when user2 logs in, does Shiro logout user1? Can there be
2 principals(users) associated with a single Shiro session in the same
thread?
Alternatively, should I do the user2 operations in a Runnable in a
new thread? E.g.,
loginAsUser1;
accessResource(user1);
runinNewThread( user2 login, checkResourceAccess);
joinThreads
I've looked a bit into classes like SubjectAwareEXecutorService but
I don't think this is right for this situation - this class runs a
currently authenticated users's task in a new thread.
Any pointers would be greatly appreciated.
Thanks,
Richard Adams
Re: Acquiring Shiro session for multiple users simultaneously in
JUnit tests
Posted by otter606 <ri...@researchspace.com>.
I have, after a few abandoned attempts, eventually solved this using Shiro's
TestUtilities class and a mechanism based on Countdown Latches to sequence
actions run in multiple threads.
A full explanation is at my blog
http://richardadams606blog.blogspot.co.uk/2014/11/running-multithreaded-junit-tests-with.html
and code on Github at https://github.com/otter606/shiro-multithread-junit
Hope this is useful for someone!
--
View this message in context: http://shiro-user.582556.n2.nabble.com/Acquiring-Shiro-session-for-multiple-users-simultaneously-in-JUnit-tests-tp7579604p7580313.html
Sent from the Shiro User mailing list archive at Nabble.com.