|
| 1 | +/* |
| 2 | + * Copyright (c) 2012-2025 Red Hat, Inc. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Red Hat, Inc. - initial API and implementation |
| 11 | + */ |
| 12 | +package org.eclipse.che.api.factory.server.gitlab; |
| 13 | + |
| 14 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 15 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 16 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 17 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 18 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.testng.Assert.assertEquals; |
| 21 | +import static org.testng.Assert.assertFalse; |
| 22 | +import static org.testng.Assert.assertTrue; |
| 23 | + |
| 24 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 25 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 26 | +import com.github.tomakehurst.wiremock.common.Slf4jNotifier; |
| 27 | +import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager; |
| 28 | +import org.eclipse.che.api.factory.server.urlfactory.DevfileFilenamesProvider; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.testng.MockitoTestNGListener; |
| 31 | +import org.testng.annotations.BeforeClass; |
| 32 | +import org.testng.annotations.BeforeMethod; |
| 33 | +import org.testng.annotations.DataProvider; |
| 34 | +import org.testng.annotations.Listeners; |
| 35 | +import org.testng.annotations.Test; |
| 36 | + |
| 37 | +@Listeners(MockitoTestNGListener.class) |
| 38 | +public class GitlabCustomPortUrlParserTest { |
| 39 | + |
| 40 | + @Mock private DevfileFilenamesProvider devfileFilenamesProvider; |
| 41 | + |
| 42 | + /** Instance of component that will be tested. */ |
| 43 | + private GitlabUrlParser gitlabUrlParser; |
| 44 | + |
| 45 | + private WireMockServer wireMockServer; |
| 46 | + private WireMock wireMock; |
| 47 | + |
| 48 | + @BeforeClass |
| 49 | + public void prepare() { |
| 50 | + wireMockServer = |
| 51 | + new WireMockServer(wireMockConfig().notifier(new Slf4jNotifier(false)).dynamicPort()); |
| 52 | + wireMockServer.start(); |
| 53 | + WireMock.configureFor("localhost", wireMockServer.port()); |
| 54 | + wireMock = new WireMock("localhost", wireMockServer.port()); |
| 55 | + } |
| 56 | + |
| 57 | + @BeforeMethod |
| 58 | + public void setUp() { |
| 59 | + gitlabUrlParser = |
| 60 | + new GitlabUrlParser( |
| 61 | + "https://gitlab.custom.com:31280", |
| 62 | + devfileFilenamesProvider, |
| 63 | + mock(PersonalAccessTokenManager.class)); |
| 64 | + } |
| 65 | + |
| 66 | + /** Check URLs are valid with regexp */ |
| 67 | + @Test(dataProvider = "UrlsProvider") |
| 68 | + public void checkRegexp(String url) { |
| 69 | + assertTrue(gitlabUrlParser.isValid(url), "url " + url + " is invalid"); |
| 70 | + } |
| 71 | + |
| 72 | + /** Compare parsing */ |
| 73 | + @Test(dataProvider = "parsing") |
| 74 | + public void checkParsing(String url, String project, String subGroups, String branch) { |
| 75 | + GitlabUrl gitlabUrl = gitlabUrlParser.parse(url); |
| 76 | + |
| 77 | + assertEquals(gitlabUrl.getProject(), project); |
| 78 | + assertEquals(gitlabUrl.getSubGroups(), subGroups); |
| 79 | + assertEquals(gitlabUrl.getBranch(), branch); |
| 80 | + } |
| 81 | + |
| 82 | + /** Compare parsing */ |
| 83 | + @Test(dataProvider = "parsing") |
| 84 | + public void shouldParseWithoutPredefinedEndpoint( |
| 85 | + String url, String project, String subGroups, String branch) { |
| 86 | + // given |
| 87 | + gitlabUrlParser = |
| 88 | + new GitlabUrlParser(null, devfileFilenamesProvider, mock(PersonalAccessTokenManager.class)); |
| 89 | + // when |
| 90 | + GitlabUrl gitlabUrl = gitlabUrlParser.parse(url); |
| 91 | + |
| 92 | + // then |
| 93 | + assertEquals(gitlabUrl.getProject(), project); |
| 94 | + assertEquals(gitlabUrl.getSubGroups(), subGroups); |
| 95 | + assertEquals(gitlabUrl.getBranch(), branch); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void shouldValidateUrlByApiRequest() { |
| 100 | + // given |
| 101 | + String url = wireMockServer.url("/user/repo"); |
| 102 | + stubFor( |
| 103 | + get(urlEqualTo("/oauth/token/info")) |
| 104 | + .willReturn( |
| 105 | + aResponse() |
| 106 | + .withStatus(401) |
| 107 | + .withBody( |
| 108 | + "{\"error\":\"invalid_token\",\"error_description\":\"The access token is invalid\",\"state\":\"unauthorized\"}"))); |
| 109 | + |
| 110 | + // when |
| 111 | + boolean result = gitlabUrlParser.isValid(url); |
| 112 | + |
| 113 | + // then |
| 114 | + assertTrue(result); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void shouldNotValidateUrlByApiRequestWithPlainStringResponse() { |
| 119 | + // given |
| 120 | + String url = wireMockServer.url("/user/repo"); |
| 121 | + stubFor( |
| 122 | + get(urlEqualTo("/oauth/token/info")) |
| 123 | + .willReturn(aResponse().withStatus(401).withBody("plain string error"))); |
| 124 | + |
| 125 | + // when |
| 126 | + boolean result = gitlabUrlParser.isValid(url); |
| 127 | + |
| 128 | + // then |
| 129 | + assertFalse(result); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void shouldNotValidateUrlByApiRequest() { |
| 134 | + // given |
| 135 | + String url = wireMockServer.url("/user/repo"); |
| 136 | + stubFor(get(urlEqualTo("/oauth/token/info")).willReturn(aResponse().withStatus(500))); |
| 137 | + |
| 138 | + // when |
| 139 | + boolean result = gitlabUrlParser.isValid(url); |
| 140 | + |
| 141 | + // then |
| 142 | + assertFalse(result); |
| 143 | + } |
| 144 | + |
| 145 | + @DataProvider(name = "UrlsProvider") |
| 146 | + public Object[][] urls() { |
| 147 | + return new Object[][] { |
| 148 | + {"https://gitlab.custom.com:31280/user/project/test1.git"}, |
| 149 | + {"https://gitlab.custom.com:31280/user/project1.git"}, |
| 150 | + {"https://gitlab.custom.com:31280/scm/project/test1.git"}, |
| 151 | + {"https://gitlab.custom.com:31280/user/project/"}, |
| 152 | + {"https://gitlab.custom.com:31280/user/project/repo/"}, |
| 153 | + {"https://gitlab.custom.com:31280/user/project/-/tree/master/"}, |
| 154 | + {"https://gitlab.custom.com:31280/user/project/repo/-/tree/master/subfolder"}, |
| 155 | + { "[email protected]:user/project/test1.git"}, |
| 156 | + { "[email protected]:user/project1.git"}, |
| 157 | + { "[email protected]:scm/project/test1.git"}, |
| 158 | + { "[email protected]:user/project/"}, |
| 159 | + { "[email protected]:user/project/repo/"}, |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + @DataProvider(name = "parsing") |
| 164 | + public Object[][] expectedParsing() { |
| 165 | + return new Object[][] { |
| 166 | + {"https://gitlab.custom.com:31280/user/project1.git", "project1", "user/project1", null}, |
| 167 | + { |
| 168 | + "https://gitlab.custom.com:31280/user/project/test1.git", |
| 169 | + "test1", |
| 170 | + "user/project/test1", |
| 171 | + null |
| 172 | + }, |
| 173 | + { |
| 174 | + "https://gitlab.custom.com:31280/user/project/group1/group2/test1.git", |
| 175 | + "test1", |
| 176 | + "user/project/group1/group2/test1", |
| 177 | + null |
| 178 | + }, |
| 179 | + {"https://gitlab.custom.com:31280/user/project/", "project", "user/project", null}, |
| 180 | + {"https://gitlab.custom.com:31280/user/project/repo/", "repo", "user/project/repo", null}, |
| 181 | + { "[email protected]:user/project1.git", "project1", "user/project1", null}, |
| 182 | + { "[email protected]:user/project/test1.git", "test1", "user/project/test1", null}, |
| 183 | + { |
| 184 | + "[email protected]:user/project/group1/group2/test1.git", |
| 185 | + "test1", |
| 186 | + "user/project/group1/group2/test1", |
| 187 | + null |
| 188 | + }, |
| 189 | + { "[email protected]:user/project/", "project", "user/project", null}, |
| 190 | + { "[email protected]:user/project/repo/", "repo", "user/project/repo", null}, |
| 191 | + { |
| 192 | + "https://gitlab.custom.com:31280/user/project/-/tree/master/", |
| 193 | + "project", |
| 194 | + "user/project", |
| 195 | + "master" |
| 196 | + }, |
| 197 | + { |
| 198 | + "https://gitlab.custom.com:31280/user/project/repo/-/tree/foo", |
| 199 | + "repo", |
| 200 | + "user/project/repo", |
| 201 | + "foo" |
| 202 | + }, |
| 203 | + { |
| 204 | + "https://gitlab.custom.com:31280/user/project/repo/-/tree/branch/with/slash", |
| 205 | + "repo", |
| 206 | + "user/project/repo", |
| 207 | + "branch/with/slash" |
| 208 | + }, |
| 209 | + { |
| 210 | + "https://gitlab.custom.com:31280/user/project/group1/group2/repo/-/tree/foo/", |
| 211 | + "repo", |
| 212 | + "user/project/group1/group2/repo", |
| 213 | + "foo" |
| 214 | + } |
| 215 | + }; |
| 216 | + } |
| 217 | +} |
0 commit comments