1   /*
2    * Copyright (C) 2010 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package it;
18  
19  import com.atlassian.jira.rest.client.ExpandableProperty;
20  import com.atlassian.jira.rest.client.IntegrationTestUtil;
21  import com.atlassian.jira.rest.client.TestUtil;
22  import com.atlassian.jira.rest.client.domain.User;
23  import com.atlassian.jira.rest.client.internal.json.TestConstants;
24  import com.google.common.collect.ImmutableList;
25  import org.codehaus.jettison.json.JSONException;
26  import org.junit.Test;
27  
28  import javax.ws.rs.core.Response;
29  
30  import static com.atlassian.jira.rest.client.IntegrationTestUtil.USER_SLASH;
31  
32  public class JerseyUserRestClientTest extends AbstractRestoringJiraStateJerseyRestClientTest {
33  
34      @Test
35      public void testGetUser() throws JSONException {
36  		final User user = client.getUserClient().getUser(ADMIN_USERNAME, pm);
37  		assertEquals("wojciech.seliga@spartez.com", user.getEmailAddress());
38  		assertEquals("admin", user.getName());
39  		assertEquals("Administrator", user.getDisplayName());
40  		assertEquals(new ExpandableProperty<String>(3, ImmutableList.of("jira-administrators", "jira-developers", "jira-users")), user.getGroups());
41  		assertEquals(IntegrationTestUtil.USER_ADMIN.getSelf(), user.getSelf());
42  		assertTrue(user.getAvatarUri().toString().contains("ownerId=" + user.getName()));
43  
44  		final User user2 = client.getUserClient().getUser(TestConstants.USER1_USERNAME, pm);
45  		assertEquals(new ExpandableProperty<String>(ImmutableList.of("jira-users")), user2.getGroups());
46      }
47  
48  	public void testGetUserWithSlash() {
49  		final User user = client.getUserClient().getUser(USER_SLASH.getName(), pm);
50  		assertEquals(USER_SLASH.getSelf(), user.getSelf());
51  		assertEquals(USER_SLASH.getDisplayName(), user.getDisplayName());
52  	}
53  
54  	public void testGetNonExistingUser() {
55  		final String username = "same-fake-user-which-does-not-exist";
56  		TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "The user named '" + username + "' does not exist",
57  				new Runnable() {
58  			@Override
59  			public void run() {
60  				client.getUserClient().getUser(username, pm);
61  			}
62  		});
63  	}
64  
65  	public void testGetUserAnonymously() {
66  		TestUtil.assertErrorCode(Response.Status.UNAUTHORIZED, new Runnable() {
67  			@Override
68  			public void run() {
69  				setAnonymousMode();
70  				client.getUserClient().getUser(TestConstants.USER1_USERNAME, pm);
71  			}
72  		});
73  
74  	}
75  
76  }