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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 static java.lang.String.format;
  35. import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  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 MockWebServer mockWebServer = new MockWebServer();
  44. private OAuth2AccessToken auth2AccessToken = mock(OAuth2AccessToken.class);
  45. private String serverUrl;
  46. private OAuth20Service oAuth20Service = new ServiceBuilder("API_KEY")
  47. .apiSecret("API_SECRET")
  48. .callback("CALLBACK")
  49. .build(new TestAPI());
  50. @Before
  51. public void setUp() {
  52. this.serverUrl = format("http://%s:%d", mockWebServer.getHostName(), mockWebServer.getPort());
  53. }
  54. @Test
  55. public void execute_request() throws IOException {
  56. String body = randomAlphanumeric(10);
  57. mockWebServer.enqueue(new MockResponse().setBody(body));
  58. Response response = executeRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken);
  59. assertThat(response.getBody()).isEqualTo(body);
  60. }
  61. @Test
  62. public void fail_to_execute_request() throws IOException {
  63. mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("Error!"));
  64. assertThatThrownBy(() -> executeRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken))
  65. .isInstanceOf(IllegalStateException.class)
  66. .hasMessage(format("Fail to execute request '%s/test'. HTTP code: 404, response: Error!", serverUrl));
  67. }
  68. @Test
  69. public void execute_paginated_request() {
  70. mockWebServer.enqueue(new MockResponse()
  71. .setHeader("Link", "<" + serverUrl + "/test?per_page=100&page=2>; rel=\"next\", <" + serverUrl + "/test?per_page=100&page=2>; rel=\"last\"")
  72. .setBody("A"));
  73. mockWebServer.enqueue(new MockResponse()
  74. .setHeader("Link", "<" + serverUrl + "/test?per_page=100&page=1>; rel=\"prev\", <" + serverUrl + "/test?per_page=100&page=1>; rel=\"first\"")
  75. .setBody("B"));
  76. List<String> response = executePaginatedRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken, Arrays::asList);
  77. assertThat(response).contains("A", "B");
  78. }
  79. @Test
  80. public void execute_paginated_request_with_query_parameter() throws InterruptedException {
  81. mockWebServer.enqueue(new MockResponse()
  82. .setHeader("Link", "<" + serverUrl + "/test?param=value&per_page=100&page=2>; rel=\"next\", <" + serverUrl + "/test?param=value&per_page=100&page=2>; rel=\"last\"")
  83. .setBody("A"));
  84. mockWebServer.enqueue(new MockResponse()
  85. .setHeader("Link", "<" + serverUrl + "/test?param=value&per_page=100&page=1>; rel=\"prev\", <" + serverUrl + "/test?param=value&per_page=100&page=1>; rel=\"first\"")
  86. .setBody("B"));
  87. List<String> response = executePaginatedRequest(serverUrl + "/test?param=value", oAuth20Service, auth2AccessToken, Arrays::asList);
  88. assertThat(response).contains("A", "B");
  89. assertThat(mockWebServer.takeRequest().getPath()).isEqualTo("/test?param=value&per_page=100");
  90. assertThat(mockWebServer.takeRequest().getPath()).isEqualTo("/test?param=value&per_page=100&page=2");
  91. }
  92. @Test
  93. public void execute_paginated_request_case_insensitive_headers() {
  94. mockWebServer.enqueue(new MockResponse()
  95. .setHeader("link", "<" + serverUrl + "/test?per_page=100&page=2>; rel=\"next\", <" + serverUrl + "/test?per_page=100&page=2>; rel=\"last\"")
  96. .setBody("A"));
  97. mockWebServer.enqueue(new MockResponse()
  98. .setHeader("link", "<" + serverUrl + "/test?per_page=100&page=1>; rel=\"prev\", <" + serverUrl + "/test?per_page=100&page=1>; rel=\"first\"")
  99. .setBody("B"));
  100. List<String> response = executePaginatedRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken, Arrays::asList);
  101. assertThat(response).contains("A", "B");
  102. }
  103. @Test
  104. public void fail_to_executed_paginated_request() {
  105. mockWebServer.enqueue(new MockResponse()
  106. .setHeader("Link", "<" + serverUrl + "/test?per_page=100&page=2>; rel=\"next\", <" + serverUrl + "/test?per_page=100&page=2>; rel=\"last\"")
  107. .setBody("A"));
  108. mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("Error!"));
  109. assertThatThrownBy(() -> executePaginatedRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken, Arrays::asList))
  110. .isInstanceOf(IllegalStateException.class)
  111. .hasMessage(format("Fail to execute request '%s/test?per_page=100&page=2'. HTTP code: 404, response: Error!", serverUrl));
  112. }
  113. private class TestAPI extends DefaultApi20 {
  114. @Override
  115. public String getAccessTokenEndpoint() {
  116. return serverUrl + "/login/oauth/access_token";
  117. }
  118. @Override
  119. protected String getAuthorizationBaseUrl() {
  120. return serverUrl + "/login/oauth/authorize";
  121. }
  122. }
  123. }