{"id":335,"date":"2009-12-07T15:16:25","date_gmt":"2009-12-07T09:46:25","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=335"},"modified":"2016-12-19T15:04:55","modified_gmt":"2016-12-19T09:34:55","slug":"login-user-for-integration-test-when-using-jsecurity-plugin","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/login-user-for-integration-test-when-using-jsecurity-plugin\/","title":{"rendered":"Login user for Integration test when using Jsecurity plugin"},"content":{"rendered":"<p>Hello Friends,<\/p>\n<p>I was using Jsecurity plugin in my project. There was an action in a controller which needed logged in user information and I was finding it difficult to write an integration test for the same. Then Brent Fisher shared the following code which worked nicely for both services and controllers:<\/p>\n<div class=\"wp_syntax\">\n<div class=\"code\">\n<pre class=\"groovy\">\r\nimport org.jsecurity.SecurityUtils\r\nimport com.aps.domain.security.JsecUser\r\nimport org.jsecurity.subject.Subject\r\n\r\nclass MyControllerTests extends GrailsUnitTestCase {\t\r\n\r\n  protected void setUp() {\r\n\tsuper.setUp()\t\r\n\t\/\/following code sets admin as a logged in user\t\t\t\t\r\n\tdef subject = [isAuthenticated: true,\r\n\t\t       principal: \"admin\"\r\n\t\t      ] as Subject\r\n\r\n\tSecurityUtils.metaClass.static.getSubject = {-> return subject }\r\n\tSubject.metaClass.getPrincipal = {-> return \"admin\" }\r\n\t...\r\n  }\t\r\n ...\r\n}\t<\/pre>\n<\/div>\n<\/div>\n<p>Using metaclass, We changed the implementation of getPrincipal() and getSubject() to work in our way. So looking at this, I could see the power of a metaclass, which can be used to change or add new method implementations to an API which is not even accessible to us.<\/p>\n<p>Cheers!<\/p>\n<p>~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nIntelliGrape Softwares<br \/>\nhttp:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Friends, I was using Jsecurity plugin in my project. There was an action in a controller which needed logged in user information and I was finding it difficult to write an integration test for the same. Then Brent Fisher shared the following code which worked nicely for both services and controllers: import org.jsecurity.SecurityUtils import [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":19},"categories":[7],"tags":[4840,142,156,157],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/335"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}