Search in sources :

Example 36 with Helpers

use of com.searchcode.app.util.Helpers in project searchcode-server by boyter.

the class ApiRouteServiceTest method testRepoListApiEnabledAuthInvalidSigned.

public void testRepoListApiEnabledAuthInvalidSigned() {
    Request mockRequest = mock(Request.class);
    ApiService mockApiService = mock(ApiService.class);
    when(mockApiService.validateRequest("test", "test", "pub=test", ApiService.HmacType.SHA1)).thenReturn(false);
    ApiRouteService apiRouteService = new ApiRouteService(mockApiService, null, null, null, null, null, new Helpers(), new LoggerWrapper());
    apiRouteService.apiEnabled = true;
    apiRouteService.apiAuth = true;
    when(mockRequest.queryParams("pub")).thenReturn("test");
    when(mockRequest.queryParams("sig")).thenReturn("test");
    RepoResultApiResponse apiResponse = apiRouteService.repoList(mockRequest, null);
    assertThat(apiResponse.getMessage()).isEqualTo("invalid signed url");
    assertThat(apiResponse.getRepoResultList()).isNull();
    assertThat(apiResponse.isSucessful()).isFalse();
}
Also used : ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Helpers(com.searchcode.app.util.Helpers) LoggerWrapper(com.searchcode.app.util.LoggerWrapper) Request(spark.Request) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Example 37 with Helpers

use of com.searchcode.app.util.Helpers in project searchcode-server by boyter.

the class ApiRouteServiceTest method testRepoAddMissingRepoPassword.

public void testRepoAddMissingRepoPassword() {
    Request mockRequest = mock(Request.class);
    SQLiteRepo mockSQLiteRepo = mock(SQLiteRepo.class);
    ApiRouteService apiRouteService = new ApiRouteService(null, null, mockSQLiteRepo, null, null, null, new Helpers(), new LoggerWrapper());
    apiRouteService.apiEnabled = true;
    apiRouteService.apiAuth = false;
    when(mockRequest.queryParams("reponame")).thenReturn("test");
    when(mockRequest.queryParams("repourl")).thenReturn("test");
    when(mockRequest.queryParams("repotype")).thenReturn("test");
    when(mockRequest.queryParams("repousername")).thenReturn("test");
    ApiResponse apiResponse = apiRouteService.repoAdd(mockRequest, null);
    assertThat(apiResponse.getMessage()).isEqualTo("repopassword is a required parameter");
    assertThat(apiResponse.isSucessful()).isFalse();
}
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) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Example 38 with Helpers

use of com.searchcode.app.util.Helpers in project searchcode-server by boyter.

the class CodeRouteServiceTest method testGetCodeWithParamsWithMatch.

public void testGetCodeWithParamsWithMatch() {
    var request = mock(Request.class);
    var response = mock(Response.class);
    var indexService = mock(IndexService.class);
    var highlight = mock(Highlight.class);
    var codeRouteService = new CodeRouteService(indexService, new Helpers(), new SQLiteRepo(), new Data(), null, null, null, null, highlight);
    var codeResult = new CodeResult(new ArrayList<>(), new ArrayList<>());
    codeResult.setLines("100");
    codeResult.setLanguageName("LanguageName");
    codeResult.setMd5hash("md5hash");
    codeResult.setRepoName("myRepo");
    codeResult.setRepoLocation("repoLocation");
    codeResult.setCodeOwner("codeOwner");
    codeResult.setDisplayLocation("myDisplayLocation");
    when(request.params(":codeid")).thenReturn("MATCH-MOCK");
    when(indexService.getCodeResultByCodeId("MATCH-MOCK")).thenReturn(codeResult);
    when(highlight.highlightCodeResult(any())).thenReturn(new HashMap<>() {

        {
            put("codeValue", "");
        }
    });
    var map = codeRouteService.getCode(request, response);
    assertThat(map.get("codePath")).isEqualTo("myDisplayLocation");
    assertThat(map.get("codeLength")).isEqualTo("100");
    assertThat(map.get("languageName")).isEqualTo("LanguageName");
    assertThat(map.get("md5Hash")).isEqualTo("md5hash");
    assertThat(map.get("repoName")).isEqualTo("myRepo");
    assertThat(map.get("highlight")).isEqualTo(true);
    assertThat(map.get("repoLocation")).isEqualTo("repoLocation");
    // TODO depends on the highlighter being used
    assertThat(map.get("codeValue")).isEqualTo("");
    assertThat(map.get("highligher")).isNotNull();
    assertThat(map.get("codeOwner")).isEqualTo("codeOwner");
    assertThat(map.get("owaspResults")).isNotNull();
    assertThat(map.get("logoImage")).isNotNull();
    assertThat(map.get("isCommunity")).isEqualTo(App.IS_COMMUNITY);
    assertThat(map.get("estimatedCost")).isEqualTo(2395);
}
Also used : CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Helpers(com.searchcode.app.util.Helpers) SQLiteRepo(com.searchcode.app.dao.SQLiteRepo) CodeResult(com.searchcode.app.dto.CodeResult) Data(com.searchcode.app.dao.Data)

Example 39 with Helpers

use of com.searchcode.app.util.Helpers in project searchcode-server by boyter.

the class ApiServiceTest method testCase2.

public void testCase2() {
    var apiMock = mock(Api.class);
    when(apiMock.getApiByPublicKey("publicKey")).thenReturn(Optional.of(new ApiResult(1, "publicKey", "privateKey", "", "")));
    var service = new ApiService(apiMock, new Helpers());
    var actual = service.validateRequest("publicKey", "1577b8c8f5781bf2817a45bfb47ded066c579c37", "testmessage1", ApiService.HmacType.SHA1);
    assertTrue(actual);
}
Also used : ApiResult(com.searchcode.app.model.ApiResult) Helpers(com.searchcode.app.util.Helpers)

Example 40 with Helpers

use of com.searchcode.app.util.Helpers in project searchcode-server by boyter.

the class ApiServiceTest method testCase1.

public void testCase1() {
    var apiMock = mock(Api.class);
    when(apiMock.getApiByPublicKey("publicKey")).thenReturn(Optional.of(new ApiResult(1, "publicKey", "privateKey", "", "")));
    var service = new ApiService(apiMock, new Helpers());
    var actual = service.validateRequest("publicKey", "e15db69d711f0f25ce07a9c11ebebe821e6fc312", "", ApiService.HmacType.SHA1);
    assertTrue(actual);
}
Also used : ApiResult(com.searchcode.app.model.ApiResult) Helpers(com.searchcode.app.util.Helpers)

Aggregations

Helpers (com.searchcode.app.util.Helpers)53 LoggerWrapper (com.searchcode.app.util.LoggerWrapper)32 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)31 Request (spark.Request)31 RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)29 SQLiteRepo (com.searchcode.app.dao.SQLiteRepo)27 ApiResponse (com.searchcode.app.dto.api.ApiResponse)26 RepoResult (com.searchcode.app.model.RepoResult)12 ApiResult (com.searchcode.app.model.ApiResult)9 Data (com.searchcode.app.dao.Data)5 ValidatorResult (com.searchcode.app.model.ValidatorResult)4 IndexService (com.searchcode.app.service.index.IndexService)3 UniqueRepoQueue (com.searchcode.app.util.UniqueRepoQueue)3 SQLiteMemoryDatabaseConfig (com.searchcode.app.config.SQLiteMemoryDatabaseConfig)2 SourceResult (com.searchcode.app.model.SourceResult)2 Matchers.anyString (org.mockito.Matchers.anyString)2 CodeIndexDocument (com.searchcode.app.dto.CodeIndexDocument)1 CodeResult (com.searchcode.app.dto.CodeResult)1 SearchResult (com.searchcode.app.dto.SearchResult)1 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)1