use of com.searchcode.app.dto.api.RepoResultApiResponse in project searchcode-server by boyter.
the class ApiRouteServiceTest method testRepoListApiNotEnabled.
// ///////////////////////////////////////////////////////////////////
public void testRepoListApiNotEnabled() {
ApiRouteService apiRouteService = new ApiRouteService();
apiRouteService.apiEnabled = false;
RepoResultApiResponse apiResponse = apiRouteService.repoList(null, null);
assertThat(apiResponse.getMessage()).isEqualTo("API not enabled");
assertThat(apiResponse.isSucessful()).isFalse();
}
use of com.searchcode.app.dto.api.RepoResultApiResponse in project searchcode-server by boyter.
the class ApiRouteServiceTest method testRepoListApiEnabledAuthMissingPub.
public void testRepoListApiEnabledAuthMissingPub() {
Request mockRequest = mock(Request.class);
ApiRouteService apiRouteService = new ApiRouteService();
apiRouteService.apiEnabled = true;
apiRouteService.apiAuth = true;
RepoResultApiResponse apiResponse = apiRouteService.repoList(mockRequest, null);
assertThat(apiResponse.getMessage()).isEqualTo("pub is a required parameter");
assertThat(apiResponse.getRepoResultList()).isNull();
assertThat(apiResponse.isSucessful()).isFalse();
}
use of com.searchcode.app.dto.api.RepoResultApiResponse in project searchcode-server by boyter.
the class ApiRouteServiceTest method testRepoListApiEnabledNoAuth.
public void testRepoListApiEnabledNoAuth() {
Request mockRequest = mock(Request.class);
SQLiteRepo SQLiteRepo = new SQLiteRepo(new SQLiteMemoryDatabaseConfig(), new Helpers(), Singleton.getLogger());
SQLiteRepo.createTableIfMissing();
ApiRouteService apiRouteService = new ApiRouteService(null, null, SQLiteRepo, null, null, null, new Helpers(), new LoggerWrapper());
apiRouteService.apiEnabled = true;
apiRouteService.apiAuth = false;
RepoResultApiResponse apiResponse = apiRouteService.repoList(mockRequest, null);
assertThat(apiResponse.getMessage()).isEmpty();
assertThat(apiResponse.getRepoResultList()).hasSize(0);
assertThat(apiResponse.isSucessful()).isTrue();
}
use of com.searchcode.app.dto.api.RepoResultApiResponse in project searchcode-server by boyter.
the class ApiRouteService method repoList.
public RepoResultApiResponse repoList(Request request, Response response) {
if (!this.apiEnabled) {
return new RepoResultApiResponse(false, "API not enabled", null);
}
String publicKey = request.queryParams(Values.PUB);
String signedKey = request.queryParams(Values.SIG);
String hmacTypeString = request.queryParams(Values.HMAC);
hmacTypeString = hmacTypeString == null ? Values.EMPTYSTRING : hmacTypeString;
if (apiAuth) {
if (publicKey == null || publicKey.trim().equals(Values.EMPTYSTRING)) {
return new RepoResultApiResponse(false, "pub is a required parameter", null);
}
if (signedKey == null || signedKey.trim().equals(Values.EMPTYSTRING)) {
return new RepoResultApiResponse(false, "sig is a required parameter", null);
}
String toValidate = null;
try {
toValidate = String.format("pub=%s", URLEncoder.encode(publicKey, java.nio.charset.StandardCharsets.UTF_8.toString()));
} catch (UnsupportedEncodingException ex) {
this.logger.severe(String.format("23d9f609::error in class %s exception %s", ex.getClass(), ex.getMessage()));
return new RepoResultApiResponse(false, "invalid signed url", null);
}
ApiService.HmacType hmacType = hmacTypeString.toLowerCase().equals("sha512") ? ApiService.HmacType.SHA512 : ApiService.HmacType.SHA1;
boolean validRequest = apiService.validateRequest(publicKey, signedKey, toValidate, hmacType);
if (!validRequest) {
this.logger.apiLog("ca5183a1::invalid signed repolist call using publickey=" + publicKey);
return new RepoResultApiResponse(false, "invalid signed url", null);
}
}
List<RepoResult> repoResultList = this.repo.getAllRepo();
this.logger.apiLog("48112c1b::valid signed repoList API call using publicKey=" + publicKey);
return new RepoResultApiResponse(true, Values.EMPTYSTRING, repoResultList);
}
use of com.searchcode.app.dto.api.RepoResultApiResponse in project searchcode-server by boyter.
the class ApiRouteServiceTest method testRepoListApiEnabledAuthMissingSig.
public void testRepoListApiEnabledAuthMissingSig() {
Request mockRequest = mock(Request.class);
ApiRouteService apiRouteService = new ApiRouteService();
apiRouteService.apiEnabled = true;
apiRouteService.apiAuth = true;
when(mockRequest.queryParams("pub")).thenReturn("test");
RepoResultApiResponse apiResponse = apiRouteService.repoList(mockRequest, null);
assertThat(apiResponse.getMessage()).isEqualTo("sig is a required parameter");
assertThat(apiResponse.getRepoResultList()).isNull();
assertThat(apiResponse.isSucessful()).isFalse();
}
Aggregations