PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-functional-tests/jira-func-tests/src/main/java/com/atlassian/jira/webtests/ztests/bundledplugins2/rest/TestLogin.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 274 lines | 199 code | 53 blank | 22 comment | 5 complexity | 1c77f59dcd6f56e6433c2029f8a90a57 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.webtests.ztests.bundledplugins2.rest;
  2. import com.atlassian.jira.functest.framework.BaseJiraFuncTest;
  3. import com.atlassian.jira.functest.framework.FuncTestRestClient;
  4. import com.atlassian.jira.functest.framework.FuncTestUrlHelper;
  5. import com.atlassian.jira.functest.framework.LoginAs;
  6. import com.atlassian.jira.functest.framework.RestoreBlankInstance;
  7. import com.atlassian.jira.functest.framework.suite.Category;
  8. import com.atlassian.jira.functest.framework.suite.WebTest;
  9. import com.atlassian.jira.util.collect.MapBuilder;
  10. import com.atlassian.jira.util.json.JSONException;
  11. import com.atlassian.jira.util.json.JSONObject;
  12. import com.google.inject.Inject;
  13. import com.meterware.httpunit.WebClient;
  14. import com.meterware.httpunit.WebResponse;
  15. import org.junit.Before;
  16. import org.junit.Test;
  17. import org.xml.sax.SAXException;
  18. import java.io.IOException;
  19. import java.net.URLEncoder;
  20. import static com.atlassian.jira.functest.framework.FunctTestConstants.ADMIN_USERNAME;
  21. import static com.atlassian.jira.functest.framework.FunctTestConstants.FRED_PASSWORD;
  22. import static com.atlassian.jira.functest.framework.FunctTestConstants.FRED_USERNAME;
  23. import static com.atlassian.jira.functest.matcher.HeaderValue.header;
  24. import static org.hamcrest.core.IsEqual.equalTo;
  25. import static org.junit.Assert.assertEquals;
  26. import static org.junit.Assert.assertThat;
  27. import static org.junit.Assert.assertTrue;
  28. import static org.junit.Assert.fail;
  29. /**
  30. * Testing for the /auth/session login/logout resource
  31. *
  32. * @since v4.2
  33. */
  34. @WebTest({Category.FUNC_TEST, Category.REST, Category.SECURITY})
  35. @LoginAs(user = ADMIN_USERNAME)
  36. @RestoreBlankInstance
  37. public class TestLogin extends BaseJiraFuncTest {
  38. public static final int CAPTCHA_MAX_TRIES = 10;
  39. public static final String X_AUTHENTICATION_DENIED_REASON = "X-Authentication-Denied-Reason";
  40. public static final String REST_AUTH_RESOURCE = "/rest/auth/latest/session";
  41. public static final String SESSION_PARAM = "JSESSIONID";
  42. private JSONObject fredBadCredentials;
  43. private JSONObject fredGoodCredentials;
  44. @Inject
  45. FuncTestRestClient funcTestRestClient;
  46. @Inject
  47. FuncTestUrlHelper funcTestUrlHelper;
  48. @Before
  49. public void setUpTest() {
  50. try {
  51. fredBadCredentials = new JSONObject();
  52. fredBadCredentials.put("username", FRED_USERNAME);
  53. fredBadCredentials.put("password", FRED_PASSWORD + "zzz");
  54. fredGoodCredentials = new JSONObject();
  55. fredGoodCredentials.put("username", FRED_USERNAME);
  56. fredGoodCredentials.put("password", FRED_PASSWORD);
  57. } catch (JSONException e) {
  58. throw new RuntimeException(e);
  59. }
  60. }
  61. @Test
  62. public void testCurrentUser() throws Exception {
  63. final JSONObject json = funcTestRestClient.getJSON(REST_AUTH_RESOURCE);
  64. assertEquals(ADMIN_USERNAME, json.getString("name"));
  65. assertTrue(json.has("loginInfo"));
  66. }
  67. @Test
  68. public void testCurrentUserUglyName() throws Exception {
  69. backdoor.usersAndGroups().addUser("jo smith");
  70. navigation.login("jo smith");
  71. final JSONObject json = funcTestRestClient.getJSON(REST_AUTH_RESOURCE);
  72. assertEquals("jo smith", json.getString("name"));
  73. assertTrue(json.has("loginInfo"));
  74. }
  75. @Test
  76. public void testCurrentUserAnon() throws Exception {
  77. navigation.logout();
  78. final WebResponse response = funcTestRestClient.GET(REST_AUTH_RESOURCE);
  79. assertEquals(401, response.getResponseCode());
  80. }
  81. @Test
  82. public void testLogin() throws Exception {
  83. navigation.logout();
  84. backdoor.usersAndGroups().resetLoginCount(ADMIN_USERNAME);
  85. long currentLoginCount = backdoor.usersAndGroups().getLoginInfo(ADMIN_USERNAME).getLoginCount();
  86. JSONObject json = new JSONObject();
  87. json.put("username", ADMIN_USERNAME);
  88. json.put("password", "BAD-PASSWORD");
  89. WebResponse response = loginAs(json);
  90. assertEquals(401, response.getResponseCode());
  91. assertEquals("JIRA REST POST", response.getHeaderField("WWW-Authenticate"));
  92. // JRADEV-2313
  93. JSONObject userThatDoesntExist = new JSONObject().put("username", "wtf").put("password", "kljasdfjkl;dfs");
  94. assertEquals("should return 401 if user doesn't exist", 401, loginAs(userThatDoesntExist).getResponseCode());
  95. json = new JSONObject();
  96. json.put("username", ADMIN_USERNAME);
  97. json.put("password", ADMIN_USERNAME);
  98. response = loginAs(json);
  99. assertEquals(200, response.getResponseCode());
  100. final JSONObject responseJson = new JSONObject(response.getText());
  101. final JSONObject session = responseJson.getJSONObject("session");
  102. assertEquals(SESSION_PARAM, session.getString("name"));
  103. assertEquals(tester.getDialog().getWebClient().getCookieValue(SESSION_PARAM), session.getString("value"));
  104. final JSONObject loginInfo = responseJson.getJSONObject("loginInfo");
  105. assertTrue(loginInfo.has("previousLoginTime"));
  106. assertTrue(loginInfo.has("lastFailedLoginTime"));
  107. assertEquals(currentLoginCount + 1L, loginInfo.getLong("loginCount"));
  108. assertEquals(1L, loginInfo.getLong("failedLoginCount"));
  109. }
  110. // JRA-22172 This test is similar to above. But this time we not only login but we try to use it somewhere else.
  111. @Test
  112. public void testWhenTheLoginResourceGivesYouACookieYouShouldBeAbleToActuallyDoSomethingWithIt() throws Exception {
  113. final String key = navigation.issue().createIssue("homosapien", "Bug", "this is a summary");
  114. navigation.logout();
  115. JSONObject json = new JSONObject();
  116. json.put("username", ADMIN_USERNAME);
  117. json.put("password", ADMIN_USERNAME);
  118. final String jsessionId = getSessionId(loginAs(json));
  119. tester.getDialog().getWebClient().clearCookies();
  120. WebResponse response = funcTestRestClient.GET("/rest/api/latest/issue/" + key, MapBuilder.<String, String>newBuilder().add("Cookie", SESSION_PARAM + "=" + jsessionId).toImmutableMap());
  121. assertThat(response.getResponseCode(), equalTo(200));
  122. final JSONObject issue = new JSONObject(response.getText());
  123. assertTrue(issue.has("key"));
  124. }
  125. // JRA-25405: Can you call '/rest/auth' after your session has timed out?
  126. @Test
  127. public void testCanLoginAfterSessionTimeout() throws Exception {
  128. navigation.logout();
  129. JSONObject json = new JSONObject();
  130. json.put("username", ADMIN_USERNAME);
  131. json.put("password", ADMIN_USERNAME);
  132. //Login.
  133. WebResponse response = loginAs(json);
  134. assertEquals(200, response.getResponseCode());
  135. //Make sure we are logged in.
  136. response = funcTestRestClient.GET(REST_AUTH_RESOURCE);
  137. assertEquals(200, response.getResponseCode());
  138. //Make sure the logged in user can see HSP.
  139. response = funcTestRestClient.GET("/rest/api/latest/project/HSP");
  140. assertEquals(200, response.getResponseCode());
  141. //Change the session id to simulate a session timeout.
  142. final WebClient client = tester.getDialog().getWebClient();
  143. client.clearCookies();
  144. client.addCookie(SESSION_PARAM, "BAD1");
  145. //Make sure we logged out.
  146. response = funcTestRestClient.GET(REST_AUTH_RESOURCE);
  147. assertEquals(401, response.getResponseCode());
  148. //Make sure we get a 401 when session times out on non-auth resources. Normally this would return a 404
  149. //but in this case we expect a 401 because the user passed an invalid session id.
  150. client.clearCookies();
  151. client.addCookie(SESSION_PARAM, "BAD2");
  152. response = funcTestRestClient.GET("/rest/api/latest/project/HSP");
  153. assertEquals(401, response.getResponseCode());
  154. //Make sure that we can login again.
  155. client.clearCookies();
  156. client.addCookie(SESSION_PARAM, "BAD3");
  157. response = loginAs(json);
  158. assertEquals(200, response.getResponseCode());
  159. }
  160. private static String getSessionId(WebResponse response) throws JSONException, IOException {
  161. final JSONObject responseJson = new JSONObject(response.getText());
  162. final JSONObject session = responseJson.getJSONObject("session");
  163. return session.getString("value");
  164. }
  165. @Test
  166. public void testLoginsThatAreDeniedDueToCaptchaProtectionShouldReturn403() throws Exception {
  167. navigation.logout();
  168. // attempt to trigger a CAPTCHA request
  169. WebResponse response = provokeCaptchaFailure(fredBadCredentials);
  170. // now check the headers
  171. assertThat(response.getResponseCode(), equalTo(403));
  172. assertThat(response, header(X_AUTHENTICATION_DENIED_REASON,
  173. equalTo(String.format("CAPTCHA_CHALLENGE; login-url=%s", funcTestUrlHelper.getBaseUrlPlus("login.jsp")))));
  174. }
  175. @Test
  176. public void testCaptchaFailureWithWrongPasswordIsIdenticalToCaptchaFailureWithRightPassword() throws Exception {
  177. navigation.logout();
  178. WebResponse wrongPassResponse = provokeCaptchaFailure(fredBadCredentials);
  179. WebResponse rightPassResponse = loginAs(fredGoodCredentials);
  180. assertThat(rightPassResponse.getHeaderField(X_AUTHENTICATION_DENIED_REASON), equalTo(wrongPassResponse.getHeaderField(X_AUTHENTICATION_DENIED_REASON)));
  181. }
  182. /**
  183. * @see JIRA Agile (GHS-10385)
  184. */
  185. @Test
  186. public void testLoginReturnEncodedUsername_IncludingUtf8Chars_InResponseHeader() throws Exception {
  187. final String utf8Username = "łTestł";
  188. final String urlEncodedUsername = URLEncoder.encode(utf8Username, "UTF-8");
  189. navigation.logout();
  190. backdoor.usersAndGroups().addUser(utf8Username);
  191. navigation.login(utf8Username);
  192. final WebResponse response = funcTestRestClient.GET(REST_AUTH_RESOURCE);
  193. assertEquals(200, response.getResponseCode());
  194. assertEquals(urlEncodedUsername, response.getHeaderField("X-AUSERNAME"));
  195. }
  196. protected WebResponse loginAs(JSONObject json) throws IOException, SAXException {
  197. return funcTestRestClient.POST(REST_AUTH_RESOURCE, json);
  198. }
  199. protected WebResponse provokeCaptchaFailure(JSONObject badCredentials) throws IOException, SAXException {
  200. int tries = CAPTCHA_MAX_TRIES;
  201. WebResponse response;
  202. do {
  203. response = loginAs(badCredentials);
  204. navigation.logout();
  205. }
  206. while (response.getResponseCode() == 401 && --tries > 0);
  207. if (tries == 0) {
  208. fail(String.format("Captcha did not kick in after %d failed logins", CAPTCHA_MAX_TRIES));
  209. }
  210. return response;
  211. }
  212. @Test
  213. public void testLogout() throws Exception {
  214. navigation.login(ADMIN_USERNAME);
  215. WebResponse response = funcTestRestClient.DELETE(REST_AUTH_RESOURCE);
  216. assertEquals(204, response.getResponseCode());
  217. // anonymous users should get a different response
  218. response = funcTestRestClient.DELETE(REST_AUTH_RESOURCE);
  219. assertEquals(401, response.getResponseCode());
  220. }
  221. }