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

ash couchdb view fix #283

Merged
merged 2 commits into from
Oct 29, 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 @@ -118,7 +118,7 @@ private AuthDBNameViewDesign parseTokenDesignFromJson(String docJson) throws Cou
return tableDesign;
}

private boolean updateDesignDocToDesiredDesignDoc(AuthDBNameViewDesign tableDesign, String dbName) {
protected boolean updateDesignDocToDesiredDesignDoc(AuthDBNameViewDesign tableDesign, String dbName) {
boolean isUpdated = false;

if (tableDesign.views == null) {
Expand All @@ -133,7 +133,7 @@ private boolean updateDesignDocToDesiredDesignDoc(AuthDBNameViewDesign tableDesi

if (tableDesign.views.loginIdView.map == null) {
isUpdated = true;
if(dbName.equals(DB_TABLE_TOKENS_DESIGN)){
if(dbName.equals(CouchdbAuthStore.TOKENS_DATABASE_NAME)){
tableDesign.views.loginIdView.map = DB_TABLE_TOKENS_DESIGN;
}
else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
package dev.galasa.auth.couchdb.internal;

import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -520,5 +524,43 @@ public void testCheckCouchdbDatabaseIsValidWithUpdateDesignDocResponseThrowsErro
assertThat(thrown).isNotNull();
assertThat(thrown.getMessage()).contains("Update of galasa_tokens design");
}

// Test when all fields are null and dbName is TOKENS_DATABASE_NAME
@Test
public void testAllFieldsNull_TokensDatabase() {
AuthDBNameViewDesign tableDesign = new AuthDBNameViewDesign();
String dbName = CouchdbAuthStore.TOKENS_DATABASE_NAME;

CouchdbAuthStoreValidator validator = new CouchdbAuthStoreValidator();

boolean isUpdated = validator.updateDesignDocToDesiredDesignDoc(tableDesign, dbName);

String DB_TABLE_TOKENS_DESIGN = "function (doc) { if (doc.owner && doc.owner.loginId) {emit(doc.owner.loginId, doc); } }";

assertTrue(isUpdated);
assertNotNull(tableDesign.views);
assertNotNull(tableDesign.views.loginIdView);
assertEquals(DB_TABLE_TOKENS_DESIGN, tableDesign.views.loginIdView.map);
assertEquals("javascript", tableDesign.language);
}

// Test when all fields are null and dbName is not TOKENS_DATABASE_NAME
@Test
public void testAllFieldsNull_OtherDatabase() {
AuthDBNameViewDesign tableDesign = new AuthDBNameViewDesign();
String dbName = CouchdbAuthStore.USERS_DATABASE_NAME;

CouchdbAuthStoreValidator validator = new CouchdbAuthStoreValidator();

boolean isUpdated = validator.updateDesignDocToDesiredDesignDoc(tableDesign, dbName);

String DB_TABLE_USERS_DESIGN = "function (doc) { if (doc['login-id']) { emit(doc['login-id'], doc); } }";

assertTrue(isUpdated);
assertNotNull(tableDesign.views);
assertNotNull(tableDesign.views.loginIdView);
assertEquals(DB_TABLE_USERS_DESIGN, tableDesign.views.loginIdView.map);
assertEquals("javascript", tableDesign.language);
}

}