Skip to content

Commit

Permalink
fix: fetching sampleData from a random API only if it's not available…
Browse files Browse the repository at this point in the history
… for test editor
  • Loading branch information
TangoBeeAkto committed Dec 12, 2024
1 parent 1e0f9f3 commit b338955
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
13 changes: 3 additions & 10 deletions apps/dashboard/src/main/java/com/akto/action/TrafficAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,24 @@ public String fetchSampleData() {

public String fetchSampleDataForTestEditor() {
fetchSampleData();
if(sampleDataList == null || sampleDataList.isEmpty()) {
if(sampleDataList == null || sampleDataList.isEmpty() || sampleDataList.get(0).getSamples().isEmpty()) {
sampleDataList = new ArrayList<>();
ApiCollection randomActiveCollection = ApiCollectionsDao.instance.findOne(Filters.and(
Filters.eq(ApiCollection._DEACTIVATED, false),
Filters.not(
Filters.size(ApiCollection.URLS_STRING, 0)
),
Filters.ne(ApiCollection.ID, 0)
)
));

if(randomActiveCollection == null) {
return Action.SUCCESS.toUpperCase();
}

int activeCollectionId = randomActiveCollection.getId();
List<String> endpointsList = new ArrayList<>(randomActiveCollection.getUrls());
String[] endpointAndMethod = endpointsList.get(0).split(" ");

sampleDataList = SampleDataDao.instance.findAll(Filters.and(
Filters.eq("_id.url", endpointAndMethod[0]),
Filters.in(SingleTypeInfo._COLLECTION_IDS, activeCollectionId),
Filters.eq("_id.responseCode", -1),
Filters.eq("_id.method", endpointAndMethod[1]),
Filters.gte("_id.bucketStartEpoch", 0),
Filters.lte("_id.bucketEndEpoch", 0)
Filters.not(Filters.size(SampleData.SAMPLES, 0))
));
}
return Action.SUCCESS.toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import request from "@/util/request"

const testEditorRequests = {
fetchSampleData(collectionId, apiEndpointUrl, apiEndpointMethod) {
return request({
url: '/api/fetchSampleData',
method: 'post',
data: {
apiCollectionId: collectionId,
url: apiEndpointUrl,
method: apiEndpointMethod
}
})
},
fetchSampleDataForTestEditor(collectionId, apiEndpointUrl, apiEndpointMethod) {
return request({
url: '/api/fetchSampleDataForTestEditor',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,15 @@ const SampleApi = () => {
const selectedSampleApi = PersistStore(state => state.selectedSampleApi)
const setSelectedSampleApi = PersistStore(state => state.setSelectedSampleApi)

const [isCustomAPI, setIsCustomAPI] = useState(Object.keys(selectedSampleApi)?.length > 0 || false)

const tabs = [{ id: 'request', content: 'Request' }, { id: 'response', content: 'Response'}];
const mapCollectionIdToName = func.mapCollectionIdToName(allCollections)

useEffect(()=>{
if(showEmptyLayout) return
let testId = selectedTest.value
let sampleData = null
if(sampleDataList?.length > 0) {
sampleData = {
apiCollectionId: sampleDataList[0].id.apiCollectionId,
method: {_name: sampleDataList[0].id.method},
url: sampleDataList[0].id.url
}
}
let selectedUrl = sampleData ? sampleData : Object.keys(selectedSampleApi).length > 0 ? selectedSampleApi : vulnerableRequestsObj?.[testId]
let selectedUrl = Object.keys(selectedSampleApi).length > 0 ? selectedSampleApi : vulnerableRequestsObj?.[testId]
setSelectedCollectionId(null)
setCopyCollectionId(null)
setTestResult(null)
Expand Down Expand Up @@ -123,8 +117,6 @@ const SampleApi = () => {
useEffect(() => {
if (selectedCollectionId && selectedApiEndpoint) {
fetchSampleData(selectedCollectionId, func.toMethodUrlObject(selectedApiEndpoint).url, func.toMethodUrlObject(selectedApiEndpoint).method)
}else{
setEditorData({message: ''})
}
setTestResult(null)
}, [selectedApiEndpoint])
Expand Down Expand Up @@ -162,6 +154,7 @@ const SampleApi = () => {
})

const fetchApiEndpoints = async (collectionId) => {
if(!collectionId) return
const apiEndpointsResponse = await api.fetchCollectionWiseApiEndpoints(collectionId)
if (apiEndpointsResponse) {
setApiEndpoints(apiEndpointsResponse.listOfEndpointsInCollection)
Expand All @@ -179,7 +172,12 @@ const SampleApi = () => {

const fetchSampleData = async (collectionId, apiEndpointUrl, apiEndpointMethod) => {
setShowEmptyLayout(false)
const sampleDataResponse = await testEditorRequests.fetchSampleDataForTestEditor(collectionId, apiEndpointUrl, apiEndpointMethod)
let sampleDataResponse
if(isCustomAPI) {
sampleDataResponse = await testEditorRequests.fetchSampleData(collectionId, apiEndpointUrl, apiEndpointMethod)
} else {
sampleDataResponse = await testEditorRequests.fetchSampleDataForTestEditor(collectionId, apiEndpointUrl, apiEndpointMethod)
}
if (sampleDataResponse) {
if (sampleDataResponse.sampleDataList.length > 0 && sampleDataResponse.sampleDataList[0].samples && sampleDataResponse.sampleDataList[0].samples.length > 0) {
const sampleDataJson = JSON.parse(sampleDataResponse.sampleDataList[0].samples[sampleDataResponse.sampleDataList[0].samples.length - 1])
Expand All @@ -206,6 +204,7 @@ const SampleApi = () => {

const toggleSelectApiActive = () => setSelectApiActive(prev => !prev)
const saveFunc = () =>{
setIsCustomAPI(true)
setSelectedApiEndpoint(copySelectedApiEndpoint)
const urlObj = func.toMethodUrlObject(copySelectedApiEndpoint)
const sampleApi = {
Expand Down

0 comments on commit b338955

Please sign in to comment.