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