Hello community,
I am trying to write unittest for a plugin I wrote. I have servlet-filters or rest apis that checks the logged-in user details to view if it belong to some groups, if it is administrator, etc...
To get the logged in user detail I use:
ConfluenceUser aUser = AuthenticatedUserThreadLocal.get();
This works well when I am actually browsing pages.
But this does not work when running atlas-integration-test, where my actions are always executed as "anonymous user".
I have read the two related topics but it does not help:
I am trying now to use userAccessor in my test case and retrieve the system user, to then explicitly force the user value in the AuthenticatedUserThreadLocal.
But I wonder if there is another way to do this: does someone know when Atlassian is calling AuthenticatedUserThreadLocal.set( ) itself ? Maybe I should explicitly follow a sequence such as loginPage - myTestAction - logoutPage ?
Any more ideas will be welcome
Community moderators have prevented the ability to post new answers.
Finally, instead of mocking or forcing the AuthenticatedUserThreadLocal user variable, I am using for integration test (not unittest) HttpClient and performing queries with basic http authentication. As example:
import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpHost; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; // ... // extract : CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpHost target = new HttpHost("localhost", 1990, "http"); HttpClientContext localContext = HttpClientContext.create(); HttpGet httpGet = new HttpGet(getUrl); if(username != null) { // add Authorization param String authStr = username + ":" + password; byte[] authEncBytes = Base64.encodeBase64(authStr.getBytes()); String authStringEnc = new String(authEncBytes); httpGet.setHeader("Authorization", "Basic " + authStringEnc); } // execute and get the response CloseableHttpResponse response = httpclient.execute(target, httpGet, localContext);
Authentication had to be with the header of the HttpGet and not in the HttpClient configuration.
This is working fine with Confluence 5.8.2 and org.apache.httpcomponents:httpclient 4.5
Even if mentioner in REST Authentication, the URL parameter 'os_authType=basic
' does not seem to be mandatoy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.