Improved permission query extensions
And added tests for them