View Javadoc

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.IntegrationTestUtil;
20  import com.atlassian.jira.rest.client.TestUtil;
21  import com.atlassian.jira.rest.client.api.JiraRestClient;
22  import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
23  import com.atlassian.jira.rest.client.api.domain.Session;
24  import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
25  import com.atlassian.jira.rest.client.internal.json.TestConstants;
26  import org.joda.time.DateTime;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import static com.atlassian.jira.rest.client.internal.json.TestConstants.ADMIN_PASSWORD;
31  import static com.atlassian.jira.rest.client.internal.json.TestConstants.ADMIN_USERNAME;
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  @SuppressWarnings("ConstantConditions")
36  public class AsynchronousSessionRestClientTest extends AbstractAsynchronousRestClientTest {
37  
38      private final JiraRestClientFactory clientFactory = new AsynchronousJiraRestClientFactory();
39  
40      private static boolean alreadyRestored;
41  
42      @Before
43      public void setup() {
44          if (!alreadyRestored) {
45              IntegrationTestUtil.restoreAppropriateJiraData(TestConstants.DEFAULT_JIRA_DUMP_FILE, administration);
46              alreadyRestored = true;
47          }
48      }
49  
50      @Test
51      public void testValidSession() {
52          final Session session = client.getSessionClient().getCurrentSession().claim();
53          assertEquals(ADMIN_USERNAME, session.getUsername());
54  
55      }
56  
57      @Test
58      public void testInvalidCredentials() {
59          client = clientFactory.createWithBasicHttpAuthentication(jiraUri, ADMIN_USERNAME, ADMIN_PASSWORD + "invalid");
60          TestUtil.assertErrorCode(401, new Runnable() {
61              @Override
62              public void run() {
63                  client.getSessionClient().getCurrentSession().claim();
64              }
65          });
66      }
67  
68      @Test
69      public void testGetCurrentSession() throws Exception {
70          final Session session = client.getSessionClient().getCurrentSession().claim();
71          assertEquals(ADMIN_USERNAME, session.getUsername());
72  
73          // that is not a mistake - username and the password for this user is the same
74          client = clientFactory.createWithBasicHttpAuthentication(jiraUri, TestConstants.USER1.getName(), TestConstants.USER1
75                  .getName());
76          final Session session2 = client.getSessionClient().getCurrentSession().claim();
77          assertEquals(TestConstants.USER1.getName(), session2.getUsername());
78          final DateTime lastFailedLoginDate = session2.getLoginInfo().getLastFailedLoginDate();
79  
80          final JiraRestClient client2 = clientFactory.createWithBasicHttpAuthentication(jiraUri, TestConstants.USER1
81                  .getName(), "bad-ppassword");
82          final DateTime now = new DateTime();
83          TestUtil.assertErrorCode(401, new Runnable() {
84              @Override
85              public void run() {
86                  client2.getSessionClient().getCurrentSession().claim();
87              }
88          });
89          while (!new DateTime().isAfter(lastFailedLoginDate)) {
90              Thread.sleep(20);
91          }
92  
93          final Session sessionAfterFailedLogin = client.getSessionClient().getCurrentSession().claim();
94          assertTrue(sessionAfterFailedLogin.getLoginInfo().getLastFailedLoginDate().isAfter(lastFailedLoginDate));
95          assertTrue(sessionAfterFailedLogin.getLoginInfo().getLastFailedLoginDate().isAfter(now));
96      }
97  
98  }