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.NullProgressMonitor;
20  import com.atlassian.jira.rest.client.TestUtil;
21  import com.atlassian.jira.rest.client.annotation.Restore;
22  import com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler;
23  import com.atlassian.jira.rest.client.domain.Session;
24  import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClient;
25  import com.atlassian.jira.rest.client.internal.json.TestConstants;
26  import org.joda.time.DateTime;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.*;
30  import static com.atlassian.jira.rest.client.internal.json.TestConstants.*;
31  
32  @Restore(TestConstants.DEFAULT_JIRA_DUMP_FILE)
33  public class JerseySessionRestClientTest extends AbstractJerseyRestClientTest {
34  
35  	@Test
36  	public void testValidSession() {
37  		final Session session = client.getSessionClient().getCurrentSession(new NullProgressMonitor());
38  		assertEquals(ADMIN_USERNAME, session.getUsername());
39  
40  	}
41  
42  	@Test
43  	public void testInvalidCredentials() {
44  		client = new JerseyJiraRestClient(jiraUri, new BasicHttpAuthenticationHandler(ADMIN_USERNAME, ADMIN_PASSWORD + "invalid"));
45  		TestUtil.assertErrorCode(401, new Runnable() {
46  			@Override
47  			public void run() {
48  				client.getSessionClient().getCurrentSession(new NullProgressMonitor());
49  			}
50  		});
51  	}
52  
53  	@Test
54  	public void testGetCurrentSession() throws Exception {
55  		final Session session = client.getSessionClient().getCurrentSession(new NullProgressMonitor());
56  		assertEquals(ADMIN_USERNAME, session.getUsername());
57  
58  		// that is not a mistake - username and the password for this user is the same
59  		client = new JerseyJiraRestClient(jiraUri, new BasicHttpAuthenticationHandler(TestConstants.USER1.getName(),
60  				TestConstants.USER1.getName()));
61  		final Session session2 = client.getSessionClient().getCurrentSession(new NullProgressMonitor());
62  		assertEquals(TestConstants.USER1.getName(), session2.getUsername());
63  		final DateTime lastFailedLoginDate = session2.getLoginInfo().getLastFailedLoginDate();
64  
65  		final JerseyJiraRestClient client2 = new JerseyJiraRestClient(jiraUri, new BasicHttpAuthenticationHandler(TestConstants.USER1.getName(),
66  				"bad-password"));
67  		final DateTime now = new DateTime();
68  		TestUtil.assertErrorCode(401, new Runnable() {
69  			@Override
70  			public void run() {
71  				client2.getSessionClient().getCurrentSession(new NullProgressMonitor());
72  			}
73  		});
74  		while (!new DateTime().isAfter(lastFailedLoginDate)) {
75  			Thread.sleep(20);
76  		}
77  
78  		final Session sessionAfterFailedLogin = client.getSessionClient().getCurrentSession(new NullProgressMonitor());
79  		assertTrue(sessionAfterFailedLogin.getLoginInfo().getLastFailedLoginDate().isAfter(lastFailedLoginDate));
80  		assertTrue(sessionAfterFailedLogin.getLoginInfo().getLastFailedLoginDate().isAfter(now));
81  	}
82  
83  }