Fixed permission check when granting permission.

And fixed error messages of v2 permission endpoints, and added tests.