You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OAuthRestClientTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.auth;
  21. import com.github.scribejava.core.builder.ServiceBuilder;
  22. import com.github.scribejava.core.builder.api.DefaultApi20;
  23. import com.github.scribejava.core.model.OAuth2AccessToken;
  24. import com.github.scribejava.core.model.Response;
  25. import com.github.scribejava.core.oauth.OAuth20Service;
  26. import java.io.IOException;
  27. import java.util.Arrays;
  28. import java.util.List;
  29. import okhttp3.mockwebserver.MockResponse;
  30. import okhttp3.mockwebserver.MockWebServer;
  31. import org.junit.Before;
  32. import org.junit.Rule;
  33. import org.junit.Test;
  34. import org.junit.rules.ExpectedException;
  35. import static java.lang.String.format;
  36. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.mockito.Mockito.mock;
  39. import static org.sonar.auth.OAuthRestClient.executePaginatedRequest;
  40. import static org.sonar.auth.OAuthRestClient.executeRequest;
  41. public class OAuthRestClientTest {
  42. @Rule
  43. public ExpectedException expectedException = ExpectedException.none();
  44. @Rule
  45. public MockWebServer mockWebServer = new MockWebServer();
  46. private OAuth2AccessToken auth2AccessToken = mock(OAuth2AccessToken.class);
  47. private String serverUrl;
  48. private OAuth20Service oAuth20Service = new ServiceBuilder("API_KEY")
  49. .apiSecret("API_SECRET")
  50. .callback("CALLBACK")
  51. .build(new TestAPI());
  52. @Before
  53. public void setUp() {
  54. this.serverUrl = format("http://%s:%d", mockWebServer.getHostName(), mockWebServer.getPort());
  55. }
  56. @Test
  57. public void execute_request() throws IOException {
  58. String body = randomAlphanumeric(10);
  59. mockWebServer.enqueue(new MockResponse().setBody(body));
  60. Response response = executeRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken);
  61. assertThat(response.getBody()).isEqualTo(body);
  62. }
  63. @Test
  64. public void fail_to_execute_request() throws IOException {
  65. mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("Error!"));
  66. expectedException.expect(IllegalStateException.class);
  67. expectedException.expectMessage(format("Fail to execute request '%s/test'. HTTP code: 404, response: Error!", serverUrl));
  68. executeRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken);
  69. }
  70. @Test
  71. public void execute_paginated_request() {
  72. mockWebServer.enqueue(new MockResponse()
  73. .setHeader("Link", "<" + serverUrl + "/test?per_page=100&page=2>; rel=\"next\", <" + serverUrl + "/test?per_page=100&page=2>; rel=\"last\"")
  74. .setBody("A"));
  75. mockWebServer.enqueue(new MockResponse()
  76. .setHeader("Link", "<" + serverUrl + "/test?per_page=100&page=1>; rel=\"prev\", <" + serverUrl + "/test?per_page=100&page=1>; rel=\"first\"")
  77. .setBody("B"));
  78. List<String> response = executePaginatedRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken, Arrays::asList);
  79. assertThat(response).contains("A", "B");
  80. }
  81. @Test
  82. public void fail_to_executed_paginated_request() {
  83. mockWebServer.enqueue(new MockResponse()
  84. .setHeader("Link", "<" + serverUrl + "/test?per_page=100&page=2>; rel=\"next\", <" + serverUrl + "/test?per_page=100&page=2>; rel=\"last\"")
  85. .setBody("A"));
  86. mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("Error!"));
  87. expectedException.expect(IllegalStateException.class);
  88. expectedException.expectMessage(format("Fail to execute request '%s/test?per_page=100&page=2'. HTTP code: 404, response: Error!", serverUrl));
  89. executePaginatedRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken, Arrays::asList);
  90. }
  91. private class TestAPI extends DefaultApi20 {
  92. @Override
  93. public String getAccessTokenEndpoint() {
  94. return serverUrl + "/login/oauth/access_token";
  95. }
  96. @Override
  97. protected String getAuthorizationBaseUrl() {
  98. return serverUrl + "/login/oauth/authorize";
  99. }
  100. }
  101. }