You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Matt Raible <ma...@raibledesigns.com> on 2004/01/08 01:21:23 UTC
Login once using (FormAuthentication) for all tests in a class
Quick and dirty question:
Is it possible to login once (using FormAuthentication) for a class,
rather than adding a beginXXTestName befor every testXXTestName method?
Longer explanation:
I have a number of tests in my ContactActionTest - this test extends
CactusStrutsTestCase, which extends ServletTestCase - nothing is really
Struts specific in this scenario.
In order to simulate logging in, I've simply retrieved a user object in
by setUp() method and stuffed it into the session - this seems to
simulate a login good enough. However, today, I started logging down
my Actions/Servlets with roles. So now this doesn't work - do I have
to have a beginXX before each testXX that logs the user in (with
FormAuthentication)?
Thanks,
Matt
RE: Login once using (FormAuthentication) for all tests in a class
Posted by Vincent Massol <vm...@pivolis.com>.
> -----Original Message-----
> From: Matt Raible [mailto:matt@raibledesigns.com]
> Sent: 08 January 2004 01:48
> To: Cactus Users List
> Subject: Re: Login once using (FormAuthentication) for all tests in a
> class
>
> A visual might be better. In my BaseStrutsTestCase class I have a
> login method that is designed so all subclasses can use it.
>
>
> public void login(WebRequest request) {
> request.setRedirectorName("ServletRedirectorSecure");
> request.setAuthentication(new FormAuthentication(
> login.getString("username"),
> login.getString("encryptedPassword")));
> }
>
> And in my ContactActionTest, I have to login for each method - below
is
> a trimmed down version of all my begin/test methods:
>
> public void beginEdit(WebRequest wr) { login(wr); }
>
> public void testEdit() throws Exception {
> ...
> }
>
> public void beginSave(WebRequest wr) { login(wr); }
>
> public void testSave() throws Exception {
> ...
> }
>
> public void beginSearch(WebRequest wr) { login(wr); }
>
> public void testSearch() throws Exception {
> ...
> }
>
> public void beginDisplayContactsForPosition(WebRequest wr) {
> login(wr); }
>
> public void testDisplayContactsForPosition() throws Exception {
> ...
> }
>
> public void beginPostJobs(WebRequest wr) { login(wr); }
>
> public void testPostJobs() throws Exception {
> ...
> }
>
> public void beginRemove(WebRequest wr) { login(wr); }
>
> public void testRemove() throws Exception {
> ....
> }
>
> I'd like to get rid of all the beginXX methods, or a confirmation that
> this is the easiest way to do this.
Why not use the global begin(WebRequest) method (or setUp() on the
server side)?
Thanks
-Vincent
>
> Thanks,
>
> Matt
>
> On Jan 7, 2004, at 5:21 PM, Matt Raible wrote:
>
> > Quick and dirty question:
> >
> > Is it possible to login once (using FormAuthentication) for a class,
> > rather than adding a beginXXTestName befor every testXXTestName
> > method?
> >
> > Longer explanation:
> >
> > I have a number of tests in my ContactActionTest - this test extends
> > CactusStrutsTestCase, which extends ServletTestCase - nothing is
> > really Struts specific in this scenario.
> >
> > In order to simulate logging in, I've simply retrieved a user object
> > in by setUp() method and stuffed it into the session - this seems to
> > simulate a login good enough. However, today, I started logging
down
> > my Actions/Servlets with roles. So now this doesn't work - do I
have
> > to have a beginXX before each testXX that logs the user in (with
> > FormAuthentication)?
> >
> > Thanks,
> >
> > Matt
> >
> >
> >
> >
> >
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: cactus-user-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org
Re: Login once using (FormAuthentication) for all tests in a class
Posted by Matt Raible <ma...@raibledesigns.com>.
A visual might be better. In my BaseStrutsTestCase class I have a
login method that is designed so all subclasses can use it.
public void login(WebRequest request) {
request.setRedirectorName("ServletRedirectorSecure");
request.setAuthentication(new FormAuthentication(
login.getString("username"),
login.getString("encryptedPassword")));
}
And in my ContactActionTest, I have to login for each method - below is
a trimmed down version of all my begin/test methods:
public void beginEdit(WebRequest wr) { login(wr); }
public void testEdit() throws Exception {
...
}
public void beginSave(WebRequest wr) { login(wr); }
public void testSave() throws Exception {
...
}
public void beginSearch(WebRequest wr) { login(wr); }
public void testSearch() throws Exception {
...
}
public void beginDisplayContactsForPosition(WebRequest wr) {
login(wr); }
public void testDisplayContactsForPosition() throws Exception {
...
}
public void beginPostJobs(WebRequest wr) { login(wr); }
public void testPostJobs() throws Exception {
...
}
public void beginRemove(WebRequest wr) { login(wr); }
public void testRemove() throws Exception {
....
}
I'd like to get rid of all the beginXX methods, or a confirmation that
this is the easiest way to do this.
Thanks,
Matt
On Jan 7, 2004, at 5:21 PM, Matt Raible wrote:
> Quick and dirty question:
>
> Is it possible to login once (using FormAuthentication) for a class,
> rather than adding a beginXXTestName befor every testXXTestName
> method?
>
> Longer explanation:
>
> I have a number of tests in my ContactActionTest - this test extends
> CactusStrutsTestCase, which extends ServletTestCase - nothing is
> really Struts specific in this scenario.
>
> In order to simulate logging in, I've simply retrieved a user object
> in by setUp() method and stuffed it into the session - this seems to
> simulate a login good enough. However, today, I started logging down
> my Actions/Servlets with roles. So now this doesn't work - do I have
> to have a beginXX before each testXX that logs the user in (with
> FormAuthentication)?
>
> Thanks,
>
> Matt
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org
Re: Login once using (FormAuthentication) for all tests in a class
Posted by Matt Raible <ma...@raibledesigns.com>.
On Jan 8, 2004, at 7:05 AM, Vincent Massol wrote:
>> So the question is - is it possible to combine the following 2
>> methods?
>>
>> public void begin(WebRequest request) {
>> request.setRedirectorName("ServletRedirectorSecure");
>> request.setAuthentication(new FormAuthentication(
>> login.getString("username"),
>> login.getString("encryptedPassword")));
>> }
>>
>> public void setUp() throws Exception {
>> super.setUp();
>>
>> conn = ServiceLocator.getConnection();
>> // populate the userForm and place into session
>> String username = login.getString("username");
>> UserManager userMgr = new UserManagerImpl(conn);
>> userForm = (UserForm) userMgr.getUser(username);
>> session.setAttribute(Constants.USER_KEY, userForm);
>> }
>>
>
> I don't think so. The begin method is used to connect to the server
> side, whereas the setup is used to set up server side objects. They are
> executed in different JVM (client and server side).
>
> Thanks
> -Vincent
>
Cool - just wanted to clarify I'm doing it right. After using the
begin() and setUp() - I seem to like that better, then I can override
begin() as needed in subclass - for instance, to cancel form
authentication with an empty method.
Thanks,
Matt
RE: Login once using (FormAuthentication) for all tests in a class
Posted by Vincent Massol <vm...@pivolis.com>.
Matt,
> -----Original Message-----
> From: Matt Raible [mailto:matt@raibledesigns.com]
> Sent: 08 January 2004 14:21
> To: Cactus Users List
> Subject: Re: Login once using (FormAuthentication) for all tests in a
> class
>
> > Thinking more about it, I'm not so sure that the solution is a
method
> > that is called only once... Each test must set its own fixture. So
you
> > should group test using the same fixture (i.e. user being logging
in)
> > in
> > the same TestCase class. Then simply use either begin() or even
better
> > setUp() (it has direct access to the session) to log the user in
before
> > *each* test.
> >
>
> Ah ha - the begin() method is exactly what I was looking for. Of
> course, I'd prefer to use setUp() because then my test looks more like
> JUnit than Cactus (making it easier to understand for those familiar
> with JUnit). However, I'm unsure of how to get the WebRequest (in
> order to set authorization) in the setUp() method.
begin() is executed on the client side and setUp() on the server side.
Thus setup cannot have access to the WebRequest. However it has access
to the HttpServletRequest and other container objects.
>
> I tried removing all my beginXX() methods and just using one begin()
> method and this worked great - thanks!
>
> So the question is - is it possible to combine the following 2
methods?
>
> public void begin(WebRequest request) {
> request.setRedirectorName("ServletRedirectorSecure");
> request.setAuthentication(new FormAuthentication(
> login.getString("username"),
> login.getString("encryptedPassword")));
> }
>
> public void setUp() throws Exception {
> super.setUp();
>
> conn = ServiceLocator.getConnection();
> // populate the userForm and place into session
> String username = login.getString("username");
> UserManager userMgr = new UserManagerImpl(conn);
> userForm = (UserForm) userMgr.getUser(username);
> session.setAttribute(Constants.USER_KEY, userForm);
> }
>
I don't think so. The begin method is used to connect to the server
side, whereas the setup is used to set up server side objects. They are
executed in different JVM (client and server side).
Thanks
-Vincent
Re: Login once using (FormAuthentication) for all tests in a class
Posted by Matt Raible <ma...@raibledesigns.com>.
> Thinking more about it, I'm not so sure that the solution is a method
> that is called only once... Each test must set its own fixture. So you
> should group test using the same fixture (i.e. user being logging in)
> in
> the same TestCase class. Then simply use either begin() or even better
> setUp() (it has direct access to the session) to log the user in before
> *each* test.
>
Ah ha - the begin() method is exactly what I was looking for. Of
course, I'd prefer to use setUp() because then my test looks more like
JUnit than Cactus (making it easier to understand for those familiar
with JUnit). However, I'm unsure of how to get the WebRequest (in
order to set authorization) in the setUp() method.
I tried removing all my beginXX() methods and just using one begin()
method and this worked great - thanks!
So the question is - is it possible to combine the following 2 methods?
public void begin(WebRequest request) {
request.setRedirectorName("ServletRedirectorSecure");
request.setAuthentication(new FormAuthentication(
login.getString("username"),
login.getString("encryptedPassword")));
}
public void setUp() throws Exception {
super.setUp();
conn = ServiceLocator.getConnection();
// populate the userForm and place into session
String username = login.getString("username");
UserManager userMgr = new UserManagerImpl(conn);
userForm = (UserForm) userMgr.getUser(username);
session.setAttribute(Constants.USER_KEY, userForm);
}
Matt
RE: Login once using (FormAuthentication) for all tests in a class
Posted by Vincent Massol <vm...@pivolis.com>.
Hi Matt,
> -----Original Message-----
> From: Matt Raible [mailto:matt@raibledesigns.com]
> Sent: 08 January 2004 01:21
> To: Cactus Users List
> Subject: Login once using (FormAuthentication) for all tests in a
class
>
> Quick and dirty question:
>
> Is it possible to login once (using FormAuthentication) for a class,
> rather than adding a beginXXTestName befor every testXXTestName
method?
>
hmm... There is a global begin(WebRequest) method. But it is called
before every test (it's the equivalent of setUp() but on the client
side). What we should do really is create the equivalent of JUnit
TestSetup but with methods names begin/end instead of SetUp/tearDown.
Patches much welcome :-)
> Longer explanation:
>
> I have a number of tests in my ContactActionTest - this test extends
> CactusStrutsTestCase, which extends ServletTestCase - nothing is
really
> Struts specific in this scenario.
>
> In order to simulate logging in, I've simply retrieved a user object
in
> by setUp() method and stuffed it into the session - this seems to
> simulate a login good enough. However, today, I started logging down
> my Actions/Servlets with roles. So now this doesn't work - do I have
> to have a beginXX before each testXX that logs the user in (with
> FormAuthentication)?
Thinking more about it, I'm not so sure that the solution is a method
that is called only once... Each test must set its own fixture. So you
should group test using the same fixture (i.e. user being logging in) in
the same TestCase class. Then simply use either begin() or even better
setUp() (it has direct access to the session) to log the user in before
*each* test.
Cactus is about unitary tests so each test must set its own fixture and
not depend on other tests or side effects.
Thanks
-Vincent