Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

atlas-integration-test and AuthenticatedUserThreadLocal usage

ColinM
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 12, 2015

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

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
ColinM
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 19, 2015

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.

TAGS
AUG Leaders

Atlassian Community Events