Skip to content

Commit

Permalink
Stop token docs from being filtered out by couchdb when getting tokens (
Browse files Browse the repository at this point in the history
#274)

Signed-off-by: Eamonn Mansour <47121388+eamansour@users.noreply.github.com>
  • Loading branch information
eamansour committed Sep 16, 2024
1 parent 8914b41 commit 2f5484f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
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

0 comments on commit 2f5484f

Please sign in to comment.