Skip to content

Commit

Permalink
Metadata Repositories: Support Bearer Token Authentication
Browse files Browse the repository at this point in the history
Signed-off-by: Valentijn Scholten <[email protected]>
  • Loading branch information
valentijnscholten committed Jan 3, 2025
1 parent 23d9fa8 commit 610dcf8
Showing 1 changed file with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*/
package org.dependencytrack.resources.v1;

import alpine.server.filters.ApiFilter;
import alpine.server.filters.AuthenticationFilter;
import java.util.Date;
import java.util.List;

import org.dependencytrack.JerseyTestRule;
import org.dependencytrack.ResourceTest;
import org.dependencytrack.model.Repository;
Expand All @@ -33,13 +34,13 @@
import org.junit.ClassRule;
import org.junit.Test;

import alpine.server.filters.ApiFilter;
import alpine.server.filters.AuthenticationFilter;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.Date;
import java.util.List;

public class RepositoryResourceTest extends ResourceTest {

Expand Down Expand Up @@ -173,7 +174,7 @@ public void getRepositoryMetaUntrackedComponentTest() {


@Test
public void createRepositoryTest() {
public void createRepositoryTestWithBasicAuth() {
Repository repository = new Repository();
repository.setAuthenticationRequired(true);
repository.setEnabled(true);
Expand Down Expand Up @@ -203,6 +204,35 @@ public void createRepositoryTest() {
Assert.assertTrue(json.getJsonObject(13).getBoolean("enabled"));
}

@Test
public void createRepositoryTestWithBearerAuth() {
Repository repository = new Repository();
repository.setAuthenticationRequired(true);
repository.setEnabled(true);
repository.setPassword("testToken");
repository.setInternal(true);
repository.setIdentifier("test");
repository.setUrl("www.foobar.com");
repository.setType(RepositoryType.MAVEN);
Response response = jersey.target(V1_REPOSITORY).request().header(X_API_KEY, apiKey)
.put(Entity.entity(repository, MediaType.APPLICATION_JSON));
Assert.assertEquals(201, response.getStatus());

response = jersey.target(V1_REPOSITORY).request().header(X_API_KEY, apiKey).get(Response.class);
Assert.assertEquals(200, response.getStatus(), 0);
Assert.assertEquals(String.valueOf(18), response.getHeaderString(TOTAL_COUNT_HEADER));
JsonArray json = parseJsonArray(response);
Assert.assertNotNull(json);
Assert.assertEquals(18, json.size());
Assert.assertEquals("MAVEN", json.getJsonObject(13).getString("type"));
Assert.assertEquals("test", json.getJsonObject(13).getString("identifier"));
Assert.assertEquals("www.foobar.com", json.getJsonObject(13).getString("url"));
Assert.assertTrue(json.getJsonObject(13).getInt("resolutionOrder") > 0);
Assert.assertTrue(json.getJsonObject(13).getBoolean("authenticationRequired"));
Assert.assertNull(json.getJsonObject(13).getString("username"));
Assert.assertTrue(json.getJsonObject(13).getBoolean("enabled"));
}

@Test
public void createNonInternalRepositoryTest() {
Repository repository = new Repository();
Expand Down Expand Up @@ -262,7 +292,6 @@ public void createRepositoryAuthFalseTest() {
Assert.assertTrue(json.getJsonObject(13).getInt("resolutionOrder") > 0);
Assert.assertFalse(json.getJsonObject(13).getBoolean("authenticationRequired"));
Assert.assertTrue(json.getJsonObject(13).getBoolean("enabled"));

}

@Test
Expand Down Expand Up @@ -298,6 +327,5 @@ public void updateRepositoryTest() throws Exception {
}
}
}

}
}

0 comments on commit 610dcf8

Please sign in to comment.