Missed Team ’24? Catch up on announcements here.

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

ConfluenceUser unittesting

Team Fourum May 21, 2014

Hello,

I'm trying to unittest my code that requires a authenticated user in AuthenticatedUserThreadLocal.

I tried the following:

ConfluenceUser user = new ConfluenceUserImpl("test", "test tttt", "no@mail.com");
AuthenticatedUserThreadLocal.set(user);

but the resulting ConfluenceUser doesn't have a userkey which we use to check for authentication. Since we can't set the userkey how do we go ahead and get a user with a userkey in AuthenticatedUserThreadLocal.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 21, 2014

Because ConfluenceUser is an interface, you can create your own implementation of it within your test code, either by writing a stub class or by using a mocking library like Mockito.

Here's a class I wrote recently to create mock users:

package testsupport;

import java.util.UUID;

import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.sal.api.user.UserKey;

import org.apache.commons.lang.RandomStringUtils;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Generates mock instances of {@link ConfluenceUser}.
 */
public class MockConfluenceUserFactory
{
    /**
     * Generates a mock user with a random user key and user name. There is no guarantee that the generated user won't
     * clash with a user who's already been created, although the chances of this are quite slim.
     */
    public static ConfluenceUser mockUser()
    {
        return mockUser(getRandomUserKey(), RandomStringUtils.randomAlphabetic(10), RandomStringUtils.randomAlphabetic(10), RandomStringUtils.randomAlphabetic(10) + "@example.com");
    }

    public static ConfluenceUser mockUser(final String userKey, final String username, final String displayName, final String email)
    {
        ConfluenceUser user = mock(ConfluenceUser.class);
        when(user.getKey()).thenReturn(new UserKey(userKey));
        when(user.getEmail()).thenReturn(email);
        when(user.getName()).thenReturn(username);
        when(user.getFullName()).thenReturn(displayName);
        return user;
    }

    private static String getRandomUserKey()
    {
        return UUID.randomUUID().toString().replace("-", "");
    }
}

And then you can use it in your tests like this:

package com.atlassian.confluence.plugins.example;

import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.ConfluenceUser;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import testsupport.MockConfluenceUserFactory;

/**
 * An example JUnit Test
 */
public class TestTest
{
    private ConfluenceUser mockUser;

    @Before
    public void before()
    {
        // Create the user
        mockUser = MockConfluenceUserFactory.mockUser();
    }

    @After
    public void after()
    {
        // Always clean up the thread local after the test so it doesn't affect other tests
        AuthenticatedUserThreadLocal.reset();
    }

    @Test
    public void myTest()
    {
        AuthenticatedUserThreadLocal.set(mockUser);

        // TODO: Write your test here.
    }
}

Make sure you have a dependency on Mockito and JUnit in your pom.xml's dependency section:

<dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.8.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

2 votes
Gorka Puente _Appfire_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
May 21, 2014

Hi,

How do you want to get the user? what are you trying to test? I suppose it is either an integration or wired test.
I mean, If you are testing an authenticated user while doing a request you can use

adminUser = userManager.getUser("admin");
ConfluenceUser confluenceAdminUser = new ConfluenceUserImpl(adminUser);
		AuthenticatedUserThreadLocal.set((ConfluenceUser) confluenceAdminUser);

		Request request = requestFactory.createRequest(Request.MethodType.POST, restUrl);
		request.addTrustedTokenAuthentication(); // this takes username from ThreadLocal
		request.execute();

Could you provide more info?

Gorka

TAGS
AUG Leaders

Atlassian Community Events