Search in sources :

Example 1 with RepoResultApiResponse

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();
}
Also used : ApiRouteService(com.searchcode.app.service.route.ApiRouteService) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Example 2 with RepoResultApiResponse

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();
}
Also used : ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Request(spark.Request) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Example 3 with RepoResultApiResponse

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();
}
Also used : ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Helpers(com.searchcode.app.util.Helpers) LoggerWrapper(com.searchcode.app.util.LoggerWrapper) Request(spark.Request) SQLiteRepo(com.searchcode.app.dao.SQLiteRepo) SQLiteMemoryDatabaseConfig(com.searchcode.app.config.SQLiteMemoryDatabaseConfig) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Example 4 with RepoResultApiResponse

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);
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse) RepoResult(com.searchcode.app.model.RepoResult)

Example 5 with RepoResultApiResponse

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();
}
Also used : ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Request(spark.Request) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Aggregations

RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)7 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)6 Request (spark.Request)5 Helpers (com.searchcode.app.util.Helpers)3 LoggerWrapper (com.searchcode.app.util.LoggerWrapper)3 SQLiteRepo (com.searchcode.app.dao.SQLiteRepo)2 SQLiteMemoryDatabaseConfig (com.searchcode.app.config.SQLiteMemoryDatabaseConfig)1 RepoResult (com.searchcode.app.model.RepoResult)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1