Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Stop token docs from being filtered out by couchdb when getting tokens #274

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testGetTokensReturnsTokensWithFailingRequestReturnsError() throws Ex
MockLogFactory logFactory = new MockLogFactory();

List<HttpInteraction> interactions = new ArrayList<HttpInteraction>();
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs?include_docs=true&endkey=%22_%22", HttpStatus.SC_INTERNAL_SERVER_ERROR, null));
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs", HttpStatus.SC_INTERNAL_SERVER_ERROR, null));

MockCloseableHttpClient mockHttpClient = new MockCloseableHttpClient(interactions);

Expand Down Expand Up @@ -137,7 +137,7 @@ public void testGetTokensReturnsTokensFromCouchdbOK() throws Exception {

CouchdbAuthToken mockToken = new CouchdbAuthToken("token1", "dex-client", "my test token", Instant.now(), new CouchdbUser("johndoe", "dex-user-id"));
List<HttpInteraction> interactions = new ArrayList<HttpInteraction>();
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs?include_docs=true&endkey=%22_%22", HttpStatus.SC_OK, mockAllDocsResponse));
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs", HttpStatus.SC_OK, mockAllDocsResponse));
interactions.add(new GetTokenDocumentInteraction<CouchdbAuthToken>("https://my-auth-store/galasa_tokens/token1", HttpStatus.SC_OK, mockToken));

MockCloseableHttpClient mockHttpClient = new MockCloseableHttpClient(interactions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static dev.galasa.extensions.common.Errors.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
Expand All @@ -20,8 +19,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
Expand Down Expand Up @@ -55,8 +52,6 @@ public abstract class CouchdbStore {

protected final URI storeUri;

private Log logger = LogFactory.getLog(this.getClass());

protected HttpRequestFactory httpRequestFactory;
protected CloseableHttpClient httpClient;
protected GalasaGson gson = new GalasaGson();
Expand Down Expand Up @@ -117,9 +112,7 @@ protected PutPostResponse createDocument(String dbName, String jsonContent) thro
*/
protected List<ViewRow> getAllDocsFromDatabase(String dbName) throws CouchdbException {

//The end key is "_" because, design docs start with "_design",
// this will exclude any design documents from being fetched from couchdb.
HttpGet getTokensDocs = httpRequestFactory.getHttpGetRequest(storeUri + "/" + dbName + "/_all_docs?include_docs=true&endkey=%22_%22");
HttpGet getTokensDocs = httpRequestFactory.getHttpGetRequest(storeUri + "/" + dbName + "/_all_docs");
String responseEntity = sendHttpRequest(getTokensDocs, HttpStatus.SC_OK);

ViewResponse allDocs = gson.fromJson(responseEntity, ViewResponse.class);
Expand All @@ -129,6 +122,11 @@ protected List<ViewRow> getAllDocsFromDatabase(String dbName) throws CouchdbExce
String errorMessage = ERROR_FAILED_TO_GET_DOCUMENTS_FROM_DATABASE.getMessage(dbName);
throw new CouchdbException(errorMessage);
}

// Filter out design documents from the results
viewRows = viewRows.stream()
.filter((row) -> !row.key.equals("_design/docs"))
.collect(Collectors.toList());

return viewRows;
}
Expand All @@ -144,7 +142,6 @@ protected List<ViewRow> getAllDocsFromDatabase(String dbName) throws CouchdbExce
* @return a list of rows corresponding to documents within the database
* @throws CouchdbException if there was a problem accessing the
* CouchDB store or its response
* @throws UnsupportedEncodingException A failure occurred.
*/
protected List<ViewRow> getAllDocsByLoginId(String dbName, String loginId) throws CouchdbException {

Expand Down