Unit Testing Filters mapped by URI

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Unit Testing Filters mapped by URI

Elmar Kretzer-2
Hi all,

i have defined a filter based on a URI pattern.
It seems that unit testing of filters based on URI is not working for me.

Filter looks like:

def filters = {
secureFilter(uri: '/sec/**') {
after = { map ->
map << [sec: 'sec']
}
}
}

Unit test looks like:

void testSecureFilter() {
def model
withFilters(uri: '/sec/**') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}

Anyone knows how to handle this?

thx in advance!

Elmar
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

xunitc
Hi, I have the same question.
http://grails.1312388.n4.nabble.com/How-can-I-pass-the-unit-test-of-filter-with-uri-quot-quot-td4281636.html

Did you find a solution?

I think maybe the docs need to include this part.
Xunitc
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

Elmar Kretzer-2
no solution yet.

i looked at the https://github.com/grails/grails-core/blob/master/grails-plugin-testing/src/main/groovy/grails/test/mixin/web/FiltersUnitTestMixin.groovy
and while debugging the test i saw the filter was actually set.
but it did not get called.
 
even if i call secureFilter with controller and action ( which is mapped to the corresponding URI) there is no result.



Am 27.01.2012 um 12:07 schrieb lsx:

> Hi, I have the same question.
> http://grails.1312388.n4.nabble.com/How-can-I-pass-the-unit-test-of-filter-with-uri-quot-quot-td4281636.html
>
> Did you find a solution?
>
> I think maybe the docs need to include this part.
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Unit-Testing-Filters-mapped-by-URI-tp4333132p4333149.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

Andre Steingress
Hi Elmar, 

you need to set the requestURI of the mocked HttpServletRequest and the filter should be called (at least this is the case with Grails 2.0):

void testSecureFilter()  {

GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/dashboards/add'


}

Cheers, Andre

On Friday, January 27, 2012 at 12:21 PM, Elmar Kretzer wrote:

no solution yet.

and while debugging the test i saw the filter was actually set.
but it did not get called.
even if i call secureFilter with controller and action ( which is mapped to the corresponding URI) there is no result.



Am 27.01.2012 um 12:07 schrieb lsx:

Hi, I have the same question.

Did you find a solution?

I think maybe the docs need to include this part.

--
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:



---------------------------------------------------------------------
To unsubscribe from this list, please visit:


Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

Elmar Kretzer-2
Hi Andre,

thx for your fast response.
I added the request URI, but its still not working:

Do you see any misconfiguration in the unit test?
Is there still something missing?



@TestFor(SecureController)
@Mock(SecureFilters)
class NavigationFiltersTest {

void testSecureFilter() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(controller: 'secure', action: 'doAnything') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
void testSecureFilter2() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(uri: '/sec/**') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
}


With SecureFilters: 

def filters = {
secureFilter(uri: '/sec/**') {
after = { map ->
map << [sec: 'sec']
}
}
}



Elmar



Am 27.01.2012 um 12:28 schrieb Andre Steingress:

Hi Elmar, 

you need to set the requestURI of the mocked HttpServletRequest and the filter should be called (at least this is the case with Grails 2.0):

void testSecureFilter()  {

GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/dashboards/add'


}

Cheers, Andre

On Friday, January 27, 2012 at 12:21 PM, Elmar Kretzer wrote:

no solution yet.

and while debugging the test i saw the filter was actually set.
but it did not get called.
even if i call secureFilter with controller and action ( which is mapped to the corresponding URI) there is no result.



Am 27.01.2012 um 12:07 schrieb lsx:

Hi, I have the same question.

Did you find a solution?

I think maybe the docs need to include this part.

--
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:



---------------------------------------------------------------------
To unsubscribe from this list, please visit:



Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

Andre Steingress
Elmar,

testSecureFilter2 doesn't work because uri is not supported in the withFilters parameter map.

testSecureFilter should work when calling it with an empty map

withFilters([:]) { ... }

that way only the requestURI is taken into account afaik (right know I do not have access  to the source...)

Cheers, Andre


On 27.01.2012, at 14:14, Elmar Kretzer <[hidden email]> wrote:

Hi Andre,

thx for your fast response.
I added the request URI, but its still not working:

Do you see any misconfiguration in the unit test?
Is there still something missing?



@TestFor(SecureController)
@Mock(SecureFilters)
class NavigationFiltersTest {

void testSecureFilter() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(controller: 'secure', action: 'doAnything') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
void testSecureFilter2() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(uri: '/sec/**') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
}


With SecureFilters: 

def filters = {
secureFilter(uri: '/sec/**') {
after = { map ->
map << [sec: 'sec']
}
}
}



Elmar



Am 27.01.2012 um 12:28 schrieb Andre Steingress:

Hi Elmar, 

you need to set the requestURI of the mocked HttpServletRequest and the filter should be called (at least this is the case with Grails 2.0):

void testSecureFilter()  {

GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/dashboards/add'


}

Cheers, Andre

On Friday, January 27, 2012 at 12:21 PM, Elmar Kretzer wrote:

no solution yet.

and while debugging the test i saw the filter was actually set.
but it did not get called.
even if i call secureFilter with controller and action ( which is mapped to the corresponding URI) there is no result.



Am 27.01.2012 um 12:07 schrieb lsx:

Hi, I have the same question.

Did you find a solution?

I think maybe the docs need to include this part.

--
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:



---------------------------------------------------------------------
To unsubscribe from this list, please visit:



Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

Elmar Kretzer-2
Hi Andre,

hm - its not working either.
The model only contains the result from the controller.
(in run-app the filter is working fine for the same controller)

I have to sleep on it.
If its not working next week - i will try it with a fresh grails app.
Then i will provide it on git - and if the error remains - i will add a feature request in jira.

Again - thx for your help Andre.

void testSecureFilter() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters([:]) {
model = controller.doAnything()
}
assert model.sec == 'sec'
}



Am 27.01.2012 um 14:39 schrieb Andre Steingress:

Elmar,

testSecureFilter2 doesn't work because uri is not supported in the withFilters parameter map.

testSecureFilter should work when calling it with an empty map

withFilters([:]) { ... }

that way only the requestURI is taken into account afaik (right know I do not have access  to the source...)

Cheers, Andre


On 27.01.2012, at 14:14, Elmar Kretzer <[hidden email]> wrote:

Hi Andre,

thx for your fast response.
I added the request URI, but its still not working:

Do you see any misconfiguration in the unit test?
Is there still something missing?



@TestFor(SecureController)
@Mock(SecureFilters)
class NavigationFiltersTest {

void testSecureFilter() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(controller: 'secure', action: 'doAnything') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
void testSecureFilter2() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(uri: '/sec/**') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
}


With SecureFilters: 

def filters = {
secureFilter(uri: '/sec/**') {
after = { map ->
map << [sec: 'sec']
}
}
}



Elmar



Am 27.01.2012 um 12:28 schrieb Andre Steingress:

Hi Elmar, 

you need to set the requestURI of the mocked HttpServletRequest and the filter should be called (at least this is the case with Grails 2.0):

void testSecureFilter()  {

GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/dashboards/add'


}

Cheers, Andre

On Friday, January 27, 2012 at 12:21 PM, Elmar Kretzer wrote:

no solution yet.

and while debugging the test i saw the filter was actually set.
but it did not get called.
even if i call secureFilter with controller and action ( which is mapped to the corresponding URI) there is no result.



Am 27.01.2012 um 12:07 schrieb lsx:

Hi, I have the same question.

Did you find a solution?

I think maybe the docs need to include this part.

--
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:



---------------------------------------------------------------------
To unsubscribe from this list, please visit:




Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

Andre Steingress
I created a test project too - what does the corresponding URL mapping look like?


On 27.01.2012, at 16:28, Elmar Kretzer <[hidden email]> wrote:

Hi Andre,

hm - its not working either.
The model only contains the result from the controller.
(in run-app the filter is working fine for the same controller)

I have to sleep on it.
If its not working next week - i will try it with a fresh grails app.
Then i will provide it on git - and if the error remains - i will add a feature request in jira.

Again - thx for your help Andre.

void testSecureFilter() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters([:]) {
model = controller.doAnything()
}
assert model.sec == 'sec'
}



Am 27.01.2012 um 14:39 schrieb Andre Steingress:

Elmar,

testSecureFilter2 doesn't work because uri is not supported in the withFilters parameter map.

testSecureFilter should work when calling it with an empty map

withFilters([:]) { ... }

that way only the requestURI is taken into account afaik (right know I do not have access  to the source...)

Cheers, Andre


On 27.01.2012, at 14:14, Elmar Kretzer <[hidden email]> wrote:

Hi Andre,

thx for your fast response.
I added the request URI, but its still not working:

Do you see any misconfiguration in the unit test?
Is there still something missing?



@TestFor(SecureController)
@Mock(SecureFilters)
class NavigationFiltersTest {

void testSecureFilter() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(controller: 'secure', action: 'doAnything') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
void testSecureFilter2() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(uri: '/sec/**') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
}


With SecureFilters: 

def filters = {
secureFilter(uri: '/sec/**') {
after = { map ->
map << [sec: 'sec']
}
}
}



Elmar



Am 27.01.2012 um 12:28 schrieb Andre Steingress:

Hi Elmar, 

you need to set the requestURI of the mocked HttpServletRequest and the filter should be called (at least this is the case with Grails 2.0):

void testSecureFilter()  {

GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/dashboards/add'


}

Cheers, Andre

On Friday, January 27, 2012 at 12:21 PM, Elmar Kretzer wrote:

no solution yet.

and while debugging the test i saw the filter was actually set.
but it did not get called.
even if i call secureFilter with controller and action ( which is mapped to the corresponding URI) there is no result.



Am 27.01.2012 um 12:07 schrieb lsx:

Hi, I have the same question.

Did you find a solution?

I think maybe the docs need to include this part.

--
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:



---------------------------------------------------------------------
To unsubscribe from this list, please visit:




Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

Elmar Kretzer-2
i put the example on github.


while run-app, the page shows both attributes (controller and filter)
while test-app the model only contains the controller attribute


thx elmar




Am 28.01.2012 um 21:50 schrieb Andre Steingress:

I created a test project too - what does the corresponding URL mapping look like?


On 27.01.2012, at 16:28, Elmar Kretzer <[hidden email]> wrote:

Hi Andre,

hm - its not working either.
The model only contains the result from the controller.
(in run-app the filter is working fine for the same controller)

I have to sleep on it.
If its not working next week - i will try it with a fresh grails app.
Then i will provide it on git - and if the error remains - i will add a feature request in jira.

Again - thx for your help Andre.

void testSecureFilter() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters([:]) {
model = controller.doAnything()
}
assert model.sec == 'sec'
}



Am 27.01.2012 um 14:39 schrieb Andre Steingress:

Elmar,

testSecureFilter2 doesn't work because uri is not supported in the withFilters parameter map.

testSecureFilter should work when calling it with an empty map

withFilters([:]) { ... }

that way only the requestURI is taken into account afaik (right know I do not have access  to the source...)

Cheers, Andre


On 27.01.2012, at 14:14, Elmar Kretzer <[hidden email]> wrote:

Hi Andre,

thx for your fast response.
I added the request URI, but its still not working:

Do you see any misconfiguration in the unit test?
Is there still something missing?



@TestFor(SecureController)
@Mock(SecureFilters)
class NavigationFiltersTest {

void testSecureFilter() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(controller: 'secure', action: 'doAnything') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
void testSecureFilter2() {
def model
GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/sec/test'
withFilters(uri: '/sec/**') {
model = controller.doAnything()
}
assert model.sec == 'sec'
}
}


With SecureFilters: 

def filters = {
secureFilter(uri: '/sec/**') {
after = { map ->
map << [sec: 'sec']
}
}
}



Elmar



Am 27.01.2012 um 12:28 schrieb Andre Steingress:

Hi Elmar, 

you need to set the requestURI of the mocked HttpServletRequest and the filter should be called (at least this is the case with Grails 2.0):

void testSecureFilter()  {

GrailsMockHttpServletRequest mockHttpServletRequest = request
mockHttpServletRequest.requestURI = '/dashboards/add'


}

Cheers, Andre

On Friday, January 27, 2012 at 12:21 PM, Elmar Kretzer wrote:

no solution yet.

and while debugging the test i saw the filter was actually set.
but it did not get called.
even if i call secureFilter with controller and action ( which is mapped to the corresponding URI) there is no result.



Am 27.01.2012 um 12:07 schrieb lsx:

Hi, I have the same question.

Did you find a solution?

I think maybe the docs need to include this part.

--
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:



---------------------------------------------------------------------
To unsubscribe from this list, please visit:





Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing Filters mapped by URI

Vineel
I am trying to get similar unit tests to work.  what is the solution?